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
94 lines
2.6 KiB
TypeScript
94 lines
2.6 KiB
TypeScript
import type { Knex } from 'knex';
|
|
import { v4 as uuid4 } from 'uuid';
|
|
import type { ITaxRate, ITaxRegion, ISO8601String } from '@alga-psa/types';
|
|
|
|
function isDateObject(val: unknown): val is Date {
|
|
return Object.prototype.toString.call(val) === '[object Date]';
|
|
}
|
|
|
|
function normalizeDbIsoUtcMidnight(value: unknown): ISO8601String {
|
|
if (typeof value === 'string') {
|
|
return value as ISO8601String;
|
|
}
|
|
if (isDateObject(value)) {
|
|
return value.toISOString() as ISO8601String;
|
|
}
|
|
return String(value) as ISO8601String;
|
|
}
|
|
|
|
export async function validateTaxRateDateRange(
|
|
knexOrTrx: Knex | Knex.Transaction,
|
|
tenant: string,
|
|
regionCode: string,
|
|
startDate: ISO8601String,
|
|
endDate: ISO8601String | null,
|
|
excludeTaxRateId?: string
|
|
): Promise<void> {
|
|
const query = knexOrTrx('tax_rates')
|
|
.where({
|
|
region_code: regionCode,
|
|
tenant
|
|
})
|
|
.andWhere(function () {
|
|
this.where(function () {
|
|
this.whereNull('end_date').andWhere('start_date', '<', endDate || startDate);
|
|
}).orWhere(function () {
|
|
this.whereNotNull('end_date')
|
|
.andWhere('start_date', '<', endDate || startDate)
|
|
.andWhere('end_date', '>', startDate);
|
|
});
|
|
});
|
|
|
|
if (excludeTaxRateId) {
|
|
query.andWhereNot('tax_rate_id', excludeTaxRateId);
|
|
}
|
|
|
|
const overlappingRates = await query;
|
|
if (overlappingRates.length > 0) {
|
|
throw new Error(`Tax rate date range overlaps with existing rate(s) in region ${regionCode}`);
|
|
}
|
|
}
|
|
|
|
export async function getTaxRates(
|
|
knexOrTrx: Knex | Knex.Transaction,
|
|
tenant: string
|
|
): Promise<ITaxRate[]> {
|
|
return knexOrTrx<ITaxRate>('tax_rates').where({ tenant }).select('*');
|
|
}
|
|
|
|
export async function addTaxRate(
|
|
knexOrTrx: Knex | Knex.Transaction,
|
|
tenant: string,
|
|
taxRateData: Omit<ITaxRate, 'tax_rate_id'>
|
|
): Promise<ITaxRate> {
|
|
if (!taxRateData.region_code) {
|
|
throw new Error('Region is required');
|
|
}
|
|
|
|
await validateTaxRateDateRange(
|
|
knexOrTrx,
|
|
tenant,
|
|
taxRateData.region_code,
|
|
normalizeDbIsoUtcMidnight(taxRateData.start_date),
|
|
taxRateData.end_date ? normalizeDbIsoUtcMidnight(taxRateData.end_date) : null
|
|
);
|
|
|
|
const tax_rate_id = uuid4();
|
|
const [newTaxRate] = await knexOrTrx('tax_rates')
|
|
.insert({ ...taxRateData, tax_rate_id, tenant })
|
|
.returning('*');
|
|
return newTaxRate as ITaxRate;
|
|
}
|
|
|
|
export async function getActiveTaxRegions(
|
|
knexOrTrx: Knex | Knex.Transaction,
|
|
tenant: string
|
|
): Promise<Pick<ITaxRegion, 'region_code' | 'region_name'>[]> {
|
|
return knexOrTrx<ITaxRegion>('tax_regions')
|
|
.select('region_code', 'region_name')
|
|
.where('is_active', true)
|
|
.where('tenant', tenant)
|
|
.orderBy('region_name', 'asc');
|
|
}
|
|
|