PSA/server/migrations/20251110223310_create_appointment_requests.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

116 lines
3.8 KiB
JavaScript

/**
* Create appointment_requests table for managing client appointment requests
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = async function(knex) {
await knex.schema.createTable('appointment_requests', function(table) {
// Primary key columns
table.uuid('appointment_request_id').defaultTo(knex.raw('gen_random_uuid()'));
table.uuid('tenant').notNullable();
// Related entities
table.uuid('client_id').nullable(); // Nullable for unauthenticated requests
table.uuid('contact_id').nullable();
table.uuid('service_id').notNullable();
// Requested appointment details
table.date('requested_date').notNullable();
table.time('requested_time').notNullable();
table.integer('requested_duration').notNullable(); // Duration in minutes
table.uuid('preferred_assigned_user_id').nullable();
// Status and description
table.text('status').notNullable().defaultTo('pending');
table.text('description').nullable();
table.uuid('ticket_id').nullable(); // Link to existing ticket if applicable
// Authentication and requester info (for unauthenticated requests)
table.boolean('is_authenticated').notNullable().defaultTo(true);
table.text('requester_name').nullable();
table.text('requester_email').nullable();
table.text('requester_phone').nullable();
table.text('company_name').nullable();
// Approval workflow fields
table.uuid('schedule_entry_id').nullable(); // Set when approved
table.uuid('approved_by_user_id').nullable();
table.timestamp('approved_at').nullable();
table.text('declined_reason').nullable();
// Timestamps
table.timestamp('created_at').notNullable().defaultTo(knex.fn.now());
table.timestamp('updated_at').notNullable().defaultTo(knex.fn.now());
// Primary key
table.primary(['tenant', 'appointment_request_id']);
});
// Add check constraint for status
await knex.schema.raw(`
ALTER TABLE appointment_requests
ADD CONSTRAINT appointment_requests_status_check
CHECK (status IN ('pending', 'approved', 'declined', 'cancelled'))
`);
// Create indexes for common query patterns
await knex.schema.raw(`
CREATE INDEX idx_appointment_requests_tenant
ON appointment_requests (tenant)
`);
await knex.schema.raw(`
CREATE INDEX idx_appointment_requests_tenant_status
ON appointment_requests (tenant, status)
`);
await knex.schema.raw(`
CREATE INDEX idx_appointment_requests_tenant_client
ON appointment_requests (tenant, client_id)
`);
await knex.schema.raw(`
CREATE INDEX idx_appointment_requests_tenant_service
ON appointment_requests (tenant, service_id)
`);
await knex.schema.raw(`
CREATE INDEX idx_appointment_requests_tenant_date
ON appointment_requests (tenant, requested_date)
`);
await knex.schema.raw(`
CREATE INDEX idx_appointment_requests_tenant_schedule_entry
ON appointment_requests (tenant, schedule_entry_id)
`);
// Check if Citus is enabled
const citusFn = await knex.raw(`
SELECT EXISTS (
SELECT 1 FROM pg_proc
WHERE proname = 'create_distributed_table'
) AS exists;
`);
if (citusFn.rows?.[0]?.exists) {
await knex.raw("SELECT create_distributed_table('appointment_requests', 'tenant')");
} else {
console.warn('[create_appointment_requests] Skipping create_distributed_table (function unavailable)');
}
console.log('✅ Created appointment_requests table');
};
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = async function(knex) {
await knex.schema.dropTableIfExists('appointment_requests');
console.log('✅ Dropped appointment_requests table');
};
// Disable transaction for Citus DB compatibility
// create_distributed_table cannot run inside a transaction block
exports.config = { transaction: false };