PSA/shared/billingClients/recurringServicePeriodParity.ts
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

147 lines
5.7 KiB
TypeScript

import type {
IRecurringDuePeriodSelection,
IRecurringServicePeriodParityComparisonResult,
IRecurringServicePeriodParityDrift,
IRecurringServicePeriodRecord,
ISO8601String,
RecurringServicePeriodParityComparisonState,
} from '@alga-psa/types';
import { DEFAULT_RECURRING_SERVICE_PERIOD_PARITY_COMPARISON_STATES } from '@alga-psa/types';
import {
buildRecurringServicePeriodPeriodKey,
buildRecurringServicePeriodScheduleKey,
} from './recurringServicePeriodKeys';
function toDateOnly(value: ISO8601String): ISO8601String {
return `${value.slice(0, 10)}` as ISO8601String;
}
function buildCompositeKey(scheduleKey: string, periodKey: string) {
return `${scheduleKey}::${periodKey}`;
}
function normalizeDerivedSelection(tenant: string, selection: IRecurringDuePeriodSelection) {
const scheduleKey = buildRecurringServicePeriodScheduleKey({
tenant,
obligationType: selection.servicePeriod.sourceObligation.obligationType,
obligationId: selection.servicePeriod.sourceObligation.obligationId,
cadenceOwner: selection.servicePeriod.cadenceOwner,
duePosition: selection.servicePeriod.duePosition,
});
const periodKey = buildRecurringServicePeriodPeriodKey(selection.servicePeriod);
return {
compositeKey: buildCompositeKey(scheduleKey, periodKey),
scheduleKey,
periodKey,
obligationId: selection.servicePeriod.sourceObligation.obligationId,
cadenceOwner: selection.servicePeriod.cadenceOwner,
duePosition: selection.servicePeriod.duePosition,
servicePeriodStart: toDateOnly(selection.servicePeriod.start),
servicePeriodEnd: toDateOnly(selection.servicePeriod.end),
invoiceWindowStart: toDateOnly(selection.invoiceWindow.start),
invoiceWindowEnd: toDateOnly(selection.invoiceWindow.end),
};
}
function normalizePersistedRecord(record: IRecurringServicePeriodRecord) {
return {
compositeKey: buildCompositeKey(record.scheduleKey, record.periodKey),
scheduleKey: record.scheduleKey,
periodKey: record.periodKey,
obligationId: record.sourceObligation.obligationId,
cadenceOwner: record.cadenceOwner,
duePosition: record.duePosition,
servicePeriodStart: toDateOnly(record.servicePeriod.start),
servicePeriodEnd: toDateOnly(record.servicePeriod.end),
invoiceWindowStart: toDateOnly(record.invoiceWindow.start),
invoiceWindowEnd: toDateOnly(record.invoiceWindow.end),
lifecycleState: record.lifecycleState,
};
}
export function compareDerivedRecurringTimingToPersistedSchedule(input: {
tenant: string;
derivedSelections: IRecurringDuePeriodSelection[];
persistedRecords: IRecurringServicePeriodRecord[];
lifecycleStates?: RecurringServicePeriodParityComparisonState[];
}): IRecurringServicePeriodParityComparisonResult {
const allowedStates = new Set(
input.lifecycleStates ?? DEFAULT_RECURRING_SERVICE_PERIOD_PARITY_COMPARISON_STATES,
);
const persistedByKey = new Map(
input.persistedRecords
.filter((record) => allowedStates.has(record.lifecycleState as RecurringServicePeriodParityComparisonState))
.map((record) => {
const normalized = normalizePersistedRecord(record);
return [normalized.compositeKey, normalized] as const;
}),
);
const drifts: IRecurringServicePeriodParityDrift[] = [];
for (const selection of input.derivedSelections) {
const normalizedDerived = normalizeDerivedSelection(input.tenant, selection);
const persisted = persistedByKey.get(normalizedDerived.compositeKey);
if (!persisted) {
drifts.push({
kind: 'missing_persisted_period',
scheduleKey: normalizedDerived.scheduleKey,
periodKey: normalizedDerived.periodKey,
obligationId: normalizedDerived.obligationId,
cadenceOwner: normalizedDerived.cadenceOwner,
duePosition: normalizedDerived.duePosition,
servicePeriodStart: normalizedDerived.servicePeriodStart,
servicePeriodEnd: normalizedDerived.servicePeriodEnd,
derivedInvoiceWindowStart: normalizedDerived.invoiceWindowStart,
derivedInvoiceWindowEnd: normalizedDerived.invoiceWindowEnd,
});
continue;
}
persistedByKey.delete(normalizedDerived.compositeKey);
if (
persisted.invoiceWindowStart !== normalizedDerived.invoiceWindowStart
|| persisted.invoiceWindowEnd !== normalizedDerived.invoiceWindowEnd
) {
drifts.push({
kind: 'invoice_window_mismatch',
scheduleKey: normalizedDerived.scheduleKey,
periodKey: normalizedDerived.periodKey,
obligationId: normalizedDerived.obligationId,
cadenceOwner: normalizedDerived.cadenceOwner,
duePosition: normalizedDerived.duePosition,
servicePeriodStart: normalizedDerived.servicePeriodStart,
servicePeriodEnd: normalizedDerived.servicePeriodEnd,
derivedInvoiceWindowStart: normalizedDerived.invoiceWindowStart,
derivedInvoiceWindowEnd: normalizedDerived.invoiceWindowEnd,
persistedInvoiceWindowStart: persisted.invoiceWindowStart,
persistedInvoiceWindowEnd: persisted.invoiceWindowEnd,
persistedLifecycleState: persisted.lifecycleState,
});
}
}
for (const persisted of persistedByKey.values()) {
drifts.push({
kind: 'unexpected_persisted_period',
scheduleKey: persisted.scheduleKey,
periodKey: persisted.periodKey,
obligationId: persisted.obligationId,
cadenceOwner: persisted.cadenceOwner,
duePosition: persisted.duePosition,
servicePeriodStart: persisted.servicePeriodStart,
servicePeriodEnd: persisted.servicePeriodEnd,
persistedInvoiceWindowStart: persisted.invoiceWindowStart,
persistedInvoiceWindowEnd: persisted.invoiceWindowEnd,
persistedLifecycleState: persisted.lifecycleState,
});
}
return {
matches: drifts.length === 0,
drifts,
};
}