Implemented KYB, compliance decisions, integration, and testing

This commit is contained in:
Oleksandr Prokhorenko
2026-09-24 21:31:42 +03:00
parent 382f6e8841
commit 1f8721e742
36 changed files with 2279 additions and 233 deletions
+69 -6
View File
@@ -1,5 +1,13 @@
import { config } from 'dotenv';
import { documentCodes, sources, successfulStages, type Status } from '@merchantflow/contracts';
import {
documentCodes,
requiredDocumentCodes,
activationCodes,
isIntegrationCheck,
sources,
successfulStages,
type Status,
} from '@merchantflow/contracts';
import { createDatabase, type Prisma } from './index.js';
import { hashPassword } from './password.js';
import { reviewSnapshot } from './workflow.js';
@@ -296,7 +304,7 @@ async function createMerchant(tx: Prisma.TransactionClient, index: number, ancho
})),
},
checklist: {
create: documentCodes.map((documentCode, j) => ({
create: requiredDocumentCodes.map((documentCode, j) => ({
id: `${id}-doc-${j}`,
documentCode,
state:
@@ -371,6 +379,11 @@ async function enrich(
anchor: number,
) {
const actorId = m.ownerId ?? 'user-head';
const policyVersion = m.checklist.some(
(item) => item.documentCode === 'AUTHORIZED_REPRESENTATIVE',
)
? 2
: 1;
const allReceived = m.checklist.map((item) => ({ ...item, state: 'RECEIVED', note: null }));
for (const item of m.checklist)
await tx.checklistChange.create({
@@ -417,6 +430,18 @@ async function enrich(
id: `${m.id}-review-${cycle}`,
merchantId: m.id,
cycle,
policyVersion,
riskLevel: policyVersion === 2 && decision === 'APPROVED' ? 'LOW' : null,
corrections:
policyVersion === 2 && decision === 'CORRECTIONS'
? m.checklist
.filter((item) => item.documentCode === 'AUTHORIZED_REPRESENTATIVE')
.map((item) => ({
itemId: item.id,
documentCode: item.documentCode,
note: 'Provide a signed identification record for the authorized representative.',
}))
: [],
submittedById: actorId,
reviewerId: next ? 'user-compliance' : null,
decision,
@@ -429,16 +454,54 @@ async function enrich(
? (next?.note ??
'Unable to verify the beneficial owner; the submitted register conflicts with the ownership declaration.')
: decision === 'APPROVED'
? 'Company registration, ownership, UBO identification and business activity checks passed.'
? policyVersion === 2
? 'Company registration, ownership and authorized-representative identification checks passed.'
: 'Company registration, ownership, UBO identification and business activity checks passed.'
: null,
snapshot: reviewSnapshot({ ...m, checklist: allReceived }),
snapshot: reviewSnapshot({ ...m, checklist: allReceived }, policyVersion),
},
});
if (decision === 'APPROVED' && policyVersion === 2) {
for (const code of activationCodes) {
const stage = isIntegrationCheck(code) ? 'TESTING' : 'LIVE';
const event = m.events.find((entry) => entry.toStatus === stage);
const initial = isIntegrationCheck(code) ? ('INCOMPLETE' as const) : ('NOT_RUN' as const);
const state = event
? isIntegrationCheck(code)
? ('COMPLETE' as const)
: ('PASSED' as const)
: initial;
await tx.activationCheck.create({
data: {
id: `${m.id}-${cycle}-${code}`,
reviewId: `${m.id}-review-${cycle}`,
code,
state,
note: event ? 'Fictional demo check recorded for this onboarding cycle.' : null,
...(event
? {
changes: {
create: {
actorId,
fromState: initial,
toState: state,
note: 'Fictional demo check recorded for this onboarding cycle.',
occurredAt: new Date(event.occurredAt.getTime() - 1),
},
},
}
: {}),
},
});
}
}
if (decision === 'CORRECTIONS')
for (const item of m.checklist.filter((item) =>
['UBO_IDENTIFICATION', 'BUSINESS_ACTIVITY'].includes(item.documentCode),
['UBO_IDENTIFICATION', 'BUSINESS_ACTIVITY', 'AUTHORIZED_REPRESENTATIVE'].includes(
item.documentCode,
),
)) {
const state = item.documentCode === 'UBO_IDENTIFICATION' ? 'NEEDS_CORRECTION' : 'MISSING';
const state = item.documentCode !== 'BUSINESS_ACTIVITY' ? 'NEEDS_CORRECTION' : 'MISSING';
await tx.checklistChange.create({
data: {
id: `${item.id}-correction-${cycle}`,
+9 -7
View File
@@ -1,4 +1,4 @@
import { documentCodes } from '@merchantflow/contracts';
import { requiredDocumentCodes, isRequiredDocument } from '@merchantflow/contracts';
export const reviewedFields = [
'name',
@@ -13,20 +13,22 @@ type ReviewedMerchant = Record<(typeof reviewedFields)[number], string | null> &
checklist: { id: string; documentCode: string; state: string; note: string | null }[];
};
// Stable, minimal evidence: commercial estimates/contact edits do not invalidate approval.
export function reviewSnapshot(merchant: ReviewedMerchant) {
export function reviewSnapshot(merchant: ReviewedMerchant, policyVersion = 2) {
return {
company: Object.fromEntries(reviewedFields.map((key) => [key, merchant[key]])),
checklist: [...merchant.checklist]
.filter((item) =>
policyVersion >= 2
? isRequiredDocument(item.documentCode)
: item.documentCode !== 'AUTHORIZED_REPRESENTATIVE',
)
.sort((a, b) => a.documentCode.localeCompare(b.documentCode))
.map(({ id, documentCode, state, note }) => ({ id, documentCode, state, note })),
};
}
export function checklistComplete(items: { documentCode: string; state: string }[]) {
return (
items.length === documentCodes.length &&
documentCodes.every((code) =>
items.some((item) => item.documentCode === code && item.state === 'RECEIVED'),
)
return requiredDocumentCodes.every((code) =>
items.some((item) => item.documentCode === code && item.state === 'RECEIVED'),
);
}
export function snapshotsEqual(a: unknown, b: unknown): boolean {