@accounter/server 0.0.8-alpha-20251102090006-19af6733a3520f285296a817bc4c06801e8a37b4 → 0.0.8-alpha-20251102162123-4116da86b4346a2ba1598da87dfbed6dff5b3393

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 (43) hide show
  1. package/CHANGELOG.md +5 -5
  2. package/dist/green-invoice-graphql/src/mesh-artifacts/index.d.ts +1454 -1201
  3. package/dist/green-invoice-graphql/src/mesh-artifacts/index.js +602 -415
  4. package/dist/green-invoice-graphql/src/mesh-artifacts/index.js.map +1 -1
  5. package/dist/green-invoice-graphql/src/mesh-artifacts/sources/GreenInvoice/schemaWithAnnotations.js +12447 -10635
  6. package/dist/green-invoice-graphql/src/mesh-artifacts/sources/GreenInvoice/schemaWithAnnotations.js.map +1 -1
  7. package/dist/green-invoice-graphql/src/mesh-artifacts/sources/GreenInvoice/types.d.ts +780 -643
  8. package/dist/green-invoice-graphql/src/mesh-artifacts/sources/GreenInvoiceNew/schemaWithAnnotations.js +8 -8
  9. package/dist/green-invoice-graphql/src/mesh-artifacts/sources/GreenInvoiceNew/schemaWithAnnotations.js.map +1 -1
  10. package/dist/green-invoice-graphql/src/mesh-artifacts/sources/GreenInvoiceNew/types.d.ts +8 -8
  11. package/dist/server/src/modules/app-providers/green-invoice-client.d.ts +28 -24
  12. package/dist/server/src/modules/app-providers/green-invoice-client.js +67 -1
  13. package/dist/server/src/modules/app-providers/green-invoice-client.js.map +1 -1
  14. package/dist/server/src/modules/countries/types.d.ts +251 -0
  15. package/dist/server/src/modules/countries/types.js +252 -0
  16. package/dist/server/src/modules/countries/types.js.map +1 -1
  17. package/dist/server/src/modules/financial-entities/resolvers/businesses.resolver.js +6 -2
  18. package/dist/server/src/modules/financial-entities/resolvers/businesses.resolver.js.map +1 -1
  19. package/dist/server/src/modules/financial-entities/resolvers/clients.resolvers.js +5 -0
  20. package/dist/server/src/modules/financial-entities/resolvers/clients.resolvers.js.map +1 -1
  21. package/dist/server/src/modules/green-invoice/helpers/contract-to-draft.helper.js +1 -1
  22. package/dist/server/src/modules/green-invoice/helpers/contract-to-draft.helper.js.map +1 -1
  23. package/dist/server/src/modules/green-invoice/helpers/green-invoice-clients.helper.d.ts +5 -0
  24. package/dist/server/src/modules/green-invoice/helpers/green-invoice-clients.helper.js +194 -0
  25. package/dist/server/src/modules/green-invoice/helpers/green-invoice-clients.helper.js.map +1 -0
  26. package/dist/server/src/modules/green-invoice/helpers/green-invoice.helper.d.ts +26 -23
  27. package/dist/server/src/modules/green-invoice/helpers/green-invoice.helper.js +1018 -4
  28. package/dist/server/src/modules/green-invoice/helpers/green-invoice.helper.js.map +1 -1
  29. package/dist/server/src/modules/green-invoice/helpers/issue-document.helper.d.ts +4 -5
  30. package/dist/server/src/modules/green-invoice/helpers/issue-document.helper.js +0 -29
  31. package/dist/server/src/modules/green-invoice/helpers/issue-document.helper.js.map +1 -1
  32. package/dist/server/src/modules/green-invoice/resolvers/green-invoice.resolvers.js +2 -1
  33. package/dist/server/src/modules/green-invoice/resolvers/green-invoice.resolvers.js.map +1 -1
  34. package/package.json +2 -2
  35. package/src/modules/app-providers/green-invoice-client.ts +82 -1
  36. package/src/modules/countries/types.ts +252 -0
  37. package/src/modules/financial-entities/resolvers/businesses.resolver.ts +10 -2
  38. package/src/modules/financial-entities/resolvers/clients.resolvers.ts +11 -0
  39. package/src/modules/green-invoice/helpers/contract-to-draft.helper.ts +1 -1
  40. package/src/modules/green-invoice/helpers/green-invoice-clients.helper.ts +241 -0
  41. package/src/modules/green-invoice/helpers/green-invoice.helper.ts +1068 -43
  42. package/src/modules/green-invoice/helpers/issue-document.helper.ts +3 -41
  43. package/src/modules/green-invoice/resolvers/green-invoice.resolvers.ts +6 -6
