import nullthrows from '@expo/nullthrows'; import type { Result } from '@expo/results'; import { result } from '@expo/results'; import invariant from 'invariant'; import type { IEntityClass } from './Entity.ts'; import type { EntityCompositeField, EntityCompositeFieldValue, EntityConfiguration, } from './EntityConfiguration.ts'; import type { EntityConstructionUtils } from './EntityConstructionUtils.ts'; import type { EntityPrivacyPolicy } from './EntityPrivacyPolicy.ts'; import type { EntityQueryContext } from './EntityQueryContext.ts'; import type { ReadonlyEntity } from './ReadonlyEntity.ts'; import type { ViewerContext } from './ViewerContext.ts'; import { EntityNotFoundError } from './errors/EntityNotFoundError.ts'; import type { CompositeFieldHolder } from './internal/CompositeFieldHolder.ts'; import { CompositeFieldValueHolder } from './internal/CompositeFieldHolder.ts'; import { CompositeFieldValueMap } from './internal/CompositeFieldValueMap.ts'; import type { EntityDataManager } from './internal/EntityDataManager.ts'; import { SingleFieldHolder, SingleFieldValueHolder } from './internal/SingleFieldHolder.ts'; import { mapKeys, mapMap } from './utils/collections/maps.ts'; import { areSetsEqual } from './utils/collections/sets.ts'; /** * Authorization-result-based entity loader. All normal loads are batched, * cached, and authorized against the entity's EntityPrivacyPolicy. All loads through this * loader are are results (or null for some loader methods), where an unsuccessful result * means an authorization error or entity construction error occurred. Other errors are thrown. */ export class AuthorizationResultBasedEntityLoader< TFields extends Record, TIDField extends keyof NonNullable>, TViewerContext extends ViewerContext, TEntity extends ReadonlyEntity, TPrivacyPolicy extends EntityPrivacyPolicy< TFields, TIDField, TViewerContext, TEntity, TSelectedFields >, TSelectedFields extends keyof TFields, > { constructor( private readonly queryContext: EntityQueryContext, private readonly entityConfiguration: EntityConfiguration, private readonly entityClass: IEntityClass< TFields, TIDField, TViewerContext, TEntity, TPrivacyPolicy, TSelectedFields >, private readonly dataManager: EntityDataManager, private readonly constructionUtils: EntityConstructionUtils< TFields, TIDField, TViewerContext, TEntity, TPrivacyPolicy, TSelectedFields >, ) {} /** * Authorization-result-based version of the EnforcingEntityLoader method by the same name. * @returns map from fieldValue to entity results that match the query for that fieldValue, * where result errors can be UnauthorizedError */ async loadManyByFieldEqualingManyAsync>( fieldName: N, fieldValues: readonly NonNullable[], ): Promise, readonly Result[]>> { const { loadKey, loadValues } = this.validateFieldAndValuesAndConvertToHolders( fieldName, fieldValues, ); const loadValuesToFieldObjects = await this.dataManager.loadManyEqualingAsync( this.queryContext, loadKey, loadValues, ); const fieldValuesToFieldObjects = mapKeys( loadValuesToFieldObjects, (loadValue) => loadValue.fieldValue, ); return await this.constructionUtils.constructAndAuthorizeEntitiesAsync( fieldValuesToFieldObjects, ); } /** * Authorization-result-based version of the EnforcingEntityLoader method by the same name. * @returns map from compositeField to entity results that match the query for that compositeField, * where result errors can be UnauthorizedError */ async loadManyByCompositeFieldEqualingManyAsync< N extends EntityCompositeField>, >( compositeField: N, compositeFieldValues: readonly EntityCompositeFieldValue, N>[], ): Promise, readonly Result[]>> { const { compositeFieldHolder, compositeFieldValueHolders } = this.validateCompositeFieldAndValuesAndConvertToHolders(compositeField, compositeFieldValues); const compositeFieldValuesToFieldObjects = await this.dataManager.loadManyEqualingAsync( this.queryContext, compositeFieldHolder, compositeFieldValueHolders, ); return await this.constructAndAuthorizeEntitiesFromCompositeFieldValueHolderMapAsync( compositeFieldValuesToFieldObjects, ); } /** * Authorization-result-based version of the EnforcingEntityLoader method by the same name. * @returns array of entity results that match the query for fieldValue, where result error can be UnauthorizedError */ async loadManyByFieldEqualingAsync>( fieldName: N, fieldValue: NonNullable, ): Promise[]> { const entityResults = await this.loadManyByFieldEqualingManyAsync(fieldName, [fieldValue]); const entityResultsForFieldValue = entityResults.get(fieldValue); invariant( entityResultsForFieldValue !== undefined, `${fieldValue} should be guaranteed to be present in returned map of entities`, ); return entityResultsForFieldValue; } /** * Load one entity where fieldName equals fieldValue, or null if no entity exists matching the condition. * Not cached or coalesced, and not guaranteed to be deterministic if multiple entities match the condition. * * Only used when evaluating EntityEdgeDeletionAuthorizationInferenceBehavior.ONE_IMPLIES_ALL. * * @param fieldName - entity field being queried * @param fieldValue - fieldName field value being queried * @returns entity result matching the query for fieldValue. Returns null if no entity matches the query. * @throws EntityNotAuthorizedError when viewer is not authorized to view the returned entity * * @internal */ // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-expect-error -- this method is used in EntityPrivacyUtils, but is not intended to be part of the public API of this class, so it is marked as private. private async loadOneByFieldEqualingAsync>( fieldName: N, fieldValue: NonNullable, ): Promise | null> { const { loadKey, loadValue } = this.validateFieldAndValueAndConvertToHolders( fieldName, fieldValue, ); const result = await this.dataManager.loadOneEqualingAsync( this.queryContext, loadKey, loadValue, ); if (!result) { return null; } return await this.constructionUtils.constructAndAuthorizeEntityAsync(result); } /** * Authorization-result-based version of the EnforcingEntityLoader method by the same name. * @returns array of entity results that match the query for compositeFieldValue, where result error can be UnauthorizedError */ async loadManyByCompositeFieldEqualingAsync< N extends EntityCompositeField>, >( compositeField: N, compositeFieldValue: EntityCompositeFieldValue, N>, ): Promise[]> { const entityResults = await this.loadManyByCompositeFieldEqualingManyAsync(compositeField, [ compositeFieldValue, ]); const entityResultForCompositeFieldValue = entityResults.get(compositeFieldValue); invariant( entityResultForCompositeFieldValue !== undefined, `${compositeFieldValue} should be guaranteed to be present in returned map of entities`, ); return entityResultForCompositeFieldValue; } /** * Authorization-result-based version of the EnforcingEntityLoader method by the same name. * @returns entity result where uniqueFieldName equals fieldValue, or null if no entity matches the condition. * @throws when multiple entities match the condition */ async loadByFieldEqualingAsync>( uniqueFieldName: N, fieldValue: NonNullable, ): Promise | null> { const entityResults = await this.loadManyByFieldEqualingAsync(uniqueFieldName, fieldValue); invariant( entityResults.length <= 1, `loadByFieldEqualing: Multiple entities of type ${this.entityClass.name} found for ${String( uniqueFieldName, )}=${fieldValue}`, ); return entityResults[0] ?? null; } /** * Authorization-result-based version of the EnforcingEntityLoader method by the same name. * @returns entity result where uniqueFieldName equals fieldValue, where result error can be UnauthorizedError. */ async loadByCompositeFieldEqualingAsync< N extends EntityCompositeField>, >( compositeField: N, compositeFieldValue: EntityCompositeFieldValue, N>, ): Promise | null> { const entityResults = await this.loadManyByCompositeFieldEqualingAsync( compositeField, compositeFieldValue, ); invariant( entityResults.length <= 1, `loadByCompositeFieldEqualing: Multiple entities of type ${this.entityClass.name} found for composite field (${compositeField.join(',')})=${JSON.stringify(compositeFieldValue)}`, ); return entityResults[0] ?? null; } /** * Authorization-result-based version of the EnforcingEntityLoader method by the same name. * @returns entity result for matching ID, where result error can be UnauthorizedError or EntityNotFoundError. */ async loadByIDAsync(id: TFields[TIDField]): Promise> { const entityResults = await this.loadManyByIDsAsync([id]); // loadManyByIDsAsync is always populated for each id supplied return nullthrows(entityResults.get(id)); } /** * Authorization-result-based version of the EnforcingEntityLoader method by the same name. * @returns entity result for matching ID, or null if no entity exists for ID. */ async loadByIDNullableAsync(id: TFields[TIDField]): Promise | null> { return await this.loadByFieldEqualingAsync(this.entityConfiguration.idField, id); } /** * Authorization-result-based version of the EnforcingEntityLoader method by the same name. * @returns map from ID to corresponding entity result, where result error can be UnauthorizedError or EntityNotFoundError. */ async loadManyByIDsAsync( ids: readonly TFields[TIDField][], ): Promise>> { const entityResults = (await this.loadManyByFieldEqualingManyAsync( this.entityConfiguration.idField, ids, )) as ReadonlyMap[]>; return mapMap(entityResults, (entityResultsForId, id) => { const entityResult = entityResultsForId[0]; return ( entityResult ?? result( new EntityNotFoundError({ entityClass: this.entityClass, fieldName: this.entityConfiguration.idField, fieldValue: id, }), ) ); }); } /** * Authorization-result-based version of the EnforcingEntityLoader method by the same name. * @returns map from ID to nullable corresponding entity result, where result error can be UnauthorizedError. */ async loadManyByIDsNullableAsync( ids: readonly TFields[TIDField][], ): Promise | null>> { const entityResults = (await this.loadManyByFieldEqualingManyAsync( this.entityConfiguration.idField, ids, )) as ReadonlyMap[]>; return mapMap(entityResults, (entityResultsForId) => { return entityResultsForId[0] ?? null; }); } private validateFieldAndValuesAndConvertToHolders>( fieldName: N, fieldValues: readonly NonNullable[], ): { loadKey: SingleFieldHolder; loadValues: readonly SingleFieldValueHolder[]; } { this.constructionUtils.validateFieldAndValues(fieldName, fieldValues); return { loadKey: new SingleFieldHolder(fieldName), loadValues: fieldValues.map( (fieldValue) => new SingleFieldValueHolder(fieldValue), ), }; } private validateFieldAndValueAndConvertToHolders>( fieldName: N, fieldValue: NonNullable, ): { loadKey: SingleFieldHolder; loadValue: SingleFieldValueHolder; } { this.constructionUtils.validateFieldAndValues(fieldName, [fieldValue]); return { loadKey: new SingleFieldHolder(fieldName), loadValue: new SingleFieldValueHolder(fieldValue), }; } private validateCompositeFieldAndValuesAndConvertToHolders< N extends EntityCompositeField>, >( compositeField: N, compositeFieldValues: readonly EntityCompositeFieldValue[], ): { compositeFieldHolder: CompositeFieldHolder; compositeFieldValueHolders: readonly CompositeFieldValueHolder[]; } { // validate that the composite field input is defined in the entity configuration const compositeFieldHolder = this.entityConfiguration.compositeFieldInfo.getCompositeFieldHolderForCompositeField( compositeField, ); invariant( compositeFieldHolder, `must have composite field definition for composite field = ${String(compositeField)}`, ); const cacheableCompositeFieldFieldsSet = compositeFieldHolder.getFieldSet(); const compositeFieldValueHolders = compositeFieldValues.map( (compositeFieldValue) => new CompositeFieldValueHolder(compositeFieldValue), ); // validate that the composite field values are valid for (const compositeFieldValueHolder of compositeFieldValueHolders) { invariant( areSetsEqual(cacheableCompositeFieldFieldsSet, compositeFieldValueHolder.getFieldSet()), `composite field values must contain exactly the fields defined in the composite field definition: ${compositeField}`, ); for (const field of compositeField) { const fieldValue = compositeFieldValueHolder.compositeFieldValue[field]; this.constructionUtils.validateFieldAndValues(field, [fieldValue]); } } return { compositeFieldHolder, compositeFieldValueHolders, }; } private async constructAndAuthorizeEntitiesFromCompositeFieldValueHolderMapAsync< N extends EntityCompositeField>, >( map: ReadonlyMap, readonly Readonly[]>, ): Promise, readonly Result[]>> { return new CompositeFieldValueMap( await Promise.all( Array.from(map.entries()).map(async ([compositeFieldValueHolder, fieldObjects]) => { return [ compositeFieldValueHolder, await this.constructionUtils.constructAndAuthorizeEntitiesArrayAsync(fieldObjects), ]; }), ), ); } }