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
+42 -4
View File
@@ -1,7 +1,8 @@
import type { User } from '@merchantflow/contracts';
import type { Prisma } from '@merchantflow/database';
import type { Capabilities } from '@merchantflow/contracts';
import { workflowActions, type Evidence } from './workflow-policy.js';
import { workflowActions, validApproval, canReopenKyb, type Evidence } from './workflow-policy.js';
import { activationCodes, isIntegrationCheck, reviewPolicyVersion } from '@merchantflow/contracts';
import { ApiError } from './shared.js';
export function merchantScope(user: User): Prisma.MerchantWhereInput {
switch (user.role) {
@@ -29,16 +30,53 @@ export function capabilities(user: User, merchant: Evidence): Capabilities {
const sales =
user.role === 'HEAD_OF_SALES' ||
(user.role === 'ACCOUNT_MANAGER' && merchant.ownerId === user.id);
const locked = merchant.reviews.some((review) =>
['PENDING', 'APPROVED'].includes(review.decision),
);
const locked =
merchant.reviews.some((review) => review.decision === 'PENDING') ||
(merchant.reviews[0]?.decision === 'APPROVED' &&
(merchant.reviews[0].policyVersion >= reviewPolicyVersion || merchant.status !== 'KYB'));
const actions = workflowActions(user, merchant);
const terminal = ['LIVE', 'REJECTED'].includes(merchant.status);
const reopen = canReopenKyb(merchant);
return {
editCommercial: sales,
editIdentity: sales && !locked,
assign: user.role === 'HEAD_OF_SALES',
collectChecklist: sales && merchant.status === 'KYB' && !locked,
review: user.role === 'COMPLIANCE_OFFICER' && merchant.status === 'COMPLIANCE_REVIEW',
reopenKyb: sales && reopen,
activationCodes:
sales && validApproval(merchant)
? activationCodes.filter((code) =>
isIntegrationCheck(code)
? merchant.status === 'INTEGRATION'
: merchant.status === 'TESTING',
)
: [],
handoff: {
owner: terminal
? 'NONE'
: merchant.status === 'COMPLIANCE_REVIEW'
? 'COMPLIANCE_OFFICER'
: !merchant.ownerId
? 'HEAD_OF_SALES'
: 'ACCOUNT_MANAGER',
ownerId: terminal || merchant.status === 'COMPLIANCE_REVIEW' ? null : merchant.ownerId,
action: terminal
? 'COMPLETE'
: merchant.status === 'COMPLIANCE_REVIEW'
? 'REVIEW'
: !merchant.ownerId
? 'ASSIGN'
: reopen
? 'REOPEN_KYB'
: merchant.status === 'KYB'
? 'COLLECT'
: merchant.status === 'INTEGRATION'
? 'INTEGRATE'
: merchant.status === 'TESTING'
? 'TEST'
: 'COMMERCIAL',
},
activityKinds:
user.role === 'COMPLIANCE_OFFICER'
? (['KYB_NOTE'] as const)