@bisondesk/core-sdk 1.0.21 → 1.0.23-B

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 (225) hide show
  1. package/lib/apis/crm.d.ts +39 -0
  2. package/lib/apis/crm.d.ts.map +1 -0
  3. package/lib/apis/crm.js +201 -0
  4. package/lib/apis/crm.js.map +1 -0
  5. package/lib/apis/documents.d.ts +4 -0
  6. package/lib/apis/documents.d.ts.map +1 -0
  7. package/lib/apis/documents.js +47 -0
  8. package/lib/apis/documents.js.map +1 -0
  9. package/lib/apis/internet-vehicles.d.ts +3 -0
  10. package/lib/apis/internet-vehicles.d.ts.map +1 -0
  11. package/lib/apis/internet-vehicles.js +27 -0
  12. package/lib/apis/internet-vehicles.js.map +1 -0
  13. package/lib/apis/leasing-administration.d.ts +8 -0
  14. package/lib/apis/leasing-administration.d.ts.map +1 -0
  15. package/lib/apis/leasing-administration.js +49 -0
  16. package/lib/apis/leasing-administration.js.map +1 -0
  17. package/lib/apis/leasing.d.ts +19 -0
  18. package/lib/apis/leasing.d.ts.map +1 -0
  19. package/lib/apis/leasing.js +168 -0
  20. package/lib/apis/leasing.js.map +1 -0
  21. package/lib/apis/opportunities.d.ts +9 -0
  22. package/lib/apis/opportunities.d.ts.map +1 -0
  23. package/lib/apis/opportunities.js +94 -0
  24. package/lib/apis/opportunities.js.map +1 -0
  25. package/lib/apis/picklists.d.ts +5 -0
  26. package/lib/apis/picklists.d.ts.map +1 -0
  27. package/lib/apis/picklists.js +44 -0
  28. package/lib/apis/picklists.js.map +1 -0
  29. package/lib/apis/tenants.d.ts +7 -0
  30. package/lib/apis/tenants.d.ts.map +1 -0
  31. package/lib/apis/tenants.js +80 -0
  32. package/lib/apis/tenants.js.map +1 -0
  33. package/lib/apis/users.d.ts +3 -0
  34. package/lib/apis/users.d.ts.map +1 -0
  35. package/lib/apis/users.js +26 -0
  36. package/lib/apis/users.js.map +1 -0
  37. package/lib/apis/utils.d.ts +9 -0
  38. package/lib/apis/utils.d.ts.map +1 -0
  39. package/lib/apis/utils.js +23 -0
  40. package/lib/apis/utils.js.map +1 -0
  41. package/lib/apis/vehicles.d.ts +18 -0
  42. package/lib/apis/vehicles.d.ts.map +1 -0
  43. package/lib/apis/vehicles.js +159 -0
  44. package/lib/apis/vehicles.js.map +1 -0
  45. package/lib/constants.d.ts +77 -0
  46. package/lib/constants.d.ts.map +1 -1
  47. package/lib/constants.js +924 -1
  48. package/lib/constants.js.map +1 -1
  49. package/lib/ddb-entities.d.ts +2 -0
  50. package/lib/ddb-entities.d.ts.map +1 -0
  51. package/lib/ddb-entities.js +5 -0
  52. package/lib/ddb-entities.js.map +1 -0
  53. package/lib/types/activities.d.ts +38 -0
  54. package/lib/types/activities.d.ts.map +1 -0
  55. package/lib/types/activities.js +16 -0
  56. package/lib/types/activities.js.map +1 -0
  57. package/lib/types/crm.d.ts +103 -30
  58. package/lib/types/crm.d.ts.map +1 -1
  59. package/lib/types/crm.js +16 -0
  60. package/lib/types/crm.js.map +1 -1
  61. package/lib/types/definitions.d.ts +15 -13
  62. package/lib/types/definitions.d.ts.map +1 -1
  63. package/lib/types/documents.d.ts +105 -0
  64. package/lib/types/documents.d.ts.map +1 -0
  65. package/lib/types/{interests.js → documents.js} +1 -1
  66. package/lib/types/documents.js.map +1 -0
  67. package/lib/types/fields.d.ts +26 -25
  68. package/lib/types/fields.d.ts.map +1 -1
  69. package/lib/types/fields.js +4 -0
  70. package/lib/types/fields.js.map +1 -1
  71. package/lib/types/internet-vehicles.d.ts +64 -0
  72. package/lib/types/internet-vehicles.d.ts.map +1 -0
  73. package/lib/types/internet-vehicles.js +3 -0
  74. package/lib/types/internet-vehicles.js.map +1 -0
  75. package/lib/types/leads.d.ts +134 -0
  76. package/lib/types/leads.d.ts.map +1 -0
  77. package/lib/types/leads.js +3 -0
  78. package/lib/types/leads.js.map +1 -0
  79. package/lib/types/leasing-administration.d.ts +107 -0
  80. package/lib/types/leasing-administration.d.ts.map +1 -0
  81. package/lib/types/leasing-administration.js +3 -0
  82. package/lib/types/leasing-administration.js.map +1 -0
  83. package/lib/types/leasing-search.d.ts +22 -0
  84. package/lib/types/leasing-search.d.ts.map +1 -0
  85. package/lib/types/leasing-search.js +3 -0
  86. package/lib/types/leasing-search.js.map +1 -0
  87. package/lib/types/leasing-settings.d.ts +113 -0
  88. package/lib/types/leasing-settings.d.ts.map +1 -0
  89. package/lib/types/leasing-settings.js +10 -0
  90. package/lib/types/leasing-settings.js.map +1 -0
  91. package/lib/types/leasing.d.ts +322 -0
  92. package/lib/types/leasing.d.ts.map +1 -0
  93. package/lib/types/leasing.js +24 -0
  94. package/lib/types/leasing.js.map +1 -0
  95. package/lib/types/machines.d.ts +38 -0
  96. package/lib/types/machines.d.ts.map +1 -0
  97. package/lib/types/machines.js +3 -0
  98. package/lib/types/machines.js.map +1 -0
  99. package/lib/types/opportunities.d.ts +448 -0
  100. package/lib/types/opportunities.d.ts.map +1 -0
  101. package/lib/types/opportunities.js +68 -0
  102. package/lib/types/opportunities.js.map +1 -0
  103. package/lib/types/payments.d.ts +38 -0
  104. package/lib/types/payments.d.ts.map +1 -0
  105. package/lib/types/payments.js +9 -0
  106. package/lib/types/payments.js.map +1 -0
  107. package/lib/types/picklists.d.ts +15 -4
  108. package/lib/types/picklists.d.ts.map +1 -1
  109. package/lib/types/quotes.d.ts +173 -0
  110. package/lib/types/quotes.d.ts.map +1 -0
  111. package/lib/types/quotes.js +26 -0
  112. package/lib/types/quotes.js.map +1 -0
  113. package/lib/types/recently-viewed.d.ts +33 -0
  114. package/lib/types/recently-viewed.d.ts.map +1 -0
  115. package/lib/types/recently-viewed.js +3 -0
  116. package/lib/types/recently-viewed.js.map +1 -0
  117. package/lib/types/saved-filters.d.ts +20 -0
  118. package/lib/types/saved-filters.d.ts.map +1 -0
  119. package/lib/types/saved-filters.js +3 -0
  120. package/lib/types/saved-filters.js.map +1 -0
  121. package/lib/types/search.d.ts +26 -4
  122. package/lib/types/search.d.ts.map +1 -1
  123. package/lib/types/search.js.map +1 -1
  124. package/lib/types/settings.d.ts +50 -0
  125. package/lib/types/settings.d.ts.map +1 -0
  126. package/lib/types/settings.js +3 -0
  127. package/lib/types/settings.js.map +1 -0
  128. package/lib/types/storage.d.ts +1 -11
  129. package/lib/types/storage.d.ts.map +1 -1
  130. package/lib/types/storage.js +15 -0
  131. package/lib/types/storage.js.map +1 -1
  132. package/lib/types/templates.d.ts +35 -0
  133. package/lib/types/templates.d.ts.map +1 -0
  134. package/lib/types/templates.js +9 -0
  135. package/lib/types/templates.js.map +1 -0
  136. package/lib/types/tenants.d.ts +32 -9
  137. package/lib/types/tenants.d.ts.map +1 -1
  138. package/lib/types/tenants.js +11 -0
  139. package/lib/types/tenants.js.map +1 -1
  140. package/lib/types/users.d.ts +11 -3
  141. package/lib/types/users.d.ts.map +1 -1
  142. package/lib/types/utils.d.ts +20 -0
  143. package/lib/types/utils.d.ts.map +1 -1
  144. package/lib/types/utils.js +6 -0
  145. package/lib/types/utils.js.map +1 -1
  146. package/lib/types/vehicles.d.ts +406 -92
  147. package/lib/types/vehicles.d.ts.map +1 -1
  148. package/lib/types/vehicles.js +54 -1
  149. package/lib/types/vehicles.js.map +1 -1
  150. package/lib/types/website.d.ts +24 -0
  151. package/lib/types/website.d.ts.map +1 -0
  152. package/lib/types/website.js +3 -0
  153. package/lib/types/website.js.map +1 -0
  154. package/lib/utils/forms.d.ts +6 -0
  155. package/lib/utils/forms.d.ts.map +1 -0
  156. package/lib/utils/forms.js +33 -0
  157. package/lib/utils/forms.js.map +1 -0
  158. package/lib/utils/leasing.d.ts +3 -0
  159. package/lib/utils/leasing.d.ts.map +1 -0
  160. package/lib/utils/leasing.js +10 -0
  161. package/lib/utils/leasing.js.map +1 -0
  162. package/lib/utils/slug.d.ts +3 -0
  163. package/lib/utils/slug.d.ts.map +1 -0
  164. package/lib/utils/slug.js +23 -0
  165. package/lib/utils/slug.js.map +1 -0
  166. package/lib/utils/tenants.d.ts +4 -0
  167. package/lib/utils/tenants.d.ts.map +1 -0
  168. package/lib/utils/tenants.js +16 -0
  169. package/lib/utils/tenants.js.map +1 -0
  170. package/lib/utils/vehicles.d.ts +12 -0
  171. package/lib/utils/vehicles.d.ts.map +1 -0
  172. package/lib/utils/vehicles.js +73 -0
  173. package/lib/utils/vehicles.js.map +1 -0
  174. package/package.json +8 -6
  175. package/src/apis/crm.ts +302 -0
  176. package/src/apis/documents.ts +54 -0
  177. package/src/apis/internet-vehicles.ts +23 -0
  178. package/src/apis/leasing-administration.ts +70 -0
  179. package/src/apis/leasing.ts +257 -0
  180. package/src/apis/opportunities.ts +133 -0
  181. package/src/apis/picklists.ts +50 -0
  182. package/src/apis/tenants.ts +100 -0
  183. package/src/apis/users.ts +22 -0
  184. package/src/apis/utils.ts +23 -0
  185. package/src/apis/vehicles.ts +229 -0
  186. package/src/constants.ts +930 -1
  187. package/src/ddb-entities.ts +1 -0
  188. package/src/environment.d.ts +6 -0
  189. package/src/types/activities.ts +41 -0
  190. package/src/types/crm.ts +118 -30
  191. package/src/types/definitions.ts +19 -12
  192. package/src/types/documents.ts +110 -0
  193. package/src/types/fields.ts +72 -31
  194. package/src/types/internet-vehicles.ts +67 -0
  195. package/src/types/leads.ts +144 -0
  196. package/src/types/leasing-administration.ts +131 -0
  197. package/src/types/leasing-search.ts +24 -0
  198. package/src/types/leasing-settings.ts +111 -0
  199. package/src/types/leasing.ts +360 -0
  200. package/src/types/machines.ts +40 -0
  201. package/src/types/opportunities.ts +496 -0
  202. package/src/types/payments.ts +40 -0
  203. package/src/types/picklists.ts +26 -7
  204. package/src/types/quotes.ts +186 -0
  205. package/src/types/recently-viewed.ts +45 -0
  206. package/src/types/saved-filters.ts +21 -0
  207. package/src/types/search.ts +34 -4
  208. package/src/types/settings.ts +51 -0
  209. package/src/types/storage.ts +1 -12
  210. package/src/types/templates.ts +40 -0
  211. package/src/types/tenants.ts +37 -11
  212. package/src/types/users.ts +17 -3
  213. package/src/types/utils.ts +25 -0
  214. package/src/types/vehicles.ts +461 -93
  215. package/src/types/website.ts +28 -0
  216. package/src/utils/forms.ts +32 -0
  217. package/src/utils/leasing.ts +14 -0
  218. package/src/utils/slug.ts +19 -0
  219. package/src/utils/tenants.ts +14 -0
  220. package/src/utils/vehicles.ts +92 -0
  221. package/tsconfig.tsbuildinfo +1 -2995
  222. package/lib/types/interests.d.ts +0 -23
  223. package/lib/types/interests.d.ts.map +0 -1
  224. package/lib/types/interests.js.map +0 -1
  225. package/src/types/interests.ts +0 -20
