Build MerchantFlow onboarding CRM foundation
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
import { PrismaClient } from './generated/prisma/client.js';
|
||||
import { PrismaPg } from '@prisma/adapter-pg';
|
||||
export { PrismaClient };
|
||||
export { hashPassword, verifyPassword } from './password.js';
|
||||
export type { Prisma } from './generated/prisma/client.js';
|
||||
export function createDatabase(url: string) {
|
||||
return new PrismaClient({
|
||||
adapter: new PrismaPg({ connectionString: url, connectionTimeoutMillis: 3000 }),
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import { scryptSync, randomBytes, timingSafeEqual } from 'node:crypto';
|
||||
export function hashPassword(password: string) {
|
||||
const salt = randomBytes(16).toString('hex');
|
||||
return `${salt}:${scryptSync(password, salt, 64).toString('hex')}`;
|
||||
}
|
||||
export function verifyPassword(password: string, stored: string) {
|
||||
const [salt, hash] = stored.split(':');
|
||||
if (!salt || !hash) return false;
|
||||
const expected = Buffer.from(hash, 'hex');
|
||||
const actual = scryptSync(password, salt, 64);
|
||||
return expected.length === actual.length && timingSafeEqual(actual, expected);
|
||||
}
|
||||
@@ -0,0 +1,173 @@
|
||||
import { config } from 'dotenv';
|
||||
import { statuses, successfulStages, documentCodes, type Status } from '@merchantflow/contracts';
|
||||
import { createDatabase } from './index.js';
|
||||
import { hashPassword } from './password.js';
|
||||
config({ path: new URL('../../../.env', import.meta.url).pathname, quiet: true });
|
||||
const db = createDatabase(
|
||||
process.env.DATABASE_URL ??
|
||||
'postgresql://merchantflow:merchantflow_local@localhost:55440/merchantflow',
|
||||
);
|
||||
const users = [
|
||||
{
|
||||
id: 'user-head',
|
||||
email: '[email protected]',
|
||||
name: 'Elena Carter',
|
||||
role: 'HEAD_OF_SALES' as const,
|
||||
},
|
||||
{
|
||||
id: 'user-compliance',
|
||||
email: '[email protected]',
|
||||
name: 'Pavel Sokolov',
|
||||
role: 'COMPLIANCE_OFFICER' as const,
|
||||
},
|
||||
{
|
||||
id: 'user-manager1',
|
||||
email: '[email protected]',
|
||||
name: 'Alex Morgan',
|
||||
role: 'ACCOUNT_MANAGER' as const,
|
||||
},
|
||||
{
|
||||
id: 'user-manager2',
|
||||
email: '[email protected]',
|
||||
name: 'Maria Chen',
|
||||
role: 'ACCOUNT_MANAGER' as const,
|
||||
},
|
||||
];
|
||||
const names = [
|
||||
'Orbit Goods',
|
||||
'Northstar Studio',
|
||||
'Forma Market',
|
||||
'Sora Travel',
|
||||
'Atlas Commerce',
|
||||
'Mellow Living',
|
||||
'Luma Labs',
|
||||
'Nomad Supply',
|
||||
'Aster Digital',
|
||||
'Fieldwork',
|
||||
'Contour Studio',
|
||||
'Evergreen Shop',
|
||||
'Nova Collective',
|
||||
'Plain Goods',
|
||||
'Sunday Market',
|
||||
'Kinfolk Supply',
|
||||
'Peak Ventures',
|
||||
'Amber Exchange',
|
||||
];
|
||||
const industries = ['ECOMMERCE', 'SOFTWARE', 'TRAVEL', 'SERVICES'];
|
||||
const countries = ['GB', 'DE', 'NL', 'EE', 'FR', 'CA'];
|
||||
const now = Date.now();
|
||||
const day = 86400000;
|
||||
try {
|
||||
for (const u of users)
|
||||
await db.user.upsert({
|
||||
where: { id: u.id },
|
||||
create: { ...u, passwordHash: hashPassword('MerchantFlow123!') },
|
||||
update: {},
|
||||
});
|
||||
for (let index = 0; index < names.length; index++) {
|
||||
const id = `merchant-${String(index + 1).padStart(2, '0')}`;
|
||||
// A second seed never rewrites the user's existing records, dates or history.
|
||||
if (await db.merchant.findUnique({ where: { id } })) continue;
|
||||
const status = statuses[Math.floor(index / 2)]!;
|
||||
const ownerId = index % 2 === 0 ? 'user-manager1' : 'user-manager2';
|
||||
const name = names[index]!;
|
||||
let journey: Status[] =
|
||||
status === 'REJECTED'
|
||||
? [...successfulStages.slice(0, 5), 'REJECTED']
|
||||
: successfulStages.slice(0, successfulStages.indexOf(status) + 1);
|
||||
if (status === 'KYB' && index % 2 === 0) journey = [...successfulStages.slice(0, 5), 'KYB'];
|
||||
const stageEnteredAt = new Date(now - ((index % 4) + 1) * day);
|
||||
const createdAt = new Date(stageEnteredAt.getTime() - (journey.length - 1) * 3 * day);
|
||||
const domain = name.toLowerCase().replaceAll(' ', '') + '.example';
|
||||
await db.merchant.create({
|
||||
data: {
|
||||
id,
|
||||
name,
|
||||
website: `https://${domain}`,
|
||||
industry: industries[index % 4]!,
|
||||
country: countries[index % 6]!,
|
||||
contactName: index % 2 ? 'Jamie Wilson' : 'Sam Taylor',
|
||||
contactEmail: `hello@${domain}`,
|
||||
status,
|
||||
ownerId,
|
||||
createdAt,
|
||||
stageEnteredAt,
|
||||
events: {
|
||||
create: journey.map((toStatus, i) => ({
|
||||
id: `${id}-event-${i}`,
|
||||
fromStatus: journey[i - 1] ?? null,
|
||||
toStatus,
|
||||
actorId:
|
||||
toStatus === 'COMPLIANCE_REVIEW' || (i > 0 && toStatus === 'KYB')
|
||||
? 'user-compliance'
|
||||
: ownerId,
|
||||
occurredAt: new Date(createdAt.getTime() + i * 3 * day),
|
||||
note:
|
||||
journey[i - 1] === 'COMPLIANCE_REVIEW' && toStatus === 'KYB'
|
||||
? 'Please clarify the beneficial ownership structure before resubmitting.'
|
||||
: null,
|
||||
})),
|
||||
},
|
||||
checklist: {
|
||||
create: documentCodes.map((documentCode, j) => ({
|
||||
id: `${id}-doc-${j}`,
|
||||
documentCode,
|
||||
state:
|
||||
status === 'KYB'
|
||||
? j === 2
|
||||
? 'NEEDS_CORRECTION'
|
||||
: j === 3
|
||||
? 'MISSING'
|
||||
: 'RECEIVED'
|
||||
: ['NEW_LEAD', 'CONTACTED', 'QUALIFIED'].includes(status)
|
||||
? 'MISSING'
|
||||
: 'RECEIVED',
|
||||
note:
|
||||
status === 'KYB' && j === 2
|
||||
? 'Ownership declaration needs an updated signature.'
|
||||
: null,
|
||||
})),
|
||||
},
|
||||
notes: {
|
||||
create: {
|
||||
id: `${id}-note`,
|
||||
authorId: ownerId,
|
||||
text:
|
||||
index % 2
|
||||
? 'Команда заинтересована в подключении. Следующий контакт — после проверки документов.'
|
||||
: 'Initial discovery complete. The team is interested in a straightforward checkout integration.',
|
||||
createdAt: new Date(stageEnteredAt.getTime() + 3600000),
|
||||
},
|
||||
},
|
||||
tasks: {
|
||||
create: [
|
||||
{
|
||||
id: `${id}-task-1`,
|
||||
assigneeId: ownerId,
|
||||
title:
|
||||
status === 'KYB'
|
||||
? 'Follow up on ownership documentation'
|
||||
: status === 'COMPLIANCE_REVIEW'
|
||||
? 'Review the merchant document checklist'
|
||||
: 'Schedule the next merchant check-in',
|
||||
status: 'OPEN',
|
||||
dueAt: new Date(now + ((index % 5) - 2) * day),
|
||||
remindAt: new Date(now + ((index % 5) - 3) * day),
|
||||
},
|
||||
{
|
||||
id: `${id}-task-2`,
|
||||
assigneeId: ownerId,
|
||||
title: 'Confirm the primary contact details',
|
||||
status: 'DONE',
|
||||
dueAt: stageEnteredAt,
|
||||
completedAt: stageEnteredAt,
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
console.log('Seed ready: 4 accounts, 18 merchants. Password: MerchantFlow123!');
|
||||
} finally {
|
||||
await db.$disconnect();
|
||||
}
|
||||
Reference in New Issue
Block a user