import type { EntityClass, EntityProps, FilterQuery, FindOneOptions } from '@mikro-orm/postgresql'; import { Features } from '../../Feature'; import { EntityFeature } from '../enum'; import { StandardBaseEntity } from '../StandardBaseEntity'; import type { AnyStandardEntity } from '../types'; import { resolveEntityContext, type ResolveEntityContextOptions } from './resolveEntityContext'; export type ResolveEntityOptions = | (BuildResolveEntityOptions & { entity?: E; where?: FilterQuery; resolve?: (o: O & BuildResolveEntityOptions, ctx: { where: FilterQuery[] }) => void; } & O) | E; // | string // | { id: string } // | { eid: string } // | { where: FilterQuery }; export interface ResolveEntityResult { entity?: E | null; } interface BuildResolveEntityOptions { id?: string; sid?: number; uid?: string; eid?: string; cid?: string; rid?: string; code?: string; } export function buildResolveEntityWhere( Entity: EntityClass, opts: BuildResolveEntityOptions, ): { where: FilterQuery & EntityProps; hasWhere: boolean } { const where: FilterQuery = {}; if (opts.id) { where['id'] = opts.id; } else if (opts.uid) { where['uid'] = opts.uid; } else if (opts.eid) { where['eid'] = opts.eid; } else if (opts.sid && Features.hasFeature(Entity, EntityFeature.HasSid)) { where['sid'] = opts.sid; } else if (opts.code && Features.hasFeature(Entity, EntityFeature.HasCode)) { where['code'] = opts.code; } else if ((opts.cid || opts.rid) && Features.hasFeature(Entity, EntityFeature.HasVendorRef)) { opts.cid && (where['cid'] = opts.cid); opts.rid && (where['rid'] = opts.rid); } else { return { where: where as any, hasWhere: false }; } return { where: where as any, hasWhere: true }; } export async function resolveEntity( opts: ResolveEntityOptions, contextOptions: ResolveEntityContextOptions, pass?: FindOneOptions, ): Promise> { if (opts instanceof StandardBaseEntity) { return { entity: opts }; } const { repo, def, Entity } = resolveEntityContext(contextOptions); if (opts.entity) { return { entity: opts.entity }; } let entity: E | null; const where: FilterQuery[] = []; { const out = buildResolveEntityWhere(Entity, opts); if (out.hasWhere) { where.push(out.where); } if (opts.where) { where.push(opts.where); } if (opts.resolve) { opts.resolve(opts, { where }); } } if (where.length) { entity = await repo.findOne(where, pass); } else { return {}; } return { entity }; }