@accounter/server 0.0.9-alpha-20251216144137-dc7316f892e1fcf0e350cfbbaa9ca0746e1c6c50 → 0.0.9-alpha-20251216161545-668306e40cf3aed663f444e0004246489fbec6d4

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 (51) hide show
  1. package/CHANGELOG.md +5 -28
  2. package/dist/green-invoice-graphql/src/mesh-artifacts/index.d.ts +1 -1
  3. package/dist/server/src/__generated__/types.d.ts +301 -282
  4. package/dist/server/src/__generated__/types.js.map +1 -1
  5. package/dist/server/src/modules/app-providers/green-invoice-client.d.ts +3 -3
  6. package/dist/server/src/modules/documents/__generated__/types.d.ts +7 -121
  7. package/dist/server/src/modules/documents/__generated__/types.js.map +1 -1
  8. package/dist/server/src/modules/documents/helpers/common.helper.d.ts +0 -2
  9. package/dist/server/src/modules/documents/helpers/common.helper.js +0 -20
  10. package/dist/server/src/modules/documents/helpers/common.helper.js.map +1 -1
  11. package/dist/server/src/modules/documents/index.js +2 -9
  12. package/dist/server/src/modules/documents/index.js.map +1 -1
  13. package/dist/server/src/modules/green-invoice/__generated__/types.d.ts +135 -6
  14. package/dist/server/src/modules/green-invoice/__generated__/types.js +2 -0
  15. package/dist/server/src/modules/green-invoice/__generated__/types.js.map +1 -1
  16. package/dist/server/src/modules/green-invoice/helpers/contract-to-draft.helper.d.ts +7 -0
  17. package/dist/server/src/modules/green-invoice/helpers/contract-to-draft.helper.js +53 -0
  18. package/dist/server/src/modules/green-invoice/helpers/contract-to-draft.helper.js.map +1 -0
  19. package/dist/server/src/modules/green-invoice/helpers/green-invoice.helper.d.ts +27 -21
  20. package/dist/server/src/modules/green-invoice/helpers/green-invoice.helper.js +146 -151
  21. package/dist/server/src/modules/green-invoice/helpers/green-invoice.helper.js.map +1 -1
  22. package/dist/server/src/modules/green-invoice/helpers/issue-document.helper.d.ts +19 -0
  23. package/dist/server/src/modules/{documents → green-invoice}/helpers/issue-document.helper.js +11 -59
  24. package/dist/server/src/modules/green-invoice/helpers/issue-document.helper.js.map +1 -0
  25. package/dist/server/src/modules/green-invoice/resolvers/green-invoice.resolvers.js +347 -4
  26. package/dist/server/src/modules/green-invoice/resolvers/green-invoice.resolvers.js.map +1 -1
  27. package/dist/server/src/modules/green-invoice/typeDefs/green-invoice.graphql.js +512 -4
  28. package/dist/server/src/modules/green-invoice/typeDefs/green-invoice.graphql.js.map +1 -1
  29. package/dist/shaam-uniform-format-generator/src/generator/records/b110.d.ts +35 -35
  30. package/dist/shaam-uniform-format-generator/src/types/enums.d.ts +71 -71
  31. package/package.json +1 -1
  32. package/src/__generated__/types.ts +620 -327
  33. package/src/modules/documents/__generated__/types.ts +7 -121
  34. package/src/modules/documents/helpers/common.helper.ts +0 -21
  35. package/src/modules/documents/index.ts +2 -9
  36. package/src/modules/green-invoice/__generated__/types.ts +137 -6
  37. package/src/modules/green-invoice/helpers/contract-to-draft.helper.ts +69 -0
  38. package/src/modules/green-invoice/helpers/green-invoice.helper.ts +199 -183
  39. package/src/modules/{documents → green-invoice}/helpers/issue-document.helper.ts +44 -96
  40. package/src/modules/green-invoice/resolvers/green-invoice.resolvers.ts +520 -5
  41. package/src/modules/green-invoice/typeDefs/green-invoice.graphql.ts +512 -4
  42. package/dist/server/src/modules/documents/helpers/issue-document.helper.d.ts +0 -21
  43. package/dist/server/src/modules/documents/helpers/issue-document.helper.js.map +0 -1
  44. package/dist/server/src/modules/documents/resolvers/documents-issuing.resolver.d.ts +0 -2
  45. package/dist/server/src/modules/documents/resolvers/documents-issuing.resolver.js +0 -346
  46. package/dist/server/src/modules/documents/resolvers/documents-issuing.resolver.js.map +0 -1
  47. package/dist/server/src/modules/documents/typeDefs/documents-issuing.graphql.d.ts +0 -2
  48. package/dist/server/src/modules/documents/typeDefs/documents-issuing.graphql.js +0 -228
  49. package/dist/server/src/modules/documents/typeDefs/documents-issuing.graphql.js.map +0 -1
  50. package/src/modules/documents/resolvers/documents-issuing.resolver.ts +0 -529
  51. package/src/modules/documents/typeDefs/documents-issuing.graphql.ts +0 -228
