@artu-ai/compliance-sdk 0.4.1 → 0.4.3

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.
Files changed (110) hide show
  1. package/package.json +3 -4
  2. package/src/errors/api.ts +0 -305
  3. package/src/errors/base.ts +0 -127
  4. package/src/errors/index.ts +0 -63
  5. package/src/errors/upload.ts +0 -204
  6. package/src/errors/validation.ts +0 -163
  7. package/src/exports/base.ts +0 -139
  8. package/src/exports/index.ts +0 -187
  9. package/src/exports/mexico/actividad-vulnerable/avi.ts +0 -207
  10. package/src/exports/mexico/actividad-vulnerable/jys.ts +0 -214
  11. package/src/exports/mexico/actividad-vulnerable/tsc.ts +0 -202
  12. package/src/exports/mexico/index.ts +0 -215
  13. package/src/models/Address.ts +0 -235
  14. package/src/models/BankAccount.ts +0 -235
  15. package/src/models/Client.ts +0 -363
  16. package/src/models/ContactMethod.ts +0 -197
  17. package/src/models/Document.ts +0 -355
  18. package/src/models/LinkedClient.ts +0 -84
  19. package/src/models/Report.ts +0 -193
  20. package/src/models/ReportItem.ts +0 -211
  21. package/src/models/Transaction.ts +0 -219
  22. package/src/models/base.ts +0 -56
  23. package/src/models/index.ts +0 -148
  24. package/src/models/mex/MexAddress.ts +0 -184
  25. package/src/models/mex/MexBankAccount.ts +0 -121
  26. package/src/models/mex/MexClient.ts +0 -177
  27. package/src/models/mex/MexDocument.ts +0 -890
  28. package/src/models/mex/MexReport.ts +0 -99
  29. package/src/models/mex/MexReportItem.ts +0 -79
  30. package/src/models/mex/MexTransaction.ts +0 -105
  31. package/src/models/mex/actividad-vulnerable/MexActividadVulnerableClient.ts +0 -66
  32. package/src/models/mex/actividad-vulnerable/MexActividadVulnerableReport.ts +0 -73
  33. package/src/models/mex/actividad-vulnerable/MexActividadVulnerableReportItem.ts +0 -74
  34. package/src/models/mex/actividad-vulnerable/MexActividadVulnerableTransaction.ts +0 -50
  35. package/src/models/mex/actividad-vulnerable/avi/MexAVIClient.ts +0 -80
  36. package/src/models/mex/actividad-vulnerable/avi/MexAVIReport.ts +0 -114
  37. package/src/models/mex/actividad-vulnerable/avi/MexAVIReportItem.ts +0 -105
  38. package/src/models/mex/actividad-vulnerable/avi/MexAVITransaction.ts +0 -157
  39. package/src/models/mex/actividad-vulnerable/index.ts +0 -78
  40. package/src/models/mex/actividad-vulnerable/jys/MexJYSClient.ts +0 -73
  41. package/src/models/mex/actividad-vulnerable/jys/MexJYSReport.ts +0 -114
  42. package/src/models/mex/actividad-vulnerable/jys/MexJYSReportItem.ts +0 -105
  43. package/src/models/mex/actividad-vulnerable/jys/MexJYSTransaction.ts +0 -178
  44. package/src/models/mex/actividad-vulnerable/tsc/MexTSCClient.ts +0 -73
  45. package/src/models/mex/actividad-vulnerable/tsc/MexTSCReport.ts +0 -114
  46. package/src/models/mex/actividad-vulnerable/tsc/MexTSCReportItem.ts +0 -105
  47. package/src/models/mex/actividad-vulnerable/tsc/MexTSCTransaction.ts +0 -104
  48. package/src/models/mex/index.ts +0 -57
  49. package/src/models/types.ts +0 -196
  50. package/src/namespaces/index.ts +0 -29
  51. package/src/namespaces/mexico/actividad-vulnerable/avi/index.ts +0 -62
  52. package/src/namespaces/mexico/actividad-vulnerable/index.ts +0 -79
  53. package/src/namespaces/mexico/actividad-vulnerable/jys/index.ts +0 -72
  54. package/src/namespaces/mexico/actividad-vulnerable/tsc/index.ts +0 -60
  55. package/src/namespaces/mexico/index.ts +0 -325
  56. package/src/resources/addresses.ts +0 -391
  57. package/src/resources/bank-accounts.ts +0 -417
  58. package/src/resources/base.ts +0 -327
  59. package/src/resources/clients.ts +0 -808
  60. package/src/resources/contact-methods.ts +0 -412
  61. package/src/resources/documents.ts +0 -688
  62. package/src/resources/index.ts +0 -40
  63. package/src/resources/mex/actividad-vulnerable/avi/clients.ts +0 -559
  64. package/src/resources/mex/actividad-vulnerable/avi/index.ts +0 -7
  65. package/src/resources/mex/actividad-vulnerable/avi/reports.ts +0 -609
  66. package/src/resources/mex/actividad-vulnerable/avi/transactions.ts +0 -404
  67. package/src/resources/mex/actividad-vulnerable/index.ts +0 -12
  68. package/src/resources/mex/actividad-vulnerable/jys/clients.ts +0 -445
  69. package/src/resources/mex/actividad-vulnerable/jys/index.ts +0 -7
  70. package/src/resources/mex/actividad-vulnerable/jys/reports.ts +0 -591
  71. package/src/resources/mex/actividad-vulnerable/jys/transactions.ts +0 -395
  72. package/src/resources/mex/actividad-vulnerable/tsc/clients.ts +0 -445
  73. package/src/resources/mex/actividad-vulnerable/tsc/index.ts +0 -7
  74. package/src/resources/mex/actividad-vulnerable/tsc/reports.ts +0 -591
  75. package/src/resources/mex/actividad-vulnerable/tsc/transactions.ts +0 -404
  76. package/src/resources/mex/addresses.ts +0 -493
  77. package/src/resources/mex/bank-accounts.ts +0 -283
  78. package/src/resources/mex/clients.ts +0 -758
  79. package/src/resources/mex/ebr.ts +0 -621
  80. package/src/resources/mex/index.ts +0 -23
  81. package/src/resources/mex/reports.ts +0 -643
  82. package/src/resources/mex/transactions.ts +0 -422
  83. package/src/resources/reports.ts +0 -515
  84. package/src/resources/transactions.ts +0 -358
  85. package/src/sdk/ComplianceSDK.ts +0 -812
  86. package/src/sdk/base.ts +0 -43
  87. package/src/sdk/index.ts +0 -98
  88. package/src/sdk/mex/ComplianceSDK.ts +0 -147
  89. package/src/sdk/mex/actividad-vulnerable/avi/ComplianceSDK.ts +0 -55
  90. package/src/sdk/mex/actividad-vulnerable/avi/index.ts +0 -16
  91. package/src/sdk/mex/actividad-vulnerable/avi/types.ts +0 -86
  92. package/src/sdk/mex/actividad-vulnerable/index.ts +0 -58
  93. package/src/sdk/mex/actividad-vulnerable/jys/ComplianceSDK.ts +0 -54
  94. package/src/sdk/mex/actividad-vulnerable/jys/index.ts +0 -16
  95. package/src/sdk/mex/actividad-vulnerable/jys/types.ts +0 -86
  96. package/src/sdk/mex/actividad-vulnerable/tsc/ComplianceSDK.ts +0 -54
  97. package/src/sdk/mex/actividad-vulnerable/tsc/index.ts +0 -16
  98. package/src/sdk/mex/actividad-vulnerable/tsc/types.ts +0 -86
  99. package/src/sdk/mex/index.ts +0 -66
  100. package/src/sdk/mex/types.ts +0 -88
  101. package/src/sdk/resource-registry.ts +0 -204
  102. package/src/sdk/sdk-registry.ts +0 -99
  103. package/src/sdk/types.ts +0 -304
  104. package/src/utils/environment.ts +0 -187
  105. package/src/utils/filters.ts +0 -412
  106. package/src/utils/index.ts +0 -134
  107. package/src/utils/pagination.ts +0 -143
  108. package/src/utils/session.ts +0 -303
  109. package/src/utils/trpc-client.ts +0 -242
  110. package/src/utils/upload.ts +0 -388
