@accounter/server 0.0.9-alpha-20251216161545-668306e40cf3aed663f444e0004246489fbec6d4 → 0.0.9-alpha-20251216205426-5f8c6ec241db9449687000a5cc5dbb8cbb861623

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 (57) hide show
  1. package/CHANGELOG.md +28 -5
  2. package/dist/green-invoice-graphql/src/mesh-artifacts/index.d.ts +1 -1
  3. package/dist/green-invoice-graphql/src/mesh-artifacts/index.js +2 -2
  4. package/dist/green-invoice-graphql/src/mesh-artifacts/index.js.map +1 -1
  5. package/dist/server/src/__generated__/types.d.ts +282 -301
  6. package/dist/server/src/__generated__/types.js.map +1 -1
  7. package/dist/server/src/modules/app-providers/green-invoice-client.d.ts +3 -3
  8. package/dist/server/src/modules/contracts/helpers/contracts.helper.d.ts +2 -0
  9. package/dist/server/src/modules/contracts/helpers/contracts.helper.js +20 -0
  10. package/dist/server/src/modules/contracts/helpers/contracts.helper.js.map +1 -1
  11. package/dist/server/src/modules/documents/__generated__/types.d.ts +121 -7
  12. package/dist/server/src/modules/documents/__generated__/types.js.map +1 -1
  13. package/dist/server/src/modules/documents/helpers/common.helper.d.ts +2 -0
  14. package/dist/server/src/modules/documents/helpers/common.helper.js +20 -0
  15. package/dist/server/src/modules/documents/helpers/common.helper.js.map +1 -1
  16. package/dist/server/src/modules/documents/helpers/issue-document.helper.d.ts +21 -0
  17. package/dist/server/src/modules/{green-invoice → documents}/helpers/issue-document.helper.js +61 -11
  18. package/dist/server/src/modules/documents/helpers/issue-document.helper.js.map +1 -0
  19. package/dist/server/src/modules/documents/index.js +9 -2
  20. package/dist/server/src/modules/documents/index.js.map +1 -1
  21. package/dist/server/src/modules/documents/resolvers/documents-issuing.resolver.d.ts +2 -0
  22. package/dist/server/src/modules/documents/resolvers/documents-issuing.resolver.js +357 -0
  23. package/dist/server/src/modules/documents/resolvers/documents-issuing.resolver.js.map +1 -0
  24. package/dist/server/src/modules/documents/typeDefs/documents-issuing.graphql.d.ts +2 -0
  25. package/dist/server/src/modules/documents/typeDefs/documents-issuing.graphql.js +228 -0
  26. package/dist/server/src/modules/documents/typeDefs/documents-issuing.graphql.js.map +1 -0
  27. package/dist/server/src/modules/green-invoice/__generated__/types.d.ts +6 -135
  28. package/dist/server/src/modules/green-invoice/__generated__/types.js +0 -2
  29. package/dist/server/src/modules/green-invoice/__generated__/types.js.map +1 -1
  30. package/dist/server/src/modules/green-invoice/helpers/green-invoice.helper.d.ts +21 -27
  31. package/dist/server/src/modules/green-invoice/helpers/green-invoice.helper.js +160 -151
  32. package/dist/server/src/modules/green-invoice/helpers/green-invoice.helper.js.map +1 -1
  33. package/dist/server/src/modules/green-invoice/resolvers/green-invoice.resolvers.js +4 -347
  34. package/dist/server/src/modules/green-invoice/resolvers/green-invoice.resolvers.js.map +1 -1
  35. package/dist/server/src/modules/green-invoice/typeDefs/green-invoice.graphql.js +4 -512
  36. package/dist/server/src/modules/green-invoice/typeDefs/green-invoice.graphql.js.map +1 -1
  37. package/dist/shaam-uniform-format-generator/src/generator/records/b110.d.ts +35 -35
  38. package/dist/shaam-uniform-format-generator/src/types/enums.d.ts +71 -71
  39. package/package.json +2 -2
  40. package/src/__generated__/types.ts +327 -620
  41. package/src/modules/contracts/helpers/contracts.helper.ts +22 -0
  42. package/src/modules/documents/__generated__/types.ts +121 -7
  43. package/src/modules/documents/helpers/common.helper.ts +21 -0
  44. package/src/modules/{green-invoice → documents}/helpers/issue-document.helper.ts +104 -44
  45. package/src/modules/documents/index.ts +9 -2
  46. package/src/modules/documents/resolvers/documents-issuing.resolver.ts +554 -0
  47. package/src/modules/documents/typeDefs/documents-issuing.graphql.ts +228 -0
  48. package/src/modules/green-invoice/__generated__/types.ts +6 -137
  49. package/src/modules/green-invoice/helpers/green-invoice.helper.ts +195 -201
  50. package/src/modules/green-invoice/resolvers/green-invoice.resolvers.ts +5 -520
  51. package/src/modules/green-invoice/typeDefs/green-invoice.graphql.ts +4 -512
  52. package/dist/server/src/modules/green-invoice/helpers/contract-to-draft.helper.d.ts +0 -7
  53. package/dist/server/src/modules/green-invoice/helpers/contract-to-draft.helper.js +0 -53
  54. package/dist/server/src/modules/green-invoice/helpers/contract-to-draft.helper.js.map +0 -1
  55. package/dist/server/src/modules/green-invoice/helpers/issue-document.helper.d.ts +0 -19
  56. package/dist/server/src/modules/green-invoice/helpers/issue-document.helper.js.map +0 -1
  57. package/src/modules/green-invoice/helpers/contract-to-draft.helper.ts +0 -69