@@ -1,2 +1,254 @@
1
1
  export * from './__generated__/types.js';
2
2
  export * from './__generated__/countries.types.js';
3
+
4
+ export enum CountryCode {
5
+ AFG = 'Afghanistan',
6
+ ALB = 'Albania',
7
+ DZA = 'Algeria',
8
+ ASM = 'American Samoa',
9
+ AND = 'Andorra',
10
+ AGO = 'Angola',
11
+ AIA = 'Anguilla',
12
+ ATA = 'Antarctica',
13
+ ATG = 'Antigua and Barbuda',
14
+ ARG = 'Argentina',
15
+ ARM = 'Armenia',
16
+ ABW = 'Aruba',
17
+ AUS = 'Australia',
18
+ AUT = 'Austria',
19
+ AZE = 'Azerbaijan',
20
+ BHS = 'Bahamas (the)',
21
+ BHR = 'Bahrain',
22
+ BGD = 'Bangladesh',
23
+ BRB = 'Barbados',
24
+ BLR = 'Belarus',
25
+ BEL = 'Belgium',
26
+ BLZ = 'Belize',
27
+ BEN = 'Benin',
28
+ BMU = 'Bermuda',
29
+ BTN = 'Bhutan',
30
+ BOL = 'Bolivia (Plurinational State of)',
31
+ BES = 'Bonaire, Sint Eustatius and Saba',
32
+ BIH = 'Bosnia and Herzegovina',
33
+ BWA = 'Botswana',
34
+ BVT = 'Bouvet Island',
35
+ BRA = 'Brazil',
36
+ IOT = 'British Indian Ocean Territory (the)',
37
+ BRN = 'Brunei Darussalam',
38
+ BGR = 'Bulgaria',
39
+ BFA = 'Burkina Faso',
40
+ BDI = 'Burundi',
41
+ CPV = 'Cabo Verde',
42
+ KHM = 'Cambodia',
43
+ CMR = 'Cameroon',
44
+ CAN = 'Canada',
45
+ CYM = 'Cayman Islands (the)',
46
+ CAF = 'Central African Republic (the)',
47
+ TCD = 'Chad',
48
+ CHL = 'Chile',
49
+ CHN = 'China',
50
+ CXR = 'Christmas Island',
51
+ CCK = 'Cocos (Keeling) Islands (the)',
52
+ COL = 'Colombia',
53
+ COM = 'Comoros (the)',
54
+ COD = 'Congo (the Democratic Republic of the)',
55
+ COG = 'Congo (the)',
56
+ COK = 'Cook Islands (the)',
57
+ CRI = 'Costa Rica',
58
+ HRV = 'Croatia',
59
+ CUB = 'Cuba',
60
+ CUW = 'Curaçao',
61
+ CYP = 'Cyprus',
62
+ CZE = 'Czechia',
63
+ CIV = "Côte d'Ivoire",
64
+ DNK = 'Denmark',
65
+ DJI = 'Djibouti',
66
+ DMA = 'Dominica',
67
+ DOM = 'Dominican Republic (the)',
68
+ ECU = 'Ecuador',
69
+ EGY = 'Egypt',
70
+ SLV = 'El Salvador',
71
+ GNQ = 'Equatorial Guinea',
72
+ ERI = 'Eritrea',
73
+ EST = 'Estonia',
74
+ SWZ = 'Eswatini',
75
+ ETH = 'Ethiopia',
76
+ FLK = 'Falkland Islands (the) [Malvinas]',
77
+ FRO = 'Faroe Islands (the)',
78
+ FJI = 'Fiji',
79
+ FIN = 'Finland',
80
+ FRA = 'France',
81
+ GUF = 'French Guiana',
82
+ PYF = 'French Polynesia',
83
+ ATF = 'French Southern Territories (the)',
84
+ GAB = 'Gabon',
85
+ GMB = 'Gambia (the)',
86
+ GEO = 'Georgia',
87
+ DEU = 'Germany',
88
+ GHA = 'Ghana',
89
+ GIB = 'Gibraltar',
90
+ GRC = 'Greece',
91
+ GRL = 'Greenland',
92
+ GRD = 'Grenada',
93
+ GLP = 'Guadeloupe',
94
+ GUM = 'Guam',
95
+ GTM = 'Guatemala',
96
+ GGY = 'Guernsey',
97
+ GIN = 'Guinea',
98
+ GNB = 'Guinea-Bissau',
99
+ GUY = 'Guyana',
100
+ HTI = 'Haiti',
101
+ HMD = 'Heard Island and McDonald Islands',
102
+ VAT = 'Holy See (the)',
103
+ HND = 'Honduras',
104
+ HKG = 'Hong Kong',
105
+ HUN = 'Hungary',
106
+ ISL = 'Iceland',
107
+ IND = 'India',
108
+ IDN = 'Indonesia',
109
+ IRN = 'Iran (Islamic Republic of)',
110
+ IRQ = 'Iraq',
111
+ IRL = 'Ireland',
112
+ IMN = 'Isle of Man',
113
+ ISR = 'Israel',
114
+ ITA = 'Italy',
115
+ JAM = 'Jamaica',
116
+ JPN = 'Japan',
117
+ JEY = 'Jersey',
118
+ JOR = 'Jordan',
119
+ KAZ = 'Kazakhstan',
120
+ KEN = 'Kenya',
121
+ KIR = 'Kiribati',
122
+ PRK = "Korea (the Democratic People's Republic of)",
123
+ KOR = 'Korea (the Republic of)',
124
+ KWT = 'Kuwait',
125
+ KGZ = 'Kyrgyzstan',
126
+ LAO = "Lao People's Democratic Republic (the)",
127
+ LVA = 'Latvia',
128
+ LBN = 'Lebanon',
129
+ LSO = 'Lesotho',
130
+ LBR = 'Liberia',
131
+ LBY = 'Libya',
132
+ LIE = 'Liechtenstein',
133
+ LTU = 'Lithuania',
134
+ LUX = 'Luxembourg',
135
+ MAC = 'Macao',
136
+ MDG = 'Madagascar',
137
+ MWI = 'Malawi',
138
+ MYS = 'Malaysia',
139
+ MDV = 'Maldives',
140
+ MLI = 'Mali',
141
+ MLT = 'Malta',
142
+ MHL = 'Marshall Islands (the)',
143
+ MTQ = 'Martinique',
144
+ MRT = 'Mauritania',
145
+ MUS = 'Mauritius',
146
+ MYT = 'Mayotte',
147
+ MEX = 'Mexico',
148
+ FSM = 'Micronesia (Federated States of)',
149
+ MDA = 'Moldova (the Republic of)',
150
+ MCO = 'Monaco',
151
+ MNG = 'Mongolia',
152
+ MNE = 'Montenegro',
153
+ MSR = 'Montserrat',
154
+ MAR = 'Morocco',
155
+ MOZ = 'Mozambique',
156
+ MMR = 'Myanmar',
157
+ NAM = 'Namibia',
158
+ NRU = 'Nauru',
159
+ NPL = 'Nepal',
160
+ NLD = 'Netherlands (the)',
161
+ NCL = 'New Caledonia',
162
+ NZL = 'New Zealand',
163
+ NIC = 'Nicaragua',
164
+ NER = 'Niger (the)',
165
+ NGA = 'Nigeria',
166
+ NIU = 'Niue',
167
+ NFK = 'Norfolk Island',
168
+ MNP = 'Northern Mariana Islands (the)',
169
+ NOR = 'Norway',
170
+ OMN = 'Oman',
171
+ PAK = 'Pakistan',
172
+ PLW = 'Palau',
173
+ PSE = 'Palestine, State of',
174
+ PAN = 'Panama',
175
+ PNG = 'Papua New Guinea',
176
+ PRY = 'Paraguay',
177
+ PER = 'Peru',
178
+ PHL = 'Philippines (the)',
179
+ PCN = 'Pitcairn',
180
+ POL = 'Poland',
181
+ PRT = 'Portugal',
182
+ PRI = 'Puerto Rico',
183
+ QAT = 'Qatar',
184
+ MKD = 'Republic of North Macedonia',
185
+ ROU = 'Romania',
186
+ RUS = 'Russian Federation (the)',
187
+ RWA = 'Rwanda',
188
+ REU = 'Réunion',
189
+ BLM = 'Saint Barthélemy',
190
+ SHN = 'Saint Helena, Ascension and Tristan da Cunha',
191
+ KNA = 'Saint Kitts and Nevis',
192
+ LCA = 'Saint Lucia',
193
+ MAF = 'Saint Martin (French part)',
194
+ SPM = 'Saint Pierre and Miquelon',
195
+ VCT = 'Saint Vincent and the Grenadines',
196
+ WSM = 'Samoa',
197
+ SMR = 'San Marino',
198
+ STP = 'Sao Tome and Principe',
199
+ SAU = 'Saudi Arabia',
200
+ SEN = 'Senegal',
201
+ SRB = 'Serbia',
202
+ SYC = 'Seychelles',
203
+ SLE = 'Sierra Leone',
204
+ SGP = 'Singapore',
205
+ SXM = 'Sint Maarten (Dutch part)',
206
+ SVK = 'Slovakia',
207
+ SVN = 'Slovenia',
208
+ SLB = 'Solomon Islands',
209
+ SOM = 'Somalia',
210
+ ZAF = 'South Africa',
211
+ SGS = 'South Georgia and the South Sandwich Islands',
212
+ SSD = 'South Sudan',
213
+ ESP = 'Spain',
214
+ LKA = 'Sri Lanka',
215
+ SDN = 'Sudan (the)',
216
+ SUR = 'Suriname',
217
+ SJM = 'Svalbard and Jan Mayen',
218
+ SWE = 'Sweden',
219
+ CHE = 'Switzerland',
220
+ SYR = 'Syrian Arab Republic',
221
+ TWN = 'Taiwan (Province of China)',
222
+ TJK = 'Tajikistan',
223
+ TZA = 'Tanzania, United Republic of',
224
+ THA = 'Thailand',
225
+ TLS = 'Timor-Leste',
226
+ TGO = 'Togo',
227
+ TKL = 'Tokelau',
228
+ TON = 'Tonga',
229
+ TTO = 'Trinidad and Tobago',
230
+ TUN = 'Tunisia',
231
+ TUR = 'Turkey',
232
+ TKM = 'Turkmenistan',
233
+ TCA = 'Turks and Caicos Islands (the)',
234
+ TUV = 'Tuvalu',
235
+ UGA = 'Uganda',
236
+ UKR = 'Ukraine',
237
+ ARE = 'United Arab Emirates (the)',
238
+ GBR = 'United Kingdom of Great Britain and Northern Ireland (the)',
239
+ UMI = 'United States Minor Outlying Islands (the)',
240
+ USA = 'United States of America (the)',
241
+ URY = 'Uruguay',
242
+ UZB = 'Uzbekistan',
243
+ VUT = 'Vanuatu',
244
+ VEN = 'Venezuela (Bolivarian Republic of)',
245
+ VNM = 'Viet Nam',
246
+ VGB = 'Virgin Islands (British)',
247
+ VIR = 'Virgin Islands (U.S.)',
248
+ WLF = 'Wallis and Futuna',
249
+ ESH = 'Western Sahara',
250
+ YEM = 'Yemen',
251
+ ZMB = 'Zambia',
252
+ ZWE = 'Zimbabwe',
253
+ ALA = 'Åland Islands',
254
+ }
@@ -3,6 +3,7 @@ import {
3
3
  SuggestionData,
4
4
  suggestionDataSchema,
5
5
  } from '@modules/financial-entities/helpers/business-suggestion-data-schema.helper.js';
