import { enforceAsyncResult } from '@expo/results'; import type { AuthorizationResultBasedUpdateMutator } from './AuthorizationResultBasedEntityMutator.ts'; import type { EntityPrivacyPolicy } from './EntityPrivacyPolicy.ts'; import type { ReadonlyEntity } from './ReadonlyEntity.ts'; import type { ViewerContext } from './ViewerContext.ts'; /** * Enforcing entity updater. All updates * through this updater will throw if authorization is not successful. */ export class EnforcingEntityUpdater< 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 entityUpdater: AuthorizationResultBasedUpdateMutator< TFields, TIDField, TViewerContext, TEntity, TPrivacyPolicy, TSelectedFields >, ) {} /** * Set the value for entity field. * @param fieldName - entity field being updated * @param value - value for entity field */ setField>(fieldName: K, value: TFields[K]): this { this.entityUpdater.setField(fieldName, value); return this; } /** * Commit the changes to the entity after authorizing against update privacy rules. * Invalidates all caches for pre-update entity. * @returns authorized updated entity, throws upon update failure */ async updateAsync(): Promise { return await enforceAsyncResult(this.entityUpdater.updateAsync()); } }