task f.3
This commit is contained in:
@@ -680,6 +680,27 @@ export const crmApprovalSteps = pgTable(
|
||||
})
|
||||
);
|
||||
|
||||
export const crmApprovalMatrices = pgTable('crm_approval_matrices', {
|
||||
id: text('id').primaryKey(),
|
||||
organizationId: text('organization_id').notNull(),
|
||||
entityType: text('entity_type').notNull(),
|
||||
workflowId: text('workflow_id').notNull(),
|
||||
branchId: text('branch_id'),
|
||||
productType: text('product_type'),
|
||||
minAmount: doublePrecision('min_amount'),
|
||||
maxAmount: doublePrecision('max_amount'),
|
||||
currency: text('currency'),
|
||||
priority: integer('priority').default(100).notNull(),
|
||||
isDefault: boolean('is_default').default(false).notNull(),
|
||||
isActive: boolean('is_active').default(true).notNull(),
|
||||
description: text('description'),
|
||||
createdAt: timestamp('created_at', { withTimezone: true }).defaultNow().notNull(),
|
||||
updatedAt: timestamp('updated_at', { withTimezone: true }).defaultNow().notNull(),
|
||||
deletedAt: timestamp('deleted_at', { withTimezone: true }),
|
||||
createdBy: text('created_by').notNull(),
|
||||
updatedBy: text('updated_by').notNull()
|
||||
});
|
||||
|
||||
export const crmApprovalRequests = pgTable('crm_approval_requests', {
|
||||
id: text('id').primaryKey(),
|
||||
organizationId: text('organization_id').notNull(),
|
||||
|
||||
@@ -666,6 +666,22 @@ const APPROVAL_WORKFLOWS = [
|
||||
}
|
||||
];
|
||||
|
||||
const APPROVAL_MATRIX_DEFAULTS = [
|
||||
{
|
||||
entityType: 'quotation',
|
||||
workflowCode: 'quotation_standard_approval',
|
||||
branchId: null,
|
||||
productType: null,
|
||||
minAmount: null,
|
||||
maxAmount: null,
|
||||
currency: null,
|
||||
priority: 999,
|
||||
isDefault: true,
|
||||
isActive: true,
|
||||
description: 'Default quotation approval matrix'
|
||||
}
|
||||
];
|
||||
|
||||
const DOCUMENT_TEMPLATE_MAPPINGS: TemplateMappingSeed[] = [
|
||||
{
|
||||
placeholderKey: 'customer_name',
|
||||
@@ -1031,6 +1047,95 @@ async function upsertApprovalWorkflowsForOrganization(sql: SqlClient, organizati
|
||||
}
|
||||
}
|
||||
|
||||
async function upsertApprovalMatricesForOrganization(
|
||||
sql: SqlClient,
|
||||
organization: { id: string; createdBy: string }
|
||||
) {
|
||||
const workflows = await sql`
|
||||
select id, code
|
||||
from crm_approval_workflows
|
||||
where organization_id = ${organization.id}
|
||||
and deleted_at is null
|
||||
`;
|
||||
|
||||
const workflowIdByCode = new Map(
|
||||
workflows.map((workflow: { code: string; id: string }) => [workflow.code, workflow.id])
|
||||
);
|
||||
|
||||
for (const matrix of APPROVAL_MATRIX_DEFAULTS) {
|
||||
const workflowId = workflowIdByCode.get(matrix.workflowCode);
|
||||
if (!workflowId) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const [existing] = await sql`
|
||||
select id
|
||||
from crm_approval_matrices
|
||||
where organization_id = ${organization.id}
|
||||
and entity_type = ${matrix.entityType}
|
||||
and is_default = ${matrix.isDefault}
|
||||
and deleted_at is null
|
||||
order by created_at asc
|
||||
limit 1
|
||||
`;
|
||||
|
||||
if (existing?.id) {
|
||||
await sql`
|
||||
update crm_approval_matrices
|
||||
set workflow_id = ${workflowId},
|
||||
branch_id = ${matrix.branchId},
|
||||
product_type = ${matrix.productType},
|
||||
min_amount = ${matrix.minAmount},
|
||||
max_amount = ${matrix.maxAmount},
|
||||
currency = ${matrix.currency},
|
||||
priority = ${matrix.priority},
|
||||
is_active = ${matrix.isActive},
|
||||
description = ${matrix.description},
|
||||
updated_at = now(),
|
||||
updated_by = ${organization.createdBy}
|
||||
where id = ${existing.id}
|
||||
`;
|
||||
continue;
|
||||
}
|
||||
|
||||
await sql`
|
||||
insert into crm_approval_matrices (
|
||||
id,
|
||||
organization_id,
|
||||
entity_type,
|
||||
workflow_id,
|
||||
branch_id,
|
||||
product_type,
|
||||
min_amount,
|
||||
max_amount,
|
||||
currency,
|
||||
priority,
|
||||
is_default,
|
||||
is_active,
|
||||
description,
|
||||
created_by,
|
||||
updated_by
|
||||
) values (
|
||||
${crypto.randomUUID()},
|
||||
${organization.id},
|
||||
${matrix.entityType},
|
||||
${workflowId},
|
||||
${matrix.branchId},
|
||||
${matrix.productType},
|
||||
${matrix.minAmount},
|
||||
${matrix.maxAmount},
|
||||
${matrix.currency},
|
||||
${matrix.priority},
|
||||
${matrix.isDefault},
|
||||
${matrix.isActive},
|
||||
${matrix.description},
|
||||
${organization.createdBy},
|
||||
${organization.createdBy}
|
||||
)
|
||||
`;
|
||||
}
|
||||
}
|
||||
|
||||
async function upsertDocumentTemplatesForOrganization(
|
||||
sql: SqlClient,
|
||||
organization: { id: string; name: string; createdBy: string }
|
||||
@@ -1336,12 +1441,13 @@ async function main() {
|
||||
);
|
||||
}
|
||||
|
||||
for (const organization of organizations) {
|
||||
const branchRows = await upsertMasterOptionsForOrganization(sql, organization.id);
|
||||
await upsertDocumentSequencesForOrganization(sql, organization.id, branchRows);
|
||||
await upsertApprovalWorkflowsForOrganization(sql, organization.id);
|
||||
await upsertDocumentTemplatesForOrganization(sql, organization);
|
||||
await upsertReportDefinitionsForOrganization(sql, organization);
|
||||
for (const organization of organizations) {
|
||||
const branchRows = await upsertMasterOptionsForOrganization(sql, organization.id);
|
||||
await upsertDocumentSequencesForOrganization(sql, organization.id, branchRows);
|
||||
await upsertApprovalWorkflowsForOrganization(sql, organization.id);
|
||||
await upsertApprovalMatricesForOrganization(sql, organization);
|
||||
await upsertDocumentTemplatesForOrganization(sql, organization);
|
||||
await upsertReportDefinitionsForOrganization(sql, organization);
|
||||
console.log(`[seed-foundation] Seeded foundation data for ${organization.name}`);
|
||||
}
|
||||
} finally {
|
||||
|
||||
Reference in New Issue
Block a user