6
+ import { updateGreenInvoiceClient } from '@modules/green-invoice/helpers/green-invoice-clients.helper.js';
6
7
  import { SortCodesProvider } from '@modules/sort-codes/providers/sort-codes.provider.js';
7
8
  import { TagsProvider } from '@modules/tags/providers/tags.provider.js';
8
9
  import { TransactionsProvider } from '@modules/transactions/providers/transactions.provider.js';
@@ -109,6 +110,9 @@ export const businessesResolvers: FinancialEntitiesModule.Resolvers &
109
110
  suggestionData,
110
111
  pcn874RecordTypeOverride: fields.pcn874RecordType,
111
112
  };
113
+
114
+ let updatedBusiness: IGetBusinessesByIdsResult | undefined = undefined;
115
+
112
116
  try {
113
117
  if (
114
118
  fields.hebrewName ||
@@ -156,19 +160,23 @@ export const businessesResolvers: FinancialEntitiesModule.Resolvers &
156
160
  }
157
161
  }
158
162
 
159
- const updatedBusiness = await injector
163
+ updatedBusiness = await injector
160
164
  .get(BusinessesProvider)
161
165
  .getBusinessByIdLoader.load(businessId);
162
166
  if (!updatedBusiness) {
163
167
  throw new Error(`Updated business not found`);
164
168
  }
