Build MerchantFlow onboarding CRM foundation

This commit is contained in:
Oleksandr Prokhorenko
2026-09-24 16:43:22 +03:00
commit 3aa47d825f
78 changed files with 10739 additions and 0 deletions
+21
View File
@@ -0,0 +1,21 @@
{
"name": "@merchantflow/contracts",
"version": "0.0.0",
"private": true,
"type": "module",
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js"
}
},
"scripts": {
"build": "tsc",
"typecheck": "tsc --noEmit",
"test": "vitest run",
"dev": "tsc --watch --preserveWatchOutput"
},
"dependencies": {
"zod": "4.6.5"
}
}
+31
View File
@@ -0,0 +1,31 @@
import { describe, it, expect } from 'vitest';
import {
loginSchema,
merchantQuerySchema,
successfulStages,
statuses,
canSeeReviewQueue,
} from './index.js';
describe('browser-safe contracts', () => {
it('keeps rejection outside the successful sequence', () => {
expect(successfulStages.at(-1)).toBe('LIVE');
expect(successfulStages).not.toContain('REJECTED');
expect(statuses).toHaveLength(9);
});
it('validates and normalizes login without changing the password', () => {
expect(
loginSchema.parse({ email: '[email protected]', password: ' keep spaces ' }),
).toEqual({ email: '[email protected]', password: ' keep spaces ' });
expect(loginSchema.safeParse({ email: 'invalid', password: '' }).success).toBe(false);
});
it('rejects malformed filters and pages', () => {
expect(merchantQuerySchema.safeParse({ status: 'Live' }).success).toBe(false);
expect(merchantQuerySchema.safeParse({ page: '-1' }).success).toBe(false);
expect(merchantQuerySchema.parse({}).page).toBe(1);
});
it('keeps review queue exclusive to Compliance', () => {
expect(canSeeReviewQueue('HEAD_OF_SALES')).toBe(false);
expect(canSeeReviewQueue('ACCOUNT_MANAGER')).toBe(false);
expect(canSeeReviewQueue('COMPLIANCE_OFFICER')).toBe(true);
});
});
+140
View File
@@ -0,0 +1,140 @@
import { z } from 'zod';
export const roles = ['HEAD_OF_SALES', 'COMPLIANCE_OFFICER', 'ACCOUNT_MANAGER'] as const;
export const successfulStages = [
'NEW_LEAD',
'CONTACTED',
'QUALIFIED',
'KYB',
'COMPLIANCE_REVIEW',
'INTEGRATION',
'TESTING',
'LIVE',
] as const;
export const statuses = [...successfulStages, 'REJECTED'] as const;
export const roleSchema = z.enum(roles);
export const statusSchema = z.enum(statuses);
export type Role = z.infer<typeof roleSchema>;
export type Status = z.infer<typeof statusSchema>;
export const loginSchema = z.object({
email: z
.email({ error: 'validation.email' })
.max(254, 'validation.email')
.transform((v) => v.toLowerCase()),
password: z.string().min(1, 'validation.password').max(128, 'validation.passwordMax'),
});
export type LoginInput = z.input<typeof loginSchema>;
export const merchantQuerySchema = z.object({
search: z.string().trim().max(100).default(''),
status: statusSchema.optional(),
page: z.coerce.number().int().min(1).max(10000).default(1),
});
export const taskFilters = ['ALL', 'OPEN', 'OVERDUE', 'UPCOMING', 'DONE'] as const;
export const taskQuerySchema = z.object({ filter: z.enum(taskFilters).default('ALL') });
export type TaskFilter = (typeof taskFilters)[number];
export const errorCodes = [
'UNAUTHORIZED',
'INVALID_CREDENTIALS',
'FORBIDDEN',
'NOT_FOUND',
'VALIDATION_ERROR',
'INTERNAL_ERROR',
'DATABASE_UNAVAILABLE',
'TOO_MANY_REQUESTS',
] as const;
export const apiErrorSchema = z.object({
code: z.enum(errorCodes),
issues: z.array(z.object({ path: z.string(), code: z.string() })).optional(),
});
export const userSchema = z.object({
id: z.string(),
name: z.string(),
email: z.string(),
role: roleSchema,
});
export type User = z.infer<typeof userSchema>;
const date = z.iso.datetime();
export const merchantSchema = z.object({
id: z.string(),
name: z.string(),
website: z.string(),
industry: z.string(),
country: z.string(),
contactName: z.string(),
contactEmail: z.string(),
status: statusSchema,
createdAt: date,
stageEnteredAt: date,
owner: userSchema.pick({ id: true, name: true }),
});
export type Merchant = z.infer<typeof merchantSchema>;
export const merchantListSchema = z.object({
items: z.array(merchantSchema),
total: z.number(),
page: z.number(),
pageSize: z.number(),
});
export const eventSchema = z.object({
id: z.string(),
fromStatus: statusSchema.nullable(),
toStatus: statusSchema,
occurredAt: date,
note: z.string().nullable(),
merchant: z.object({ id: z.string(), name: z.string() }),
actor: z.object({ name: z.string() }),
});
export type StageEvent = z.infer<typeof eventSchema>;
export const taskSchema = z.object({
id: z.string(),
title: z.string(),
status: z.enum(['OPEN', 'DONE']),
dueAt: date,
remindAt: date.nullable(),
completedAt: date.nullable(),
merchant: z.object({ id: z.string(), name: z.string() }),
assignee: z.object({ name: z.string() }),
});
export type Task = z.infer<typeof taskSchema>;
export const taskListSchema = z.array(taskSchema);
export const documentCodes = [
'INCORPORATION',
'OWNERSHIP',
'UBO_IDENTIFICATION',
'BUSINESS_ACTIVITY',
] as const;
export const merchantDetailSchema = merchantSchema.extend({
checklist: z.array(
z.object({
id: z.string(),
documentCode: z.enum(documentCodes),
state: z.enum(['MISSING', 'RECEIVED', 'NEEDS_CORRECTION']),
note: z.string().nullable(),
}),
),
notes: z.array(
z.object({
id: z.string(),
text: z.string(),
createdAt: date,
author: z.object({ name: z.string() }),
}),
),
events: z.array(eventSchema),
tasks: taskListSchema,
});
export const dashboardSchema = z.object({
merchantCount: z.number(),
reviewCount: z.number(),
overdueTasks: z.number(),
transitionsLast30Days: z.number(),
distribution: z.array(z.object({ status: statusSchema, count: z.number() })),
recentEvents: z.array(eventSchema),
upcomingTasks: taskListSchema,
});
export const pipelineSchema = z.array(merchantSchema);
export const healthSchema = z.object({ status: z.literal('ok'), database: z.literal('connected') });
// Navigation metadata only; write permissions are intentionally deferred.
export function canSeeReviewQueue(role: Role) {
return role === 'COMPLIANCE_OFFICER';
}
+12
View File
@@ -0,0 +1,12 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"module": "NodeNext",
"moduleResolution": "NodeNext",
"rootDir": "src",
"outDir": "dist",
"declaration": true
},
"include": ["src"],
"exclude": ["src/**/*.test.ts"]
}