This commit is contained in:
phaichayon
2026-06-26 15:27:05 +07:00
parent 2ad57f0ad9
commit 2ace5dbb83
26 changed files with 7463 additions and 240 deletions

View File

@@ -188,6 +188,99 @@ export const trAuditLogs = pgTable('tr_audit_logs', {
createdAt: timestamp('created_at', { withTimezone: true }).defaultNow().notNull()
});
export const appNotificationEvents = pgTable(
'app_notification_events',
{
id: text('id').primaryKey(),
organizationId: text('organization_id').notNull(),
eventType: text('event_type').notNull(),
entityType: text('entity_type').notNull(),
entityId: text('entity_id').notNull(),
actorUserId: text('actor_user_id'),
payload: jsonb('payload'),
dedupeKey: text('dedupe_key'),
status: text('status').default('pending').notNull(),
lastError: text('last_error'),
publishedAt: timestamp('published_at', { withTimezone: true }).defaultNow().notNull(),
processedAt: timestamp('processed_at', { withTimezone: true }),
createdAt: timestamp('created_at', { withTimezone: true }).defaultNow().notNull(),
updatedAt: timestamp('updated_at', { withTimezone: true }).defaultNow().notNull()
},
(table) => ({
organizationDedupeIdx: uniqueIndex('app_notification_events_org_dedupe_idx').on(
table.organizationId,
table.dedupeKey
)
})
);
export const appNotifications = pgTable(
'app_notifications',
{
id: text('id').primaryKey(),
organizationId: text('organization_id').notNull(),
recipientUserId: text('recipient_user_id').notNull(),
eventId: text('event_id').notNull(),
title: text('title').notNull(),
body: text('body').notNull(),
linkUrl: text('link_url'),
severity: text('severity').default('info').notNull(),
status: text('status').default('unread').notNull(),
readAt: timestamp('read_at', { withTimezone: true }),
archivedAt: timestamp('archived_at', { withTimezone: true }),
metadata: jsonb('metadata'),
createdAt: timestamp('created_at', { withTimezone: true }).defaultNow().notNull(),
updatedAt: timestamp('updated_at', { withTimezone: true }).defaultNow().notNull()
},
(table) => ({
recipientEventIdx: uniqueIndex('app_notifications_recipient_event_idx').on(
table.recipientUserId,
table.eventId
)
})
);
export const appNotificationTemplates = pgTable(
'app_notification_templates',
{
id: text('id').primaryKey(),
organizationId: text('organization_id').notNull(),
eventType: text('event_type').notNull(),
channel: text('channel').notNull(),
titleTemplate: text('title_template').notNull(),
bodyTemplate: text('body_template').notNull(),
linkTemplate: text('link_template'),
isActive: boolean('is_active').default(true).notNull(),
metadata: jsonb('metadata'),
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()
},
(table) => ({
organizationEventChannelIdx: uniqueIndex('app_notification_templates_org_event_channel_idx').on(
table.organizationId,
table.eventType,
table.channel
)
})
);
export const appNotificationDeliveries = pgTable('app_notification_deliveries', {
id: text('id').primaryKey(),
organizationId: text('organization_id').notNull(),
notificationId: text('notification_id').notNull(),
channel: text('channel').notNull(),
recipientAddress: text('recipient_address'),
status: text('status').default('pending').notNull(),
attemptCount: integer('attempt_count').default(0).notNull(),
lastError: text('last_error'),
sentAt: timestamp('sent_at', { withTimezone: true }),
createdAt: timestamp('created_at', { withTimezone: true }).defaultNow().notNull(),
updatedAt: timestamp('updated_at', { withTimezone: true }).defaultNow().notNull()
});
export const crmCustomers = pgTable(
'crm_customers',
{

View File

@@ -1439,6 +1439,93 @@ async function upsertReportDefinitionsForOrganization(
}
}
async function upsertNotificationTemplatesForOrganization(
sql: SqlClient,
organization: { id: string; createdBy: string }
) {
const templates = [
{
eventType: 'approval.requested',
titleTemplate: 'Approval Required: {{quotationCode}}',
bodyTemplate:
'{{actorName}} submitted {{quotationCode}} for approval in {{workflowName}}.',
linkTemplate: '{{entityLink}}'
},
{
eventType: 'approval.step.approved',
titleTemplate: 'Approval Updated: {{quotationCode}}',
bodyTemplate: '{{actorName}} approved {{quotationCode}}. Next step: {{currentStepRoleName}}.',
linkTemplate: '{{entityLink}}'
},
{
eventType: 'approval.step.rejected',
titleTemplate: 'Approval Rejected: {{quotationCode}}',
bodyTemplate: '{{actorName}} rejected {{quotationCode}}. Reason: {{remark}}',
linkTemplate: '{{entityLink}}'
},
{
eventType: 'approval.completed',
titleTemplate: 'Approval Completed: {{quotationCode}}',
bodyTemplate: '{{quotationCode}} has been fully approved in {{workflowName}}.',
linkTemplate: '{{entityLink}}'
},
{
eventType: 'approval.cancelled',
titleTemplate: 'Approval Cancelled: {{quotationCode}}',
bodyTemplate: '{{actorName}} cancelled the approval request for {{quotationCode}}.',
linkTemplate: '{{entityLink}}'
},
{
eventType: 'approval.returned',
titleTemplate: 'Approval Returned: {{quotationCode}}',
bodyTemplate: '{{actorName}} returned {{quotationCode}} for revision. Reason: {{remark}}',
linkTemplate: '{{entityLink}}'
}
];
for (const template of templates) {
await sql`
insert into app_notification_templates (
id,
organization_id,
event_type,
channel,
title_template,
body_template,
link_template,
is_active,
metadata,
deleted_at,
created_by,
updated_by
) values (
${crypto.randomUUID()},
${organization.id},
${template.eventType},
${'in_app'},
${template.titleTemplate},
${template.bodyTemplate},
${template.linkTemplate},
${true},
${JSON.stringify({ seededBy: 'foundation.seed' })},
${null},
${organization.createdBy},
${organization.createdBy}
)
on conflict (organization_id, event_type, channel) do update
set
title_template = excluded.title_template,
body_template = excluded.body_template,
link_template = excluded.link_template,
is_active = excluded.is_active,
metadata = excluded.metadata,
deleted_at = excluded.deleted_at,
updated_by = excluded.updated_by,
updated_at = now()
`;
}
}
async function main() {
loadLocalEnv();
@@ -1464,9 +1551,10 @@ async function main() {
await upsertApprovalWorkflowsForOrganization(sql, organization.id);
await upsertApprovalMatricesForOrganization(sql, organization);
await upsertDocumentTemplatesForOrganization(sql, organization);
await upsertNotificationTemplatesForOrganization(sql, organization);
await upsertReportDefinitionsForOrganization(sql, organization);
console.log(`[seed-foundation] Seeded foundation data for ${organization.name}`);
}
console.log(`[seed-foundation] Seeded foundation data for ${organization.name}`);
}
} finally {
await sql.end();
}