@@ -0,0 +1,554 @@
1
+ import { addMonths, endOfMonth, format, startOfMonth, subMonths } from 'date-fns';
2
+ import { GraphQLError } from 'graphql';
3
+ import type { _DOLLAR_defs_Document } from '@accounter/green-invoice-graphql';
4
+ import type { BillingCycle, ResolversTypes } from '../../../__generated__/types.js';
5
+ import { Currency, DocumentType } from '../../../shared/enums.js';
6
+ import { dateToTimelessDateString } from '../../../shared/helpers/index.js';
7
+ import { GreenInvoiceClientProvider } from '../../app-providers/green-invoice-client.js';
8
+ import {
9
+ getChargeBusinesses,
10
+ getChargeDocumentsMeta,
11
+ } from '../../charges/helpers/common.helper.js';
12
+ import { ChargesProvider } from '../../charges/providers/charges.provider.js';
13
+ import {
14
+ getProductName,
15
+ getSubscriptionPlanName,
16
+ normalizeProduct,
17
+ normalizeSubscriptionPlan,
18
+ } from '../../contracts/helpers/contracts.helper.js';
19
+ import { ContractsProvider } from '../../contracts/providers/contracts.provider.js';
20
+ import { IGetContractsByIdsResult } from '../../contracts/types.js';
21
+ import { validateClientIntegrations } from '../../financial-entities/helpers/clients.helper.js';
22
+ import { BusinessesProvider } from '../../financial-entities/providers/businesses.provider.js';
23
+ import { ClientsProvider } from '../../financial-entities/providers/clients.provider.js';
24
+ import {
25
+ convertDocumentInputIntoGreenInvoiceInput,
26
+ getDocumentNameFromGreenInvoiceType,
27
+ normalizeGreenInvoiceDocumentType,
28
+ } from '../../green-invoice/helpers/green-invoice.helper.js';
29
+ import { TransactionsProvider } from '../../transactions/providers/transactions.provider.js';
30
+ import { getDocumentNameFromType } from '../helpers/common.helper.js';
31
+ import {
32
+ convertContractToDraft,
33
+ deduceVatTypeFromBusiness,
34
+ executeDocumentIssue,
35
+ filterAndHandleSwiftTransactions,
36
+ getDocumentDateOutOfTransactions,
37
+ getIncomeRecordsFromDocuments,
38
+ getLinkedDocumentsAttributes,
39
+ getPaymentsFromTransactions,
40
+ getTypeFromDocumentsAndTransactions,
41
+ } from '../helpers/issue-document.helper.js';
42
+ import { DocumentsProvider } from '../providers/documents.provider.js';
43
+ import { IssuedDocumentsProvider } from '../providers/issued-documents.provider.js';
44
+ import type { DocumentsModule, IGetIssuedDocumentsByIdsResult } from '../types.js';
45
+ import { normalizeDocumentType } from './common.js';
46
+
47
+ export const documentsIssuingResolvers: DocumentsModule.Resolvers = {
48
+ Query: {
49
+ newDocumentDraftByCharge: async (
50
+ _,
51
+ { chargeId },
52
+ {
53
+ injector,
54
+ adminContext: {
55
+ defaultCryptoConversionFiatCurrency,
56
+ financialAccounts: { swiftBusinessId },
57
+ locality,
58
+ },
59
+ },
60
+ ) => {
61
+ if (!chargeId) {
62
+ throw new GraphQLError('Charge ID is required to fetch document draft');
63
+ }
64
+
65
+ const chargePromise = injector.get(ChargesProvider).getChargeByIdLoader.load(chargeId);
66
+ const documentsPromise = injector
67
+ .get(DocumentsProvider)
68
+ .getDocumentsByChargeIdLoader.load(chargeId);
69
+ const transactionsPromise = injector
70
+ .get(TransactionsProvider)
71
+ .transactionsByChargeIDLoader.load(chargeId)
72
+ .then(res => filterAndHandleSwiftTransactions(res, swiftBusinessId));
73
+
74
+ const [charge, documents, transactions, { documentsCurrency }, { mainBusinessId }] =
75
+ await Promise.all([
76
+ chargePromise,
77
+ documentsPromise,
78
+ transactionsPromise,
79
+ getChargeDocumentsMeta(chargeId, injector),
80
+ getChargeBusinesses(chargeId, injector),
81
+ ]);
82
+
83
+ if (!charge) {
84
+ throw new GraphQLError(`Charge with ID "${chargeId}" not found`);
85
+ }
86
+
87
+ const clientPromise = mainBusinessId
88
+ ? injector.get(ClientsProvider).getClientByIdLoader.load(mainBusinessId)
89
+ : Promise.resolve(undefined);
90
+
91
+ const openIssuedDocumentsPromise = injector
92
+ .get(IssuedDocumentsProvider)
93
+ .getIssuedDocumentsByIdLoader.loadMany(documents.map(d => d.id))
94
+ .then(
95
+ res =>
96
+ res.filter(r => {
97
+ if (!r) return false;
98
+
99
+ if (r instanceof Error) {
100
+ console.error('Failed to fetch issued document', r);
101
+ return false;
102
+ }
103
+
104
+ if (r.status !== 'OPEN') {
105
+ return false;
106
+ }
107
+
108
+ return true;
109
+ }) as IGetIssuedDocumentsByIdsResult[],
110
+ );
111
+
112
+ const paymentPromise = getPaymentsFromTransactions(injector, transactions);
113
+
114
+ const vatTypePromise = deduceVatTypeFromBusiness(injector, locality, mainBusinessId);
115
+
116
+ const [client, openIssuedDocuments, payment, vatType] = await Promise.all([
117
+ clientPromise,
118
+ openIssuedDocumentsPromise,
119
+ paymentPromise,
120
+ vatTypePromise,
121
+ ]);
122
+
123
+ const greenInvoiceDocuments = await Promise.all(
124
+ openIssuedDocuments.map(doc =>
125
+ injector
126
+ .get(GreenInvoiceClientProvider)
127
+ .documentLoader.load(doc.external_id)
128
+ .then(res => {
129
+ if (!res) {
130
+ console.error('Failed to fetch document from Green Invoice', doc.external_id);
131
+ return null;
132
+ }
133
+ return res;
134
+ }),
135
+ ),
136
+ ).then(res => res.filter(Boolean) as _DOLLAR_defs_Document[]);
137
+
138
+ const greenInvoiceClientId = validateClientIntegrations(
139
+ client?.integrations ?? {},
140
+ ).greenInvoiceId;
141
+ if (!greenInvoiceClientId) {
142
+ throw new GraphQLError(
143
+ `Green invoice integration missing for business ID="${mainBusinessId}"`,
144
+ );
145
+ }
146
+
147
+ const income = getIncomeRecordsFromDocuments(
148
+ openIssuedDocuments.map(
149
+ doc => greenInvoiceDocuments.find(gd => gd.id === doc.external_id)!,
150
+ ),
151
+ );
152
+
153
+ if (income.length === 0 && transactions.length > 0) {
154
+ income.push(
155
+ ...transactions.map(transaction => ({
156
+ description: transaction.source_description ?? '',
157
+ quantity: 1,
158
+ price: Number(transaction.amount),
159
+ currency: transaction.currency as Currency,
160
+ currencyRate: undefined,
161
+ vatType,
162
+ })),
163
+ );
164
+ }
165
+
166
+ const type = getTypeFromDocumentsAndTransactions(
167
+ greenInvoiceDocuments.map(doc => ({ type: normalizeGreenInvoiceDocumentType(doc.type) })),
168
+ transactions,
169
+ );
170
+
171
+ const linkedDocsAttributes = getLinkedDocumentsAttributes(
172
+ openIssuedDocuments,
173
+ type === DocumentType.CreditInvoice,
174
+ );
175
+
176
+ let remarks = charge.user_description;
177
+ if (greenInvoiceDocuments.length) {
178
+ remarks = greenInvoiceDocuments.map(doc => doc.remarks).join(', ');
179
+ switch (type) {
180
+ case DocumentType.Receipt:
181
+ case DocumentType.InvoiceReceipt:
182
+ remarks = `${getDocumentNameFromType(type)} for ${greenInvoiceDocuments.map(doc => `${getDocumentNameFromGreenInvoiceType(doc.type)} ${doc.number}`).join(', ')}`;
183
+ break;
184
+ }
185
+ } else if (client?.remark) {
186
+ remarks = client.remark;
187
+ }
188
+
189
+ const documentDate = getDocumentDateOutOfTransactions(transactions);
190
+
191
+ const transactionsCurrencies = Array.from(new Set(transactions.map(t => t.currency)));
192
+ const transactionsCurrency =
193
+ transactionsCurrencies.length === 1 ? transactionsCurrencies[0] : undefined;
194
+
195
+ const draft: ResolversTypes['DocumentDraft'] = {
196
+ remarks,
197
+ // description: ____,
198
+ // footer: ____,
199
+ type,
200
+ date: documentDate,
201
+ dueDate: dateToTimelessDateString(endOfMonth(new Date())),
202
+ language: 'ENGLISH',
203
+ currency: (transactionsCurrency ||
204
+ documentsCurrency ||
205
+ defaultCryptoConversionFiatCurrency) as Currency,
206
+ vatType,
207
+ rounding: false,
208
+ signed: true,
209
+ client,
210
+ income,
211
+ payment,
212
+ // linkedPaymentId: ____,
213
+ // maxPayments: _____,
214
+ // discount: _____,
215
+ ...linkedDocsAttributes,
216
+ };
217
+ return draft;
218
+ },
219
+ newDocumentDraftByDocument: async (
220
+ _,
221
+ { documentId },
222
+ {
223
+ injector,
224
+ adminContext: {
225
+ defaultCryptoConversionFiatCurrency,
226
+ financialAccounts: { swiftBusinessId },
227
+ locality,
228
+ },
229
+ },
230
+ ) => {
231
+ if (!documentId) {
232
+ throw new GraphQLError('Document ID is required to fetch document draft');
233
+ }
234
+
235
+ const document = await injector
236
+ .get(DocumentsProvider)
237
+ .getDocumentsByIdLoader.load(documentId);
238
+
239
+ if (!document) {
240
+ throw new GraphQLError(`Document with ID "${documentId}" not found`);
241
+ }
242
+
243
+ const chargePromise = document.charge_id
244
+ ? injector.get(ChargesProvider).getChargeByIdLoader.load(document.charge_id)
245
+ : Promise.resolve(undefined);
246
+ const transactionsPromise = document.charge_id
247
+ ? injector
248
+ .get(TransactionsProvider)
249
+ .transactionsByChargeIDLoader.load(document.charge_id)
250
+ .then(res => filterAndHandleSwiftTransactions(res, swiftBusinessId))
251
+ : Promise.resolve(undefined);
252
+ const mainBusinessIdPromise = document.charge_id
253
+ ? getChargeBusinesses(document.charge_id, injector).then(res => res.mainBusinessId)
254
+ : Promise.resolve(undefined);
255
+
256
+ const [charge, transactions, mainBusinessId] = await Promise.all([
257
+ chargePromise,
258
+ transactionsPromise,
259
+ mainBusinessIdPromise,
260
+ ]);
261
+
262
+ if (!charge) {
263
+ throw new GraphQLError(`Charge with ID "${document.charge_id}" not found`);
264
+ }
265
+
266
+ const clientPromise = mainBusinessId
267
+ ? injector.get(ClientsProvider).getClientByIdLoader.load(mainBusinessId)
268
+ : Promise.resolve(undefined);
269
+
270
+ const openIssuedDocumentPromise = injector
271
+ .get(IssuedDocumentsProvider)
272
+ .getIssuedDocumentsByIdLoader.load(document.id);
273
+
274
+ const vatTypePromise = deduceVatTypeFromBusiness(injector, locality, mainBusinessId);
275
+
276
+ const [client, openIssuedDocument, vatType] = await Promise.all([
277
+ clientPromise,
278
+ openIssuedDocumentPromise,
279
+ vatTypePromise,
280
+ ]);
281
+
282
+ if (!openIssuedDocument) {
283
+ throw new GraphQLError(
284
+ `Document with ID "${document.id}" doesn't seem like a document we issued`,
285
+ );
286
+ }
287
+
288
+ if (openIssuedDocument.status !== 'OPEN') {
289
+ throw new GraphQLError(`Document with ID "${document.id}" is closed`);
290
+ }
291
+
292
+ const greenInvoiceDocumentPromise: Promise<_DOLLAR_defs_Document | null> = injector
293
+ .get(GreenInvoiceClientProvider)
294
+ .documentLoader.load(openIssuedDocument.external_id)
295
+ .then(res => {
296
+ if (!res) {
297
+ console.error(
298
+ 'Failed to fetch document from Green Invoice',
299
+ openIssuedDocument.external_id,
300
+ );
301
+ return null;
302
+ }
303
+ return res;
304
+ });
305
+
306
+ const paymentPromise = getPaymentsFromTransactions(injector, transactions ?? []);
307
+
308
+ const greenInvoiceClientId = validateClientIntegrations(
309
+ client?.integrations ?? {},
310
+ ).greenInvoiceId;
311
+ if (!greenInvoiceClientId) {
312
+ throw new GraphQLError(
313
+ `Green invoice integration missing for business ID="${mainBusinessId}"`,
314
+ );
315
+ }
316
+
317
+ const [greenInvoiceDocument, payment, { documentsCurrency }] = await Promise.all([
318
+ greenInvoiceDocumentPromise,
319
+ paymentPromise,
320
+ getChargeDocumentsMeta(charge.id, injector),
321
+ ]);
322
+
323
+ if (!greenInvoiceDocument) {
324
+ throw new GraphQLError(
325
+ `Document with ID "${document.id}" doesn't have a Green Invoice matching document`,
326
+ );
327
+ }
328
+
329
+ const income = getIncomeRecordsFromDocuments([greenInvoiceDocument]);
330
+
331
+ const type = getTypeFromDocumentsAndTransactions(
332
+ [{ type: normalizeDocumentType(document.type) }],
333
+ transactions ?? [],
334
+ );
335
+
336
+ const linkedDocsAttributes = getLinkedDocumentsAttributes(
337
+ [openIssuedDocument],
338
+ type === DocumentType.CreditInvoice,
339
+ );
340
+
341
+ let remarks = greenInvoiceDocument.remarks;
342
+ switch (type) {
343
+ case DocumentType.Receipt:
344
+ case DocumentType.InvoiceReceipt:
345
+ remarks = `${getDocumentNameFromType(type)} for ${getDocumentNameFromGreenInvoiceType(greenInvoiceDocument.type)} ${greenInvoiceDocument.number}`;
346
+ break;
347
+ }
348
+
349
+ if (!remarks && client?.remark) {
350
+ remarks = client.remark;
351
+ }
352
+
353
+ const documentDate = getDocumentDateOutOfTransactions(transactions ?? []);
354
+
355
+ const draft: ResolversTypes['DocumentDraft'] = {
356
+ remarks,
357
+ // description: ____,
358
+ // footer: ____,
359
+ type,
360
+ date: documentDate,
361
+ dueDate: dateToTimelessDateString(endOfMonth(new Date())),
362
+ language: 'ENGLISH',
363
+ currency: (documentsCurrency || defaultCryptoConversionFiatCurrency) as Currency,
364
+ vatType,
365
+ rounding: false,
366
+ signed: true,
367
+ client,
368
+ income,
369
+ payment,
370
+ // linkedPaymentId: ____,
371
+ // maxPayments: _____,
372
+ // discount: _____,
373
+ ...linkedDocsAttributes,
374
+ };
375
+ return draft;
376
+ },
377
+ periodicalDocumentDrafts: async (_, { issueMonth }, { injector }) => {
378
+ const openContracts = await injector.get(ContractsProvider).getAllOpenContracts();
379
+ const monthlyBillingCycle: BillingCycle = 'MONTHLY';
380
+ const monthlyContracts = openContracts.filter(
381
+ contract => contract.billing_cycle.toLocaleUpperCase() === monthlyBillingCycle,
382
+ );
383
+ const drafts = await Promise.all(
384
+ monthlyContracts.map(async contract =>
385
+ convertContractToDraft(contract, injector, issueMonth),
386
+ ),
387
+ );
388
+
389
+ return drafts;
390
+ },
391
+ periodicalDocumentDraftsByContracts: async (_, { issueMonth, contractIds }, { injector }) => {
392
+ const contracts = await injector
393
+ .get(ContractsProvider)
394
+ .getContractsByIdLoader.loadMany(contractIds)
395
+ .then(res => res.filter(c => !!c && !(c instanceof Error)) as IGetContractsByIdsResult[]);
396
+
397
+ const drafts = await Promise.all(
398
+ contracts.map(async contract => convertContractToDraft(contract, injector, issueMonth)),
399
+ );
400
+
401
+ return drafts;
402
+ },
403
+ clientMonthlyChargeDraft: async (_, { clientId, issueMonth }, { injector }) => {
404
+ const clientContracts = await injector
405
+ .get(ContractsProvider)
406
+ .getContractsByClientIdLoader.load(clientId);
407
+ const contracts = clientContracts.filter(clientContract => clientContract.is_active);
408
+
409
+ if (contracts.length === 0) {
410
+ throw new GraphQLError(`No active contracts found for client ID="${clientId}"`);
411
+ }
412
+ if (contracts.length > 1) {
413
+ throw new GraphQLError(
414
+ `Multiple active contracts found for client ID="${clientId}", cannot deduce which one to use`,
415
+ );
416
+ }
417
+
418
+ const contract = contracts[0];
419
+
420
+ if (!contract) {
421
+ throw new GraphQLError(`Contract not found for client ID="${clientId}"`);
422
+ }
423
+
424
+ const businessPromise = injector
425
+ .get(BusinessesProvider)
426
+ .getBusinessByIdLoader.load(contract.client_id);
427
+ const clientPromise = injector
428
+ .get(ClientsProvider)
429
+ .getClientByIdLoader.load(contract.client_id);
430
+ const [business, client] = await Promise.all([businessPromise, clientPromise]);
431
+
432
+ if (!business) {
433
+ throw new GraphQLError(`Business ID="${contract.client_id}" not found`);
434
+ }
435
+
436
+ if (!client) {
437
+ throw new GraphQLError(`Client not found for business ID="${contract.client_id}"`);
438
+ }
439
+
440
+ const greenInvoiceId = validateClientIntegrations(client.integrations)?.greenInvoiceId;
441
+
442
+ if (!greenInvoiceId) {
443
+ throw new GraphQLError(
444
+ `Green invoice match not found for business ID="${contract.client_id}"`,
445
+ );
446
+ }
447
+
448
+ const today = issueMonth ? addMonths(new Date(issueMonth), 1) : new Date();
449
+ const monthStart = dateToTimelessDateString(startOfMonth(today));
450
+ const monthEnd = dateToTimelessDateString(endOfMonth(today));
451
+ const year = today.getFullYear() + (today.getMonth() === 0 ? -1 : 0);
452
+ const month = format(subMonths(today, 1), 'MMMM');
453
+
454
+ const vatType = await deduceVatTypeFromBusiness(
455
+ injector,
456
+ business.country,
457
+ contract.client_id,
458
+ );
459
+
460
+ const draft: ResolversTypes['DocumentDraft'] = {
461
+ remarks: `${contract.purchase_orders[0] ? `PO: ${contract.purchase_orders[0]}${contract.remarks ? ', ' : ''}` : ''}${contract.remarks ?? ''}`,
462
+ description: `${getProductName(normalizeProduct(contract.product ?? '')!)} ${getSubscriptionPlanName(normalizeSubscriptionPlan(contract.plan ?? '')!)} - ${month} ${year}`,
463
+ type: normalizeDocumentType(contract.document_type),
464
+ date: monthStart,
465
+ dueDate: monthEnd,
466
+ language: 'ENGLISH',
467
+ currency: contract.currency as Currency,
468
+ vatType,
469
+ rounding: false,
470
+ signed: true,
471
+ client,
472
+ income: [
473
+ {
474
+ description: `${getProductName(normalizeProduct(contract.product ?? '')!)} ${getSubscriptionPlanName(normalizeSubscriptionPlan(contract.plan ?? '')!)} - ${month} ${year}`,
475
+ quantity: 1,
476
+ price: contract.amount,
477
+ currency: contract.currency as Currency,
478
+ vatType,
479
+ },
480
+ ],
481
+ };
482
+
483
+ return draft;
484
+ },
485
+ },
486
+ Mutation: {
487
+ issueGreenInvoiceDocuments: async (
488
+ _,
489
+ { generateDocumentsInfo },
490
+ { injector, adminContext: { defaultAdminBusinessId } },
491
+ ) => {
492
+ const errors: string[] = [];
493
+
494
+ await Promise.all(
495
+ generateDocumentsInfo.map(document => {
496
+ executeDocumentIssue(
497
+ injector,
498
+ defaultAdminBusinessId,
499
+ document,
500
+ undefined,
501
+ true,
502
+ undefined,
503
+ true,
504
+ ).catch(e => {
505
+ console.error(e);
506
+ errors.push(`${document.client?.name ?? document.client?.id}: ${e.message}`);
507
+ });
508
+ }),
509
+ );
510
+
511
+ return {
512
+ success: true,
513
+ errors,
514
+ };
515
+ },
516
+ issueGreenInvoiceDocument: async (
517
+ _,
518
+ { input: initialInput, emailContent, attachment, chargeId, sendEmail = false },
519
+ { injector, adminContext: { defaultAdminBusinessId } },
520
+ ) => {
521
+ return executeDocumentIssue(
522
+ injector,
523
+ defaultAdminBusinessId,
524
+ initialInput,
525
+ emailContent ?? undefined,
526
+ attachment ?? undefined,
527
+ chargeId ?? undefined,
528
+ sendEmail ?? false,
529
+ );
530
+ },
531
+ previewDocument: async (_, { input: initialInput }, { injector }) => {
532
+ const input = await convertDocumentInputIntoGreenInvoiceInput(initialInput, injector);
533
+ const document = await injector.get(GreenInvoiceClientProvider).previewDocuments({ input });
534
+
535
+ if (!document) {
536
+ throw new GraphQLError('Failed to generate document preview');
537
+ }
538
+
539
+ if ('errorMessage' in document) {
540
+ console.error('Failed to generate document preview', document);
541
+ throw new GraphQLError(
542
+ `Failed to generate document preview, Green Invoice returned: ${document.errorMessage}`,
543
+ );
544
+ }
545
+
546
+ if ('file' in document && document.file) {
547
+ return document.file;
548
+ }
549
+
550
+ console.error('Document preview does not contain a file', document);
551
+ throw new GraphQLError('Document preview does not contain a file');
552
+ },
553
+ },
554
+ };