165
- return updatedBusiness;
166
169
  } catch (e) {
167
170
  return {
168
171
  __typename: 'CommonError',
169
172
  message: `Failed to update business ID="${businessId}": ${(e as Error).message}`,
170
173
  };
171
174
  }
175
+
176
+ // update green invoice client if needed
177
+ await updateGreenInvoiceClient(businessId, injector, fields);
178
+
179
+ return updatedBusiness;
172
180
  },
173
181
  insertNewBusiness: async (
174
182
  _,
@@ -1,6 +1,10 @@
1
1
  import { GraphQLError } from 'graphql';
2
2
  import { GreenInvoiceClientProvider } from '@modules/app-providers/green-invoice-client.js';
3
3
  import { BusinessesProvider } from '@modules/financial-entities/providers/businesses.provider.js';
4
+ import {
5
+ addGreenInvoiceClient,
6
+ updateGreenInvoiceClient,
7
+ } from '@modules/green-invoice/helpers/green-invoice-clients.helper.js';
4
8
  import { ClientsProvider } from '../providers/clients.provider.js';
5
9
  import type {
6
10
  FinancialEntitiesModule,
@@ -58,6 +62,10 @@ export const clientsResolvers: FinancialEntitiesModule.Resolvers = {
58
62
  }
59
63
  throw new Error(message);
60
64
  });
