Implemented the database, authentication, access rules, and seed data
This commit is contained in:
+41
-1
@@ -1,14 +1,54 @@
|
||||
import type { User } from '@merchantflow/contracts';
|
||||
import type { Prisma } from '@merchantflow/database';
|
||||
import { successfulStages, type Status } from '@merchantflow/contracts';
|
||||
import { ApiError } from './shared.js';
|
||||
export function merchantScope(user: User): Prisma.MerchantWhereInput {
|
||||
switch (user.role) {
|
||||
case 'HEAD_OF_SALES':
|
||||
return {};
|
||||
case 'COMPLIANCE_OFFICER':
|
||||
return { status: { in: ['KYB', 'COMPLIANCE_REVIEW'] } };
|
||||
return {};
|
||||
case 'ACCOUNT_MANAGER':
|
||||
return { ownerId: user.id };
|
||||
default:
|
||||
return { id: '__no_access__' };
|
||||
}
|
||||
}
|
||||
export function taskScope(user: User): Prisma.TaskWhereInput {
|
||||
return {
|
||||
merchant: merchantScope(user),
|
||||
...(user.role === 'COMPLIANCE_OFFICER' ? { assigneeId: user.id, kind: 'COMPLIANCE' } : {}),
|
||||
};
|
||||
}
|
||||
export function requireSales(user: User) {
|
||||
if (!['HEAD_OF_SALES', 'ACCOUNT_MANAGER'].includes(user.role))
|
||||
throw new ApiError('FORBIDDEN', 403);
|
||||
}
|
||||
export function capabilities(
|
||||
user: User,
|
||||
merchant: { status: Status; ownerId: string | null; reviews: { decision: string }[] },
|
||||
) {
|
||||
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 index = successfulStages.findIndex((stage) => stage === merchant.status);
|
||||
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',
|
||||
activityKinds:
|
||||
user.role === 'COMPLIANCE_OFFICER'
|
||||
? (['KYB_NOTE'] as const)
|
||||
: (['NOTE', 'CALL', 'EMAIL', 'MEETING'] as const),
|
||||
manageTaskKind: user.role === 'COMPLIANCE_OFFICER' ? 'COMPLIANCE' : 'COMMERCIAL',
|
||||
nextStage:
|
||||
sales && merchant.ownerId && index >= 0 && merchant.status !== 'COMPLIANCE_REVIEW'
|
||||
? (successfulStages[index + 1] ?? null)
|
||||
: null,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user