hotfix-add subgroup customer

This commit is contained in:
phaichayon
2026-06-18 08:58:01 +07:00
parent 04a886ea26
commit 0650cf6297
38 changed files with 8674 additions and 687 deletions

View File

@@ -9,6 +9,8 @@ type SeedOption = {
label: string;
value: string;
sortOrder: number;
parentCode?: string;
parentCategory?: string;
};
type BranchSeedRow = {
@@ -110,9 +112,85 @@ const FOUNDATION_OPTIONS = {
{ code: 'individual', label: 'Individual', value: 'individual', sortOrder: 2 }
],
crm_customer_group: [
{ code: 'strategic', label: 'Strategic', value: 'strategic', sortOrder: 1 },
{ code: 'standard', label: 'Standard', value: 'standard', sortOrder: 2 },
{ code: 'project', label: 'Project', value: 'project', sortOrder: 3 }
{ code: 'industrial', label: 'Industrial', value: 'industrial', sortOrder: 1 },
{ code: 'construction', label: 'Construction', value: 'construction', sortOrder: 2 },
{ code: 'government', label: 'Government', value: 'government', sortOrder: 3 },
{ code: 'dealer', label: 'Dealer', value: 'dealer', sortOrder: 4 },
{ code: 'other', label: 'Other', value: 'other', sortOrder: 5 }
],
crm_customer_sub_group: [
{
code: 'factory',
label: 'Factory',
value: 'factory',
sortOrder: 1,
parentCode: 'industrial',
parentCategory: 'crm_customer_group'
},
{
code: 'warehouse',
label: 'Warehouse',
value: 'warehouse',
sortOrder: 2,
parentCode: 'industrial',
parentCategory: 'crm_customer_group'
},
{
code: 'logistics',
label: 'Logistics',
value: 'logistics',
sortOrder: 3,
parentCode: 'industrial',
parentCategory: 'crm_customer_group'
},
{
code: 'contractor',
label: 'Contractor',
value: 'contractor',
sortOrder: 4,
parentCode: 'construction',
parentCategory: 'crm_customer_group'
},
{
code: 'consultant',
label: 'Consultant',
value: 'consultant',
sortOrder: 5,
parentCode: 'construction',
parentCategory: 'crm_customer_group'
},
{
code: 'state_enterprise',
label: 'State Enterprise',
value: 'state_enterprise',
sortOrder: 6,
parentCode: 'government',
parentCategory: 'crm_customer_group'
},
{
code: 'government_agency',
label: 'Government Agency',
value: 'government_agency',
sortOrder: 7,
parentCode: 'government',
parentCategory: 'crm_customer_group'
},
{
code: 'reseller',
label: 'Reseller',
value: 'reseller',
sortOrder: 8,
parentCode: 'dealer',
parentCategory: 'crm_customer_group'
},
{
code: 'other',
label: 'Other',
value: 'other',
sortOrder: 9,
parentCode: 'other',
parentCategory: 'crm_customer_group'
}
],
crm_enquiry_status: [
{ code: 'new', label: 'New', value: 'new', sortOrder: 1 },
@@ -265,7 +343,7 @@ const DOCUMENT_TEMPLATE_MAPPINGS: TemplateMappingSeed[] = [
},
{
placeholderKey: 'customer_att',
sourcePath: 'contact.name',
sourcePath: 'quotation.attention',
dataType: 'scalar',
sortOrder: 5
},
@@ -283,9 +361,9 @@ const DOCUMENT_TEMPLATE_MAPPINGS: TemplateMappingSeed[] = [
},
{
placeholderKey: 'quotation_date',
sourcePath: 'quotation.quotationDate',
sourcePath: 'pdfme.quotation_date',
dataType: 'scalar',
formatMask: 'date',
formatMask: null,
sortOrder: 8
},
{
@@ -296,34 +374,70 @@ const DOCUMENT_TEMPLATE_MAPPINGS: TemplateMappingSeed[] = [
},
{
placeholderKey: 'quotation_price',
sourcePath: 'quotation.totalAmount',
sourcePath: 'pdfme.quotation_price',
dataType: 'scalar',
formatMask: 'currency',
formatMask: null,
sortOrder: 10
},
{
placeholderKey: 'currency',
sourcePath: 'quotation.currencyCode',
sourcePath: 'quotation.currency',
dataType: 'scalar',
sortOrder: 11
},
{
placeholderKey: 'exclusion_data',
sourcePath: 'topics.exclusion',
dataType: 'multiline',
sourcePath: 'pdfme.exclusion_data',
dataType: 'table',
sortOrder: 12
},
{
placeholderKey: 'item_topic',
sourcePath: 'topics.scope',
dataType: 'multiline',
placeholderKey: 'app1',
sourcePath: 'signatures.preparedBy',
dataType: 'scalar',
defaultValue: '-',
sortOrder: 13
},
{
placeholderKey: 'app1_position',
sourcePath: 'signatures.preparedByPosition',
dataType: 'scalar',
defaultValue: '-',
sortOrder: 14
},
{
placeholderKey: 'app2',
sourcePath: 'signatures.salesManager',
dataType: 'scalar',
defaultValue: '-',
sortOrder: 15
},
{
placeholderKey: 'app2_position',
sourcePath: 'signatures.salesManagerPosition',
dataType: 'scalar',
defaultValue: '-',
sortOrder: 16
},
{
placeholderKey: 'app3',
sourcePath: 'signatures.topManager',
dataType: 'scalar',
defaultValue: '-',
sortOrder: 17
},
{
placeholderKey: 'app3_position',
sourcePath: 'signatures.topManagerPosition',
dataType: 'scalar',
defaultValue: '-',
sortOrder: 18
},
{
placeholderKey: 'items_table',
sourcePath: 'items',
dataType: 'table',
sortOrder: 14,
sortOrder: 19,
columns: [
{ columnName: 'Item', sourceField: 'itemNumber', columnLetter: 'A', sortOrder: 1 },
{ columnName: 'Description', sourceField: 'description', columnLetter: 'B', sortOrder: 2 },
@@ -387,6 +501,20 @@ async function upsertMasterOptionsForOrganization(
[string, SeedOption[]]
>) {
for (const option of options) {
const parentRow =
option.parentCode && option.parentCategory
? (
await sql`
select id
from ms_options
where organization_id = ${organizationId}
and category = ${option.parentCategory}
and code = ${option.parentCode}
and deleted_at is null
limit 1
`
)[0] ?? null
: null;
const [row] = await sql`
insert into ms_options (
id,
@@ -407,7 +535,7 @@ async function upsertMasterOptionsForOrganization(
${option.code},
${option.label},
${option.value},
${null},
${parentRow?.id ?? null},
${option.sortOrder},
${true},
${null},
@@ -651,38 +779,7 @@ async function upsertDocumentTemplatesForOrganization(
}
for (const mapping of DOCUMENT_TEMPLATE_MAPPINGS) {
const [mappingRow] = await sql`
insert into crm_document_template_mappings (
id,
organization_id,
template_version_id,
placeholder_key,
source_path,
data_type,
sheet_name,
default_value,
format_mask,
sort_order,
deleted_at
) values (
${crypto.randomUUID()},
${organization.id},
${resolvedVersionId},
${mapping.placeholderKey},
${mapping.sourcePath},
${mapping.dataType},
${null},
${mapping.defaultValue ?? null},
${mapping.formatMask ?? null},
${mapping.sortOrder},
${null}
)
on conflict do nothing
returning id
`;
const resolvedMappingId =
mappingRow?.id ??
const existingMapping =
(
await sql`
select id
@@ -690,16 +787,88 @@ async function upsertDocumentTemplatesForOrganization(
where organization_id = ${organization.id}
and template_version_id = ${resolvedVersionId}
and placeholder_key = ${mapping.placeholderKey}
and deleted_at is null
limit 1
`
)[0]?.id;
)[0] ?? null;
const resolvedMappingId = existingMapping?.id ?? crypto.randomUUID();
if (existingMapping) {
await sql`
update crm_document_template_mappings
set
source_path = ${mapping.sourcePath},
data_type = ${mapping.dataType},
sheet_name = ${null},
default_value = ${mapping.defaultValue ?? null},
format_mask = ${mapping.formatMask ?? null},
sort_order = ${mapping.sortOrder},
deleted_at = ${null},
updated_at = now()
where id = ${resolvedMappingId}
`;
} else {
await sql`
insert into crm_document_template_mappings (
id,
organization_id,
template_version_id,
placeholder_key,
source_path,
data_type,
sheet_name,
default_value,
format_mask,
sort_order,
deleted_at
) values (
${resolvedMappingId},
${organization.id},
${resolvedVersionId},
${mapping.placeholderKey},
${mapping.sourcePath},
${mapping.dataType},
${null},
${mapping.defaultValue ?? null},
${mapping.formatMask ?? null},
${mapping.sortOrder},
${null}
)
`;
}
if (!resolvedMappingId || !mapping.columns?.length) {
continue;
}
for (const column of mapping.columns) {
const existingColumn =
(
await sql`
select id
from crm_document_template_table_columns
where organization_id = ${organization.id}
and mapping_id = ${resolvedMappingId}
and column_name = ${column.columnName}
limit 1
`
)[0] ?? null;
if (existingColumn) {
await sql`
update crm_document_template_table_columns
set
source_field = ${column.sourceField},
column_letter = ${column.columnLetter ?? null},
sort_order = ${column.sortOrder},
format_mask = ${column.formatMask ?? null},
deleted_at = ${null},
updated_at = now()
where id = ${existingColumn.id}
`;
continue;
}
await sql`
insert into crm_document_template_table_columns (
id,
@@ -722,10 +891,25 @@ async function upsertDocumentTemplatesForOrganization(
${column.formatMask ?? null},
${null}
)
on conflict do nothing
`;
}
}
await sql`
update crm_document_template_mappings
set
deleted_at = now(),
updated_at = now()
where organization_id = ${organization.id}
and template_version_id = ${resolvedVersionId}
and (
placeholder_key in (${'topic'}, ${'data_topic'}, ${'item_topic'})
or placeholder_key like 'topic\_%' escape '\'
or placeholder_key like 'data_topic\_%' escape '\'
or placeholder_key like 'item_topic\_%' escape '\'
)
and deleted_at is null
`;
}
async function main() {