import nullthrows from '@expo/nullthrows'; import type { Result } from '@expo/results'; import { asyncResult, result } from '@expo/results'; import invariant from 'invariant'; import type { IEntityClass } from './Entity.ts'; import type { EntityConfiguration } from './EntityConfiguration.ts'; import type { EntityPrivacyPolicy, EntityPrivacyPolicyEvaluationContext, } from './EntityPrivacyPolicy.ts'; import type { EntityQueryContext } from './EntityQueryContext.ts'; import type { ReadonlyEntity } from './ReadonlyEntity.ts'; import type { ViewerContext } from './ViewerContext.ts'; import { pick } from './entityUtils.ts'; import { EntityInvalidFieldValueError } from './errors/EntityInvalidFieldValueError.ts'; import type { IEntityMetricsAdapter } from './metrics/IEntityMetricsAdapter.ts'; import { mapMapAsync } from './utils/collections/maps.ts'; /** * Common entity loader utilities for entity construction and authorization. * Methods are exposed publicly since in rare cases they may need to be called manually. */ export class EntityConstructionUtils< 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 viewerContext: TViewerContext, private readonly queryContext: EntityQueryContext, private readonly privacyPolicyEvaluationContext: EntityPrivacyPolicyEvaluationContext< TFields, TIDField, TViewerContext, TEntity, TSelectedFields >, private readonly entityConfiguration: EntityConfiguration, private readonly entityClass: IEntityClass< TFields, TIDField, TViewerContext, TEntity, TPrivacyPolicy, TSelectedFields >, private readonly entitySelectedFields: TSelectedFields[] | undefined, private readonly privacyPolicy: TPrivacyPolicy, protected readonly metricsAdapter: IEntityMetricsAdapter, ) {} /** * Construct an entity from a fields object (applying field selection if applicable), * checking that the ID field is specified. * * @param fieldsObject - fields object */ public constructEntity(fieldsObject: TFields): TEntity { const idField = this.entityConfiguration.idField; const id = nullthrows(fieldsObject[idField], 'must provide ID to create an entity'); const entitySelectedFields = this.entitySelectedFields ?? Array.from(this.entityConfiguration.schema.keys()); const selectedFields = pick(fieldsObject, entitySelectedFields); return new this.entityClass({ viewerContext: this.viewerContext, id, databaseFields: fieldsObject, selectedFields, }); } /** * Construct and authorize entities from fields map, returning error results for entities that fail * to construct or fail to authorize. * * @param map - map from an arbitrary key type to an array of entity field objects */ public async constructAndAuthorizeEntitiesAsync( map: ReadonlyMap[]>, ): Promise[]>> { return await mapMapAsync(map, async (fieldObjects) => { return await this.constructAndAuthorizeEntitiesArrayAsync(fieldObjects); }); } /** * Construct and authorize entities from field objects array, returning error results for entities that fail * to construct or fail to authorize. * * @param fieldObjects - array of field objects */ public async constructAndAuthorizeEntitiesArrayAsync( fieldObjects: readonly Readonly[], ): Promise[]> { const uncheckedEntityResults = this.tryConstructEntities(fieldObjects); return await Promise.all( uncheckedEntityResults.map((uncheckedEntityResult) => this.authorizeEntityResultAsync(uncheckedEntityResult), ), ); } private async authorizeEntityResultAsync( uncheckedEntityResult: Result, ): Promise> { if (!uncheckedEntityResult.ok) { return uncheckedEntityResult; } return await asyncResult( this.privacyPolicy.authorizeReadAsync( this.viewerContext, this.queryContext, this.privacyPolicyEvaluationContext, uncheckedEntityResult.value, this.metricsAdapter, ), ); } public async constructAndAuthorizeEntityAsync( fieldsObject: Readonly, ): Promise> { const uncheckedEntityResult = this.tryConstructEntity(fieldsObject); return await this.authorizeEntityResultAsync(uncheckedEntityResult); } /** * Validate that field values are valid according to the field's validation function. * * @param fieldName - field name to validate * @param fieldValues - field values to validate * @throws EntityInvalidFieldValueError when a field value is invalid */ public validateFieldAndValues>( fieldName: N, fieldValues: readonly TFields[N][], ): void { const fieldDefinition = this.entityConfiguration.schema.get(fieldName); invariant(fieldDefinition, `must have field definition for field = ${String(fieldName)}`); for (const fieldValue of fieldValues) { const isInputValid = fieldDefinition.validateInputValue(fieldValue); if (!isInputValid) { throw new EntityInvalidFieldValueError(this.entityClass, fieldName, fieldValue); } } } private tryConstructEntities(fieldsObjects: readonly TFields[]): readonly Result[] { return fieldsObjects.map((fieldsObject) => this.tryConstructEntity(fieldsObject)); } private tryConstructEntity(fieldsObject: TFields): Result { try { return result(this.constructEntity(fieldsObject)); } catch (e) { if (!(e instanceof Error)) { throw e; } return result(e); } } }