PSA/server/migrations/20251024131616_create_contract_line_preset_relations.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

102 lines
3.8 KiB
JavaScript

/**
* Create contract_line_preset_services and contract_line_preset_fixed_config tables
*
* These tables store the configuration details for contract line presets,
* mirroring the structure used for actual contract lines.
*
* @param { import('knex').Knex } knex
* @returns { Promise<void> }
*/
exports.up = async function up(knex) {
// Create contract_line_preset_services table if it doesn't exist
const hasPresetServicesTable = await knex.schema.hasTable('contract_line_preset_services');
if (!hasPresetServicesTable) {
await knex.schema.createTable('contract_line_preset_services', (table) => {
table.uuid('tenant').notNullable();
table.uuid('preset_id').notNullable();
table.uuid('service_id').notNullable();
table.integer('quantity'); // For fixed fee services
table.bigInteger('custom_rate'); // For hourly/usage services (stored in cents)
table.string('unit_of_measure', 50); // For usage services
table
.timestamp('created_at', { useTz: true })
.notNullable()
.defaultTo(knex.fn.now());
table
.timestamp('updated_at', { useTz: true })
.notNullable()
.defaultTo(knex.fn.now());
// Composite primary key
table.primary(['tenant', 'preset_id', 'service_id']);
// Indexes
table.index(['tenant', 'preset_id'], 'idx_preset_services_preset');
table.index(['tenant', 'service_id'], 'idx_preset_services_service');
});
// Add foreign keys for preset_services table
await knex.raw(`
ALTER TABLE contract_line_preset_services
ADD CONSTRAINT contract_line_preset_services_tenant_fk
FOREIGN KEY (tenant) REFERENCES tenants(tenant) ON DELETE CASCADE
`);
await knex.raw(`
ALTER TABLE contract_line_preset_services
ADD CONSTRAINT contract_line_preset_services_preset_fk
FOREIGN KEY (tenant, preset_id) REFERENCES contract_line_presets(tenant, preset_id) ON DELETE CASCADE
`);
}
// Create contract_line_preset_fixed_config table if it doesn't exist
const hasPresetFixedConfigTable = await knex.schema.hasTable('contract_line_preset_fixed_config');
if (!hasPresetFixedConfigTable) {
await knex.schema.createTable('contract_line_preset_fixed_config', (table) => {
table.uuid('tenant').notNullable();
table.uuid('preset_id').notNullable();
table.decimal('base_rate', 10, 2); // Monthly price for fixed fee plans
table.boolean('enable_proration').notNullable().defaultTo(false);
table.string('billing_cycle_alignment', 50).notNullable().defaultTo('start'); // 'start', 'end', or 'prorated'
table
.timestamp('created_at', { useTz: true })
.notNullable()
.defaultTo(knex.fn.now());
table
.timestamp('updated_at', { useTz: true })
.notNullable()
.defaultTo(knex.fn.now());
// Primary key
table.primary(['tenant', 'preset_id']);
// Index
table.index(['tenant', 'preset_id'], 'idx_preset_fixed_config_preset');
});
// Add foreign keys for preset_fixed_config table
await knex.raw(`
ALTER TABLE contract_line_preset_fixed_config
ADD CONSTRAINT contract_line_preset_fixed_config_tenant_fk
FOREIGN KEY (tenant) REFERENCES tenants(tenant) ON DELETE CASCADE
`);
await knex.raw(`
ALTER TABLE contract_line_preset_fixed_config
ADD CONSTRAINT contract_line_preset_fixed_config_preset_fk
FOREIGN KEY (tenant, preset_id) REFERENCES contract_line_presets(tenant, preset_id) ON DELETE CASCADE
`);
}
};
/**
* @param { import('knex').Knex } knex
* @returns { Promise<void> }
*/
exports.down = async function down(knex) {
await knex.schema.dropTableIfExists('contract_line_preset_fixed_config');
await knex.schema.dropTableIfExists('contract_line_preset_services');
};