165 lines
4.2 KiB
TypeScript
165 lines
4.2 KiB
TypeScript
|
|
import { defaultAdminPermissions, permissions, type PermissionKey } from "@mrp/shared";
|
||
|
|
|
||
|
|
import { env } from "../config/env.js";
|
||
|
|
import { prisma } from "./prisma.js";
|
||
|
|
import { hashPassword } from "./password.js";
|
||
|
|
import { ensureDataDirectories } from "./storage.js";
|
||
|
|
|
||
|
|
const permissionDescriptions: Record<PermissionKey, string> = {
|
||
|
|
[permissions.adminManage]: "Full administrative access",
|
||
|
|
[permissions.companyRead]: "View company settings",
|
||
|
|
[permissions.companyWrite]: "Update company settings",
|
||
|
|
[permissions.crmRead]: "View CRM records",
|
||
|
|
[permissions.crmWrite]: "Manage CRM records",
|
||
|
|
[permissions.filesRead]: "View attached files",
|
||
|
|
[permissions.filesWrite]: "Upload and manage attached files",
|
||
|
|
[permissions.ganttRead]: "View gantt timelines",
|
||
|
|
[permissions.salesRead]: "View sales data",
|
||
|
|
[permissions.shippingRead]: "View shipping data",
|
||
|
|
};
|
||
|
|
|
||
|
|
export async function bootstrapAppData() {
|
||
|
|
await ensureDataDirectories();
|
||
|
|
|
||
|
|
for (const permissionKey of defaultAdminPermissions) {
|
||
|
|
await prisma.permission.upsert({
|
||
|
|
where: { key: permissionKey },
|
||
|
|
update: {},
|
||
|
|
create: {
|
||
|
|
key: permissionKey,
|
||
|
|
description: permissionDescriptions[permissionKey],
|
||
|
|
},
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
const adminRole = await prisma.role.upsert({
|
||
|
|
where: { name: "Administrator" },
|
||
|
|
update: { description: "Full system access" },
|
||
|
|
create: {
|
||
|
|
name: "Administrator",
|
||
|
|
description: "Full system access",
|
||
|
|
},
|
||
|
|
});
|
||
|
|
|
||
|
|
const allPermissions = await prisma.permission.findMany({
|
||
|
|
where: {
|
||
|
|
key: {
|
||
|
|
in: defaultAdminPermissions,
|
||
|
|
},
|
||
|
|
},
|
||
|
|
});
|
||
|
|
|
||
|
|
for (const permission of allPermissions) {
|
||
|
|
await prisma.rolePermission.upsert({
|
||
|
|
where: {
|
||
|
|
roleId_permissionId: {
|
||
|
|
roleId: adminRole.id,
|
||
|
|
permissionId: permission.id,
|
||
|
|
},
|
||
|
|
},
|
||
|
|
update: {},
|
||
|
|
create: {
|
||
|
|
roleId: adminRole.id,
|
||
|
|
permissionId: permission.id,
|
||
|
|
},
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
const adminUser = await prisma.user.upsert({
|
||
|
|
where: { email: env.ADMIN_EMAIL },
|
||
|
|
update: {},
|
||
|
|
create: {
|
||
|
|
email: env.ADMIN_EMAIL,
|
||
|
|
firstName: "System",
|
||
|
|
lastName: "Administrator",
|
||
|
|
passwordHash: await hashPassword(env.ADMIN_PASSWORD),
|
||
|
|
},
|
||
|
|
});
|
||
|
|
|
||
|
|
await prisma.userRole.upsert({
|
||
|
|
where: {
|
||
|
|
userId_roleId: {
|
||
|
|
userId: adminUser.id,
|
||
|
|
roleId: adminRole.id,
|
||
|
|
},
|
||
|
|
},
|
||
|
|
update: {},
|
||
|
|
create: {
|
||
|
|
userId: adminUser.id,
|
||
|
|
roleId: adminRole.id,
|
||
|
|
},
|
||
|
|
});
|
||
|
|
|
||
|
|
const existingProfile = await prisma.companyProfile.findFirst({
|
||
|
|
where: { isActive: true },
|
||
|
|
});
|
||
|
|
|
||
|
|
if (!existingProfile) {
|
||
|
|
await prisma.companyProfile.create({
|
||
|
|
data: {
|
||
|
|
companyName: "MRP Codex Manufacturing",
|
||
|
|
legalName: "MRP Codex Manufacturing LLC",
|
||
|
|
email: "operations@example.com",
|
||
|
|
phone: "+1 (555) 010-2000",
|
||
|
|
website: "https://example.com",
|
||
|
|
taxId: "99-9999999",
|
||
|
|
addressLine1: "100 Foundry Lane",
|
||
|
|
addressLine2: "Suite 200",
|
||
|
|
city: "Chicago",
|
||
|
|
state: "IL",
|
||
|
|
postalCode: "60601",
|
||
|
|
country: "USA",
|
||
|
|
},
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
if ((await prisma.customer.count()) === 0) {
|
||
|
|
await prisma.customer.createMany({
|
||
|
|
data: [
|
||
|
|
{
|
||
|
|
name: "Acme Components",
|
||
|
|
email: "buyer@acme.example",
|
||
|
|
phone: "555-0101",
|
||
|
|
addressLine1: "1 Industrial Road",
|
||
|
|
addressLine2: "",
|
||
|
|
city: "Detroit",
|
||
|
|
state: "MI",
|
||
|
|
postalCode: "48201",
|
||
|
|
country: "USA",
|
||
|
|
notes: "Priority account",
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "Northwind Fabrication",
|
||
|
|
email: "ops@northwind.example",
|
||
|
|
phone: "555-0120",
|
||
|
|
addressLine1: "42 Assembly Ave",
|
||
|
|
addressLine2: "",
|
||
|
|
city: "Milwaukee",
|
||
|
|
state: "WI",
|
||
|
|
postalCode: "53202",
|
||
|
|
country: "USA",
|
||
|
|
notes: "Requires ASN notice",
|
||
|
|
},
|
||
|
|
],
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
if ((await prisma.vendor.count()) === 0) {
|
||
|
|
await prisma.vendor.create({
|
||
|
|
data: {
|
||
|
|
name: "SteelSource Supply",
|
||
|
|
email: "sales@steelsource.example",
|
||
|
|
phone: "555-0142",
|
||
|
|
addressLine1: "77 Mill Street",
|
||
|
|
addressLine2: "",
|
||
|
|
city: "Gary",
|
||
|
|
state: "IN",
|
||
|
|
postalCode: "46402",
|
||
|
|
country: "USA",
|
||
|
|
notes: "Lead time 5 business days",
|
||
|
|
},
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|