PSA/server/migrations/20260101093000_create_ticket_project_materials.cjs
Hermes 284313f908
Some checks are pending
Bidi Control Character Guard / bidi-control-guard (push) Waiting to run
Circular Dependency Check / Check for new circular dependencies (push) Waiting to run
Citus Migration Smoke / Combined migrations on single-node Citus (push) Waiting to run
E2E Fresh Install Tests / fresh-install-e2e (push) Waiting to run
ext-v2 guardrails / Run ext-v2 guard and ESLint (push) Waiting to run
Integration Tests / Check for relevant changes (push) Waiting to run
Integration Tests / ${{ (github.event_name == 'schedule' || github.event.inputs.suite == 'full') && 'Full integration suite' || 'Tier-1 integration subset' }} (push) Blocked by required conditions
Mobile checks / Mobile lint + typecheck (push) Waiting to run
Mobile checks / Mobile unit tests (push) Waiting to run
Mobile checks / Mobile dependency audit (report) (push) Waiting to run
Mobile checks / Mobile reproducibility checks (push) Waiting to run
Secrets guard (env backups) / Ensure no tracked env backup files (push) Waiting to run
Temporal Readiness / fast-readiness (push) Waiting to run
Temporal Readiness / docker-parity (push) Waiting to run
TypeScript Type Check / Nx affected typecheck (push) Waiting to run
Unit Tests / Skipped-test budget (push) Waiting to run
Unit Tests / Nx affected unit tests (push) Waiting to run
Unit Tests / Server unit coverage (informational) (push) Waiting to run
Validate Tenant Management Schema / Check for relevant changes (push) Waiting to run
Validate Tenant Management Schema / Validate Tenant Management Schema (push) Blocked by required conditions
EE Workflows Build Guard / ee-workflows-build-guard (push) Waiting to run
Initial import of AlgaPSA codebase from PSA server
Excluded: .git, node_modules, secrets/, compose.env, assemblyscript tgz

Source: /opt/alga-psa on psa.joliet.tech
2026-06-22 16:12:17 -05:00

93 lines
4.3 KiB
JavaScript

/**
* Create ticket_materials and project_materials tables for recording product/material usage that
* flows into invoicing via the billing engine.
*
* V1 decisions:
* - Materials are recorded on tickets and projects (not time entries yet)
* - Materials auto-bill (no approval gate)
* - Materials are ingested into billing engine as charges (like usage/time), then persisted to invoice items during invoice generation
*/
exports.up = async function up(knex) {
await knex.schema.createTable('ticket_materials', (table) => {
table.uuid('tenant').notNullable();
table.uuid('ticket_material_id').defaultTo(knex.raw('gen_random_uuid()')).notNullable();
table.uuid('ticket_id').notNullable();
table.uuid('client_id').notNullable();
table.uuid('service_id').notNullable(); // product (service_catalog)
table.integer('quantity').notNullable().defaultTo(1);
table.bigInteger('rate').notNullable(); // cents
table.text('currency_code').notNullable().defaultTo('USD');
table.text('description').nullable();
table.boolean('is_billed').notNullable().defaultTo(false);
table.uuid('billed_invoice_id').nullable();
table.timestamp('billed_at', { useTz: true }).nullable();
table.timestamp('created_at', { useTz: true }).defaultTo(knex.fn.now());
table.timestamp('updated_at', { useTz: true }).defaultTo(knex.fn.now());
table.primary(['tenant', 'ticket_material_id']);
table.foreign('tenant').references('tenants.tenant');
table.foreign(['tenant', 'ticket_id']).references(['tenant', 'ticket_id']).inTable('tickets').onDelete('CASCADE');
table.foreign(['tenant', 'client_id']).references(['tenant', 'client_id']).inTable('clients').onDelete('CASCADE');
table.foreign(['tenant', 'service_id']).references(['tenant', 'service_id']).inTable('service_catalog').onDelete('RESTRICT');
});
await knex.schema.createTable('project_materials', (table) => {
table.uuid('tenant').notNullable();
table.uuid('project_material_id').defaultTo(knex.raw('gen_random_uuid()')).notNullable();
table.uuid('project_id').notNullable();
table.uuid('client_id').notNullable();
table.uuid('service_id').notNullable(); // product (service_catalog)
table.integer('quantity').notNullable().defaultTo(1);
table.bigInteger('rate').notNullable(); // cents
table.text('currency_code').notNullable().defaultTo('USD');
table.text('description').nullable();
table.boolean('is_billed').notNullable().defaultTo(false);
table.uuid('billed_invoice_id').nullable();
table.timestamp('billed_at', { useTz: true }).nullable();
table.timestamp('created_at', { useTz: true }).defaultTo(knex.fn.now());
table.timestamp('updated_at', { useTz: true }).defaultTo(knex.fn.now());
table.primary(['tenant', 'project_material_id']);
table.foreign('tenant').references('tenants.tenant');
table.foreign(['tenant', 'project_id']).references(['tenant', 'project_id']).inTable('projects').onDelete('CASCADE');
table.foreign(['tenant', 'client_id']).references(['tenant', 'client_id']).inTable('clients').onDelete('CASCADE');
table.foreign(['tenant', 'service_id']).references(['tenant', 'service_id']).inTable('service_catalog').onDelete('RESTRICT');
});
await knex.raw(`
CREATE INDEX IF NOT EXISTS idx_ticket_materials_unbilled
ON ticket_materials (tenant, ticket_id, is_billed, created_at);
`);
await knex.raw(`
CREATE INDEX IF NOT EXISTS idx_project_materials_unbilled
ON project_materials (tenant, project_id, is_billed, created_at);
`);
await knex.raw(`
CREATE INDEX IF NOT EXISTS idx_ticket_materials_client_unbilled
ON ticket_materials (tenant, client_id, is_billed, created_at);
`);
await knex.raw(`
CREATE INDEX IF NOT EXISTS idx_project_materials_client_unbilled
ON project_materials (tenant, client_id, is_billed, created_at);
`);
};
exports.down = async function down(knex) {
await knex.raw(`DROP INDEX IF EXISTS idx_project_materials_client_unbilled;`);
await knex.raw(`DROP INDEX IF EXISTS idx_ticket_materials_client_unbilled;`);
await knex.raw(`DROP INDEX IF EXISTS idx_project_materials_unbilled;`);
await knex.raw(`DROP INDEX IF EXISTS idx_ticket_materials_unbilled;`);
await knex.schema.dropTableIfExists('project_materials');
await knex.schema.dropTableIfExists('ticket_materials');
};