import { UpdateManyToManyAssociationsOptions } from '../interfaces'; import { AuthenticatedUser } from '../interfaces/authenticated-user.interface'; import { CreatedByEntity, JoinTableEntity } from '../models'; import { AttributesOf } from '../types'; import { UpdateAssociationFillFunction } from '../types/update-association-fill-function.type'; export type UpdateAssociationSynchronousFillFunction< T extends JoinTableEntity | CreatedByEntity, AuthenticatedUserType extends AuthenticatedUser = AuthenticatedUser, NewChildrenType = any > = ( ...params: Parameters> ) => AttributesOf; export function defaultUpdateManyToManyFillFunction>( childForeignKey: keyof AttributesOf ): UpdateAssociationSynchronousFillFunction { return ( newChild: CreatedByEntity, _index: number, _existingRecord: T, updateOptions: UpdateManyToManyAssociationsOptions ) => { const { parentForeignKey, parentInstanceId } = updateOptions; const newJoinTableRecord: AttributesOf = { [childForeignKey]: newChild.id, [parentForeignKey]: parentInstanceId } as unknown as AttributesOf; return newJoinTableRecord; }; }