import { Injectable } from '@nestjs/common'; import { AppError } from '../common/errors/app-error'; import { commandHash } from '../common/idempotency'; import { DatabaseService } from '../database/database.service'; import { AccessStore } from '../identity/access.store'; import { recordAudit } from '../identity/audit'; import type { Principal } from '../identity/identity.types'; import { profileSnapshot } from './business-profile'; import type { DecideBusinessInput, SubmitBusinessInput, } from './business.schemas'; import type { Prisma } from '../generated/prisma/client'; @Injectable() export class VerificationStore { constructor( private readonly db: DatabaseService, private readonly access: AccessStore, ) {} async list(actor: Principal, accountId: string) { await this.requireAccount(this.db, actor, accountId); return this.db.businessVerificationEvent.findMany({ where: { organizationId: actor.organizationId, accountId }, orderBy: { sequence: 'asc' }, }); } submit(actor: Principal, accountId: string, input: SubmitBusinessInput) { return this.access.mutate(actor, 'b2b.manage', async (tx) => { const account = await this.requireAccount(tx, actor, accountId); const snapshot = profileSnapshot(account); const hash = commandHash(accountId, JSON.stringify(snapshot)); const replay = await this.replay(tx, actor, input.idempotencyKey, hash); if (replay) return replay; const previous = await this.latest(tx, accountId); if (previous && previous.kind !== 'REJECTED') throw new AppError('BUSINESS_SUBMIT_STATE'); const event = await tx.businessVerificationEvent.create({ data: { organizationId: actor.organizationId, accountId, actorId: actor.userId, sequence: (previous?.sequence ?? 0) + 1, kind: 'SUBMITTED', profileSnapshot: snapshot, idempotencyKey: input.idempotencyKey, requestHash: hash, }, }); await recordAudit( tx, actor.organizationId, actor.userId, 'business_verification.submitted', accountId, ); return event; }); } decide(actor: Principal, accountId: string, input: DecideBusinessInput) { return this.access.mutate(actor, 'b2b.verify', async (tx) => { await this.requireAccount(tx, actor, accountId); const hash = commandHash(accountId, input.decision, input.note); const replay = await this.replay(tx, actor, input.idempotencyKey, hash); if (replay) return replay; const previous = await this.latest(tx, accountId); if (previous?.kind !== 'SUBMITTED') throw new AppError('BUSINESS_DECISION_STATE'); if (previous.actorId === actor.userId) throw new AppError('BUSINESS_SELF_REVIEW'); const event = await tx.businessVerificationEvent.create({ data: { organizationId: actor.organizationId, accountId, actorId: actor.userId, sequence: previous.sequence + 1, kind: input.decision, note: input.note, idempotencyKey: input.idempotencyKey, requestHash: hash, }, }); await recordAudit( tx, actor.organizationId, actor.userId, `business_verification.${input.decision.toLowerCase()}`, accountId, ); return event; }); } private async requireAccount( db: Prisma.TransactionClient | DatabaseService, actor: Principal, accountId: string, ) { const account = await db.businessAccount.findFirst({ where: { id: accountId, organizationId: actor.organizationId }, }); if (!account) throw new AppError('BUSINESS_ACCOUNT_NOT_FOUND'); return account; } private latest(tx: Prisma.TransactionClient, accountId: string) { return tx.businessVerificationEvent.findFirst({ where: { accountId }, orderBy: { sequence: 'desc' }, }); } private async replay( tx: Prisma.TransactionClient, actor: Principal, key: string, hash: string, ) { const event = await tx.businessVerificationEvent.findUnique({ where: { organizationId_idempotencyKey: { organizationId: actor.organizationId, idempotencyKey: key, }, }, }); if (event && event.requestHash !== hash) throw new AppError('BUSINESS_REPLAY_CONFLICT'); return event; } }