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
Excluded: .git, node_modules, secrets/, compose.env, assemblyscript tgz Source: /opt/alga-psa on psa.joliet.tech
120 lines
3.6 KiB
TypeScript
120 lines
3.6 KiB
TypeScript
/**
|
|
* Shared Numbering Service
|
|
* Provides number generation functionality that can be used across
|
|
* server actions and workflow actions with dependency injection
|
|
*/
|
|
|
|
import type { Knex } from 'knex';
|
|
|
|
// Define supported entity types
|
|
export type EntityType = 'TICKET' | 'INVOICE' | 'PROJECT' | 'QUOTE' | 'CREDIT_NOTE';
|
|
|
|
export interface NumberingServiceDependencies {
|
|
knex: Knex | Knex.Transaction;
|
|
tenant: string;
|
|
}
|
|
|
|
export class SharedNumberingService {
|
|
/**
|
|
* Generates the next sequential number for a given entity type
|
|
* @param entityType The type of entity to generate a number for ('TICKET' | 'INVOICE')
|
|
* @param deps Database connection and tenant context
|
|
* @returns A formatted string containing the next number with prefix and padding
|
|
* @throws Error if tenant context is missing or number generation fails
|
|
*/
|
|
static async getNextNumber(
|
|
entityType: EntityType,
|
|
deps: NumberingServiceDependencies
|
|
): Promise<string> {
|
|
const { knex, tenant } = deps;
|
|
|
|
if (!tenant) {
|
|
throw new Error(`Tenant context is required for generating ${entityType.toLowerCase()} numbers`);
|
|
}
|
|
|
|
try {
|
|
if (entityType === 'QUOTE') {
|
|
await knex('next_number')
|
|
.insert({
|
|
tenant,
|
|
entity_type: 'QUOTE',
|
|
last_number: 0,
|
|
initial_value: 1,
|
|
prefix: 'Q-',
|
|
padding_length: 4,
|
|
})
|
|
.onConflict(['tenant', 'entity_type'])
|
|
.ignore();
|
|
}
|
|
|
|
if (entityType === 'CREDIT_NOTE') {
|
|
await knex('next_number')
|
|
.insert({
|
|
tenant,
|
|
entity_type: 'CREDIT_NOTE',
|
|
last_number: 0,
|
|
initial_value: 1,
|
|
prefix: 'CM-',
|
|
padding_length: 6,
|
|
})
|
|
.onConflict(['tenant', 'entity_type'])
|
|
.ignore();
|
|
}
|
|
|
|
// Use parameterized query for CitusDB compatibility
|
|
const result = await knex.raw(
|
|
'SELECT generate_next_number(:tenant::uuid, :type::text) as number',
|
|
{ tenant, type: entityType }
|
|
);
|
|
const number = result?.rows?.[0]?.number;
|
|
|
|
if (!number) {
|
|
const error = `Failed to generate ${entityType.toLowerCase()} number for tenant ${tenant}`;
|
|
console.error(error);
|
|
throw new Error(error);
|
|
}
|
|
|
|
return number;
|
|
} catch (error: unknown) {
|
|
console.error(`Error generating ${entityType.toLowerCase()} number for tenant ${tenant}:`, error);
|
|
if (error instanceof Error) {
|
|
throw new Error(`Failed to generate ${entityType.toLowerCase()} number in tenant ${tenant}: ${error.message}`);
|
|
}
|
|
throw new Error(`Failed to generate ${entityType.toLowerCase()} number in tenant ${tenant}: Unknown error`);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @deprecated Use getNextNumber('TICKET', deps) instead
|
|
*/
|
|
static async getNextTicketNumber(deps: NumberingServiceDependencies): Promise<string> {
|
|
return this.getNextNumber('TICKET', deps);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Instance-based NumberingService for compatibility with existing server code
|
|
* This wraps the static methods and handles dependency injection automatically
|
|
*/
|
|
export class NumberingService {
|
|
private deps: NumberingServiceDependencies;
|
|
|
|
constructor(deps: NumberingServiceDependencies) {
|
|
this.deps = deps;
|
|
}
|
|
|
|
/**
|
|
* Generates the next sequential number for a given entity type
|
|
*/
|
|
async getNextNumber(entityType: EntityType): Promise<string> {
|
|
return SharedNumberingService.getNextNumber(entityType, this.deps);
|
|
}
|
|
|
|
/**
|
|
* @deprecated Use getNextNumber('TICKET') instead
|
|
*/
|
|
async getNextTicketNumber(): Promise<string> {
|
|
return this.getNextNumber('TICKET');
|
|
}
|
|
}
|