@@ -1,808 +0,0 @@
1
- /**
2
- * Clients Resource
3
- *
4
- * CRUD operations, filtering, pagination, and batch operations for clients.
5
- *
6
- * This is the base (unscoped) resource. For jurisdiction-scoped SDKs, use:
7
- * - MexClientsResource - Mexico-scoped with flattened MX fields
8
- * - AVIClientsResource - Mexico AVI-scoped with flattened AVI fields
9
- * - JYSClientsResource - Mexico JYS-scoped with flattened JYS fields
10
- * - TSCClientsResource - Mexico TSC-scoped with flattened TSC fields
11
- */
12
-
13
- import { z } from "zod";
14
- import type {
15
- CreateClientInput,
16
- UpdateClientInput,
17
- CreateAddressInput,
18
- UpdateAddressInput,
19
- CreateContactMethodInput,
20
- UpdateContactMethodInput,
21
- CreateBankAccountInput,
22
- UpdateBankAccountInput,
23
- ClientFilter,
24
- ClientSort,
25
- ListOptions,
26
- PaginatedResponse,
27
- AtomicBatchResult,
28
- PartialBatchResult,
29
- ClientRelationshipType,
30
- LinkedClient as LinkedClientInput,
31
- SyncItem,
32
- SyncResult,
33
- } from "@artu-ai/shared";
34
- import {
35
- createClientInputSchema,
36
- updateClientInputSchema,
37
- createAddressInputSchema,
38
- updateAddressInputSchema,
39
- createContactMethodInputSchema,
40
- updateContactMethodInputSchema,
41
- createBankAccountInputSchema,
42
- updateBankAccountInputSchema,
43
- linkedClientSchema,
44
- replaceSyncItemSchema,
45
- } from "@artu-ai/shared";
46
- import { BaseResource } from "./base";
47
- import {
48
- Client,
49
- type ClientData,
50
- Address,
51
- ContactMethod,
52
- BankAccount,
53
- LinkedClient,
54
- } from "../models";
55
-
56
- // ===========================
57
- // Types
58
- // ===========================
59
-
60
- /**
61
- * Options for batch operations
62
- */
63
- export interface BatchOptions {
64
- /**
65
- * If true (default), all items must succeed or entire operation fails.
66
- * If false, partial success is allowed with succeeded/failed arrays.
67
- */
68
- atomic?: boolean;
69
- }
70
-
71
- export interface ClientIncludeMap {
72
- primaryContacts?: boolean;
73
- addresses?: boolean;
74
- contactMethods?: boolean;
75
- bankAccounts?: boolean;
76
- documents?: boolean;
77
- linkedClients?: boolean;
78
- }
79
-
80
- // ===========================
81
- // ClientsResource
82
- // ===========================
83
-
84
- /**
85
- * Base resource manager for client operations.
86
- *
87
- * Provides:
88
- * - CRUD operations (create, retrieve, update, delete)
89
- * - Filtering and pagination
90
- * - Batch operations (createMany, updateMany)
91
- *
92
- * @example
93
- * ```typescript
94
- * // Create a client with full nested format
95
- * const client = await sdk.clients.create({
96
- * type: "individual",
97
- * name: "Juan García",
98
- * primaryJurisdiction: "MX",
99
- * jurisdictions: {
100
- * MX: { rfc: "GAJL850101ABC" },
101
- * },
102
- * });
103
- *
104
- * // List with filtering
105
- * const { data, pagination } = await sdk.clients.list({
106
- * filter: { type: "individual", primaryJurisdiction: "MX" },
107
- * limit: 20,
108
- * });
109
- *
110
- * // Iterate all clients
111
- * for await (const client of sdk.clients.iterate()) {
112
- * console.log(client.name);
113
- * }
114
- * ```
115
- */
116
- export class ClientsResource<
117
- TSort = ClientSort,
118
- > extends BaseResource<
119
- Client,
120
- CreateClientInput,
121
- UpdateClientInput,
122
- ClientFilter,
123
- TSort
124
- > {
125
- // ─────────────────────────────────────────────────────────────────
126
- // CRUD Operations
127
- // ─────────────────────────────────────────────────────────────────
128
-
129
- /**
130
- * Creates a new client.
131
- *
132
- * @param data - Client creation data in nested format
133
- * @returns The created client model
134
- *
135
- * @example
136
- * ```typescript
137
- * const client = await sdk.clients.create({
138
- * type: ClientType.Person,
139
- * name: "Juan García",
140
- * primaryJurisdiction: Jurisdiction.Mexico,
141
- * jurisdictions: {
142
- * MX: { rfc: "GAJL850101ABC" },
143
- * },
144
- * });
145
- * ```
146
- */
147
- async create(data: CreateClientInput): Promise<Client> {
148
- const validated = this.validate(createClientInputSchema, data);
149
-
150
- const response = await this.execute(() =>
151
- this.trpc.clients.create.mutate(validated)
152
- );
153
-
154
- return this.instantiate(response);
155
- }
156
-
157
- /**
158
- * Retrieves a client by ID.
159
- *
160
- * @param id - Client ID
161
- * @param include - Optional includes (relations, primary contacts)
162
- * @returns The client model (with optional fields populated if included)
163
- */
164
- async retrieve(id: string, include?: ClientIncludeMap): Promise<Client> {
165
- const response = await this.execute(() =>
166
- this.trpc.clients.retrieve.query({ id, include })
167
- );
168
- return this.instantiate(response);
169
- }
170
-
171
- /**
172
- * Retrieves a client by metadata key/value.
173
- */
174
- async retrieveByMetadata(key: string, value: string): Promise<Client> {
175
- const response = await this.execute(() =>
176
- this.trpc.clients.retrieveByMetadata.query({ key, value })
177
- );
178
- return this.instantiate(response);
179
- }
180
-
181
- /**
182
- * Retrieves a client by externalId.
183
- *
184
- * @param externalId - External identifier assigned by your system
185
- * @returns The client model
186
- *
187
- * @example
188
- * ```typescript
189
- * const client = await sdk.clients.retrieveByExternalId("external-123");
190
- * console.log(client.name);
191
- * ```
192
- */
193
- async retrieveByExternalId(externalId: string): Promise<Client> {
194
- const response = await this.execute(() =>
195
- this.trpc.clients.retrieveByExternalId.query({ externalId })
196
- );
197
- return this.instantiate(response);
198
- }
199
-
200
- /**
201
- * Updates a client.
202
- *
203
- * @param id - Client ID
204
- * @param data - Update data (partial)
205
- * @returns The updated client model
206
- *
207
- * @example
208
- * ```typescript
209
- * const client = await sdk.clients.update("client_123", {
210
- * email: "new@example.com",
211
- * });
212
- * ```
213
- */
214
- async update(id: string, data: UpdateClientInput): Promise<Client> {
215
- const validated = this.validate(updateClientInputSchema, data);
216
-
217
- const response = await this.execute(() =>
218
- this.trpc.clients.update.mutate({ id, data: validated })
219
- );
220
- return this.instantiate(response);
221
- }
222
-
223
- /**
224
- * Deletes a client (soft delete).
225
- *
226
- * @param id - Client ID
227
- *
228
- * @example
229
- * ```typescript
230
- * await sdk.clients.delete("client_123");
231
- * ```
232
- */
233
- async delete(id: string): Promise<void> {
234
- await this.execute(() => this.trpc.clients.delete.mutate({ id }));
235
- }
236
-
237
- // ─────────────────────────────────────────────────────────────────
238
- // List & Pagination
239
- // ─────────────────────────────────────────────────────────────────
240
-
241
- /**
242
- * Lists clients with optional filtering and pagination.
243
- *
244
- * @param options - List options (filter, cursor, limit)
245
- * @param options.includePrimaryContacts - Include primary email/phone
246
- * @returns Paginated response with client models
247
- *
248
- * @example
249
- * ```typescript
250
- * const { data, pagination } = await sdk.clients.list({
251
- * filter: { type: "individual" },
252
- * limit: 20,
253
- * });
254
- *
255
- * if (pagination.hasMore) {
256
- * const nextPage = await sdk.clients.list({
257
- * cursor: pagination.nextCursor,
258
- * });
259
- * }
260
- * ```
261
- */
262
- async list(
263
- options?: ListOptions<ClientFilter, TSort> & { includePrimaryContacts?: boolean }
264
- ): Promise<PaginatedResponse<Client>> {
265
- const response = await this.execute(() =>
266
- this.trpc.clients.list.query(
267
- options as ListOptions<ClientFilter, ClientSort> & { includePrimaryContacts?: boolean }
268
- )
269
- );
270
-
271
- return {
272
- data: response.data.map((c) => this.instantiate(c)),
273
- pagination: response.pagination,
274
- };
275
- }
276
-
277
- /**
278
- * Returns an async iterator over all clients matching the filter.
279
- * Automatically fetches subsequent pages as you iterate.
280
- *
281
- * @param filter - Optional filter criteria
282
- * @returns Async generator yielding client models
283
- *
284
- * @example
285
- * ```typescript
286
- * for await (const client of sdk.clients.iterate({ type: "individual" })) {
287
- * console.log(client.name);
288
- * }
289
- * ```
290
- */
291
- iterate(filter?: ClientFilter): AsyncGenerator<Client, void, undefined> {
292
- return this.createIterator({ filter });
293
- }
294
-
295
- // ─────────────────────────────────────────────────────────────────
296
- // Batch Operations
297
- // ─────────────────────────────────────────────────────────────────
298
-
299
- /**
300
- * Creates multiple clients in a single request.
301
- *
302
- * @param clients - Array of client creation data
303
- * @param options - Batch options (atomic: true by default)
304
- * @returns Atomic result with data array, or partial result with succeeded/failed
305
- *
306
- * @example
307
- * ```typescript
308
- * // Atomic mode (default) - all or nothing
309
- * const result = await sdk.clients.createMany([...]);
310
- * if (result.atomic) {
311
- * console.log("All created:", result.data);
312
- * }
313
- *
314
- * // Non-atomic mode - partial success allowed
315
- * const result = await sdk.clients.createMany([...], { atomic: false });
316
- * if (!result.atomic) {
317
- * console.log("Succeeded:", result.succeeded.length);
318
- * console.log("Failed:", result.failed.length);
319
- * }
320
- * ```
321
- */
322
- async createMany(
323
- clients: CreateClientInput[],
324
- options?: { atomic?: true }
325
- ): Promise<AtomicBatchResult<Client>>;
326
- async createMany(
327
- clients: CreateClientInput[],
328
- options: { atomic: false }
329
- ): Promise<PartialBatchResult<Client>>;
330
- async createMany(
331
- clients: CreateClientInput[],
332
- options: BatchOptions = {}
333
- ): Promise<AtomicBatchResult<Client> | PartialBatchResult<Client>> {
334
- const atomic = options.atomic ?? true;
335
- const validated = this.validate(z.array(createClientInputSchema), clients);
336
-
337
- const response = await this.execute(() =>
338
- this.trpc.clients.createMany.mutate({ items: validated, atomic })
339
- );
340
-
341
- if (response.atomic) {
342
- return {
343
- atomic: true,
344
- data: response.data.map((c) => this.instantiate(c)),
345
- };
346
- } else {
347
- return {
348
- atomic: false,
349
- succeeded: response.succeeded.map((item) => ({
350
- index: item.index,
351
- data: this.instantiate(item.data),
352
- })),
353
- failed: response.failed,
354
- };
355
- }
356
- }
357
-
358
- /**
359
- * Updates multiple clients in a single request.
360
- *
361
- * @param updates - Array of { id, data } updates
362
- * @param options - Batch options (atomic: true by default)
363
- * @returns Atomic result with data array, or partial result with succeeded/failed
364
- *
365
- * @example
366
- * ```typescript
367
- * // Atomic mode (default) - all or nothing
368
- * const result = await sdk.clients.updateMany([...]);
369
- * if (result.atomic) {
370
- * console.log("All updated:", result.data);
371
- * }
372
- *
373
- * // Non-atomic mode - partial success allowed
374
- * const result = await sdk.clients.updateMany([...], { atomic: false });
375
- * if (!result.atomic) {
376
- * console.log("Succeeded:", result.succeeded.length);
377
- * console.log("Failed:", result.failed.length);
378
- * }
379
- * ```
380
- */
381
- async updateMany(
382
- updates: { id: string; data: UpdateClientInput }[],
383
- options?: { atomic?: true }
384
- ): Promise<AtomicBatchResult<Client>>;
385
- async updateMany(
386
- updates: { id: string; data: UpdateClientInput }[],
387
- options: { atomic: false }
388
- ): Promise<PartialBatchResult<Client>>;
389
- async updateMany(
390
- updates: { id: string; data: UpdateClientInput }[],
391
- options: BatchOptions = {}
392
- ): Promise<AtomicBatchResult<Client> | PartialBatchResult<Client>> {
393
- const atomic = options.atomic ?? true;
394
- const updateItemSchema = z.object({
395
- id: z.uuid(),
396
- data: updateClientInputSchema,
397
- });
398
-
399
- const validated = this.validate(z.array(updateItemSchema), updates);
400
-
401
- const response = await this.execute(() =>
402
- this.trpc.clients.updateMany.mutate({ items: validated, atomic })
403
- );
404
-
405
- if (response.atomic) {
406
- return {
407
- atomic: true,
408
- data: response.data.map((c) => this.instantiate(c)),
409
- };
410
- } else {
411
- return {
412
- atomic: false,
413
- succeeded: response.succeeded.map((item) => ({
414
- index: item.index,
415
- data: this.instantiate(item.data),
416
- })),
417
- failed: response.failed,
418
- };
419
- }
420
- }
421
-
422
- // ─────────────────────────────────────────────────────────────────
423
- // Client-to-Client Relationship Management
424
- // ─────────────────────────────────────────────────────────────────
425
-
426
- /**
427
- * Links a client to another client with a relationship type.
428
- *
429
- * @param parentClientId - The ID of the parent client (e.g., a company)
430
- * @param childClientId - The ID of the child client (e.g., a legal rep)
431
- * @param relationshipType - The type of relationship
432
- * @param metadata - Optional metadata for the relationship
433
- * @returns The updated parent client
434
- *
435
- * @example
436
- * ```typescript
437
- * // Link a legal representative to a company
438
- * await sdk.clients.linkClient(
439
- * companyId,
440
- * legalRepId,
441
- * ClientRelationshipType.LegalRepresentative
442
- * );
443
- *
444
- * // Link a controlling beneficiary with metadata
445
- * await sdk.clients.linkClient(
446
- * companyId,
447
- * beneficiaryId,
448
- * ClientRelationshipType.ControllingBeneficiary,
449
- * { ownershipPercentage: 51 }
450
- * );
451
- * ```
452
- */
453
- async linkClient(
454
- parentClientId: string,
455
- childClientId: string,
456
- relationshipType: ClientRelationshipType,
457
- metadata?: Record<string, unknown>
458
- ): Promise<Client> {
459
- const response = await this.execute(() =>
460
- this.trpc.clients.linkClient.mutate({
461
- parentClientId,
462
- childClientId,
463
- relationshipType,
464
- metadata,
465
- })
466
- );
467
- return this.instantiate(response);
468
- }
469
-
470
- /**
471
- * Unlinks a client from another client.
472
- *
473
- * @param parentClientId - The ID of the parent client
474
- * @param childClientId - The ID of the child client to unlink
475
- * @param relationshipType - The type of relationship to remove
476
- * @returns The updated parent client
477
- *
478
- * @example
479
- * ```typescript
480
- * // Remove a legal representative
481
- * await sdk.clients.unlinkClient(
482
- * companyId,
483
- * legalRepId,
484
- * ClientRelationshipType.LegalRepresentative
485
- * );
486
- * ```
487
- */
488
- async unlinkClient(
489
- parentClientId: string,
490
- childClientId: string,
491
- relationshipType: ClientRelationshipType
492
- ): Promise<Client> {
493
- const response = await this.execute(() =>
494
- this.trpc.clients.unlinkClient.mutate({
495
- parentClientId,
496
- childClientId,
497
- relationshipType,
498
- })
499
- );
500
- return this.instantiate(response);
501
- }
502
-
503
- /**
504
- * Lists all clients linked to a parent client.
505
- *
506
- * @param parentClientId - The ID of the parent client
507
- * @param relationshipType - Optional filter by relationship type
508
- * @returns Array of LinkedClient models
509
- *
510
- * @example
511
- * ```typescript
512
- * // List all linked clients
513
- * const links = await sdk.clients.listLinkedClients(companyId);
514
- *
515
- * // List only legal representatives
516
- * const legalReps = await sdk.clients.listLinkedClients(
517
- * companyId,
518
- * ClientRelationshipType.LegalRepresentative
519
- * );
520
- *
521
- * // Access relationship metadata
522
- * for (const link of links) {
523
- * console.log(link.id, link.relationshipType, link.metadata);
524
- * }
525
- * ```
526
- */
527
- async listLinkedClients(
528
- parentClientId: string,
529
- relationshipType?: ClientRelationshipType
530
- ): Promise<LinkedClient[]> {
531
- const response = await this.execute(() =>
532
- this.trpc.clients.listLinkedClients.query({
533
- parentClientId,
534
- relationshipType,
535
- })
536
- );
537
-
538
- return response.map((l) => new LinkedClient(l));
539
- }
540
-
541
- /**
542
- * Retrieves a single linked client relationship by ID.
543
- *
544
- * @param id - The ID of the relationship
545
- * @returns The LinkedClient model
546
- *
547
- * @example
548
- * ```typescript
549
- * const relationship = await sdk.clients.retrieveLinkedClient(relationshipId);
550
- * console.log(relationship.relationshipType, relationship.metadata);
551
- * ```
552
- */
553
- async retrieveLinkedClient(id: string): Promise<LinkedClient> {
554
- const response = await this.execute(() =>
555
- this.trpc.clients.retrieveLinkedClient.query({ id })
556
- );
557
- return new LinkedClient(response);
558
- }
559
-
560
- /**
561
- * Updates a linked client relationship's metadata.
562
- *
563
- * @param id - The ID of the relationship
564
- * @param metadata - New metadata for the relationship
565
- * @returns The updated LinkedClient model
566
- *
567
- * @example
568
- * ```typescript
569
- * const updated = await sdk.clients.updateLinkedClient(relationshipId, {
570
- * ownershipPercentage: 51,
571
- * votingRights: true,
572
- * });
573
- * ```
574
- */
575
- async updateLinkedClient(
576
- id: string,
577
- metadata?: Record<string, unknown>
578
- ): Promise<LinkedClient> {
579
- const response = await this.execute(() =>
580
- this.trpc.clients.updateLinkedClient.mutate({ id, metadata })
581
- );
582
- return new LinkedClient(response);
583
- }
584
-
585
- /**
586
- * Replaces all linked clients for a parent client.
587
- *
588
- * @param parentClientId - The ID of the parent client
589
- * @param linkedClients - Array of linked client definitions
590
- * @returns The updated parent client
591
- *
592
- * @example
593
- * ```typescript
594
- * // Replace all linked clients
595
- * await sdk.clients.setLinkedClients(companyId, [
596
- * { clientId: legalRepId, relationshipType: "legal_representative" },
597
- * { clientId: beneficiaryId, relationshipType: "controlling_beneficiary" },
598
- * ]);
599
- * ```
600
- */
601
- async setLinkedClients(
602
- parentClientId: string,
603
- linkedClients: LinkedClientInput[]
604
- ): Promise<Client> {
605
- const validated = this.validate(z.array(linkedClientSchema), linkedClients);
606
-
607
- const response = await this.execute(() =>
608
- this.trpc.clients.setLinkedClients.mutate({
609
- parentClientId,
610
- linkedClients: validated,
611
- })
612
- );
613
- return this.instantiate(response);
614
- }
615
-
616
- // ─────────────────────────────────────────────────────────────────
617
- // Sync All (Atomic)
618
- // ─────────────────────────────────────────────────────────────────
619
-
620
- /**
621
- * Atomically updates a client and all related resources in a single transaction.
622
- *
623
- * All operations succeed or fail together - if any fails, the entire
624
- * transaction is rolled back.
625
- *
626
- * @param clientId - The client ID to update
627
- * @param data - Sync data with optional client, addresses, contactMethods, bankAccounts, linkedClients
628
- * @param options - Sync options
629
- * @param options.replaceMode - When true, items not in the arrays are deleted (default: false)
630
- * @returns Result with updated client and sync results for each resource type
631
- *
632
- * @example
633
- * ```typescript
634
- * // Patch mode (default) - only mentioned items are affected
635
- * const result = await sdk.clients.syncAll("client_123", {
636
- * // Update client fields
637
- * client: { name: "Updated Name" },
638
- *
639
- * // Sync addresses (create, update, delete)
640
- * addresses: [
641
- * { street: "123 New St", city: "Mexico City", country: "MX" }, // create
642
- * { id: "addr_456", street: "Updated St" }, // update
643
- * { id: "addr_789", _delete: true }, // delete
644
- * ],
645
- *
646
- * // Sync contact methods
647
- * contactMethods: [
648
- * { type: "email", value: "new@example.com", isPrimary: true },
649
- * ],
650
- *
651
- * // Sync bank accounts
652
- * bankAccounts: [
653
- * { accountNumber: "1234567890", bankName: "BBVA" },
654
- * ],
655
- *
656
- * // Replace all linked clients
657
- * linkedClients: [
658
- * { clientId: "rep_123", relationshipType: "legal_representative" },
659
- * ],
660
- * });
661
- *
662
- * // Replace mode - provided arrays become the complete set (requires full create input for new items)
663
- * const result = await sdk.clients.syncAll("client_123", {
664
- * addresses: currentAddresses,
665
- * contactMethods: currentContacts,
666
- * bankAccounts: currentAccounts,
667
- * }, { replaceMode: true });
668
- *
669
- * console.log(result.client); // updated client
670
- * console.log(result.addresses.created); // newly created addresses
671
- * console.log(result.addresses.updated); // updated addresses
672
- * console.log(result.addresses.deleted); // deleted address IDs
673
- * ```
674
- */
675
- async syncAll(
676
- clientId: string,
677
- data: {
678
- client?: UpdateClientInput;
679
- addresses?: SyncItem<CreateAddressInput>[];
680
- contactMethods?: SyncItem<CreateContactMethodInput>[];
681
- bankAccounts?: SyncItem<CreateBankAccountInput>[];
682
- linkedClients?: LinkedClientInput[];
683
- },
684
- options: { replaceMode: true }
685
- ): Promise<{
686
- client: Client;
687
- addresses: SyncResult<Address>;
688
- contactMethods: SyncResult<ContactMethod>;
689
- bankAccounts: SyncResult<BankAccount>;
690
- }>;
691
- async syncAll(
692
- clientId: string,
693
- data: {
694
- client?: UpdateClientInput;
695
- addresses?: SyncItem<UpdateAddressInput>[];
696
- contactMethods?: SyncItem<UpdateContactMethodInput>[];
697
- bankAccounts?: SyncItem<UpdateBankAccountInput>[];
698
- linkedClients?: LinkedClientInput[];
699
- },
700
- options?: { replaceMode?: boolean }
701
- ): Promise<{
702
- client: Client;
703
- addresses: SyncResult<Address>;
704
- contactMethods: SyncResult<ContactMethod>;
705
- bankAccounts: SyncResult<BankAccount>;
706
- }>;
707
- async syncAll(
708
- clientId: string,
709
- data: {
710
- client?: UpdateClientInput;
711
- addresses?: SyncItem<UpdateAddressInput | CreateAddressInput>[];
712
- contactMethods?: SyncItem<UpdateContactMethodInput | CreateContactMethodInput>[];
713
- bankAccounts?: SyncItem<UpdateBankAccountInput | CreateBankAccountInput>[];
714
- linkedClients?: LinkedClientInput[];
715
- },
716
- options?: { replaceMode?: boolean }
717
- ): Promise<{
718
- client: Client;
719
- addresses: SyncResult<Address>;
720
- contactMethods: SyncResult<ContactMethod>;
721
- bankAccounts: SyncResult<BankAccount>;
722
- }> {
723
- // Validate inputs - use replaceSyncItemSchema which validates create vs update based on id presence
724
- const validatedClient = data.client
725
- ? this.validate(updateClientInputSchema, data.client)
726
- : undefined;
727
- const validatedAddresses = data.addresses
728
- ? this.validate(
729
- z.array(
730
- replaceSyncItemSchema(createAddressInputSchema, updateAddressInputSchema)
731
- ),
732
- data.addresses
733
- )
734
- : undefined;
735
- const validatedContactMethods = data.contactMethods
736
- ? this.validate(
737
- z.array(
738
- replaceSyncItemSchema(
739
- createContactMethodInputSchema,
740
- updateContactMethodInputSchema
741
- )
742
- ),
743
- data.contactMethods
744
- )
745
- : undefined;
746
- const validatedBankAccounts = data.bankAccounts
747
- ? this.validate(
748
- z.array(
749
- replaceSyncItemSchema(
750
- createBankAccountInputSchema,
751
- updateBankAccountInputSchema
752
- )
753
- ),
754
- data.bankAccounts
755
- )
756
- : undefined;
757
- const validatedLinkedClients = data.linkedClients
758
- ? this.validate(z.array(linkedClientSchema), data.linkedClients)
759
- : undefined;
760
-
761
- const response = await this.execute(() =>
762
- this.trpc.clients.syncAll.mutate({
763
- clientId,
764
- client: validatedClient,
765
- addresses: validatedAddresses,
766
- contactMethods: validatedContactMethods,
767
- bankAccounts: validatedBankAccounts,
768
- linkedClients: validatedLinkedClients,
769
- replaceMode: options?.replaceMode,
770
- })
771
- );
772
-
773
- return {
774
- client: this.instantiate(response.client),
775
- addresses: {
776
- created: response.addresses.created.map((a) => new Address(a)),
777
- updated: response.addresses.updated.map((a) => new Address(a)),
778
- deleted: response.addresses.deleted,
779
- },
780
- contactMethods: {
781
- created: response.contactMethods.created.map(
782
- (c) => new ContactMethod(c)
783
- ),
784
- updated: response.contactMethods.updated.map(
785
- (c) => new ContactMethod(c)
786
- ),
787
- deleted: response.contactMethods.deleted,
788
- },
789
- bankAccounts: {
790
- created: response.bankAccounts.created.map((b) => new BankAccount(b)),
791
- updated: response.bankAccounts.updated.map((b) => new BankAccount(b)),
792
- deleted: response.bankAccounts.deleted,
793
- },
794
- };
795
- }
796
-
797
- // ─────────────────────────────────────────────────────────────────
798
- // Instantiation (protected for subclasses to override)
799
- // ─────────────────────────────────────────────────────────────────
800
-
801
- /**
802
- * Instantiates a Client model from API response data.
803
- * Subclasses override this to return their specific model type.
804
- */
805
- protected instantiate(data: ClientData): Client {
806
- return new Client(data);
807
- }
808
- }