@@ -1,26 +1,471 @@
1
+ import { addMonths, endOfMonth, format, startOfMonth, subMonths } from 'date-fns';
1
2
  import { GraphQLError } from 'graphql';
2
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';
3
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 { ContractsProvider } from '../../contracts/providers/contracts.provider.js';
14
+ import { IGetContractsByIdsResult } from '../../contracts/types.js';
15
+ import { DocumentsProvider } from '../../documents/providers/documents.provider.js';
4
16
  import { IssuedDocumentsProvider } from '../../documents/providers/issued-documents.provider.js';
17
+ import { normalizeDocumentType } from '../../documents/resolvers/common.js';
5
18
  import type {
6
19
  IGetAllIssuedDocumentsResult,
20
+ IGetIssuedDocumentsByIdsResult,
7
21
  IInsertDocumentsResult,
8
22
  IUpdateIssuedDocumentParams,
9
23
  } from '../../documents/types.js';
10
24
  import { validateClientIntegrations } from '../../financial-entities/helpers/clients.helper.js';
25
+ import { BusinessesProvider } from '../../financial-entities/providers/businesses.provider.js';
11
26
  import { ClientsProvider } from '../../financial-entities/providers/clients.provider.js';
27
+ import { TransactionsProvider } from '../../transactions/providers/transactions.provider.js';
28
+ import { convertContractToDraft } from '../helpers/contract-to-draft.helper.js';
12
29
  import {
13
- convertGreenInvoiceDocumentToDocumentDraft,
30
+ convertDocumentInputIntoGreenInvoiceInput,
31
+ convertGreenInvoiceDocumentToLocalDocumentInfo,
32
+ getGreenInvoiceDocumentNameFromType,
14
33
  getGreenInvoiceDocuments,
15
34
  getLinkedDocuments,
16
- greenInvoiceCountryToCountryCode,
17
35
  greenInvoiceToDocumentStatus,
18
36
  insertNewDocumentFromGreenInvoice,
19
37
  } from '../helpers/green-invoice.helper.js';
38
+ import {
39
+ deduceVatTypeFromBusiness,
40
+ executeDocumentIssue,
41
+ filterAndHandleSwiftTransactions,
42
+ getDocumentDateOutOfTransactions,
43
+ getIncomeFromDocuments,
44
+ getLinkedDocumentsAttributes,
45
+ getPaymentsFromTransactions,
46
+ getTypeFromDocumentsAndTransactions,
47
+ } from '../helpers/issue-document.helper.js';
20
48
  import type { GreenInvoiceModule } from '../types.js';
21
49
 
