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
137 lines
4.3 KiB
TypeScript
137 lines
4.3 KiB
TypeScript
import type {
|
|
IRecurringDateRange,
|
|
IRecurringServicePeriodRecord,
|
|
ISO8601String,
|
|
} from '@alga-psa/types';
|
|
import { evaluateRecurringServicePeriodMutationPermission } from './recurringServicePeriodMutations';
|
|
import { validateRecurringServicePeriodEditContinuity } from './recurringServicePeriodEditValidation';
|
|
import { validateRecurringServicePeriodProvenance } from './recurringServicePeriodProvenance';
|
|
|
|
export interface SkipOrDeferRecurringServicePeriodInput {
|
|
record: IRecurringServicePeriodRecord;
|
|
operation: 'skip' | 'defer';
|
|
editedAt: ISO8601String;
|
|
sourceRuleVersion: string;
|
|
deferredInvoiceWindow?: IRecurringDateRange;
|
|
siblingRecords?: IRecurringServicePeriodRecord[];
|
|
sourceRunKey?: string | null;
|
|
recordIdFactory?: (input: {
|
|
scheduleKey: string;
|
|
periodKey: string;
|
|
revision: number;
|
|
}) => string;
|
|
}
|
|
|
|
export interface IRecurringServicePeriodDispositionEditResult {
|
|
supersededRecord: IRecurringServicePeriodRecord;
|
|
editedRecord: IRecurringServicePeriodRecord;
|
|
}
|
|
|
|
function defaultRecordIdFactory(input: {
|
|
scheduleKey: string;
|
|
periodKey: string;
|
|
revision: number;
|
|
}) {
|
|
return `${input.scheduleKey}:${input.periodKey}:r${input.revision}`;
|
|
}
|
|
|
|
function compareDateOnly(left: ISO8601String, right: ISO8601String) {
|
|
return left.slice(0, 10).localeCompare(right.slice(0, 10));
|
|
}
|
|
|
|
function validateDeferredInvoiceWindow(range: IRecurringDateRange) {
|
|
if (range.semantics !== 'half_open') {
|
|
throw new Error('Deferred invoice window must keep half_open semantics.');
|
|
}
|
|
if (compareDateOnly(range.start, range.end) >= 0) {
|
|
throw new Error('Deferred invoice window must have an end date after its start date.');
|
|
}
|
|
}
|
|
|
|
export function skipOrDeferRecurringServicePeriod(
|
|
input: SkipOrDeferRecurringServicePeriodInput,
|
|
): IRecurringServicePeriodDispositionEditResult {
|
|
const permission = evaluateRecurringServicePeriodMutationPermission(
|
|
input.record,
|
|
input.operation,
|
|
);
|
|
|
|
if (!permission.allowed) {
|
|
throw new Error(permission.reason);
|
|
}
|
|
|
|
let lifecycleState: IRecurringServicePeriodRecord['lifecycleState'];
|
|
let invoiceWindow = input.record.invoiceWindow;
|
|
let reasonCode: 'skip' | 'defer';
|
|
|
|
if (input.operation === 'skip') {
|
|
lifecycleState = 'skipped';
|
|
reasonCode = 'skip';
|
|
} else {
|
|
if (!input.deferredInvoiceWindow) {
|
|
throw new Error('Deferring a service period requires an explicit deferred invoice window.');
|
|
}
|
|
validateDeferredInvoiceWindow(input.deferredInvoiceWindow);
|
|
if (
|
|
input.deferredInvoiceWindow.start === input.record.invoiceWindow.start
|
|
&& input.deferredInvoiceWindow.end === input.record.invoiceWindow.end
|
|
&& input.deferredInvoiceWindow.semantics === input.record.invoiceWindow.semantics
|
|
) {
|
|
throw new Error('Defer operation must move the invoice window.');
|
|
}
|
|
|
|
lifecycleState = 'edited';
|
|
invoiceWindow = input.deferredInvoiceWindow;
|
|
reasonCode = 'defer';
|
|
}
|
|
|
|
const revision = input.record.revision + 1;
|
|
const recordIdFactory = input.recordIdFactory ?? defaultRecordIdFactory;
|
|
const editedRecord: IRecurringServicePeriodRecord = {
|
|
...input.record,
|
|
recordId: recordIdFactory({
|
|
scheduleKey: input.record.scheduleKey,
|
|
periodKey: input.record.periodKey,
|
|
revision,
|
|
}),
|
|
revision,
|
|
lifecycleState,
|
|
invoiceWindow,
|
|
provenance: {
|
|
kind: 'user_edited',
|
|
reasonCode,
|
|
sourceRuleVersion: input.sourceRuleVersion,
|
|
sourceRunKey: input.sourceRunKey ?? null,
|
|
supersedesRecordId: input.record.recordId,
|
|
},
|
|
invoiceLinkage: null,
|
|
createdAt: input.editedAt,
|
|
updatedAt: input.editedAt,
|
|
};
|
|
|
|
const provenanceValidation = validateRecurringServicePeriodProvenance(editedRecord.provenance);
|
|
if (!provenanceValidation.valid) {
|
|
throw new Error(provenanceValidation.errors.join(' '));
|
|
}
|
|
|
|
if (input.siblingRecords?.length) {
|
|
const continuityValidation = validateRecurringServicePeriodEditContinuity({
|
|
editedRecord,
|
|
siblingRecords: input.siblingRecords,
|
|
supersededRecordId: input.record.recordId,
|
|
});
|
|
if (!continuityValidation.valid) {
|
|
throw new Error(continuityValidation.errors.join(' '));
|
|
}
|
|
}
|
|
|
|
return {
|
|
supersededRecord: {
|
|
...input.record,
|
|
lifecycleState: 'superseded',
|
|
updatedAt: input.editedAt,
|
|
},
|
|
editedRecord,
|
|
};
|
|
}
|