@open-mercato/core 0.6.5-develop.4477.1.7a250f91b8 → 0.6.5-develop.4498.1.55dc06a57c
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +1 -1
- package/dist/modules/api_keys/data/validators.js +3 -1
- package/dist/modules/api_keys/data/validators.js.map +2 -2
- package/dist/modules/configs/api/openapi.js +11 -2
- package/dist/modules/configs/api/openapi.js.map +2 -2
- package/dist/modules/customers/commands/personCompanyLinks.js +77 -64
- package/dist/modules/customers/commands/personCompanyLinks.js.map +2 -2
- package/dist/modules/sales/commands/payments.js +15 -0
- package/dist/modules/sales/commands/payments.js.map +2 -2
- package/dist/modules/sales/lib/calculations.js +18 -1
- package/dist/modules/sales/lib/calculations.js.map +2 -2
- package/package.json +7 -7
- package/src/modules/api_keys/data/validators.ts +3 -1
- package/src/modules/configs/api/openapi.ts +12 -2
- package/src/modules/customers/commands/personCompanyLinks.ts +80 -65
- package/src/modules/sales/commands/payments.ts +29 -0
- package/src/modules/sales/lib/calculations.ts +32 -4
package/.turbo/turbo-build.log
CHANGED
|
@@ -4,7 +4,9 @@ const expiresAtSchema = z.preprocess((value) => {
|
|
|
4
4
|
if (value === void 0 || value === null || value === "") return null;
|
|
5
5
|
const date = value instanceof Date ? value : new Date(String(value));
|
|
6
6
|
return Number.isNaN(date.getTime()) ? void 0 : date;
|
|
7
|
-
}, z.date().nullable())
|
|
7
|
+
}, z.date().nullable().refine((date) => date === null || date.getTime() > Date.now(), {
|
|
8
|
+
message: "expiresAt must be in the future"
|
|
9
|
+
}));
|
|
8
10
|
const createApiKeySchema = z.object({
|
|
9
11
|
name: z.string().min(1).max(120),
|
|
10
12
|
description: z.string().max(1e3).optional().nullable(),
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../src/modules/api_keys/data/validators.ts"],
|
|
4
|
-
"sourcesContent": ["import { z } from 'zod'\n\nconst uuid = z.string().uuid()\n\nconst expiresAtSchema = z.preprocess((value) => {\n if (value === undefined || value === null || value === '') return null\n const date = value instanceof Date ? value : new Date(String(value))\n return Number.isNaN(date.getTime()) ? undefined : date\n}, z.date().nullable())\n\nexport const createApiKeySchema = z.object({\n name: z.string().min(1).max(120),\n description: z.string().max(1000).optional().nullable(),\n tenantId: uuid.optional().nullable(),\n organizationId: uuid.optional().nullable(),\n roles: z.array(z.string().trim().min(1)).optional().default([]),\n expiresAt: expiresAtSchema.optional(),\n})\n\nexport const updateApiKeySchema = createApiKeySchema.partial().extend({\n id: uuid,\n})\n"],
|
|
5
|
-
"mappings": "AAAA,SAAS,SAAS;AAElB,MAAM,OAAO,EAAE,OAAO,EAAE,KAAK;AAE7B,MAAM,kBAAkB,EAAE,WAAW,CAAC,UAAU;AAC9C,MAAI,UAAU,UAAa,UAAU,QAAQ,UAAU,GAAI,QAAO;AAClE,QAAM,OAAO,iBAAiB,OAAO,QAAQ,IAAI,KAAK,OAAO,KAAK,CAAC;AACnE,SAAO,OAAO,MAAM,KAAK,QAAQ,CAAC,IAAI,SAAY;AACpD,GAAG,EAAE,KAAK,EAAE,SAAS,CAAC;
|
|
4
|
+
"sourcesContent": ["import { z } from 'zod'\n\nconst uuid = z.string().uuid()\n\nconst expiresAtSchema = z.preprocess((value) => {\n if (value === undefined || value === null || value === '') return null\n const date = value instanceof Date ? value : new Date(String(value))\n return Number.isNaN(date.getTime()) ? undefined : date\n}, z.date().nullable().refine((date) => date === null || date.getTime() > Date.now(), {\n message: 'expiresAt must be in the future',\n}))\n\nexport const createApiKeySchema = z.object({\n name: z.string().min(1).max(120),\n description: z.string().max(1000).optional().nullable(),\n tenantId: uuid.optional().nullable(),\n organizationId: uuid.optional().nullable(),\n roles: z.array(z.string().trim().min(1)).optional().default([]),\n expiresAt: expiresAtSchema.optional(),\n})\n\nexport const updateApiKeySchema = createApiKeySchema.partial().extend({\n id: uuid,\n})\n"],
|
|
5
|
+
"mappings": "AAAA,SAAS,SAAS;AAElB,MAAM,OAAO,EAAE,OAAO,EAAE,KAAK;AAE7B,MAAM,kBAAkB,EAAE,WAAW,CAAC,UAAU;AAC9C,MAAI,UAAU,UAAa,UAAU,QAAQ,UAAU,GAAI,QAAO;AAClE,QAAM,OAAO,iBAAiB,OAAO,QAAQ,IAAI,KAAK,OAAO,KAAK,CAAC;AACnE,SAAO,OAAO,MAAM,KAAK,QAAQ,CAAC,IAAI,SAAY;AACpD,GAAG,EAAE,KAAK,EAAE,SAAS,EAAE,OAAO,CAAC,SAAS,SAAS,QAAQ,KAAK,QAAQ,IAAI,KAAK,IAAI,GAAG;AAAA,EACpF,SAAS;AACX,CAAC,CAAC;AAEK,MAAM,qBAAqB,EAAE,OAAO;AAAA,EACzC,MAAM,EAAE,OAAO,EAAE,IAAI,CAAC,EAAE,IAAI,GAAG;AAAA,EAC/B,aAAa,EAAE,OAAO,EAAE,IAAI,GAAI,EAAE,SAAS,EAAE,SAAS;AAAA,EACtD,UAAU,KAAK,SAAS,EAAE,SAAS;AAAA,EACnC,gBAAgB,KAAK,SAAS,EAAE,SAAS;AAAA,EACzC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,IAAI,CAAC,CAAC,EAAE,SAAS,EAAE,QAAQ,CAAC,CAAC;AAAA,EAC9D,WAAW,gBAAgB,SAAS;AACtC,CAAC;AAEM,MAAM,qBAAqB,mBAAmB,QAAQ,EAAE,OAAO;AAAA,EACpE,IAAI;AACN,CAAC;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -52,9 +52,18 @@ const systemStatusResponseSchema = z.object({
|
|
|
52
52
|
const purgeCacheResponseSchema = z.object({
|
|
53
53
|
cleared: z.boolean().describe("Whether cache was successfully cleared")
|
|
54
54
|
});
|
|
55
|
+
const cacheSegmentInfoSchema = z.object({
|
|
56
|
+
segment: z.string().describe("Sanitized segment identifier"),
|
|
57
|
+
resource: z.string().nullable().describe("Resource the segment maps to, when resolvable"),
|
|
58
|
+
method: z.string().nullable().describe("HTTP method the segment maps to, when resolvable"),
|
|
59
|
+
path: z.string().nullable().describe("Request path the segment maps to, when resolvable"),
|
|
60
|
+
keyCount: z.number().int().describe("Number of cache keys in the segment"),
|
|
61
|
+
keys: z.array(z.string()).describe("Cache keys contained in the segment")
|
|
62
|
+
});
|
|
55
63
|
const cacheStatsResponseSchema = z.object({
|
|
56
|
-
|
|
57
|
-
|
|
64
|
+
generatedAt: z.string().describe("Snapshot generation timestamp (ISO-8601)"),
|
|
65
|
+
totalKeys: z.number().int().describe("Total cache keys across all segments"),
|
|
66
|
+
segments: z.array(cacheSegmentInfoSchema).describe("Per-segment cache breakdown")
|
|
58
67
|
});
|
|
59
68
|
const cachePurgeRequestSchema = z.object({
|
|
60
69
|
action: z.enum(["purgeAll", "purgeSegment"]).describe("Cache purge action type"),
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../src/modules/configs/api/openapi.ts"],
|
|
4
|
-
"sourcesContent": ["import { z } from 'zod'\n\nexport const configsTag = 'Configs'\n\nexport const configErrorSchema = z\n .object({\n error: z.string(),\n details: z.unknown().optional(),\n })\n .passthrough()\n\nconst systemStatusCategoryKeySchema = z.enum([\n 'profiling',\n 'logging',\n 'security',\n 'caching',\n 'query_index',\n 'entities',\n])\n\nconst systemStatusVariableKindSchema = z.enum(['boolean', 'string'])\n\nconst systemStatusStateSchema = z.enum([\n 'enabled',\n 'disabled',\n 'set',\n 'unset',\n 'unknown',\n])\n\nconst systemStatusRuntimeModeSchema = z.enum([\n 'development',\n 'production',\n 'test',\n 'unknown',\n])\n\nconst systemStatusItemSchema = z.object({\n key: z.string(),\n category: systemStatusCategoryKeySchema,\n kind: systemStatusVariableKindSchema,\n labelKey: z.string(),\n descriptionKey: z.string(),\n docUrl: z.string().nullable(),\n defaultValue: z.string().nullable(),\n state: systemStatusStateSchema,\n value: z.string().nullable(),\n normalizedValue: z.string().nullable(),\n})\n\nconst systemStatusCategorySchema = z.object({\n key: systemStatusCategoryKeySchema,\n labelKey: z.string(),\n descriptionKey: z.string().nullable(),\n items: z.array(systemStatusItemSchema),\n})\n\nexport const systemStatusResponseSchema = z.object({\n generatedAt: z.string().describe('Snapshot generation timestamp (ISO-8601)'),\n runtimeMode: systemStatusRuntimeModeSchema.describe('Current runtime mode'),\n categories: z.array(systemStatusCategorySchema).describe('Grouped system status variables by category'),\n})\n\nexport const purgeCacheResponseSchema = z.object({\n cleared: z.boolean().describe('Whether cache was successfully cleared'),\n})\n\
|
|
5
|
-
"mappings": "AAAA,SAAS,SAAS;AAEX,MAAM,aAAa;AAEnB,MAAM,oBAAoB,EAC9B,OAAO;AAAA,EACN,OAAO,EAAE,OAAO;AAAA,EAChB,SAAS,EAAE,QAAQ,EAAE,SAAS;AAChC,CAAC,EACA,YAAY;AAEf,MAAM,gCAAgC,EAAE,KAAK;AAAA,EAC3C;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,CAAC;AAED,MAAM,iCAAiC,EAAE,KAAK,CAAC,WAAW,QAAQ,CAAC;AAEnE,MAAM,0BAA0B,EAAE,KAAK;AAAA,EACrC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,CAAC;AAED,MAAM,gCAAgC,EAAE,KAAK;AAAA,EAC3C;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,CAAC;AAED,MAAM,yBAAyB,EAAE,OAAO;AAAA,EACtC,KAAK,EAAE,OAAO;AAAA,EACd,UAAU;AAAA,EACV,MAAM;AAAA,EACN,UAAU,EAAE,OAAO;AAAA,EACnB,gBAAgB,EAAE,OAAO;AAAA,EACzB,QAAQ,EAAE,OAAO,EAAE,SAAS;AAAA,EAC5B,cAAc,EAAE,OAAO,EAAE,SAAS;AAAA,EAClC,OAAO;AAAA,EACP,OAAO,EAAE,OAAO,EAAE,SAAS;AAAA,EAC3B,iBAAiB,EAAE,OAAO,EAAE,SAAS;AACvC,CAAC;AAED,MAAM,6BAA6B,EAAE,OAAO;AAAA,EAC1C,KAAK;AAAA,EACL,UAAU,EAAE,OAAO;AAAA,EACnB,gBAAgB,EAAE,OAAO,EAAE,SAAS;AAAA,EACpC,OAAO,EAAE,MAAM,sBAAsB;AACvC,CAAC;AAEM,MAAM,6BAA6B,EAAE,OAAO;AAAA,EACjD,aAAa,EAAE,OAAO,EAAE,SAAS,0CAA0C;AAAA,EAC3E,aAAa,8BAA8B,SAAS,sBAAsB;AAAA,EAC1E,YAAY,EAAE,MAAM,0BAA0B,EAAE,SAAS,6CAA6C;AACxG,CAAC;AAEM,MAAM,2BAA2B,EAAE,OAAO;AAAA,EAC/C,SAAS,EAAE,QAAQ,EAAE,SAAS,wCAAwC;AACxE,CAAC;
|
|
4
|
+
"sourcesContent": ["import { z } from 'zod'\n\nexport const configsTag = 'Configs'\n\nexport const configErrorSchema = z\n .object({\n error: z.string(),\n details: z.unknown().optional(),\n })\n .passthrough()\n\nconst systemStatusCategoryKeySchema = z.enum([\n 'profiling',\n 'logging',\n 'security',\n 'caching',\n 'query_index',\n 'entities',\n])\n\nconst systemStatusVariableKindSchema = z.enum(['boolean', 'string'])\n\nconst systemStatusStateSchema = z.enum([\n 'enabled',\n 'disabled',\n 'set',\n 'unset',\n 'unknown',\n])\n\nconst systemStatusRuntimeModeSchema = z.enum([\n 'development',\n 'production',\n 'test',\n 'unknown',\n])\n\nconst systemStatusItemSchema = z.object({\n key: z.string(),\n category: systemStatusCategoryKeySchema,\n kind: systemStatusVariableKindSchema,\n labelKey: z.string(),\n descriptionKey: z.string(),\n docUrl: z.string().nullable(),\n defaultValue: z.string().nullable(),\n state: systemStatusStateSchema,\n value: z.string().nullable(),\n normalizedValue: z.string().nullable(),\n})\n\nconst systemStatusCategorySchema = z.object({\n key: systemStatusCategoryKeySchema,\n labelKey: z.string(),\n descriptionKey: z.string().nullable(),\n items: z.array(systemStatusItemSchema),\n})\n\nexport const systemStatusResponseSchema = z.object({\n generatedAt: z.string().describe('Snapshot generation timestamp (ISO-8601)'),\n runtimeMode: systemStatusRuntimeModeSchema.describe('Current runtime mode'),\n categories: z.array(systemStatusCategorySchema).describe('Grouped system status variables by category'),\n})\n\nexport const purgeCacheResponseSchema = z.object({\n cleared: z.boolean().describe('Whether cache was successfully cleared'),\n})\n\nconst cacheSegmentInfoSchema = z.object({\n segment: z.string().describe('Sanitized segment identifier'),\n resource: z.string().nullable().describe('Resource the segment maps to, when resolvable'),\n method: z.string().nullable().describe('HTTP method the segment maps to, when resolvable'),\n path: z.string().nullable().describe('Request path the segment maps to, when resolvable'),\n keyCount: z.number().int().describe('Number of cache keys in the segment'),\n keys: z.array(z.string()).describe('Cache keys contained in the segment'),\n})\n\nexport const cacheStatsResponseSchema = z.object({\n generatedAt: z.string().describe('Snapshot generation timestamp (ISO-8601)'),\n totalKeys: z.number().int().describe('Total cache keys across all segments'),\n segments: z.array(cacheSegmentInfoSchema).describe('Per-segment cache breakdown'),\n})\n\nexport const cachePurgeRequestSchema = z.object({\n action: z.enum(['purgeAll', 'purgeSegment']).describe('Cache purge action type'),\n segment: z.string().optional().describe('Segment identifier (required for purgeSegment action)'),\n})\n\nexport const cachePurgeAllResponseSchema = z.object({\n action: z.literal('purgeAll'),\n stats: cacheStatsResponseSchema,\n})\n\nexport const cachePurgeSegmentResponseSchema = z.object({\n action: z.literal('purgeSegment'),\n segment: z.string(),\n deleted: z.number().int().describe('Number of entries deleted'),\n stats: cacheStatsResponseSchema,\n})\n\nexport const upgradeActionSchema = z.object({\n id: z.string().describe('Upgrade action unique identifier'),\n version: z.string().describe('Version this action applies to'),\n message: z.string().describe('Localized description of the upgrade action'),\n ctaLabel: z.string().describe('Call-to-action button label'),\n successMessage: z.string().describe('Success message after execution'),\n loadingLabel: z.string().describe('Loading state label during execution'),\n})\n\nexport const upgradeActionsListResponseSchema = z.object({\n version: z.string().describe('Current application version'),\n actions: z.array(upgradeActionSchema).describe('List of pending upgrade actions'),\n})\n\nexport const executeUpgradeActionRequestSchema = z.object({\n actionId: z.string().min(1).describe('Upgrade action ID to execute'),\n})\n\nexport const executeUpgradeActionResponseSchema = z.object({\n status: z.string().describe('Execution status'),\n message: z.string().describe('Localized success message'),\n version: z.string().describe('Application version'),\n})\n"],
|
|
5
|
+
"mappings": "AAAA,SAAS,SAAS;AAEX,MAAM,aAAa;AAEnB,MAAM,oBAAoB,EAC9B,OAAO;AAAA,EACN,OAAO,EAAE,OAAO;AAAA,EAChB,SAAS,EAAE,QAAQ,EAAE,SAAS;AAChC,CAAC,EACA,YAAY;AAEf,MAAM,gCAAgC,EAAE,KAAK;AAAA,EAC3C;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,CAAC;AAED,MAAM,iCAAiC,EAAE,KAAK,CAAC,WAAW,QAAQ,CAAC;AAEnE,MAAM,0BAA0B,EAAE,KAAK;AAAA,EACrC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,CAAC;AAED,MAAM,gCAAgC,EAAE,KAAK;AAAA,EAC3C;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,CAAC;AAED,MAAM,yBAAyB,EAAE,OAAO;AAAA,EACtC,KAAK,EAAE,OAAO;AAAA,EACd,UAAU;AAAA,EACV,MAAM;AAAA,EACN,UAAU,EAAE,OAAO;AAAA,EACnB,gBAAgB,EAAE,OAAO;AAAA,EACzB,QAAQ,EAAE,OAAO,EAAE,SAAS;AAAA,EAC5B,cAAc,EAAE,OAAO,EAAE,SAAS;AAAA,EAClC,OAAO;AAAA,EACP,OAAO,EAAE,OAAO,EAAE,SAAS;AAAA,EAC3B,iBAAiB,EAAE,OAAO,EAAE,SAAS;AACvC,CAAC;AAED,MAAM,6BAA6B,EAAE,OAAO;AAAA,EAC1C,KAAK;AAAA,EACL,UAAU,EAAE,OAAO;AAAA,EACnB,gBAAgB,EAAE,OAAO,EAAE,SAAS;AAAA,EACpC,OAAO,E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OAAO;AACT,CAAC;AAEM,MAAM,kCAAkC,EAAE,OAAO;AAAA,EACtD,QAAQ,EAAE,QAAQ,cAAc;AAAA,EAChC,SAAS,EAAE,OAAO;AAAA,EAClB,SAAS,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,2BAA2B;AAAA,EAC9D,OAAO;AACT,CAAC;AAEM,MAAM,sBAAsB,EAAE,OAAO;AAAA,EAC1C,IAAI,EAAE,OAAO,EAAE,SAAS,kCAAkC;AAAA,EAC1D,SAAS,EAAE,OAAO,EAAE,SAAS,gCAAgC;AAAA,EAC7D,SAAS,EAAE,OAAO,EAAE,SAAS,6CAA6C;AAAA,EAC1E,UAAU,EAAE,OAAO,EAAE,SAAS,6BAA6B;AAAA,EAC3D,gBAAgB,EAAE,OAAO,EAAE,SAAS,iCAAiC;AAAA,EACrE,cAAc,EAAE,OAAO,EAAE,SAAS,sCAAsC;AAC1E,CAAC;AAEM,MAAM,mCAAmC,EAAE,OAAO;AAAA,EACvD,SAAS,EAAE,OAAO,EAAE,SAAS,6BAA6B;AAAA,EAC1D,SAAS,EAAE,MAAM,mBAAmB,EAAE,SAAS,iCAAiC;AAClF,CAAC;AAEM,MAAM,oCAAoC,EAAE,OAAO;AAAA,EACxD,UAAU,EAAE,OAAO,EAAE,IAAI,CAAC,EAAE,SAAS,8BAA8B;AACrE,CAAC;AAEM,MAAM,qCAAqC,EAAE,OAAO;AAAA,EACzD,QAAQ,EAAE,OAAO,EAAE,SAAS,kBAAkB;AAAA,EAC9C,SAAS,EAAE,OAAO,EAAE,SAAS,2BAA2B;AAAA,EACxD,SAAS,EAAE,OAAO,EAAE,SAAS,qBAAqB;AACpD,CAAC;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -230,33 +230,37 @@ const createPersonCompanyLinkCommand = {
|
|
|
230
230
|
{ tenantId: after.tenantId, organizationId: after.organizationId }
|
|
231
231
|
);
|
|
232
232
|
if (!link) return;
|
|
233
|
+
let person = null;
|
|
234
|
+
let profile = null;
|
|
235
|
+
let remainingLinks = [];
|
|
233
236
|
await withAtomicFlush(em, [
|
|
234
237
|
async () => {
|
|
235
|
-
|
|
236
|
-
link.deletedAt = /* @__PURE__ */ new Date();
|
|
237
|
-
const person = await findOneWithDecryption(
|
|
238
|
+
person = await findOneWithDecryption(
|
|
238
239
|
em,
|
|
239
240
|
CustomerEntity,
|
|
240
241
|
{ id: after.personEntityId, kind: "person", tenantId: after.tenantId, organizationId: after.organizationId, deletedAt: null },
|
|
241
242
|
void 0,
|
|
242
243
|
{ tenantId: after.tenantId, organizationId: after.organizationId }
|
|
243
244
|
);
|
|
244
|
-
if (person)
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
245
|
+
if (!person) return;
|
|
246
|
+
profile = await findOneWithDecryption(
|
|
247
|
+
em,
|
|
248
|
+
CustomerPersonProfile,
|
|
249
|
+
{ entity: person },
|
|
250
|
+
{ populate: ["company"] },
|
|
251
|
+
{ tenantId: person.tenantId, organizationId: person.organizationId }
|
|
252
|
+
);
|
|
253
|
+
if (!profile) return;
|
|
254
|
+
remainingLinks = (await loadPersonCompanyLinks(em, person)).filter((entry) => entry.id !== link.id);
|
|
255
|
+
},
|
|
256
|
+
async () => {
|
|
257
|
+
link.isPrimary = false;
|
|
258
|
+
link.deletedAt = /* @__PURE__ */ new Date();
|
|
259
|
+
if (!person || !profile) return;
|
|
260
|
+
if (after.isPrimary) {
|
|
261
|
+
await promoteFallbackPrimaryLink(em, person, profile, remainingLinks, after.companyEntityId);
|
|
262
|
+
} else if (profile.company && typeof profile.company !== "string" && profile.company.id === after.companyEntityId) {
|
|
263
|
+
profile.company = null;
|
|
260
264
|
}
|
|
261
265
|
}
|
|
262
266
|
], { transaction: true });
|
|
@@ -380,39 +384,43 @@ const updatePersonCompanyLinkCommand = {
|
|
|
380
384
|
{ tenantId: before.tenantId, organizationId: before.organizationId }
|
|
381
385
|
);
|
|
382
386
|
if (!link) return;
|
|
387
|
+
let person = null;
|
|
388
|
+
let profile = null;
|
|
389
|
+
let company = null;
|
|
383
390
|
await withAtomicFlush(em, [
|
|
384
391
|
async () => {
|
|
385
|
-
|
|
392
|
+
person = await findOneWithDecryption(
|
|
386
393
|
em,
|
|
387
394
|
CustomerEntity,
|
|
388
395
|
{ id: before.personEntityId, kind: "person", tenantId: before.tenantId, organizationId: before.organizationId, deletedAt: null },
|
|
389
396
|
void 0,
|
|
390
397
|
{ tenantId: before.tenantId, organizationId: before.organizationId }
|
|
391
398
|
);
|
|
392
|
-
if (person)
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
399
|
+
if (!person) return;
|
|
400
|
+
profile = await findOneWithDecryption(
|
|
401
|
+
em,
|
|
402
|
+
CustomerPersonProfile,
|
|
403
|
+
{ entity: person },
|
|
404
|
+
{ populate: ["company"] },
|
|
405
|
+
{ tenantId: person.tenantId, organizationId: person.organizationId }
|
|
406
|
+
);
|
|
407
|
+
if (!profile || !before.isPrimary) return;
|
|
408
|
+
company = await findOneWithDecryption(
|
|
409
|
+
em,
|
|
410
|
+
CustomerEntity,
|
|
411
|
+
{ id: before.companyEntityId, kind: "company", tenantId: before.tenantId, organizationId: before.organizationId, deletedAt: null },
|
|
412
|
+
void 0,
|
|
413
|
+
{ tenantId: before.tenantId, organizationId: before.organizationId }
|
|
414
|
+
);
|
|
415
|
+
},
|
|
416
|
+
async () => {
|
|
417
|
+
if (!person || !profile) return;
|
|
418
|
+
if (before.isPrimary) {
|
|
419
|
+
await clearPrimaryFlagsForPerson(em, person);
|
|
420
|
+
link.isPrimary = true;
|
|
421
|
+
if (company) profile.company = company;
|
|
422
|
+
} else {
|
|
423
|
+
link.isPrimary = false;
|
|
416
424
|
}
|
|
417
425
|
}
|
|
418
426
|
], { transaction: true });
|
|
@@ -527,37 +535,42 @@ const deletePersonCompanyLinkCommand = {
|
|
|
527
535
|
{ tenantId: before.tenantId, organizationId: before.organizationId }
|
|
528
536
|
);
|
|
529
537
|
if (!link) return;
|
|
538
|
+
let person = null;
|
|
539
|
+
let profile = null;
|
|
540
|
+
let company = null;
|
|
530
541
|
await withAtomicFlush(em, [
|
|
531
542
|
async () => {
|
|
532
|
-
|
|
533
|
-
link.isPrimary = before.isPrimary;
|
|
534
|
-
const person = await findOneWithDecryption(
|
|
543
|
+
person = await findOneWithDecryption(
|
|
535
544
|
em,
|
|
536
545
|
CustomerEntity,
|
|
537
546
|
{ id: before.personEntityId, kind: "person", tenantId: before.tenantId, organizationId: before.organizationId, deletedAt: null },
|
|
538
547
|
void 0,
|
|
539
548
|
{ tenantId: before.tenantId, organizationId: before.organizationId }
|
|
540
549
|
);
|
|
550
|
+
if (!person || !before.isPrimary) return;
|
|
551
|
+
profile = await findOneWithDecryption(
|
|
552
|
+
em,
|
|
553
|
+
CustomerPersonProfile,
|
|
554
|
+
{ entity: person },
|
|
555
|
+
{ populate: ["company"] },
|
|
556
|
+
{ tenantId: person.tenantId, organizationId: person.organizationId }
|
|
557
|
+
);
|
|
558
|
+
if (!profile) return;
|
|
559
|
+
company = await findOneWithDecryption(
|
|
560
|
+
em,
|
|
561
|
+
CustomerEntity,
|
|
562
|
+
{ id: before.companyEntityId, kind: "company", tenantId: before.tenantId, organizationId: before.organizationId, deletedAt: null },
|
|
563
|
+
void 0,
|
|
564
|
+
{ tenantId: before.tenantId, organizationId: before.organizationId }
|
|
565
|
+
);
|
|
566
|
+
},
|
|
567
|
+
async () => {
|
|
568
|
+
link.deletedAt = null;
|
|
569
|
+
link.isPrimary = before.isPrimary;
|
|
541
570
|
if (person && before.isPrimary) {
|
|
542
571
|
await clearPrimaryFlagsForPerson(em, person);
|
|
543
572
|
link.isPrimary = true;
|
|
544
|
-
|
|
545
|
-
em,
|
|
546
|
-
CustomerPersonProfile,
|
|
547
|
-
{ entity: person },
|
|
548
|
-
{ populate: ["company"] },
|
|
549
|
-
{ tenantId: person.tenantId, organizationId: person.organizationId }
|
|
550
|
-
);
|
|
551
|
-
if (profile) {
|
|
552
|
-
const company = await findOneWithDecryption(
|
|
553
|
-
em,
|
|
554
|
-
CustomerEntity,
|
|
555
|
-
{ id: before.companyEntityId, kind: "company", tenantId: before.tenantId, organizationId: before.organizationId, deletedAt: null },
|
|
556
|
-
void 0,
|
|
557
|
-
{ tenantId: before.tenantId, organizationId: before.organizationId }
|
|
558
|
-
);
|
|
559
|
-
if (company) profile.company = company;
|
|
560
|
-
}
|
|
573
|
+
if (profile && company) profile.company = company;
|
|
561
574
|
}
|
|
562
575
|
}
|
|
563
576
|
], { transaction: true });
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../src/modules/customers/commands/personCompanyLinks.ts"],
|
|
4
|
-
"sourcesContent": ["import { registerCommand } from '@open-mercato/shared/lib/commands'\nimport type { CommandHandler } from '@open-mercato/shared/lib/commands'\nimport type { CrudEventsConfig } from '@open-mercato/shared/lib/crud/types'\nimport { emitCrudSideEffects, emitCrudUndoSideEffects } from '@open-mercato/shared/lib/commands/helpers'\nimport { CrudHttpError } from '@open-mercato/shared/lib/crud/errors'\nimport { resolveTranslations } from '@open-mercato/shared/lib/i18n/server'\nimport { findOneWithDecryption } from '@open-mercato/shared/lib/encryption/find'\nimport type { DataEngine } from '@open-mercato/shared/lib/data/engine'\nimport type { EntityManager } from '@mikro-orm/postgresql'\nimport {\n CustomerEntity,\n CustomerPersonCompanyLink,\n CustomerPersonProfile,\n} from '../data/entities'\nimport {\n personCompanyLinkCreateSchema,\n personCompanyLinkDeleteSchema,\n personCompanyLinkUpdateSchema,\n type PersonCompanyLinkCreateInput,\n type PersonCompanyLinkDeleteInput,\n type PersonCompanyLinkUpdateInput,\n} from '../data/validators'\nimport {\n findDeletedPersonCompanyLink,\n loadPersonCompanyLinks,\n promoteFallbackPrimaryLink,\n} from '../lib/personCompanies'\nimport {\n ensureOrganizationScope,\n ensureTenantScope,\n extractUndoPayload,\n} from './shared'\nimport { withAtomicFlush } from '@open-mercato/shared/lib/commands/flush'\n\ntype PersonCompanyLinkSnapshot = {\n id: string\n personEntityId: string\n companyEntityId: string\n isPrimary: boolean\n tenantId: string\n organizationId: string\n deletedAt: string | null\n}\n\ntype PersonCompanyLinkUndoPayload = {\n before?: PersonCompanyLinkSnapshot | null\n after?: PersonCompanyLinkSnapshot | null\n}\n\nconst personCompanyLinkCrudEvents: CrudEventsConfig = {\n module: 'customers',\n entity: 'person_company_link',\n persistent: true,\n buildPayload: (ctx) => ({\n id: ctx.identifiers.id,\n organizationId: ctx.identifiers.organizationId,\n tenantId: ctx.identifiers.tenantId,\n ...(ctx.entity && typeof ctx.entity === 'object' && 'person' in (ctx.entity as Record<string, unknown>)\n ? {\n personEntityId:\n typeof (ctx.entity as CustomerPersonCompanyLink).person === 'string'\n ? (ctx.entity as any).person\n : (ctx.entity as CustomerPersonCompanyLink).person?.id ?? null,\n companyEntityId:\n typeof (ctx.entity as CustomerPersonCompanyLink).company === 'string'\n ? (ctx.entity as any).company\n : (ctx.entity as CustomerPersonCompanyLink).company?.id ?? null,\n }\n : {}),\n ...(ctx.syncOrigin ? { syncOrigin: ctx.syncOrigin } : {}),\n }),\n}\n\nfunction getLinkIdentifiers(link: CustomerPersonCompanyLink) {\n return {\n id: link.id,\n organizationId: link.organizationId,\n tenantId: link.tenantId,\n }\n}\n\nasync function loadPersonCompanyLinkSnapshot(\n em: EntityManager,\n id: string,\n scope?: { tenantId?: string | null; organizationId?: string | null },\n): Promise<PersonCompanyLinkSnapshot | null> {\n const filter: Record<string, unknown> = { id }\n if (scope?.tenantId) filter.tenantId = scope.tenantId\n if (scope?.organizationId) filter.organizationId = scope.organizationId\n const link = await findOneWithDecryption(\n em,\n CustomerPersonCompanyLink,\n filter,\n undefined,\n {\n tenantId: scope?.tenantId ?? null,\n organizationId: scope?.organizationId ?? null,\n },\n )\n if (!link) return null\n const personId = typeof link.person === 'string' ? link.person : link.person.id\n const companyId = typeof link.company === 'string' ? link.company : link.company.id\n return {\n id: link.id,\n personEntityId: personId,\n companyEntityId: companyId,\n isPrimary: Boolean(link.isPrimary),\n tenantId: link.tenantId,\n organizationId: link.organizationId,\n deletedAt: link.deletedAt ? link.deletedAt.toISOString() : null,\n }\n}\n\nasync function requirePersonEntity(\n em: EntityManager,\n entityId: string,\n tenantId: string,\n organizationId: string,\n): Promise<CustomerEntity> {\n const person = await findOneWithDecryption(\n em,\n CustomerEntity,\n { id: entityId, kind: 'person', tenantId, organizationId, deletedAt: null },\n undefined,\n { tenantId, organizationId },\n )\n if (!person) {\n throw new CrudHttpError(404, { error: 'Person not found' })\n }\n return person\n}\n\nasync function requireCompanyEntity(\n em: EntityManager,\n entityId: string,\n tenantId: string,\n organizationId: string,\n): Promise<CustomerEntity> {\n const company = await findOneWithDecryption(\n em,\n CustomerEntity,\n { id: entityId, kind: 'company', tenantId, organizationId, deletedAt: null },\n undefined,\n { tenantId, organizationId },\n )\n if (!company) {\n throw new CrudHttpError(404, { error: 'Company not found' })\n }\n return company\n}\n\nasync function requirePersonProfile(\n em: EntityManager,\n person: CustomerEntity,\n): Promise<CustomerPersonProfile> {\n const profile = await findOneWithDecryption(\n em,\n CustomerPersonProfile,\n { entity: person },\n { populate: ['company'] },\n { tenantId: person.tenantId, organizationId: person.organizationId },\n )\n if (!profile) {\n throw new CrudHttpError(404, { error: 'Person profile not found' })\n }\n return profile\n}\n\nasync function clearPrimaryFlagsForPerson(em: EntityManager, person: CustomerEntity): Promise<void> {\n await em.nativeUpdate(\n CustomerPersonCompanyLink,\n { person, organizationId: person.organizationId, tenantId: person.tenantId, isPrimary: true },\n { isPrimary: false },\n )\n}\n\nconst createPersonCompanyLinkCommand: CommandHandler<PersonCompanyLinkCreateInput, { linkId: string; created: boolean; undeleted: boolean }> = {\n id: 'customers.personCompanyLinks.create',\n async execute(rawInput, ctx) {\n const parsed = personCompanyLinkCreateSchema.parse(rawInput)\n ensureTenantScope(ctx, parsed.tenantId)\n ensureOrganizationScope(ctx, parsed.organizationId)\n\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const person = await requirePersonEntity(em, parsed.personEntityId, parsed.tenantId, parsed.organizationId)\n const company = await requireCompanyEntity(em, parsed.companyEntityId, parsed.tenantId, parsed.organizationId)\n const profile = await requirePersonProfile(em, person)\n\n const existingLinks = await loadPersonCompanyLinks(em, person)\n const makePrimary = Boolean(parsed.isPrimary) || existingLinks.length === 0\n const existingLive =\n existingLinks.find((link) => (typeof link.company === 'string' ? link.company : link.company.id) === company.id) ?? null\n\n if (existingLive) {\n if (makePrimary && !existingLive.isPrimary) {\n await withAtomicFlush(em, [\n () => clearPrimaryFlagsForPerson(em, person),\n () => {\n existingLive.isPrimary = true\n profile.company = company\n },\n ], { transaction: true })\n }\n return { linkId: existingLive.id, created: false, undeleted: false }\n }\n\n let link!: CustomerPersonCompanyLink\n let undeleted = false\n await withAtomicFlush(em, [\n async () => {\n const deletedLink = await findDeletedPersonCompanyLink(em, person, company)\n if (makePrimary) {\n await clearPrimaryFlagsForPerson(em, person)\n }\n if (deletedLink) {\n deletedLink.deletedAt = null\n deletedLink.isPrimary = makePrimary\n em.persist(deletedLink)\n link = deletedLink\n undeleted = true\n } else {\n link = em.create(CustomerPersonCompanyLink, {\n organizationId: parsed.organizationId,\n tenantId: parsed.tenantId,\n person,\n company,\n isPrimary: makePrimary,\n })\n em.persist(link)\n }\n },\n () => {\n if (makePrimary) {\n profile.company = company\n } else if (!profile.company && existingLinks.length === 0) {\n profile.company = company\n link.isPrimary = true\n }\n },\n ], { transaction: true })\n\n const dataEngine = ctx.container.resolve('dataEngine') as DataEngine\n await emitCrudSideEffects({\n dataEngine,\n action: undeleted ? 'updated' : 'created',\n entity: link,\n identifiers: getLinkIdentifiers(link),\n syncOrigin: ctx.syncOrigin,\n events: personCompanyLinkCrudEvents,\n indexer: { entityType: 'customers:customer_person_company_link' },\n })\n\n return { linkId: link.id, created: !undeleted, undeleted }\n },\n captureAfter: async (_input, result, ctx) => {\n const em = ctx.container.resolve('em') as EntityManager\n return loadPersonCompanyLinkSnapshot(em, result.linkId, {\n tenantId: ctx.auth?.tenantId ?? null,\n organizationId: ctx.selectedOrganizationId ?? ctx.auth?.orgId ?? null,\n })\n },\n buildLog: async ({ result, snapshots }) => {\n const { translate } = await resolveTranslations()\n const after = snapshots.after as PersonCompanyLinkSnapshot | undefined\n return {\n actionLabel: translate('customers.audit.personCompanyLinks.create', 'Link company to person'),\n resourceKind: 'customers.personCompanyLink',\n resourceId: result.linkId,\n parentResourceKind: 'customers.person',\n parentResourceId: after?.personEntityId ?? null,\n tenantId: after?.tenantId ?? null,\n organizationId: after?.organizationId ?? null,\n snapshotAfter: after ?? null,\n payload: {\n undo: {\n after: after ?? null,\n ...(result.undeleted ? { before: null } : {}),\n } satisfies PersonCompanyLinkUndoPayload,\n },\n }\n },\n undo: async ({ logEntry, ctx }) => {\n const payload = extractUndoPayload<PersonCompanyLinkUndoPayload>(logEntry)\n const after = payload?.after\n if (!after) return\n\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const link = await findOneWithDecryption(\n em,\n CustomerPersonCompanyLink,\n { id: after.id },\n undefined,\n { tenantId: after.tenantId, organizationId: after.organizationId },\n )\n if (!link) return\n\n await withAtomicFlush(em, [\n async () => {\n link.isPrimary = false\n link.deletedAt = new Date()\n const person = await findOneWithDecryption(\n em,\n CustomerEntity,\n { id: after.personEntityId, kind: 'person', tenantId: after.tenantId, organizationId: after.organizationId, deletedAt: null },\n undefined,\n { tenantId: after.tenantId, organizationId: after.organizationId },\n )\n if (person) {\n const profile = await findOneWithDecryption(\n em,\n CustomerPersonProfile,\n { entity: person },\n { populate: ['company'] },\n { tenantId: person.tenantId, organizationId: person.organizationId },\n )\n if (profile) {\n const remainingLinks = await loadPersonCompanyLinks(em, person)\n if (after.isPrimary) {\n await promoteFallbackPrimaryLink(em, person, profile, remainingLinks, after.companyEntityId)\n } else if (profile.company && typeof profile.company !== 'string' && profile.company.id === after.companyEntityId) {\n profile.company = null\n }\n }\n }\n },\n ], { transaction: true })\n\n const dataEngine = ctx.container.resolve('dataEngine') as DataEngine\n await emitCrudUndoSideEffects({\n dataEngine,\n action: 'deleted',\n entity: link,\n identifiers: getLinkIdentifiers(link),\n syncOrigin: ctx.syncOrigin,\n events: personCompanyLinkCrudEvents,\n indexer: { entityType: 'customers:customer_person_company_link' },\n })\n },\n}\n\nconst updatePersonCompanyLinkCommand: CommandHandler<PersonCompanyLinkUpdateInput, { linkId: string }> = {\n id: 'customers.personCompanyLinks.update',\n async prepare(rawInput, ctx) {\n const parsed = personCompanyLinkUpdateSchema.parse(rawInput)\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const snapshot = await loadPersonCompanyLinkSnapshot(em, parsed.linkId, {\n tenantId: ctx.auth?.tenantId ?? null,\n organizationId: ctx.selectedOrganizationId ?? ctx.auth?.orgId ?? null,\n })\n return snapshot ? { before: snapshot } : {}\n },\n async execute(rawInput, ctx) {\n const parsed = personCompanyLinkUpdateSchema.parse(rawInput)\n ensureTenantScope(ctx, parsed.tenantId)\n ensureOrganizationScope(ctx, parsed.organizationId)\n\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const link = await findOneWithDecryption(\n em,\n CustomerPersonCompanyLink,\n {\n id: parsed.linkId,\n tenantId: parsed.tenantId,\n organizationId: parsed.organizationId,\n deletedAt: null,\n },\n undefined,\n { tenantId: parsed.tenantId, organizationId: parsed.organizationId },\n )\n if (!link) {\n throw new CrudHttpError(404, { error: 'Company link not found' })\n }\n\n const personId = typeof link.person === 'string' ? link.person : link.person.id\n const companyId = typeof link.company === 'string' ? link.company : link.company.id\n const person = await requirePersonEntity(em, personId, parsed.tenantId, parsed.organizationId)\n const profile = await requirePersonProfile(em, person)\n const linkedCompany = await requireCompanyEntity(em, companyId, parsed.tenantId, parsed.organizationId)\n\n await withAtomicFlush(em, [\n async () => {\n if (parsed.isPrimary) {\n await clearPrimaryFlagsForPerson(em, person)\n link.isPrimary = true\n profile.company = linkedCompany\n } else if (!parsed.isPrimary) {\n const linkWasPrimary = link.isPrimary\n link.isPrimary = false\n if (linkWasPrimary) {\n const remainingLinks = (await loadPersonCompanyLinks(em, person)).filter((entry) => entry.id !== link.id)\n await promoteFallbackPrimaryLink(em, person, profile, remainingLinks, companyId)\n } else if (profile.company && typeof profile.company !== 'string' && profile.company.id === companyId) {\n profile.company = null\n }\n }\n },\n ], { transaction: true })\n\n const dataEngine = ctx.container.resolve('dataEngine') as DataEngine\n await emitCrudSideEffects({\n dataEngine,\n action: 'updated',\n entity: link,\n identifiers: getLinkIdentifiers(link),\n syncOrigin: ctx.syncOrigin,\n events: personCompanyLinkCrudEvents,\n indexer: { entityType: 'customers:customer_person_company_link' },\n })\n\n return { linkId: link.id }\n },\n captureAfter: async (_input, result, ctx) => {\n const em = ctx.container.resolve('em') as EntityManager\n return loadPersonCompanyLinkSnapshot(em, result.linkId, {\n tenantId: ctx.auth?.tenantId ?? null,\n organizationId: ctx.selectedOrganizationId ?? ctx.auth?.orgId ?? null,\n })\n },\n buildLog: async ({ result, snapshots }) => {\n const { translate } = await resolveTranslations()\n const before = snapshots.before as PersonCompanyLinkSnapshot | undefined\n const after = snapshots.after as PersonCompanyLinkSnapshot | undefined\n return {\n actionLabel: translate('customers.audit.personCompanyLinks.update', 'Update company link'),\n resourceKind: 'customers.personCompanyLink',\n resourceId: result.linkId,\n parentResourceKind: 'customers.person',\n parentResourceId: after?.personEntityId ?? before?.personEntityId ?? null,\n tenantId: after?.tenantId ?? before?.tenantId ?? null,\n organizationId: after?.organizationId ?? before?.organizationId ?? null,\n snapshotBefore: before ?? null,\n snapshotAfter: after ?? null,\n payload: {\n undo: {\n before: before ?? null,\n after: after ?? null,\n } satisfies PersonCompanyLinkUndoPayload,\n },\n }\n },\n undo: async ({ logEntry, ctx }) => {\n const payload = extractUndoPayload<PersonCompanyLinkUndoPayload>(logEntry)\n const before = payload?.before\n if (!before) return\n\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const link = await findOneWithDecryption(\n em,\n CustomerPersonCompanyLink,\n { id: before.id },\n undefined,\n { tenantId: before.tenantId, organizationId: before.organizationId },\n )\n if (!link) return\n\n await withAtomicFlush(em, [\n async () => {\n const person = await findOneWithDecryption(\n em,\n CustomerEntity,\n { id: before.personEntityId, kind: 'person', tenantId: before.tenantId, organizationId: before.organizationId, deletedAt: null },\n undefined,\n { tenantId: before.tenantId, organizationId: before.organizationId },\n )\n if (person) {\n const profile = await findOneWithDecryption(\n em,\n CustomerPersonProfile,\n { entity: person },\n { populate: ['company'] },\n { tenantId: person.tenantId, organizationId: person.organizationId },\n )\n if (profile) {\n if (before.isPrimary) {\n await clearPrimaryFlagsForPerson(em, person)\n link.isPrimary = true\n const company = await findOneWithDecryption(\n em,\n CustomerEntity,\n { id: before.companyEntityId, kind: 'company', tenantId: before.tenantId, organizationId: before.organizationId, deletedAt: null },\n undefined,\n { tenantId: before.tenantId, organizationId: before.organizationId },\n )\n if (company) profile.company = company\n } else {\n link.isPrimary = false\n }\n }\n }\n },\n ], { transaction: true })\n\n const dataEngine = ctx.container.resolve('dataEngine') as DataEngine\n await emitCrudUndoSideEffects({\n dataEngine,\n action: 'updated',\n entity: link,\n identifiers: getLinkIdentifiers(link),\n syncOrigin: ctx.syncOrigin,\n events: personCompanyLinkCrudEvents,\n indexer: { entityType: 'customers:customer_person_company_link' },\n })\n },\n}\n\nconst deletePersonCompanyLinkCommand: CommandHandler<PersonCompanyLinkDeleteInput, { linkId: string }> = {\n id: 'customers.personCompanyLinks.delete',\n async prepare(rawInput, ctx) {\n const parsed = personCompanyLinkDeleteSchema.parse(rawInput)\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const snapshot = await loadPersonCompanyLinkSnapshot(em, parsed.linkId, {\n tenantId: ctx.auth?.tenantId ?? null,\n organizationId: ctx.selectedOrganizationId ?? ctx.auth?.orgId ?? null,\n })\n return snapshot ? { before: snapshot } : {}\n },\n async execute(rawInput, ctx) {\n const parsed = personCompanyLinkDeleteSchema.parse(rawInput)\n ensureTenantScope(ctx, parsed.tenantId)\n ensureOrganizationScope(ctx, parsed.organizationId)\n\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const link = await findOneWithDecryption(\n em,\n CustomerPersonCompanyLink,\n {\n id: parsed.linkId,\n tenantId: parsed.tenantId,\n organizationId: parsed.organizationId,\n deletedAt: null,\n },\n undefined,\n { tenantId: parsed.tenantId, organizationId: parsed.organizationId },\n )\n if (!link) {\n throw new CrudHttpError(404, { error: 'Company link not found' })\n }\n\n const personId = typeof link.person === 'string' ? link.person : link.person.id\n const companyId = typeof link.company === 'string' ? link.company : link.company.id\n const person = await requirePersonEntity(em, personId, parsed.tenantId, parsed.organizationId)\n const profile = await requirePersonProfile(em, person)\n const linkWasPrimary = link.isPrimary\n\n const existingLinks = await loadPersonCompanyLinks(em, person)\n const remainingLinks = existingLinks.filter((entry) => entry.id !== link.id)\n\n await withAtomicFlush(em, [\n () => {\n link.isPrimary = false\n link.deletedAt = new Date()\n },\n async () => {\n if (linkWasPrimary) {\n await promoteFallbackPrimaryLink(em, person, profile, remainingLinks, companyId)\n } else if (profile.company && typeof profile.company !== 'string' && profile.company.id === companyId) {\n const primary = remainingLinks.find((entry) => entry.isPrimary) ?? null\n const primaryCompany = primary && typeof primary.company !== 'string' ? primary.company : null\n if (primaryCompany) {\n profile.company = primaryCompany\n }\n }\n },\n ], { transaction: true })\n\n const dataEngine = ctx.container.resolve('dataEngine') as DataEngine\n await emitCrudSideEffects({\n dataEngine,\n action: 'deleted',\n entity: link,\n identifiers: getLinkIdentifiers(link),\n syncOrigin: ctx.syncOrigin,\n events: personCompanyLinkCrudEvents,\n indexer: { entityType: 'customers:customer_person_company_link' },\n })\n\n return { linkId: link.id }\n },\n buildLog: async ({ result, snapshots }) => {\n const { translate } = await resolveTranslations()\n const before = snapshots.before as PersonCompanyLinkSnapshot | undefined\n return {\n actionLabel: translate('customers.audit.personCompanyLinks.delete', 'Remove company link'),\n resourceKind: 'customers.personCompanyLink',\n resourceId: result.linkId,\n parentResourceKind: 'customers.person',\n parentResourceId: before?.personEntityId ?? null,\n tenantId: before?.tenantId ?? null,\n organizationId: before?.organizationId ?? null,\n snapshotBefore: before ?? null,\n payload: {\n undo: {\n before: before ?? null,\n } satisfies PersonCompanyLinkUndoPayload,\n },\n }\n },\n undo: async ({ logEntry, ctx }) => {\n const payload = extractUndoPayload<PersonCompanyLinkUndoPayload>(logEntry)\n const before = payload?.before\n if (!before) return\n\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const link = await findOneWithDecryption(\n em,\n CustomerPersonCompanyLink,\n { id: before.id },\n undefined,\n { tenantId: before.tenantId, organizationId: before.organizationId },\n )\n if (!link) return\n\n await withAtomicFlush(em, [\n async () => {\n link.deletedAt = null\n link.isPrimary = before.isPrimary\n\n const person = await findOneWithDecryption(\n em,\n CustomerEntity,\n { id: before.personEntityId, kind: 'person', tenantId: before.tenantId, organizationId: before.organizationId, deletedAt: null },\n undefined,\n { tenantId: before.tenantId, organizationId: before.organizationId },\n )\n if (person && before.isPrimary) {\n await clearPrimaryFlagsForPerson(em, person)\n link.isPrimary = true\n const profile = await findOneWithDecryption(\n em,\n CustomerPersonProfile,\n { entity: person },\n { populate: ['company'] },\n { tenantId: person.tenantId, organizationId: person.organizationId },\n )\n if (profile) {\n const company = await findOneWithDecryption(\n em,\n CustomerEntity,\n { id: before.companyEntityId, kind: 'company', tenantId: before.tenantId, organizationId: before.organizationId, deletedAt: null },\n undefined,\n { tenantId: before.tenantId, organizationId: before.organizationId },\n )\n if (company) profile.company = company\n }\n }\n },\n ], { transaction: true })\n\n const dataEngine = ctx.container.resolve('dataEngine') as DataEngine\n await emitCrudUndoSideEffects({\n dataEngine,\n action: 'created',\n entity: link,\n identifiers: getLinkIdentifiers(link),\n syncOrigin: ctx.syncOrigin,\n events: personCompanyLinkCrudEvents,\n indexer: { entityType: 'customers:customer_person_company_link' },\n })\n },\n}\n\nregisterCommand(createPersonCompanyLinkCommand)\nregisterCommand(updatePersonCompanyLinkCommand)\nregisterCommand(deletePersonCompanyLinkCommand)\n"],
|
|
5
|
-
"mappings": "AAAA,SAAS,uBAAuB;AAGhC,SAAS,qBAAqB,+BAA+B;AAC7D,SAAS,qBAAqB;AAC9B,SAAS,2BAA2B;AACpC,SAAS,6BAA6B;AAGtC;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OAIK;AACP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,uBAAuB;AAiBhC,MAAM,8BAAgD;AAAA,EACpD,QAAQ;AAAA,EACR,QAAQ;AAAA,EACR,YAAY;AAAA,EACZ,cAAc,CAAC,SAAS;AAAA,IACtB,IAAI,IAAI,YAAY;AAAA,IACpB,gBAAgB,IAAI,YAAY;AAAA,IAChC,UAAU,IAAI,YAAY;AAAA,IAC1B,GAAI,IAAI,UAAU,OAAO,IAAI,WAAW,YAAY,YAAa,IAAI,SACjE;AAAA,MACE,gBACE,OAAQ,IAAI,OAAqC,WAAW,WACvD,IAAI,OAAe,SACnB,IAAI,OAAqC,QAAQ,MAAM;AAAA,MAC9D,iBACE,OAAQ,IAAI,OAAqC,YAAY,WACxD,IAAI,OAAe,UACnB,IAAI,OAAqC,SAAS,MAAM;AAAA,IACjE,IACA,CAAC;AAAA,IACL,GAAI,IAAI,aAA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GAAG,EAAE,aAAa,KAAK,CAAC;AAExB,UAAM,aAAa,IAAI,UAAU,QAAQ,YAAY;AACrD,UAAM,wBAAwB;AAAA,MAC5B;AAAA,MACA,QAAQ;AAAA,MACR,QAAQ;AAAA,MACR,aAAa,mBAAmB,IAAI;AAAA,MACpC,YAAY,IAAI;AAAA,MAChB,QAAQ;AAAA,MACR,SAAS,EAAE,YAAY,yCAAyC;AAAA,IAClE,CAAC;AAAA,EACH;AACF;AAEA,gBAAgB,8BAA8B;AAC9C,gBAAgB,8BAA8B;AAC9C,gBAAgB,8BAA8B;",
|
|
4
|
+
"sourcesContent": ["import { registerCommand } from '@open-mercato/shared/lib/commands'\nimport type { CommandHandler } from '@open-mercato/shared/lib/commands'\nimport type { CrudEventsConfig } from '@open-mercato/shared/lib/crud/types'\nimport { emitCrudSideEffects, emitCrudUndoSideEffects } from '@open-mercato/shared/lib/commands/helpers'\nimport { CrudHttpError } from '@open-mercato/shared/lib/crud/errors'\nimport { resolveTranslations } from '@open-mercato/shared/lib/i18n/server'\nimport { findOneWithDecryption } from '@open-mercato/shared/lib/encryption/find'\nimport type { DataEngine } from '@open-mercato/shared/lib/data/engine'\nimport type { EntityManager } from '@mikro-orm/postgresql'\nimport {\n CustomerEntity,\n CustomerPersonCompanyLink,\n CustomerPersonProfile,\n} from '../data/entities'\nimport {\n personCompanyLinkCreateSchema,\n personCompanyLinkDeleteSchema,\n personCompanyLinkUpdateSchema,\n type PersonCompanyLinkCreateInput,\n type PersonCompanyLinkDeleteInput,\n type PersonCompanyLinkUpdateInput,\n} from '../data/validators'\nimport {\n findDeletedPersonCompanyLink,\n loadPersonCompanyLinks,\n promoteFallbackPrimaryLink,\n} from '../lib/personCompanies'\nimport {\n ensureOrganizationScope,\n ensureTenantScope,\n extractUndoPayload,\n} from './shared'\nimport { withAtomicFlush } from '@open-mercato/shared/lib/commands/flush'\n\ntype PersonCompanyLinkSnapshot = {\n id: string\n personEntityId: string\n companyEntityId: string\n isPrimary: boolean\n tenantId: string\n organizationId: string\n deletedAt: string | null\n}\n\ntype PersonCompanyLinkUndoPayload = {\n before?: PersonCompanyLinkSnapshot | null\n after?: PersonCompanyLinkSnapshot | null\n}\n\nconst personCompanyLinkCrudEvents: CrudEventsConfig = {\n module: 'customers',\n entity: 'person_company_link',\n persistent: true,\n buildPayload: (ctx) => ({\n id: ctx.identifiers.id,\n organizationId: ctx.identifiers.organizationId,\n tenantId: ctx.identifiers.tenantId,\n ...(ctx.entity && typeof ctx.entity === 'object' && 'person' in (ctx.entity as Record<string, unknown>)\n ? {\n personEntityId:\n typeof (ctx.entity as CustomerPersonCompanyLink).person === 'string'\n ? (ctx.entity as any).person\n : (ctx.entity as CustomerPersonCompanyLink).person?.id ?? null,\n companyEntityId:\n typeof (ctx.entity as CustomerPersonCompanyLink).company === 'string'\n ? (ctx.entity as any).company\n : (ctx.entity as CustomerPersonCompanyLink).company?.id ?? null,\n }\n : {}),\n ...(ctx.syncOrigin ? { syncOrigin: ctx.syncOrigin } : {}),\n }),\n}\n\nfunction getLinkIdentifiers(link: CustomerPersonCompanyLink) {\n return {\n id: link.id,\n organizationId: link.organizationId,\n tenantId: link.tenantId,\n }\n}\n\nasync function loadPersonCompanyLinkSnapshot(\n em: EntityManager,\n id: string,\n scope?: { tenantId?: string | null; organizationId?: string | null },\n): Promise<PersonCompanyLinkSnapshot | null> {\n const filter: Record<string, unknown> = { id }\n if (scope?.tenantId) filter.tenantId = scope.tenantId\n if (scope?.organizationId) filter.organizationId = scope.organizationId\n const link = await findOneWithDecryption(\n em,\n CustomerPersonCompanyLink,\n filter,\n undefined,\n {\n tenantId: scope?.tenantId ?? null,\n organizationId: scope?.organizationId ?? null,\n },\n )\n if (!link) return null\n const personId = typeof link.person === 'string' ? link.person : link.person.id\n const companyId = typeof link.company === 'string' ? link.company : link.company.id\n return {\n id: link.id,\n personEntityId: personId,\n companyEntityId: companyId,\n isPrimary: Boolean(link.isPrimary),\n tenantId: link.tenantId,\n organizationId: link.organizationId,\n deletedAt: link.deletedAt ? link.deletedAt.toISOString() : null,\n }\n}\n\nasync function requirePersonEntity(\n em: EntityManager,\n entityId: string,\n tenantId: string,\n organizationId: string,\n): Promise<CustomerEntity> {\n const person = await findOneWithDecryption(\n em,\n CustomerEntity,\n { id: entityId, kind: 'person', tenantId, organizationId, deletedAt: null },\n undefined,\n { tenantId, organizationId },\n )\n if (!person) {\n throw new CrudHttpError(404, { error: 'Person not found' })\n }\n return person\n}\n\nasync function requireCompanyEntity(\n em: EntityManager,\n entityId: string,\n tenantId: string,\n organizationId: string,\n): Promise<CustomerEntity> {\n const company = await findOneWithDecryption(\n em,\n CustomerEntity,\n { id: entityId, kind: 'company', tenantId, organizationId, deletedAt: null },\n undefined,\n { tenantId, organizationId },\n )\n if (!company) {\n throw new CrudHttpError(404, { error: 'Company not found' })\n }\n return company\n}\n\nasync function requirePersonProfile(\n em: EntityManager,\n person: CustomerEntity,\n): Promise<CustomerPersonProfile> {\n const profile = await findOneWithDecryption(\n em,\n CustomerPersonProfile,\n { entity: person },\n { populate: ['company'] },\n { tenantId: person.tenantId, organizationId: person.organizationId },\n )\n if (!profile) {\n throw new CrudHttpError(404, { error: 'Person profile not found' })\n }\n return profile\n}\n\nasync function clearPrimaryFlagsForPerson(em: EntityManager, person: CustomerEntity): Promise<void> {\n await em.nativeUpdate(\n CustomerPersonCompanyLink,\n { person, organizationId: person.organizationId, tenantId: person.tenantId, isPrimary: true },\n { isPrimary: false },\n )\n}\n\nconst createPersonCompanyLinkCommand: CommandHandler<PersonCompanyLinkCreateInput, { linkId: string; created: boolean; undeleted: boolean }> = {\n id: 'customers.personCompanyLinks.create',\n async execute(rawInput, ctx) {\n const parsed = personCompanyLinkCreateSchema.parse(rawInput)\n ensureTenantScope(ctx, parsed.tenantId)\n ensureOrganizationScope(ctx, parsed.organizationId)\n\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const person = await requirePersonEntity(em, parsed.personEntityId, parsed.tenantId, parsed.organizationId)\n const company = await requireCompanyEntity(em, parsed.companyEntityId, parsed.tenantId, parsed.organizationId)\n const profile = await requirePersonProfile(em, person)\n\n const existingLinks = await loadPersonCompanyLinks(em, person)\n const makePrimary = Boolean(parsed.isPrimary) || existingLinks.length === 0\n const existingLive =\n existingLinks.find((link) => (typeof link.company === 'string' ? link.company : link.company.id) === company.id) ?? null\n\n if (existingLive) {\n if (makePrimary && !existingLive.isPrimary) {\n await withAtomicFlush(em, [\n () => clearPrimaryFlagsForPerson(em, person),\n () => {\n existingLive.isPrimary = true\n profile.company = company\n },\n ], { transaction: true })\n }\n return { linkId: existingLive.id, created: false, undeleted: false }\n }\n\n let link!: CustomerPersonCompanyLink\n let undeleted = false\n await withAtomicFlush(em, [\n async () => {\n const deletedLink = await findDeletedPersonCompanyLink(em, person, company)\n if (makePrimary) {\n await clearPrimaryFlagsForPerson(em, person)\n }\n if (deletedLink) {\n deletedLink.deletedAt = null\n deletedLink.isPrimary = makePrimary\n em.persist(deletedLink)\n link = deletedLink\n undeleted = true\n } else {\n link = em.create(CustomerPersonCompanyLink, {\n organizationId: parsed.organizationId,\n tenantId: parsed.tenantId,\n person,\n company,\n isPrimary: makePrimary,\n })\n em.persist(link)\n }\n },\n () => {\n if (makePrimary) {\n profile.company = company\n } else if (!profile.company && existingLinks.length === 0) {\n profile.company = company\n link.isPrimary = true\n }\n },\n ], { transaction: true })\n\n const dataEngine = ctx.container.resolve('dataEngine') as DataEngine\n await emitCrudSideEffects({\n dataEngine,\n action: undeleted ? 'updated' : 'created',\n entity: link,\n identifiers: getLinkIdentifiers(link),\n syncOrigin: ctx.syncOrigin,\n events: personCompanyLinkCrudEvents,\n indexer: { entityType: 'customers:customer_person_company_link' },\n })\n\n return { linkId: link.id, created: !undeleted, undeleted }\n },\n captureAfter: async (_input, result, ctx) => {\n const em = ctx.container.resolve('em') as EntityManager\n return loadPersonCompanyLinkSnapshot(em, result.linkId, {\n tenantId: ctx.auth?.tenantId ?? null,\n organizationId: ctx.selectedOrganizationId ?? ctx.auth?.orgId ?? null,\n })\n },\n buildLog: async ({ result, snapshots }) => {\n const { translate } = await resolveTranslations()\n const after = snapshots.after as PersonCompanyLinkSnapshot | undefined\n return {\n actionLabel: translate('customers.audit.personCompanyLinks.create', 'Link company to person'),\n resourceKind: 'customers.personCompanyLink',\n resourceId: result.linkId,\n parentResourceKind: 'customers.person',\n parentResourceId: after?.personEntityId ?? null,\n tenantId: after?.tenantId ?? null,\n organizationId: after?.organizationId ?? null,\n snapshotAfter: after ?? null,\n payload: {\n undo: {\n after: after ?? null,\n ...(result.undeleted ? { before: null } : {}),\n } satisfies PersonCompanyLinkUndoPayload,\n },\n }\n },\n undo: async ({ logEntry, ctx }) => {\n const payload = extractUndoPayload<PersonCompanyLinkUndoPayload>(logEntry)\n const after = payload?.after\n if (!after) return\n\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const link = await findOneWithDecryption(\n em,\n CustomerPersonCompanyLink,\n { id: after.id },\n undefined,\n { tenantId: after.tenantId, organizationId: after.organizationId },\n )\n if (!link) return\n\n let person: CustomerEntity | null = null\n let profile: CustomerPersonProfile | null = null\n let remainingLinks: CustomerPersonCompanyLink[] = []\n\n await withAtomicFlush(em, [\n async () => {\n person = await findOneWithDecryption(\n em,\n CustomerEntity,\n { id: after.personEntityId, kind: 'person', tenantId: after.tenantId, organizationId: after.organizationId, deletedAt: null },\n undefined,\n { tenantId: after.tenantId, organizationId: after.organizationId },\n )\n if (!person) return\n profile = await findOneWithDecryption(\n em,\n CustomerPersonProfile,\n { entity: person },\n { populate: ['company'] },\n { tenantId: person.tenantId, organizationId: person.organizationId },\n )\n if (!profile) return\n remainingLinks = (await loadPersonCompanyLinks(em, person)).filter((entry) => entry.id !== link.id)\n },\n async () => {\n link.isPrimary = false\n link.deletedAt = new Date()\n if (!person || !profile) return\n if (after.isPrimary) {\n await promoteFallbackPrimaryLink(em, person, profile, remainingLinks, after.companyEntityId)\n } else if (profile.company && typeof profile.company !== 'string' && profile.company.id === after.companyEntityId) {\n profile.company = null\n }\n },\n ], { transaction: true })\n\n const dataEngine = ctx.container.resolve('dataEngine') as DataEngine\n await emitCrudUndoSideEffects({\n dataEngine,\n action: 'deleted',\n entity: link,\n identifiers: getLinkIdentifiers(link),\n syncOrigin: ctx.syncOrigin,\n events: personCompanyLinkCrudEvents,\n indexer: { entityType: 'customers:customer_person_company_link' },\n })\n },\n}\n\nconst updatePersonCompanyLinkCommand: CommandHandler<PersonCompanyLinkUpdateInput, { linkId: string }> = {\n id: 'customers.personCompanyLinks.update',\n async prepare(rawInput, ctx) {\n const parsed = personCompanyLinkUpdateSchema.parse(rawInput)\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const snapshot = await loadPersonCompanyLinkSnapshot(em, parsed.linkId, {\n tenantId: ctx.auth?.tenantId ?? null,\n organizationId: ctx.selectedOrganizationId ?? ctx.auth?.orgId ?? null,\n })\n return snapshot ? { before: snapshot } : {}\n },\n async execute(rawInput, ctx) {\n const parsed = personCompanyLinkUpdateSchema.parse(rawInput)\n ensureTenantScope(ctx, parsed.tenantId)\n ensureOrganizationScope(ctx, parsed.organizationId)\n\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const link = await findOneWithDecryption(\n em,\n CustomerPersonCompanyLink,\n {\n id: parsed.linkId,\n tenantId: parsed.tenantId,\n organizationId: parsed.organizationId,\n deletedAt: null,\n },\n undefined,\n { tenantId: parsed.tenantId, organizationId: parsed.organizationId },\n )\n if (!link) {\n throw new CrudHttpError(404, { error: 'Company link not found' })\n }\n\n const personId = typeof link.person === 'string' ? link.person : link.person.id\n const companyId = typeof link.company === 'string' ? link.company : link.company.id\n const person = await requirePersonEntity(em, personId, parsed.tenantId, parsed.organizationId)\n const profile = await requirePersonProfile(em, person)\n const linkedCompany = await requireCompanyEntity(em, companyId, parsed.tenantId, parsed.organizationId)\n\n await withAtomicFlush(em, [\n async () => {\n if (parsed.isPrimary) {\n await clearPrimaryFlagsForPerson(em, person)\n link.isPrimary = true\n profile.company = linkedCompany\n } else if (!parsed.isPrimary) {\n const linkWasPrimary = link.isPrimary\n link.isPrimary = false\n if (linkWasPrimary) {\n const remainingLinks = (await loadPersonCompanyLinks(em, person)).filter((entry) => entry.id !== link.id)\n await promoteFallbackPrimaryLink(em, person, profile, remainingLinks, companyId)\n } else if (profile.company && typeof profile.company !== 'string' && profile.company.id === companyId) {\n profile.company = null\n }\n }\n },\n ], { transaction: true })\n\n const dataEngine = ctx.container.resolve('dataEngine') as DataEngine\n await emitCrudSideEffects({\n dataEngine,\n action: 'updated',\n entity: link,\n identifiers: getLinkIdentifiers(link),\n syncOrigin: ctx.syncOrigin,\n events: personCompanyLinkCrudEvents,\n indexer: { entityType: 'customers:customer_person_company_link' },\n })\n\n return { linkId: link.id }\n },\n captureAfter: async (_input, result, ctx) => {\n const em = ctx.container.resolve('em') as EntityManager\n return loadPersonCompanyLinkSnapshot(em, result.linkId, {\n tenantId: ctx.auth?.tenantId ?? null,\n organizationId: ctx.selectedOrganizationId ?? ctx.auth?.orgId ?? null,\n })\n },\n buildLog: async ({ result, snapshots }) => {\n const { translate } = await resolveTranslations()\n const before = snapshots.before as PersonCompanyLinkSnapshot | undefined\n const after = snapshots.after as PersonCompanyLinkSnapshot | undefined\n return {\n actionLabel: translate('customers.audit.personCompanyLinks.update', 'Update company link'),\n resourceKind: 'customers.personCompanyLink',\n resourceId: result.linkId,\n parentResourceKind: 'customers.person',\n parentResourceId: after?.personEntityId ?? before?.personEntityId ?? null,\n tenantId: after?.tenantId ?? before?.tenantId ?? null,\n organizationId: after?.organizationId ?? before?.organizationId ?? null,\n snapshotBefore: before ?? null,\n snapshotAfter: after ?? null,\n payload: {\n undo: {\n before: before ?? null,\n after: after ?? null,\n } satisfies PersonCompanyLinkUndoPayload,\n },\n }\n },\n undo: async ({ logEntry, ctx }) => {\n const payload = extractUndoPayload<PersonCompanyLinkUndoPayload>(logEntry)\n const before = payload?.before\n if (!before) return\n\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const link = await findOneWithDecryption(\n em,\n CustomerPersonCompanyLink,\n { id: before.id },\n undefined,\n { tenantId: before.tenantId, organizationId: before.organizationId },\n )\n if (!link) return\n\n let person: CustomerEntity | null = null\n let profile: CustomerPersonProfile | null = null\n let company: CustomerEntity | null = null\n\n await withAtomicFlush(em, [\n async () => {\n person = await findOneWithDecryption(\n em,\n CustomerEntity,\n { id: before.personEntityId, kind: 'person', tenantId: before.tenantId, organizationId: before.organizationId, deletedAt: null },\n undefined,\n { tenantId: before.tenantId, organizationId: before.organizationId },\n )\n if (!person) return\n profile = await findOneWithDecryption(\n em,\n CustomerPersonProfile,\n { entity: person },\n { populate: ['company'] },\n { tenantId: person.tenantId, organizationId: person.organizationId },\n )\n if (!profile || !before.isPrimary) return\n company = await findOneWithDecryption(\n em,\n CustomerEntity,\n { id: before.companyEntityId, kind: 'company', tenantId: before.tenantId, organizationId: before.organizationId, deletedAt: null },\n undefined,\n { tenantId: before.tenantId, organizationId: before.organizationId },\n )\n },\n async () => {\n if (!person || !profile) return\n if (before.isPrimary) {\n await clearPrimaryFlagsForPerson(em, person)\n link.isPrimary = true\n if (company) profile.company = company\n } else {\n link.isPrimary = false\n }\n },\n ], { transaction: true })\n\n const dataEngine = ctx.container.resolve('dataEngine') as DataEngine\n await emitCrudUndoSideEffects({\n dataEngine,\n action: 'updated',\n entity: link,\n identifiers: getLinkIdentifiers(link),\n syncOrigin: ctx.syncOrigin,\n events: personCompanyLinkCrudEvents,\n indexer: { entityType: 'customers:customer_person_company_link' },\n })\n },\n}\n\nconst deletePersonCompanyLinkCommand: CommandHandler<PersonCompanyLinkDeleteInput, { linkId: string }> = {\n id: 'customers.personCompanyLinks.delete',\n async prepare(rawInput, ctx) {\n const parsed = personCompanyLinkDeleteSchema.parse(rawInput)\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const snapshot = await loadPersonCompanyLinkSnapshot(em, parsed.linkId, {\n tenantId: ctx.auth?.tenantId ?? null,\n organizationId: ctx.selectedOrganizationId ?? ctx.auth?.orgId ?? null,\n })\n return snapshot ? { before: snapshot } : {}\n },\n async execute(rawInput, ctx) {\n const parsed = personCompanyLinkDeleteSchema.parse(rawInput)\n ensureTenantScope(ctx, parsed.tenantId)\n ensureOrganizationScope(ctx, parsed.organizationId)\n\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const link = await findOneWithDecryption(\n em,\n CustomerPersonCompanyLink,\n {\n id: parsed.linkId,\n tenantId: parsed.tenantId,\n organizationId: parsed.organizationId,\n deletedAt: null,\n },\n undefined,\n { tenantId: parsed.tenantId, organizationId: parsed.organizationId },\n )\n if (!link) {\n throw new CrudHttpError(404, { error: 'Company link not found' })\n }\n\n const personId = typeof link.person === 'string' ? link.person : link.person.id\n const companyId = typeof link.company === 'string' ? link.company : link.company.id\n const person = await requirePersonEntity(em, personId, parsed.tenantId, parsed.organizationId)\n const profile = await requirePersonProfile(em, person)\n const linkWasPrimary = link.isPrimary\n\n const existingLinks = await loadPersonCompanyLinks(em, person)\n const remainingLinks = existingLinks.filter((entry) => entry.id !== link.id)\n\n await withAtomicFlush(em, [\n () => {\n link.isPrimary = false\n link.deletedAt = new Date()\n },\n async () => {\n if (linkWasPrimary) {\n await promoteFallbackPrimaryLink(em, person, profile, remainingLinks, companyId)\n } else if (profile.company && typeof profile.company !== 'string' && profile.company.id === companyId) {\n const primary = remainingLinks.find((entry) => entry.isPrimary) ?? null\n const primaryCompany = primary && typeof primary.company !== 'string' ? primary.company : null\n if (primaryCompany) {\n profile.company = primaryCompany\n }\n }\n },\n ], { transaction: true })\n\n const dataEngine = ctx.container.resolve('dataEngine') as DataEngine\n await emitCrudSideEffects({\n dataEngine,\n action: 'deleted',\n entity: link,\n identifiers: getLinkIdentifiers(link),\n syncOrigin: ctx.syncOrigin,\n events: personCompanyLinkCrudEvents,\n indexer: { entityType: 'customers:customer_person_company_link' },\n })\n\n return { linkId: link.id }\n },\n buildLog: async ({ result, snapshots }) => {\n const { translate } = await resolveTranslations()\n const before = snapshots.before as PersonCompanyLinkSnapshot | undefined\n return {\n actionLabel: translate('customers.audit.personCompanyLinks.delete', 'Remove company link'),\n resourceKind: 'customers.personCompanyLink',\n resourceId: result.linkId,\n parentResourceKind: 'customers.person',\n parentResourceId: before?.personEntityId ?? null,\n tenantId: before?.tenantId ?? null,\n organizationId: before?.organizationId ?? null,\n snapshotBefore: before ?? null,\n payload: {\n undo: {\n before: before ?? null,\n } satisfies PersonCompanyLinkUndoPayload,\n },\n }\n },\n undo: async ({ logEntry, ctx }) => {\n const payload = extractUndoPayload<PersonCompanyLinkUndoPayload>(logEntry)\n const before = payload?.before\n if (!before) return\n\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const link = await findOneWithDecryption(\n em,\n CustomerPersonCompanyLink,\n { id: before.id },\n undefined,\n { tenantId: before.tenantId, organizationId: before.organizationId },\n )\n if (!link) return\n\n let person: CustomerEntity | null = null\n let profile: CustomerPersonProfile | null = null\n let company: CustomerEntity | null = null\n\n await withAtomicFlush(em, [\n async () => {\n person = await findOneWithDecryption(\n em,\n CustomerEntity,\n { id: before.personEntityId, kind: 'person', tenantId: before.tenantId, organizationId: before.organizationId, deletedAt: null },\n undefined,\n { tenantId: before.tenantId, organizationId: before.organizationId },\n )\n if (!person || !before.isPrimary) return\n profile = await findOneWithDecryption(\n em,\n CustomerPersonProfile,\n { entity: person },\n { populate: ['company'] },\n { tenantId: person.tenantId, organizationId: person.organizationId },\n )\n if (!profile) return\n company = await findOneWithDecryption(\n em,\n CustomerEntity,\n { id: before.companyEntityId, kind: 'company', tenantId: before.tenantId, organizationId: before.organizationId, deletedAt: null },\n undefined,\n { tenantId: before.tenantId, organizationId: before.organizationId },\n )\n },\n async () => {\n link.deletedAt = null\n link.isPrimary = before.isPrimary\n if (person && before.isPrimary) {\n await clearPrimaryFlagsForPerson(em, person)\n link.isPrimary = true\n if (profile && company) profile.company = company\n }\n },\n ], { transaction: true })\n\n const dataEngine = ctx.container.resolve('dataEngine') as DataEngine\n await emitCrudUndoSideEffects({\n dataEngine,\n action: 'created',\n entity: link,\n identifiers: getLinkIdentifiers(link),\n syncOrigin: ctx.syncOrigin,\n events: personCompanyLinkCrudEvents,\n indexer: { entityType: 'customers:customer_person_company_link' },\n })\n },\n}\n\nregisterCommand(createPersonCompanyLinkCommand)\nregisterCommand(updatePersonCompanyLinkCommand)\nregisterCommand(deletePersonCompanyLinkCommand)\n"],
|
|
5
|
+
"mappings": "AAAA,SAAS,uBAAuB;AAGhC,SAAS,qBAAqB,+BAA+B;AAC7D,SAAS,qBAAqB;AAC9B,SAAS,2BAA2B;AACpC,SAAS,6BAA6B;AAGtC;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OAIK;AACP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,uBAAuB;AAiBhC,MAAM,8BAAgD;AAAA,EACpD,QAAQ;AAAA,EACR,QAAQ;AAAA,EACR,YAAY;AAAA,EACZ,cAAc,CAAC,SAAS;AAAA,IACtB,IAAI,IAAI,YAAY;AAAA,IACpB,gBAAgB,IAAI,YAAY;AAAA,IAChC,UAAU,IAAI,YAAY;AAAA,IAC1B,GAAI,IAAI,UAAU,OAAO,IAAI,WAAW,YAAY,YAAa,IAAI,SACjE;AAAA,MACE,gBACE,OAAQ,IAAI,OAAqC,WAAW,WACvD,IAAI,OAAe,SACnB,IAAI,OAAqC,QAAQ,MAAM;AAAA,MAC9D,iBACE,OAAQ,IAAI,OAAqC,YAAY,WACxD,IAAI,OAAe,UACnB,IAAI,OAAqC,SAAS,MAAM;AAAA,IACjE,IACA,CAAC;AAAA,IACL,GAAI,IAAI,aAAa,EAAE,YAAY,IAAI,WAAW,IAAI,CAAC;AAAA,EACzD;AACF;AAEA,SAAS,mBAAmB,MAAiC;AAC3D,SAAO;AAAA,IACL,IAAI,KAAK;AAAA,IACT,gBAAgB,KA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F;AAAA,MACA,YAAY;AACV,aAAK,YAAY;AACjB,aAAK,YAAY,OAAO;AACxB,YAAI,UAAU,OAAO,WAAW;AAC9B,gBAAM,2BAA2B,IAAI,MAAM;AAC3C,eAAK,YAAY;AACjB,cAAI,WAAW,QAAS,SAAQ,UAAU;AAAA,QAC5C;AAAA,MACF;AAAA,IACF,GAAG,EAAE,aAAa,KAAK,CAAC;AAExB,UAAM,aAAa,IAAI,UAAU,QAAQ,YAAY;AACrD,UAAM,wBAAwB;AAAA,MAC5B;AAAA,MACA,QAAQ;AAAA,MACR,QAAQ;AAAA,MACR,aAAa,mBAAmB,IAAI;AAAA,MACpC,YAAY,IAAI;AAAA,MAChB,QAAQ;AAAA,MACR,SAAS,EAAE,YAAY,yCAAyC;AAAA,IAClE,CAAC;AAAA,EACH;AACF;AAEA,gBAAgB,8BAA8B;AAC9C,gBAAgB,8BAA8B;AAC9C,gBAAgB,8BAA8B;",
|
|
6
6
|
"names": ["link"]
|
|
7
7
|
}
|
|
@@ -610,6 +610,7 @@ const updatePaymentCommand = {
|
|
|
610
610
|
});
|
|
611
611
|
}
|
|
612
612
|
payment.updatedAt = /* @__PURE__ */ new Date();
|
|
613
|
+
await tx.flush();
|
|
613
614
|
if (input.allocations !== void 0) {
|
|
614
615
|
const existingAllocations = await findWithDecryption(tx, SalesPaymentAllocation, { payment }, {}, { tenantId: payment.tenantId, organizationId: payment.organizationId });
|
|
615
616
|
existingAllocations.forEach((allocation) => tx.remove(allocation));
|
|
@@ -668,6 +669,20 @@ const updatePaymentCommand = {
|
|
|
668
669
|
});
|
|
669
670
|
tx.persist(entity);
|
|
670
671
|
}
|
|
672
|
+
} else if (input.amount !== void 0 || input.currencyCode !== void 0) {
|
|
673
|
+
const existingAllocations = await findWithDecryption(tx, SalesPaymentAllocation, { payment }, {}, { tenantId: payment.tenantId, organizationId: payment.organizationId });
|
|
674
|
+
const paymentOrderId = (typeof payment.order === "string" ? payment.order : payment.order?.id) ?? null;
|
|
675
|
+
const isDefaultAllocation = (allocation) => {
|
|
676
|
+
const allocationOrderId = typeof allocation.order === "string" ? allocation.order : allocation.order?.id ?? null;
|
|
677
|
+
const allocationInvoiceId = typeof allocation.invoice === "string" ? allocation.invoice : allocation.invoice?.id ?? null;
|
|
678
|
+
return allocationInvoiceId === null && allocationOrderId === paymentOrderId;
|
|
679
|
+
};
|
|
680
|
+
if (existingAllocations.length === 1 && isDefaultAllocation(existingAllocations[0])) {
|
|
681
|
+
const [allocation] = existingAllocations;
|
|
682
|
+
allocation.amount = toNumericString(toNumber(payment.amount)) ?? "0";
|
|
683
|
+
allocation.currencyCode = payment.currencyCode;
|
|
684
|
+
tx.persist(allocation);
|
|
685
|
+
}
|
|
671
686
|
}
|
|
672
687
|
});
|
|
673
688
|
const nextOrderId = payment.order?.id ?? (typeof payment.order === "string" ? payment.order : null);
|