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
128 lines
3.7 KiB
TypeScript
128 lines
3.7 KiB
TypeScript
import type { Knex } from 'knex';
|
|
import { createTenantKnex } from '@alga-psa/db';
|
|
|
|
export interface TenantExternalEntityMapping {
|
|
id: string;
|
|
tenant_id: string;
|
|
integration_type: string;
|
|
alga_entity_type: string;
|
|
alga_entity_id: string;
|
|
external_entity_id: string;
|
|
external_realm_id: string | null;
|
|
sync_status: string | null;
|
|
last_synced_at: Date | string | null;
|
|
metadata: Record<string, unknown> | null;
|
|
created_at: Date | string;
|
|
updated_at: Date | string;
|
|
}
|
|
|
|
export interface LookupAlgaEntityByExternalIdResult {
|
|
algaEntityId: string;
|
|
mapping: TenantExternalEntityMapping;
|
|
}
|
|
|
|
export interface ExternalEntityMappingLookupOptions {
|
|
externalRealmId?: string | null;
|
|
knex?: Knex;
|
|
}
|
|
|
|
export interface WriteEntityMappingOptions {
|
|
externalRealmId?: string | null;
|
|
metadata?: Record<string, unknown> | null;
|
|
knex?: Knex;
|
|
}
|
|
|
|
export async function lookupAlgaEntityByExternalId(
|
|
tenant: string,
|
|
webhookSlug: string,
|
|
entityType: string,
|
|
externalId: string,
|
|
options: ExternalEntityMappingLookupOptions = {},
|
|
): Promise<LookupAlgaEntityByExternalIdResult | null> {
|
|
const db = options.knex ?? (await createTenantKnex(tenant)).knex;
|
|
const query = db<TenantExternalEntityMapping>('tenant_external_entity_mappings')
|
|
.where({
|
|
tenant_id: tenant,
|
|
integration_type: webhookSlug,
|
|
alga_entity_type: entityType,
|
|
external_entity_id: externalId,
|
|
})
|
|
.orderByRaw('external_realm_id IS NOT NULL ASC')
|
|
.orderBy('updated_at', 'desc');
|
|
|
|
if (options.externalRealmId !== undefined) {
|
|
if (options.externalRealmId === null || options.externalRealmId === '') {
|
|
query.whereNull('external_realm_id');
|
|
} else {
|
|
query.andWhere('external_realm_id', options.externalRealmId);
|
|
}
|
|
}
|
|
|
|
const mapping = await query.first();
|
|
|
|
return mapping
|
|
? {
|
|
algaEntityId: mapping.alga_entity_id,
|
|
mapping,
|
|
}
|
|
: null;
|
|
}
|
|
|
|
export async function writeEntityMapping(
|
|
tenant: string,
|
|
webhookSlug: string,
|
|
entityType: string,
|
|
algaId: string,
|
|
externalId: string,
|
|
options: WriteEntityMappingOptions = {},
|
|
): Promise<TenantExternalEntityMapping> {
|
|
const db = options.knex ?? (await createTenantKnex(tenant)).knex;
|
|
const externalRealmId = options.externalRealmId || null;
|
|
|
|
const existingExternalMapping = await db<TenantExternalEntityMapping>('tenant_external_entity_mappings')
|
|
.where({
|
|
tenant_id: tenant,
|
|
integration_type: webhookSlug,
|
|
external_entity_id: externalId,
|
|
})
|
|
.modify((query) => {
|
|
if (externalRealmId === null) {
|
|
query.whereNull('external_realm_id');
|
|
} else {
|
|
query.andWhere('external_realm_id', externalRealmId);
|
|
}
|
|
})
|
|
.first();
|
|
|
|
if (existingExternalMapping && existingExternalMapping.alga_entity_id !== algaId) {
|
|
throw new Error(
|
|
`External ${entityType} id "${externalId}" is already mapped to ${existingExternalMapping.alga_entity_id}`,
|
|
);
|
|
}
|
|
|
|
const [mapping] = await db<TenantExternalEntityMapping>('tenant_external_entity_mappings')
|
|
.insert({
|
|
tenant_id: tenant,
|
|
integration_type: webhookSlug,
|
|
alga_entity_type: entityType,
|
|
alga_entity_id: algaId,
|
|
external_entity_id: externalId,
|
|
external_realm_id: externalRealmId,
|
|
sync_status: 'synced',
|
|
last_synced_at: db.fn.now(),
|
|
metadata: options.metadata ?? null,
|
|
})
|
|
.onConflict(['tenant_id', 'integration_type', 'alga_entity_type', 'alga_entity_id'])
|
|
.merge({
|
|
external_entity_id: externalId,
|
|
external_realm_id: externalRealmId,
|
|
sync_status: 'synced',
|
|
last_synced_at: new Date().toISOString(),
|
|
metadata: options.metadata ?? null,
|
|
updated_at: new Date().toISOString(),
|
|
})
|
|
.returning('*');
|
|
|
|
return mapping;
|
|
}
|