Implemented the database, authentication, access rules, and seed data

This commit is contained in:
Oleksandr Prokhorenko
2026-09-24 19:08:55 +03:00
parent 3aa47d825f
commit 5de240fda3
27 changed files with 3793 additions and 277 deletions
+174
View File
@@ -0,0 +1,174 @@
// Frozen foundation fixtures for upgrade compatibility tests.
if (!process.env.DATABASE_URL?.endsWith('/merchantflow_test'))
throw new Error('Legacy fixture requires isolated test database');
import { statuses, successfulStages, documentCodes } from '../../packages/contracts/dist/index.js';
import { createDatabase } from '../../packages/database/dist/index.js';
import { hashPassword } from '../../packages/database/dist/password.js';
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',
},
{
id: 'user-compliance',
email: '[email protected]',
name: 'Pavel Sokolov',
role: 'COMPLIANCE_OFFICER',
},
{
id: 'user-manager1',
email: '[email protected]',
name: 'Alex Morgan',
role: 'ACCOUNT_MANAGER',
},
{
id: 'user-manager2',
email: '[email protected]',
name: 'Maria Chen',
role: 'ACCOUNT_MANAGER',
},
];
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 === '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();
}