import { Injectable } from '@nestjs/common'; import { AppError } from '../common/errors/app-error'; import { AccessStore } from '../identity/access.store'; import { recordAudit } from '../identity/audit'; import type { Principal } from '../identity/identity.types'; import { isVerifiedBusinessAccount } from './business-verification'; import { artworkVersionSelect, requireArtwork, requireArtworkVersion, } from './artwork-lookup'; import type { ArtworkDecisionInput } from './artwork.schemas'; @Injectable() export class ArtworkReviewStore { constructor(private readonly access: AccessStore) {} decide(actor: Principal, versionId: string, input: ArtworkDecisionInput) { return this.access.mutate(actor, 'b2b.artwork.review', async (tx) => { const version = await requireArtworkVersion( tx, actor.organizationId, versionId, ); if (version.decision) throw new AppError('B2B_ARTWORK_DECISION_EXISTS'); if (version.uploadedById === actor.userId) throw new AppError('B2B_ARTWORK_SELF_REVIEW'); const artwork = await requireArtwork( tx, actor.organizationId, version.artworkId, ); if (artwork.versions[0]?.id !== versionId) throw new AppError('B2B_ARTWORK_OLD_VERSION'); if ( input.outcome === 'APPROVED' && !(await isVerifiedBusinessAccount( tx, actor.organizationId, artwork.accountId, )) ) throw new AppError('B2B_ARTWORK_ACCOUNT_UNVERIFIED'); await tx.b2bArtworkDecision.create({ data: { versionId, organizationId: actor.organizationId, reviewerId: actor.userId, outcome: input.outcome, note: input.note, }, }); await recordAudit( tx, actor.organizationId, actor.userId, `b2b_artwork.${input.outcome.toLowerCase()}`, versionId, ); return tx.b2bArtworkVersion.findUniqueOrThrow({ where: { id: versionId }, select: artworkVersionSelect, }); }); } }