65
+
66
+ // update green invoice client if needed
67
+ await updateGreenInvoiceClient(businessId, injector, undefined, fields);
68
+
61
69
  return updatedClient;
62
70
  } catch (e) {
63
71
  return {
@@ -80,6 +88,9 @@ export const clientsResolvers: FinancialEntitiesModule.Resolvers = {
80
88
  throw new Error(`No client returned after insertion`);
81
89
  }
82
90
 
91
+ // create green invoice client record
92
+ await addGreenInvoiceClient(insertClient.business_id, injector);
93
+
83
94
  return insertClient;
84
95
  } catch (e) {
85
96
  console.error(e);
@@ -9,7 +9,7 @@ import { Currency } from '@shared/enums';
9
9
  import { NewDocumentInfo } from '@shared/gql-types';
10
10
  import { dateToTimelessDateString } from '@shared/helpers';
11
11
  import { TimelessDateString } from '@shared/types';
12
- import { getClientFromGreenInvoiceClient } from './issue-document.helper.js';
12
+ import { getClientFromGreenInvoiceClient } from './green-invoice-clients.helper';
13
13
 
14
14
  export const convertContractToDraft = async (
15
15
  contract: IGetContractsByIdsResult,
@@ -0,0 +1,241 @@
1
+ import { Injector } from 'graphql-modules';
2
+ import {
3
+ _DOLLAR_defs_addClientRequest_Input,
4
+ _DOLLAR_defs_updateClientRequest_Input,
5
+ } from '@accounter/green-invoice-graphql';
6
+ import { GreenInvoiceClientProvider } from '@modules/app-providers/green-invoice-client.js';
7
+ import { CountryCode } from '@modules/countries/types.js';
8
+ import { BusinessesProvider } from '@modules/financial-entities/providers/businesses.provider.js';
9
+ import { ClientsProvider } from '@modules/financial-entities/providers/clients.provider.js';
10
+ import {
11
+ IGetBusinessesByIdsResult,
12
+ IGetClientsByIdsResult,
13
+ } from '@modules/financial-entities/types.js';
14
+ import { ClientUpdateInput, GreenInvoiceClient, UpdateBusinessInput } from '@shared/gql-types';
15
+ import { countryCodeToGreenInvoiceCountry } from './green-invoice.helper.js';
16
+
17
+ export async function getClientFromGreenInvoiceClient(
18
+ injector: Injector,
19
+ businessId: string,
20
+ useGreenInvoiceId = false,
21
+ ): Promise<GreenInvoiceClient | undefined> {
22
+ const client = await injector.get(ClientsProvider).getClientByIdLoader.load(businessId);
23
+ if (!client) {
24
+ return useGreenInvoiceId ? undefined : { id: businessId };
25
+ }
26
+
27
+ const greenInvoiceClient = await injector
28
+ .get(GreenInvoiceClientProvider)
29
+ .clientLoader.load(client.green_invoice_id);
30
+
31
+ if (!greenInvoiceClient) {
32
+ return useGreenInvoiceId ? undefined : { id: businessId };
33
+ }
34
+
35
+ return {
36
+ id: useGreenInvoiceId && greenInvoiceClient.id ? greenInvoiceClient.id : businessId,
37
+ country: greenInvoiceClient.country,
38
+ emails: [
39
+ ...((greenInvoiceClient.emails?.filter(Boolean) as string[]) ?? []),
40
+ 'ap@the-guild.dev',
41
+ ],
42
+ name: greenInvoiceClient.name,
43
+ phone: greenInvoiceClient.phone,
44
+ taxId: greenInvoiceClient.taxId,
45
+ address: greenInvoiceClient.address,
46
+ city: greenInvoiceClient.city,
47
+ zip: greenInvoiceClient.zip,
48
+ fax: greenInvoiceClient.fax,
49
+ mobile: greenInvoiceClient.mobile,
50
+ };
51
+ }
52
+
53
+ function convertLocalClientToGreenInvoiceCreateClientInput(
54
+ localClient: IGetClientsByIdsResult,
55
+ localBusiness: IGetBusinessesByIdsResult,
56
+ ): _DOLLAR_defs_addClientRequest_Input {
57
+ return {
58
+ /** Phone number */
59
+ phone: localBusiness.phone_number,
60
+ /** Mobile number */
61
+ // mobile: localBusiness.mobile_number,
62
+ /** Email addresses */
63
+ emails: localClient.emails,
64
+ /** Fax number */
65
+ // fax: localBusiness.fax_number,
66
+ /** Contact person name */
67
+ // contactPerson: localBusiness.contact_person,
68
+ /** Street address */
69
+ address: localBusiness.address,
70
+ /** City name */
71
+ // city: localBusiness.city,
72
+ /** Zip/postal code */
73
+ // zip: localBusiness.zip_code,
74
+ country: countryCodeToGreenInvoiceCountry(localBusiness.country as CountryCode),
75
+ /** Bank name */
76
+ // bankName: localBusiness.bank_name,
77
+ /** Bank branch number */
78
+ // bankBranch: localBusiness.bank_branch_number,
79
+ /** Bank account number */
80
+ // bankAccount: localBusiness.bank_account_number,
81
+ /** The client name */
82
+ name: localBusiness.name,
83
+ /** Is the client currently active or not */
84
+ active: localBusiness.is_active,
85
+ /** The client tax ID */
86
+ // taxId: localBusiness.tax_id
87
+ // paymentTerms: localClient.payment_terms,
88
+ // labels: localClient.labels,
89
+ /** Whether to send emails to the user automatically when assigning him to an invoice or not */
90
+ // send: localClient.send,
91
+ /** The client department */
92
+ // department: localClient.department,
93
+ /** The client accounting key */
94
+ accountingKey: localBusiness.sort_code?.toString(),
95
+ /** The category this client is related to */
96
+ // category: localClient.category,
97
+ /** The sub category this client is related to */
98
+ // subCategory: localClient.sub_category,
99
+ /** Client remarks for self use */
100
+ // remarks: localClient.remarks,
101
+ };
102
+ }
103
+
104
+ export async function addGreenInvoiceClient(clientId: string, injector: Injector): Promise<void> {
105
+ try {
106
+ // validate local client
107
+ const localBusinessPromise = injector
108
+ .get(BusinessesProvider)
109
+ .getBusinessByIdLoader.load(clientId);
110
+ const localClientPromise = injector.get(ClientsProvider).getClientByIdLoader.load(clientId);
111
+ const [localBusiness, localClient] = await Promise.all([
112
+ localBusinessPromise,
113
+ localClientPromise,
114
+ ]);
115
+ if (!localBusiness) {
116
+ throw new Error('Business not found');
117
+ }
118
+ if (!localClient) {
119
+ throw new Error('Client not found');
120
+ }
121
+
122
+ // validate details
123
+ const greenInvoiceCreatClientInput = convertLocalClientToGreenInvoiceCreateClientInput(
124
+ localClient,
125
+ localBusiness,
126
+ );
127
+
128
+ // add to green invoice
129
+ const greenInvoiceClient = await injector
130
+ .get(GreenInvoiceClientProvider)
131
+ .createClient(greenInvoiceCreatClientInput);
132
+
133
+ if (!greenInvoiceClient?.id) {
134
+ throw new Error('Failed to create Green Invoice client');
135
+ }
136
+
137
+ // add green invoice id to local client
138
+ await injector.get(ClientsProvider).updateClient({
139
+ businessId: clientId,
140
+ greenInvoiceId: greenInvoiceClient.id,
141
+ });
142
+ } catch (error) {
143
+ const message = 'Error adding Green Invoice client';
144
+ console.error(`${message}: ${error}`);
145
+ throw new Error(message);
146
+ }
147
+ }
148
+
149
+ function pickGreenInvoiceClientFields(
150
+ businessFields: Omit<UpdateBusinessInput, 'id'> & { name: string },
151
+ clientFields: ClientUpdateInput,
152
+ ): _DOLLAR_defs_updateClientRequest_Input {
153
+ const fieldsToUpdate: _DOLLAR_defs_updateClientRequest_Input = {
154
+ /** Phone number */
155
+ phone: businessFields.phoneNumber,
156
+ /** Mobile number */
157
+ // mobile: businessFields.mobile_number,
158
+ /** Email addresses */
159
+ emails: clientFields.emails ? [...clientFields.emails] : undefined,
160
+ /** Fax number */
161
+ // fax: businessFields.fax_number,
162
+ /** Contact person name */
163
+ // contactPerson: businessFields.contact_person,
164
+ /** Street address */
165
+ address: businessFields.address ?? undefined,
166
+ /** City name */
167
+ // city: businessFields.city,
168
+ /** Zip/postal code */
169
+ // zip: businessFields.zip_code,
170
+ country: businessFields.country
171
+ ? countryCodeToGreenInvoiceCountry(businessFields.country as CountryCode)
172
+ : undefined,
173
+ /** Bank name */
174
+ // bankName: businessFields.bank_name,
175
+ /** Bank branch number */
176
+ // bankBranch: businessFields.bank_branch_number,
177
+ /** Bank account number */
178
+ // bankAccount: businessFields.bank_account_number,
179
+ /** The client name */
180
+ name: businessFields.name ?? undefined,
181
+ /** Is the client currently active or not */
182
+ active: businessFields.isActive,
183
+ /** The client tax ID */
184
+ // taxId: businessFields.tax_id
185
+ // paymentTerms: clientFields.payment_terms,
186
+ // labels: clientFields.labels,
187
+ /** Whether to send emails to the user automatically when assigning him to an invoice or not */
188
+ // send: clientFields.send,
189
+ /** The client department */
190
+ // department: clientFields.department,
191
+ /** The client accounting key */
192
+ accountingKey: businessFields.sortCode?.toString(),
193
+ /** The category this client is related to */
194
+ // category: clientFields.category,
195
+ /** The sub category this client is related to */
196
+ // subCategory: clientFields.sub_category,
197
+ /** Client remarks for self use */
198
+ // remarks: clientFields.remarks,
199
+ };
200
+
201
+ return fieldsToUpdate;
202
+ }
203
+
204
+ export async function updateGreenInvoiceClient(
205
+ clientId: string,
206
+ injector: Injector,
207
+ businessFields: UpdateBusinessInput = {},
208
+ clientFields: ClientUpdateInput = {},
209
+ ): Promise<void> {
210
+ // validate local client
211
+ const localBusinessPromise = injector
212
+ .get(BusinessesProvider)
213
+ .getBusinessByIdLoader.load(clientId);
214
+ const localClientPromise = injector.get(ClientsProvider).getClientByIdLoader.load(clientId);
215
+ const [localBusiness, localClient] = await Promise.all([
216
+ localBusinessPromise,
217
+ localClientPromise,
218
+ ]);
219
+ if (!localBusiness?.name || !localClient) {
220
+ return;
221
+ }
222
+
223
+ const fieldsToUpdate = pickGreenInvoiceClientFields(
224
+ { ...businessFields, name: localBusiness.name },
225
+ clientFields,
226
+ );
227
+ if (Object.keys(fieldsToUpdate).length === 0) {
228
+ return;
229
+ }
230
+
231
+ const greenInvoiceClient = await injector.get(GreenInvoiceClientProvider).updateClient({
232
+ id: clientId,
233
+ input: fieldsToUpdate,
234
+ });
235
+
236
+ if (!greenInvoiceClient?.id) {
237
+ throw new Error('Failed to create Green Invoice client');
238
+ }
239
+
240
+ return;
241
+ }