@@ -0,0 +1,302 @@
1
+ import {
2
+ SERVICE_ID_ADMIN_HEADER,
3
+ TENANT_ID_ADMIN_HEADER,
4
+ } from '@bisondesk/commons-sdk/lib/constants';
5
+ import { XError } from '@bisondesk/commons-sdk/lib/errors';
6
+ import { PaginatedList } from '@bisondesk/commons-sdk/lib/types';
7
+ import type { Patch } from 'immer';
8
+ import fetch, { Response } from 'node-fetch';
9
+ import {
10
+ Contact,
11
+ NewContact,
12
+ NewOrganization,
13
+ Organization,
14
+ SearchContact,
15
+ SearchOrganization,
16
+ } from '../types/crm';
17
+ import { SearchRequest } from '../types/search';
18
+ import { ReferenceData } from '../types/utils';
19
+ import { cleanHeaders, getAdminAuth } from './utils';
20
+
21
+ export const getCrmOrganization = async (
22
+ tenantId: string,
23
+ organizationId: string,
24
+ opts?: { serviceId: string }
25
+ ): Promise<Organization | undefined> => {
26
+ const auth = await getAdminAuth();
27
+ const response: Response = await fetch(
28
+ `${process.env.CORE_API_ORIGIN}/api/crm/organizations/${organizationId}`,
29
+ {
30
+ headers: cleanHeaders({
31
+ Authorization: auth,
32
+ [TENANT_ID_ADMIN_HEADER]: tenantId,
33
+ [SERVICE_ID_ADMIN_HEADER]: opts?.serviceId,
34
+ }),
35
+ }
36
+ );
37
+
38
+ if (response.ok) {
39
+ return response.status === 200 ? (response.json() as any) : undefined;
40
+ }
41
+
42
+ const body = await response.text();
43
+ throw new XError(response.statusText, { body, tenantId, organizationId, opts });
44
+ };
45
+
46
+ export const getCrmOrganizationByExternalId = async (
47
+ tenantId: string,
48
+ externalId: string,
49
+ opts?: { serviceId: string }
50
+ ): Promise<Organization | undefined> => {
51
+ const auth = await getAdminAuth();
52
+ const response: Response = await fetch(
53
+ `${process.env.CORE_API_ORIGIN}/api/crm/organizations/external-id/${externalId}`,
54
+ {
55
+ headers: cleanHeaders({
56
+ Authorization: auth,
57
+ [TENANT_ID_ADMIN_HEADER]: tenantId,
58
+ [SERVICE_ID_ADMIN_HEADER]: opts?.serviceId,
59
+ }),
60
+ }
61
+ );
62
+
63
+ if (response.ok) {
64
+ return response.status === 200 ? (response.json() as any) : undefined;
65
+ }
66
+
67
+ const body = await response.text();
68
+ throw new XError(response.statusText, { body, tenantId, externalId, opts });
69
+ };
70
+
71
+ export const saveCrmOrganization = async (
72
+ tenantId: string,
73
+ org: Organization | NewOrganization,
74
+ opts?: { serviceId: string }
75
+ ): Promise<Organization> => {
76
+ const auth = await getAdminAuth();
77
+ const response: Response = await fetch(`${process.env.CORE_API_ORIGIN}/api/crm/organizations`, {
78
+ method: 'POST',
79
+ headers: cleanHeaders({
80
+ Authorization: auth,
81
+ 'Content-Type': 'application/json',
82
+ [TENANT_ID_ADMIN_HEADER]: tenantId,
83
+ [SERVICE_ID_ADMIN_HEADER]: opts?.serviceId,
84
+ }),
85
+ body: JSON.stringify(org),
86
+ });
87
+
88
+ if (response.ok) {
89
+ return response.json() as any;
90
+ }
91
+
92
+ const body = await response.text();
93
+ throw new XError(response.statusText, { body, tenantId, org, opts });
94
+ };
95
+
96
+ export const patchCrmOrganization = async (
97
+ tenantId: string,
98
+ organizationId: string,
99
+ changes: Patch[],
100
+ opts?: { serviceId: string }
101
+ ): Promise<Organization> => {
102
+ const auth = await getAdminAuth();
103
+ const response: Response = await fetch(
104
+ `${process.env.CORE_API_ORIGIN}/api/crm/organizations/${organizationId}`,
105
+ {
106
+ method: 'PATCH',
107
+ headers: cleanHeaders({
108
+ Authorization: auth,
109
+ 'Content-Type': 'application/json',
110
+ [TENANT_ID_ADMIN_HEADER]: tenantId,
111
+ [SERVICE_ID_ADMIN_HEADER]: opts?.serviceId,
112
+ }),
113
+ body: JSON.stringify(changes),
114
+ }
115
+ );
116
+
117
+ if (response.ok) {
118
+ return response.json() as Promise<Organization>;
119
+ }
120
+
121
+ const body = await response.text();
122
+ throw new XError(response.statusText, { tenantId, organizationId, changes, body, opts });
123
+ };
124
+
125
+ export const searchCrmOrganizations = async (
126
+ tenantId: string,
127
+ req: SearchRequest,
128
+ opts?: { serviceId: string }
129
+ ): Promise<PaginatedList<SearchOrganization, ReferenceData>> => {
130
+ const auth = await getAdminAuth();
131
+ const response: Response = await fetch(
132
+ `${process.env.CORE_API_ORIGIN}/api/crm/organizations/search/results`,
133
+ {
134
+ method: 'POST',
135
+ headers: cleanHeaders({
136
+ Authorization: auth,
137
+ [TENANT_ID_ADMIN_HEADER]: tenantId,
138
+ [SERVICE_ID_ADMIN_HEADER]: opts?.serviceId,
139
+ }),
140
+ body: JSON.stringify(req),
141
+ }
142
+ );
143
+
144
+ if (response.ok) {
145
+ return response.status === 200 ? (response.json() as any) : undefined;
146
+ }
147
+
148
+ const body = await response.text();
149
+ throw new XError(response.statusText, { body, tenantId, req, opts });
150
+ };
151
+
152
+ export const searchCrmContacts = async (
153
+ tenantId: string,
154
+ req: SearchRequest,
155
+ opts?: { serviceId: string }
156
+ ): Promise<PaginatedList<SearchContact, ReferenceData>> => {
157
+ const auth = await getAdminAuth();
158
+ const response: Response = await fetch(
159
+ `${process.env.CORE_API_ORIGIN}/api/crm/contacts/search/results`,
160
+ {
161
+ method: 'POST',
162
+ headers: cleanHeaders({
163
+ Authorization: auth,
164
+ 'Content-Type': 'application/json',
165
+ [TENANT_ID_ADMIN_HEADER]: tenantId,
166
+ [SERVICE_ID_ADMIN_HEADER]: opts?.serviceId,
167
+ }),
168
+ body: JSON.stringify(req),
169
+ }
170
+ );
171
+
172
+ if (response.ok) {
173
+ return response.status === 200 ? (response.json() as any) : undefined;
174
+ }
175
+
176
+ const body = await response.text();
177
+ throw new XError(response.statusText, { body, tenantId, req, opts });
178
+ };
179
+
180
+ export const deleteCrmOrganization = async (
181
+ tenantId: string,
182
+ orgId: string,
183
+ opts?: { serviceId: string }
184
+ ): Promise<void> => {
185
+ const auth = await getAdminAuth();
186
+ const response: Response = await fetch(
187
+ `${process.env.CORE_API_ORIGIN}/api/crm/organizations/${orgId}`,
188
+ {
189
+ method: 'DELETE',
190
+ headers: cleanHeaders({
191
+ Authorization: auth,
192
+ [TENANT_ID_ADMIN_HEADER]: tenantId,
193
+ [SERVICE_ID_ADMIN_HEADER]: opts?.serviceId,
194
+ }),
195
+ }
196
+ );
197
+
198
+ if (!response.ok) {
199
+ const body = await response.text();
200
+ throw new XError(response.statusText, { body, tenantId, orgId, opts });
201
+ }
202
+ };
203
+
204
+ export const listCrmContacts = async (
205
+ tenantId: string,
206
+ organizationId: string,
207
+ opts?: { serviceId: string }
208
+ ): Promise<Contact[]> => {
209
+ const auth = await getAdminAuth();
210
+ const response: Response = await fetch(
211
+ `${process.env.CORE_API_ORIGIN}/admin/crm/organizations/${organizationId}/contacts`,
212
+ {
213
+ headers: cleanHeaders({
214
+ Authorization: auth,
215
+ [TENANT_ID_ADMIN_HEADER]: tenantId,
216
+ [SERVICE_ID_ADMIN_HEADER]: opts?.serviceId,
217
+ }),
218
+ }
219
+ );
220
+
221
+ if (response.ok) {
222
+ return response.status === 200 ? (response.json() as any) : undefined;
223
+ }
224
+
225
+ const body = await response.text();
226
+ throw new XError(response.statusText, { body, tenantId, organizationId, opts });
227
+ };
228
+
229
+ export const getCrmContact = async (
230
+ tenantId: string,
231
+ contactId: string,
232
+ opts?: { serviceId: string }
233
+ ): Promise<Contact | undefined> => {
234
+ const auth = await getAdminAuth();
235
+ const response: Response = await fetch(
236
+ `${process.env.CORE_API_ORIGIN}/api/crm/contacts/${contactId}`,
237
+ {
238
+ headers: cleanHeaders({
239
+ Authorization: auth,
240
+ [TENANT_ID_ADMIN_HEADER]: tenantId,
241
+ [SERVICE_ID_ADMIN_HEADER]: opts?.serviceId,
242
+ }),
243
+ }
244
+ );
245
+
246
+ if (response.ok) {
247
+ return response.status === 200 ? (response.json() as any) : undefined;
248
+ }
249
+
250
+ const body = await response.text();
251
+ throw new XError(response.statusText, { body, tenantId, contactId, opts });
252
+ };
253
+
254
+ export const saveCrmContact = async (
255
+ tenantId: string,
256
+ contact: Contact | NewContact,
257
+ opts?: { serviceId: string }
258
+ ): Promise<Contact> => {
259
+ const auth = await getAdminAuth();
260
+ const response: Response = await fetch(`${process.env.CORE_API_ORIGIN}/api/crm/contacts`, {
261
+ method: 'POST',
262
+ headers: cleanHeaders({
263
+ Authorization: auth,
264
+ 'Content-Type': 'application/json',
265
+ [TENANT_ID_ADMIN_HEADER]: tenantId,
266
+ [SERVICE_ID_ADMIN_HEADER]: opts?.serviceId,
267
+ }),
268
+ body: JSON.stringify(contact),
269
+ });
270
+
271
+ if (response.ok) {
272
+ return response.json() as any;
273
+ }
274
+
275
+ const body = await response.text();
276
+ throw new XError(response.statusText, { body, contact, tenantId, opts });
277
+ };
278
+
279
+ export const deleteCrmContact = async (
280
+ tenantId: string,
281
+ contactId: string,
282
+ orgId: string = '',
283
+ opts?: { serviceId: string }
284
+ ): Promise<void> => {
285
+ const auth = await getAdminAuth();
286
+ const response: Response = await fetch(
287
+ `${process.env.CORE_API_ORIGIN}/api/crm/contacts/${contactId}?orgId=${orgId}`,
288
+ {
289
+ method: 'DELETE',
290
+ headers: cleanHeaders({
291
+ Authorization: auth,
292
+ [TENANT_ID_ADMIN_HEADER]: tenantId,
293
+ [SERVICE_ID_ADMIN_HEADER]: opts?.serviceId,
294
+ }),
295
+ }
296
+ );
297
+
298
+ if (!response.ok) {
299
+ const body = await response.text();
300
+ throw new XError(response.statusText, { body, tenantId, contactId, orgId, opts });
301
+ }
302
+ };
@@ -0,0 +1,54 @@
1
+ import { TENANT_ID_ADMIN_HEADER } from '@bisondesk/commons-sdk/lib/constants';
2
+ import { XError } from '@bisondesk/commons-sdk/lib/errors';
3
+ import fetch, { Response } from 'node-fetch';
4
+ import { FinanceBooking, FinanceDocument } from '../types/documents';
5
+ import { getAdminAuth } from './utils';
6
+
7
+ export const upsertFinanceDoc = async (
8
+ tenantId: string,
9
+ doc: FinanceDocument
10
+ ): Promise<FinanceDocument> => {
11
+ const auth = await getAdminAuth();
12
+ const response: Response = await fetch(`${process.env.CORE_API_ORIGIN}/api/documents/finance`, {
13
+ method: 'POST',
14
+ headers: {
15
+ 'Content-Type': 'application/json',
16
+ Authorization: auth,
17
+ [TENANT_ID_ADMIN_HEADER]: tenantId,
18
+ },
19
+ body: JSON.stringify(doc),
20
+ });
21
+
22
+ if (response.status === 200) {
23
+ return response.json() as any;
24
+ }
25
+
26
+ const body = await response.text();
27
+ throw new XError(response.statusText, { tenantId, body });
28
+ };
29
+
30
+ export const upsertFinanceBooking = async (
31
+ tenantId: string,
32
+ booking: FinanceBooking
33
+ ): Promise<FinanceBooking> => {
34
+ const auth = await getAdminAuth();
35
+ const response: Response = await fetch(
36
+ `${process.env.CORE_API_ORIGIN}/api/documents/finance/booking`,
37
+ {
38
+ method: 'POST',
39
+ headers: {
40
+ 'Content-Type': 'application/json',
41
+ Authorization: auth,
42
+ [TENANT_ID_ADMIN_HEADER]: tenantId,
43
+ },
44
+ body: JSON.stringify(booking),
45
+ }
46
+ );
47
+
48
+ if (response.status === 200) {
49
+ return response.json() as any;
50
+ }
51
+
52
+ const body = await response.text();
53
+ throw new XError(response.statusText, { tenantId, body });
54
+ };
@@ -0,0 +1,23 @@
1
+ import { XError } from '@bisondesk/commons-sdk/lib/errors';
2
+ import fetch, { Response } from 'node-fetch';
3
+ import { SearchInternetVehicle } from '../types/internet-vehicles';
4
+ import { getAdminAuth } from './utils';
5
+
6
+ export const upsertInternetVehicles = async (vehicles: SearchInternetVehicle[]): Promise<void> => {
7
+ const auth = await getAdminAuth();
8
+ const response: Response = await fetch(`${process.env.CORE_API_ORIGIN}/api/internet-vehicles`, {
9
+ method: 'POST',
10
+ headers: {
11
+ 'Content-Type': 'application/json',
12
+ Authorization: auth,
13
+ },
14
+ body: JSON.stringify(vehicles),
15
+ });
16
+
17
+ if (response.ok) {
18
+ return;
19
+ }
20
+
21
+ const body = await response.text();
22
+ throw new XError(response.statusText, { body });
23
+ };
@@ -0,0 +1,70 @@
1
+ import {
2
+ SERVICE_ID_ADMIN_HEADER,
3
+ TENANT_ID_ADMIN_HEADER,
4
+ } from '@bisondesk/commons-sdk/lib/constants';
5
+ import { XError } from '@bisondesk/commons-sdk/lib/errors';
6
+ import fetch, { Response } from 'node-fetch';
7
+ import {
8
+ LeasingAdminUpload,
9
+ LeasingAdminUploadStatus,
10
+ NewLeasingAdminUploadRequest,
11
+ } from '../types/leasing-administration';
12
+ import { cleanHeaders, getAdminAuth } from './utils';
13
+
14
+ export const createLeasingAdminUpload = async (
15
+ tenantId: string,
16
+ data: NewLeasingAdminUploadRequest,
17
+ opts?: { serviceId: string }
18
+ ): Promise<LeasingAdminUpload> => {
19
+ const auth = await getAdminAuth();
20
+ const response: Response = await fetch(
21
+ `${process.env.CORE_API_ORIGIN}/api/leasing/administration/uploads`,
22
+ {
23
+ method: 'POST',
24
+ headers: cleanHeaders({
25
+ Authorization: auth,
26
+ 'Content-Type': 'application/json',
27
+ [TENANT_ID_ADMIN_HEADER]: tenantId,
28
+ [SERVICE_ID_ADMIN_HEADER]: opts?.serviceId,
29
+ }),
30
+ body: JSON.stringify(data),
31
+ }
32
+ );
33
+
34
+ if (response.ok) {
35
+ return response.json() as any;
36
+ }
37
+
38
+ const body = await response.text();
39
+ throw new XError(response.statusText, { body, data, opts });
40
+ };
41
+
42
+ export const updateLeasingAdminUploadStatus = async (
43
+ tenantId: string,
44
+ handlerId: string,
45
+ uploadId: string,
46
+ newStatus: LeasingAdminUploadStatus,
47
+ opts?: { serviceId: string }
48
+ ): Promise<LeasingAdminUpload> => {
49
+ const auth = await getAdminAuth();
50
+ const response: Response = await fetch(
51
+ `${process.env.CORE_API_ORIGIN}/api/leasing/administration/uploads/${handlerId}/${uploadId}/status`,
52
+ {
53
+ method: 'PUT',
54
+ headers: cleanHeaders({
55
+ Authorization: auth,
56
+ 'Content-Type': 'application/json',
57
+ [TENANT_ID_ADMIN_HEADER]: tenantId,
58
+ [SERVICE_ID_ADMIN_HEADER]: opts?.serviceId,
59
+ }),
60
+ body: JSON.stringify({ status: newStatus }),
61
+ }
62
+ );
63
+
64
+ if (response.ok) {
65
+ return response.json() as any;
66
+ }
67
+
68
+ const body = await response.text();
69
+ throw new XError(response.statusText, { body, tenantId, handlerId, uploadId, opts });
70
+ };
@@ -0,0 +1,257 @@
1
+ import {
2
+ SERVICE_ID_ADMIN_HEADER,
3
+ TENANT_ID_ADMIN_HEADER,
4
+ } from '@bisondesk/commons-sdk/lib/constants';
5
+ import { XError } from '@bisondesk/commons-sdk/lib/errors';
6
+ import fetch, { Response } from 'node-fetch';
7
+ import {
8
+ AmortizationDocs,
9
+ AmortizationTable,
10
+ LeasingConditions,
11
+ LeasingContract,
12
+ NewLeasingConditions,
13
+ } from '../types/leasing';
14
+ import { cleanHeaders, getAdminAuth } from './utils';
15
+
16
+ export const listLeasingClients = async (
17
+ tenantId: string,
18
+ offset: number,
19
+ limit: number,
20
+ opts?: { serviceId: string }
21
+ ): Promise<string[]> => {
22
+ const auth = await getAdminAuth();
23
+ const response: Response = await fetch(
24
+ `${process.env.CORE_API_ORIGIN}/api/leasing/contracts/clients?offset=${offset}&limit=${limit}`,
25
+ {
26
+ headers: cleanHeaders({
27
+ Authorization: auth,
28
+ [TENANT_ID_ADMIN_HEADER]: tenantId,
29
+ [SERVICE_ID_ADMIN_HEADER]: opts?.serviceId,
30
+ }),
31
+ }
32
+ );
33
+
34
+ if (response.ok) {
35
+ return response.status === 200 ? (response.json() as any) : undefined;
36
+ }
37
+
38
+ const body = await response.text();
39
+ throw new XError(response.statusText, { tenantId, offset, limit });
40
+ };
41
+
42
+ export const getLeasingConditions = async (
43
+ tenantId: string,
44
+ conditionsId: string
45
+ ): Promise<LeasingConditions | undefined> => {
46
+ const auth = await getAdminAuth();
47
+ const response: Response = await fetch(
48
+ `${process.env.CORE_API_ORIGIN}/api/leasing/conditions/${conditionsId}`,
49
+ {
50
+ headers: {
51
+ Authorization: auth,
52
+ [TENANT_ID_ADMIN_HEADER]: tenantId,
53
+ },
54
+ }
55
+ );
56
+
57
+ if (response.ok) {
58
+ return response.status === 200 ? (response.json() as any) : undefined;
59
+ }
60
+
61
+ const body = await response.text();
62
+ throw new XError(response.statusText, { tenantId, conditionsId, body });
63
+ };
64
+
65
+ export const createLeasingConditions = async (
66
+ tenantId: string,
67
+ conditions: NewLeasingConditions
68
+ ): Promise<LeasingConditions> => {
69
+ const auth = await getAdminAuth();
70
+ const response: Response = await fetch(`${process.env.CORE_API_ORIGIN}/api/leasing/conditions`, {
71
+ method: 'POST',
72
+ headers: {
73
+ 'Content-Type': 'application/json',
74
+ Authorization: auth,
75
+ [TENANT_ID_ADMIN_HEADER]: tenantId,
76
+ },
77
+ body: JSON.stringify(conditions),
78
+ });
79
+
80
+ if (response.status === 200) {
81
+ return response.json() as any;
82
+ }
83
+
84
+ const body = await response.text();
85
+ throw new XError(response.statusText, { tenantId, conditions, body });
86
+ };
87
+
88
+ export const getLeasingContractByNumber = async (
89
+ tenantId: string,
90
+ contractNumber: string
91
+ ): Promise<LeasingContract | undefined> => {
92
+ const auth = await getAdminAuth();
93
+ const response: Response = await fetch(
94
+ `${process.env.CORE_API_ORIGIN}/api/leasing/contracts/number/${contractNumber}`,
95
+ {
96
+ headers: {
97
+ Authorization: auth,
98
+ [TENANT_ID_ADMIN_HEADER]: tenantId,
99
+ },
100
+ }
101
+ );
102
+
103
+ if (response.ok) {
104
+ return response.status === 200 ? (response.json() as any) : undefined;
105
+ }
106
+
107
+ const body = await response.text();
108
+ throw new XError(response.statusText, { tenantId, contractNumber, body });
109
+ };
110
+
111
+ export const getLeasingContract = async (
112
+ tenantId: string,
113
+ contractId: string
114
+ ): Promise<LeasingContract | undefined> => {
115
+ const auth = await getAdminAuth();
116
+ const response: Response = await fetch(
117
+ `${process.env.CORE_API_ORIGIN}/api/leasing/contracts/${contractId}`,
118
+ {
119
+ headers: {
120
+ Authorization: auth,
121
+ [TENANT_ID_ADMIN_HEADER]: tenantId,
122
+ },
123
+ }
124
+ );
125
+
126
+ if (response.ok) {
127
+ return response.status === 200 ? (response.json() as any) : undefined;
128
+ }
129
+
130
+ const body = await response.text();
131
+ throw new XError(response.statusText, { tenantId, contractId, body });
132
+ };
133
+
134
+ export const findLeasingContracts = async (
135
+ tenantId: string,
136
+ clientId: string,
137
+ vehicleId: string
138
+ ): Promise<LeasingContract[]> => {
139
+ const auth = await getAdminAuth();
140
+ const response: Response = await fetch(
141
+ `${process.env.CORE_API_ORIGIN}/api/leasing/contracts/find?clientId=${clientId}&vehicleId=${vehicleId}`,
142
+ {
143
+ headers: {
144
+ Authorization: auth,
145
+ [TENANT_ID_ADMIN_HEADER]: tenantId,
146
+ },
147
+ }
148
+ );
149
+
150
+ if (response.status === 200) {
151
+ return response.json() as any;
152
+ }
153
+
154
+ const body = await response.text();
155
+ throw new XError(response.statusText, { tenantId, clientId, vehicleId, body });
156
+ };
157
+
158
+ export const upsertLeasingContract = async (
159
+ tenantId: string,
160
+ contract: LeasingContract
161
+ ): Promise<LeasingContract> => {
162
+ const auth = await getAdminAuth();
163
+ const response: Response = await fetch(`${process.env.CORE_API_ORIGIN}/api/leasing/contracts`, {
164
+ method: 'POST',
165
+ headers: {
166
+ 'Content-Type': 'application/json',
167
+ Authorization: auth,
168
+ [TENANT_ID_ADMIN_HEADER]: tenantId,
169
+ },
170
+ body: JSON.stringify(contract),
171
+ });
172
+
173
+ if (response.status === 200) {
174
+ return response.json() as any;
175
+ }
176
+
177
+ const body = await response.text();
178
+ throw new XError(response.statusText, { tenantId, contract, body });
179
+ };
180
+
181
+ export const computeFinanceDocLeasingAmounts = async (
182
+ tenantId: string,
183
+ contractId: string,
184
+ documentId: string
185
+ ): Promise<
186
+ | {
187
+ totalAmountExcl: string;
188
+ totalCapital: string;
189
+ totalInterest: string;
190
+ totalOthers: string;
191
+ }
192
+ | undefined
193
+ > => {
194
+ const auth = await getAdminAuth();
195
+ const response: Response = await fetch(
196
+ `${process.env.CORE_API_ORIGIN}/api/leasing/doc/${documentId}/amounts`,
197
+ {
198
+ headers: {
199
+ Authorization: auth,
200
+ [TENANT_ID_ADMIN_HEADER]: tenantId,
201
+ },
202
+ }
203
+ );
204
+
205
+ if (response.status === 200) {
206
+ return response.json() as any;
207
+ }
208
+
209
+ const body = await response.text();
210
+ throw new XError(response.statusText, { tenantId, contractId, body });
211
+ };
212
+
213
+ export const getLeasingAmortizations = async (
214
+ tenantId: string,
215
+ contractId: string
216
+ ): Promise<AmortizationTable | undefined> => {
217
+ const auth = await getAdminAuth();
218
+ const response: Response = await fetch(
219
+ `${process.env.CORE_API_ORIGIN}/api/leasing/contracts/${contractId}/amortization`,
220
+ {
221
+ headers: {
222
+ Authorization: auth,
223
+ [TENANT_ID_ADMIN_HEADER]: tenantId,
224
+ },
225
+ }
226
+ );
227
+
228
+ if (response.status === 200) {
229
+ return response.json() as any;
230
+ }
231
+
232
+ const body = await response.text();
233
+ throw new XError(response.statusText, { tenantId, contractId, body });
234
+ };
235
+
236
+ export const listAmortizationDocsByContract = async (
237
+ tenantId: string,
238
+ contractId: string
239
+ ): Promise<AmortizationDocs[]> => {
240
+ const auth = await getAdminAuth();
241
+ const response: Response = await fetch(
242
+ `${process.env.CORE_API_ORIGIN}/api/leasing/contracts/${contractId}/amortization/docs`,
243
+ {
244
+ headers: {
245
+ Authorization: auth,
246
+ [TENANT_ID_ADMIN_HEADER]: tenantId,
247
+ },
248
+ }
249
+ );
250
+
251
+ if (response.status === 200) {
252
+ return response.json() as any;
253
+ }
254
+
255
+ const body = await response.text();
256
+ throw new XError(response.statusText, { tenantId, contractId, body });
257
+ };