22
50
  export const greenInvoiceResolvers: GreenInvoiceModule.Resolvers = {
23
51
  Query: {
52
+ newDocumentInfoDraftByCharge: async (
53
+ _,
54
+ { chargeId },
55
+ {
56
+ injector,
57
+ adminContext: {
58
+ defaultCryptoConversionFiatCurrency,
59
+ financialAccounts: { swiftBusinessId },
60
+ locality,
61
+ },
62
+ },
63
+ ) => {
64
+ if (!chargeId) {
65
+ throw new GraphQLError('Charge ID is required to fetch document draft');
66
+ }
67
+
68
+ const chargePromise = injector.get(ChargesProvider).getChargeByIdLoader.load(chargeId);
69
+ const documentsPromise = injector
70
+ .get(DocumentsProvider)
71
+ .getDocumentsByChargeIdLoader.load(chargeId);
72
+ const transactionsPromise = injector
73
+ .get(TransactionsProvider)
74
+ .transactionsByChargeIDLoader.load(chargeId)
75
+ .then(res => filterAndHandleSwiftTransactions(res, swiftBusinessId));
76
+
77
+ const [charge, documents, transactions, { documentsCurrency }, { mainBusinessId }] =
78
+ await Promise.all([
79
+ chargePromise,
80
+ documentsPromise,
81
+ transactionsPromise,
82
+ getChargeDocumentsMeta(chargeId, injector),
83
+ getChargeBusinesses(chargeId, injector),
84
+ ]);
85
+
86
+ if (!charge) {
87
+ throw new GraphQLError(`Charge with ID "${chargeId}" not found`);
88
+ }
89
+
90
+ const businessMatchPromise = mainBusinessId
91
+ ? injector.get(ClientsProvider).getClientByIdLoader.load(mainBusinessId)
92
+ : Promise.resolve(undefined);
93
+
94
+ const openIssuedDocumentsPromise = injector
95
+ .get(IssuedDocumentsProvider)
96
+ .getIssuedDocumentsByIdLoader.loadMany(documents.map(d => d.id))
97
+ .then(
98
+ res =>
99
+ res.filter(r => {
100
+ if (!r) return false;
101
+
102
+ if (r instanceof Error) {
103
+ console.error('Failed to fetch issued document', r);
104
+ return false;
105
+ }
106
+
107
+ if (r.status !== 'OPEN') {
108
+ return false;
109
+ }
110
+
111
+ return true;
112
+ }) as IGetIssuedDocumentsByIdsResult[],
113
+ );
114
+
115
+ const paymentPromise = getPaymentsFromTransactions(injector, transactions);
116
+
117
+ const vatTypePromise = deduceVatTypeFromBusiness(injector, locality, mainBusinessId);
118
+
119
+ const [businessMatch, openIssuedDocuments, payment, vatType] = await Promise.all([
120
+ businessMatchPromise,
121
+ openIssuedDocumentsPromise,
122
+ paymentPromise,
123
+ vatTypePromise,
124
+ ]);
125
+
126
+ const greenInvoiceDocuments = await Promise.all(
127
+ openIssuedDocuments.map(doc =>
128
+ injector
129
+ .get(GreenInvoiceClientProvider)
130
+ .documentLoader.load(doc.external_id)
131
+ .then(res => {
132
+ if (!res) {
133
+ console.error('Failed to fetch document from Green Invoice', doc.external_id);
134
+ return null;
135
+ }
136
+ return res;
137
+ }),
138
+ ),
139
+ ).then(res => res.filter(Boolean) as _DOLLAR_defs_Document[]);
140
+
141
+ const greenInvoiceClientId = validateClientIntegrations(
142
+ businessMatch?.integrations ?? {},
143
+ ).greenInvoiceId;
144
+ if (!greenInvoiceClientId) {
145
+ throw new GraphQLError(
146
+ `Green invoice integration missing for business ID="${mainBusinessId}"`,
147
+ );
148
+ }
149
+
150
+ const income = getIncomeFromDocuments(
151
+ openIssuedDocuments.map(doc => ({
152
+ document: documents.find(d => d.id === doc.id)!,
153
+ issuedDocument: doc,
154
+ greenInvoiceDocument: greenInvoiceDocuments.find(gd => gd.id === doc.external_id)!,
155
+ })),
156
+ );
157
+
158
+ if (income.length === 0 && transactions.length > 0) {
159
+ income.push(
160
+ ...transactions.map(transaction => ({
161
+ description: transaction.source_description ?? '',
162
+ quantity: 1,
163
+ price: Number(transaction.amount),
164
+ currency: transaction.currency as Currency,
165
+ currencyRate: undefined,
166
+ vatType,
167
+ })),
168
+ );
169
+ }
170
+
171
+ const type = getTypeFromDocumentsAndTransactions(greenInvoiceDocuments, transactions);
172
+
173
+ const linkedDocsAttributes = getLinkedDocumentsAttributes(
174
+ openIssuedDocuments,
175
+ type === DocumentType.CreditInvoice,
176
+ );
177
+
178
+ let remarks = charge.user_description;
179
+ if (greenInvoiceDocuments.length) {
180
+ remarks = greenInvoiceDocuments.map(doc => doc.remarks).join(', ');
181
+ switch (type) {
182
+ case DocumentType.Receipt:
183
+ case DocumentType.InvoiceReceipt:
184
+ remarks = `${getGreenInvoiceDocumentNameFromType(type)} for ${greenInvoiceDocuments.map(doc => `${getGreenInvoiceDocumentNameFromType(doc.type)} ${doc.number}`).join(', ')}`;
185
+ break;
186
+ }
187
+ } else if (businessMatch?.remark) {
188
+ remarks = businessMatch.remark;
189
+ }
190
+
191
+ const documentDate = getDocumentDateOutOfTransactions(transactions);
192
+
193
+ const transactionsCurrencies = Array.from(new Set(transactions.map(t => t.currency)));
194
+ const transactionsCurrency =
195
+ transactionsCurrencies.length === 1 ? transactionsCurrencies[0] : undefined;
196
+
197
+ return {
198
+ remarks,
199
+ // description: ____,
200
+ // footer: ____,
201
+ type,
202
+ date: documentDate,
203
+ dueDate: dateToTimelessDateString(endOfMonth(new Date())),
204
+ lang: 'ENGLISH',
205
+ currency: (transactionsCurrency ||
206
+ documentsCurrency ||
207
+ defaultCryptoConversionFiatCurrency) as Currency,
208
+ vatType,
209
+ rounding: false,
210
+ signed: true,
211
+ client: greenInvoiceClientId,
212
+ income,
213
+ payment,
214
+ // linkedPaymentId: ____,
215
+ // maxPayments: _____,
216
+ // discount: _____,
217
+ ...linkedDocsAttributes,
218
+ };
219
+ },
220
+ newDocumentInfoDraftByDocument: async (
221
+ _,
222
+ { documentId },
223
+ {
224
+ injector,
225
+ adminContext: {
226
+ defaultCryptoConversionFiatCurrency,
227
+ financialAccounts: { swiftBusinessId },
228
+ locality,
229
+ },
230
+ },
231
+ ) => {
232
+ if (!documentId) {
233
+ throw new GraphQLError('Document ID is required to fetch document draft');
234
+ }
235
+
236
+ const document = await injector
237
+ .get(DocumentsProvider)
238
+ .getDocumentsByIdLoader.load(documentId);
239
+
240
+ if (!document) {
241
+ throw new GraphQLError(`Document with ID "${documentId}" not found`);
242
+ }
243
+
244
+ const chargePromise = document.charge_id
245
+ ? injector.get(ChargesProvider).getChargeByIdLoader.load(document.charge_id)
246
+ : Promise.resolve(undefined);
247
+ const transactionsPromise = document.charge_id
248
+ ? injector
249
+ .get(TransactionsProvider)
250
+ .transactionsByChargeIDLoader.load(document.charge_id)
251
+ .then(res => filterAndHandleSwiftTransactions(res, swiftBusinessId))
252
+ : Promise.resolve(undefined);
253
+ const chargeBusinessIdPromise = document.charge_id
254
+ ? getChargeBusinesses(document.charge_id, injector).then(res => res.mainBusinessId)
255
+ : Promise.resolve(undefined);
256
+
257
+ const [charge, transactions, chargeBusinessId] = await Promise.all([
258
+ chargePromise,
259
+ transactionsPromise,
260
+ chargeBusinessIdPromise,
261
+ ]);
262
+
263
+ if (!charge) {
264
+ throw new GraphQLError(`Charge with ID "${document.charge_id}" not found`);
265
+ }
266
+
267
+ const businessMatchPromise = chargeBusinessId
268
+ ? injector.get(ClientsProvider).getClientByIdLoader.load(chargeBusinessId)
269
+ : Promise.resolve(undefined);
270
+
271
+ const openIssuedDocumentPromise = injector
272
+ .get(IssuedDocumentsProvider)
273
+ .getIssuedDocumentsByIdLoader.load(document.id);
274
+
275
+ const vatTypePromise = deduceVatTypeFromBusiness(injector, locality, chargeBusinessId);
276
+
277
+ const [businessMatch, openIssuedDocument, vatType] = await Promise.all([
278
+ businessMatchPromise,
279
+ openIssuedDocumentPromise,
280
+ vatTypePromise,
281
+ ]);
282
+
283
+ if (!openIssuedDocument) {
284
+ throw new GraphQLError(
285
+ `Document with ID "${document.id}" doesn't seem like a document we issued`,
286
+ );
287
+ }
288
+
289
+ if (openIssuedDocument.status !== 'OPEN') {
290
+ throw new GraphQLError(`Document with ID "${document.id}" is closed`);
291
+ }
292
+
293
+ const greenInvoiceDocumentPromise: Promise<_DOLLAR_defs_Document | null> = injector
294
+ .get(GreenInvoiceClientProvider)
295
+ .documentLoader.load(openIssuedDocument.external_id)
296
+ .then(res => {
297
+ if (!res) {
298
+ console.error(
299
+ 'Failed to fetch document from Green Invoice',
300
+ openIssuedDocument.external_id,
301
+ );
302
+ return null;
303
+ }
304
+ return res;
305
+ });
306
+
307
+ const paymentPromise = getPaymentsFromTransactions(injector, transactions ?? []);
308
+
309
+ const greenInvoiceClientId = validateClientIntegrations(
310
+ businessMatch?.integrations ?? {},
311
+ ).greenInvoiceId;
312
+ if (!greenInvoiceClientId) {
313
+ throw new GraphQLError(
314
+ `Green invoice integration missing for business ID="${chargeBusinessId}"`,
315
+ );
316
+ }
317
+
318
+ const [greenInvoiceDocument, payment, { documentsCurrency }] = await Promise.all([
319
+ greenInvoiceDocumentPromise,
320
+ paymentPromise,
321
+ getChargeDocumentsMeta(charge.id, injector),
322
+ ]);
323
+
324
+ if (!greenInvoiceDocument) {
325
+ throw new GraphQLError(
326
+ `Document with ID "${document.id}" doesn't have a Green Invoice matching document`,
327
+ );
328
+ }
329
+
330
+ const income = getIncomeFromDocuments([
331
+ {
332
+ document,
333
+ issuedDocument: openIssuedDocument,
334
+ greenInvoiceDocument,
335
+ },
336
+ ]);
337
+
338
+ const type = getTypeFromDocumentsAndTransactions([greenInvoiceDocument], transactions ?? []);
339
+
340
+ const linkedDocsAttributes = getLinkedDocumentsAttributes(
341
+ [openIssuedDocument],
342
+ type === DocumentType.CreditInvoice,
343
+ );
344
+
345
+ let remarks = greenInvoiceDocument.remarks;
346
+ switch (type) {
347
+ case DocumentType.Receipt:
348
+ case DocumentType.InvoiceReceipt:
349
+ remarks = `${getGreenInvoiceDocumentNameFromType(type)} for ${getGreenInvoiceDocumentNameFromType(greenInvoiceDocument.type)} ${greenInvoiceDocument.number}`;
350
+ break;
351
+ }
352
+
353
+ if (!remarks && businessMatch?.remark) {
354
+ remarks = businessMatch.remark;
355
+ }
356
+
357
+ const documentDate = getDocumentDateOutOfTransactions(transactions ?? []);
358
+
359
+ return {
360
+ remarks,
361
+ // description: ____,
362
+ // footer: ____,
363
+ type,
364
+ date: documentDate,
365
+ dueDate: dateToTimelessDateString(endOfMonth(new Date())),
366
+ lang: 'ENGLISH',
367
+ currency: (documentsCurrency || defaultCryptoConversionFiatCurrency) as Currency,
368
+ vatType,
369
+ rounding: false,
370
+ signed: true,
371
+ client: greenInvoiceClientId,
372
+ income,
373
+ payment,
374
+ // linkedPaymentId: ____,
375
+ // maxPayments: _____,
376
+ // discount: _____,
377
+ ...linkedDocsAttributes,
378
+ };
379
+ },
380
+ clientMonthlyChargesDrafts: async (_, { issueMonth }, { injector }) => {
381
+ const openContracts = await injector.get(ContractsProvider).getAllOpenContracts();
382
+ const monthlyBillingCycle: BillingCycle = 'MONTHLY';
383
+ const monthlyContracts = openContracts.filter(
384
+ contract => contract.billing_cycle.toLocaleUpperCase() === monthlyBillingCycle,
385
+ );
386
+ const drafts = await Promise.all(
387
+ monthlyContracts.map(async contract =>
388
+ convertContractToDraft(contract, injector, issueMonth),
389
+ ),
390
+ );
391
+
392
+ return drafts;
393
+ },
394
+ clientChargesDraftsByContracts: async (_, { issueMonth, contractIds }, { injector }) => {
395
+ const contracts = await injector
396
+ .get(ContractsProvider)
397
+ .getContractsByIdLoader.loadMany(contractIds)
398
+ .then(res => res.filter(c => !!c && !(c instanceof Error)) as IGetContractsByIdsResult[]);
399
+
400
+ const drafts = await Promise.all(
401
+ contracts.map(async contract => convertContractToDraft(contract, injector, issueMonth)),
402
+ );
403
+
404
+ return drafts;
405
+ },
406
+ clientMonthlyChargeDraft: async (_, { clientId, issueMonth }, { injector }) => {
407
+ const openContracts = await injector.get(ContractsProvider).getAllOpenContracts();
408
+ const contract = openContracts.find(openContract => openContract.client_id === clientId);
409
+
410
+ if (!contract) {
411
+ throw new GraphQLError(`Contract not found for client ID="${clientId}"`);
412
+ }
413
+
414
+ const businessPromise = injector
415
+ .get(BusinessesProvider)
416
+ .getBusinessByIdLoader.load(contract.client_id);
417
+ const clientPromise = injector
418
+ .get(ClientsProvider)
419
+ .getClientByIdLoader.load(contract.client_id);
420
+ const [business, client] = await Promise.all([businessPromise, clientPromise]);
421
+
422
+ if (!business) {
423
+ throw new GraphQLError(`Business ID="${contract.client_id}" not found`);
424
+ }
425
+
426
+ if (!client) {
427
+ throw new GraphQLError(`Client not found for business ID="${contract.client_id}"`);
428
+ }
429
+
430
+ const greenInvoiceId = validateClientIntegrations(client.integrations)?.greenInvoiceId;
431
+
432
+ if (!greenInvoiceId) {
433
+ throw new GraphQLError(
434
+ `Green invoice match not found for business ID="${contract.client_id}"`,
435
+ );
436
+ }
437
+
438
+ const today = issueMonth ? addMonths(new Date(issueMonth), 1) : new Date();
439
+ const monthStart = dateToTimelessDateString(startOfMonth(today));
440
+ const monthEnd = dateToTimelessDateString(endOfMonth(today));
441
+ const year = today.getFullYear() + (today.getMonth() === 0 ? -1 : 0);
442
+ const month = format(subMonths(today, 1), 'MMMM');
443
+
444
+ const draft: ResolversTypes['NewDocumentInfo'] = {
445
+ remarks: `${contract.purchase_orders[0] ? `PO: ${contract.purchase_orders[0]}${contract.remarks ? ', ' : ''}` : ''}${contract.remarks ?? ''}`,
446
+ description: `GraphQL Hive Enterprise License - ${month} ${year}`,
447
+ type: normalizeDocumentType(contract.document_type),
448
+ date: monthStart,
449
+ dueDate: monthEnd,
450
+ lang: 'ENGLISH',
451
+ currency: contract.currency as Currency,
452
+ vatType: 'EXEMPT',
453
+ rounding: false,
454
+ signed: true,
455
+ client: greenInvoiceId,
456
+ income: [
457
+ {
458
+ description: `GraphQL Hive Enterprise License - ${month} ${year}`,
459
+ quantity: 1,
460
+ price: contract.amount,
461
+ currency: contract.currency as Currency,
462
+ vatType: 'EXEMPT',
463
+ },
464
+ ],
465
+ };
466
+
467
+ return draft;
468
+ },
24
469
  greenInvoiceClient: async (_, { clientId }, { injector }) => {
25
470
  try {
26
471
  const client = await injector.get(ClientsProvider).getClientByIdLoader.load(clientId);
@@ -42,7 +487,7 @@ export const greenInvoiceResolvers: GreenInvoiceModule.Resolvers = {
42
487
  },
43
488
  },
44
489
  Mutation: {
45
- syncGreenInvoiceDocuments: async (
490
+ fetchIncomeDocuments: async (
46
491
  _,
47
492
  { ownerId: inputOwnerId, singlePageLimit = true },
48
493
  { injector, adminContext: { defaultAdminBusinessId } },
@@ -146,6 +591,74 @@ export const greenInvoiceResolvers: GreenInvoiceModule.Resolvers = {
146
591
 
147
592
  return addedDocs;
148
593
  },
594
+ issueGreenInvoiceDocuments: async (
595
+ _,
596
+ { generateDocumentsInfo },
597
+ { injector, adminContext: { defaultAdminBusinessId } },
598
+ ) => {
599
+ const errors: string[] = [];
600
+
601
+ await Promise.all(
602
+ generateDocumentsInfo.map(document => {
603
+ executeDocumentIssue(
604
+ injector,
605
+ defaultAdminBusinessId,
606
+ document,
607
+ undefined,
608
+ true,
609
+ undefined,
610
+ true,
611
+ ).catch(e => {
612
+ console.error(e);
613
+ errors.push(
614
+ `${document.client?.name ?? document.client?.greenInvoiceId}: ${e.message}`,
615
+ );
616
+ });
617
+ }),
618
+ );
619
+
620
+ return {
621
+ success: true,
622
+ errors,
623
+ };
624
+ },
625
+ previewGreenInvoiceDocument: async (_, { input: initialInput }, { injector }) => {
626
+ const input = await convertDocumentInputIntoGreenInvoiceInput(initialInput, injector);
627
+ const document = await injector.get(GreenInvoiceClientProvider).previewDocuments({ input });
628
+
629
+ if (!document) {
630
+ throw new GraphQLError('Failed to generate document preview');
631
+ }
632
+
633
+ if ('errorMessage' in document) {
634
+ console.error('Failed to generate document preview', document);
635
+ throw new GraphQLError(
636
+ `Failed to generate document preview, Green Invoice returned: ${document.errorMessage}`,
637
+ );
638
+ }
639
+
640
+ if ('file' in document && document.file) {
641
+ return document.file;
642
+ }
643
+
644
+ console.error('Document preview does not contain a file', document);
645
+ throw new GraphQLError('Document preview does not contain a file');
646
+ },
647
+ issueGreenInvoiceDocument: async (
648
+ _,
649
+ { input: initialInput, emailContent, attachment, chargeId, sendEmail = false },
650
+ { injector, adminContext: { defaultAdminBusinessId } },
651
+ ) => {
652
+ return executeDocumentIssue(
653
+ injector,
654
+ defaultAdminBusinessId,
655
+ initialInput,
656
+ emailContent ?? undefined,
657
+ attachment ?? undefined,
658
+ chargeId ?? undefined,
659
+ sendEmail ?? false,
660
+ );
661
+ },
149
662
  },
150
663
  IssuedDocumentInfo: {
151
664
  originalDocument: async (info, _, { injector }) => {
@@ -159,7 +672,7 @@ export const greenInvoiceResolvers: GreenInvoiceModule.Resolvers = {
159
672
  if (!document) {
160
673
  throw new GraphQLError('Original document not found');
161
674
  }
162
- return await convertGreenInvoiceDocumentToDocumentDraft(document, injector);
675
+ return convertGreenInvoiceDocumentToLocalDocumentInfo(document);
163
676
  } catch (error) {
164
677
  console.error('Error fetching original document:', error);
165
678
  throw new GraphQLError('Error fetching original document');
@@ -187,7 +700,9 @@ export const greenInvoiceResolvers: GreenInvoiceModule.Resolvers = {
187
700
  .clientLoader.load(clientId)
188
701
  .then(client => {
189
702
  if (client?.country) {
190
- return greenInvoiceCountryToCountryCode(client.country);
703
+ // TODO: use Country for server responses
704
+ // greenInvoiceCountryToCountryCode(client.country)
705
+ return client.country;
191
706
  }
192
707
  return null;
193
708
  });