@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,133 @@
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 { AmortizationTable, AmortizationTableRequest } from '../types/leasing';
5
+ import { LeasingQuoteParams } from '../types/leasing-settings';
6
+ import {
7
+ NewOpportunity,
8
+ Opportunity,
9
+ OpportunityBFF,
10
+ OpportunityUpdate,
11
+ } from '../types/opportunities';
12
+ import { cleanHeaders, getAdminAuth } from './utils';
13
+
14
+ export const getOpportunityBff = async (
15
+ tenantId: string,
16
+ opportunityId: string
17
+ ): Promise<OpportunityBFF | undefined> => {
18
+ const auth = await getAdminAuth();
19
+ const response: Response = await fetch(
20
+ `${process.env.CORE_API_ORIGIN}/api/opportunities/${opportunityId}/bff`,
21
+ {
22
+ headers: cleanHeaders({
23
+ Authorization: auth,
24
+ [TENANT_ID_ADMIN_HEADER]: tenantId,
25
+ }),
26
+ }
27
+ );
28
+
29
+ if (response.ok) {
30
+ return response.status === 200 ? (response.json() as any) : undefined;
31
+ }
32
+
33
+ const body = await response.text();
34
+ throw new XError(response.statusText, { body, tenantId, opportunityId });
35
+ };
36
+
37
+ export const updateOpportunity = async (
38
+ tenantId: string,
39
+ opportunity: OpportunityUpdate,
40
+ opportunityId: string
41
+ ): Promise<OpportunityBFF | undefined> => {
42
+ const auth = await getAdminAuth();
43
+ const response: Response = await fetch(
44
+ `${process.env.CORE_API_ORIGIN}/api/opportunities/${opportunityId}`,
45
+ {
46
+ method: 'PATCH',
47
+ headers: cleanHeaders({
48
+ Authorization: auth,
49
+ [TENANT_ID_ADMIN_HEADER]: tenantId,
50
+ }),
51
+ body: JSON.stringify(opportunity),
52
+ }
53
+ );
54
+
55
+ if (response.ok) {
56
+ return response.status === 200 ? (response.json() as any) : undefined;
57
+ }
58
+
59
+ const body = await response.text();
60
+ throw new XError(response.statusText, { body, tenantId, opportunityId });
61
+ };
62
+
63
+ export const createOpportunity = async (
64
+ tenantId: string,
65
+ opportunity: NewOpportunity
66
+ ): Promise<Opportunity> => {
67
+ const auth = await getAdminAuth();
68
+ const response: Response = await fetch(`${process.env.CORE_API_ORIGIN}/api/opportunities`, {
69
+ method: 'POST',
70
+ headers: cleanHeaders({
71
+ Authorization: auth,
72
+ [TENANT_ID_ADMIN_HEADER]: tenantId,
73
+ 'Content-Type': 'application/json',
74
+ }),
75
+ body: JSON.stringify(opportunity),
76
+ });
77
+
78
+ if (response.ok) {
79
+ return response.status === 200 ? (response.json() as any) : undefined;
80
+ }
81
+
82
+ const body = await response.text();
83
+ throw new XError(response.statusText, { body, tenantId, opportunity });
84
+ };
85
+
86
+ export const getLeasingQuoteParams = async (
87
+ tenantId: string,
88
+ settingId: string
89
+ ): Promise<LeasingQuoteParams | undefined> => {
90
+ const auth = await getAdminAuth();
91
+ const response: Response = await fetch(
92
+ `${process.env.CORE_API_ORIGIN}/api/leasing/administration/settings/quote-params/${settingId}`,
93
+ {
94
+ headers: cleanHeaders({
95
+ Authorization: auth,
96
+ [TENANT_ID_ADMIN_HEADER]: tenantId,
97
+ }),
98
+ }
99
+ );
100
+
101
+ if (response.ok) {
102
+ return response.status === 200 ? (response.json() as any) : undefined;
103
+ }
104
+
105
+ const body = await response.text();
106
+ throw new XError(response.statusText, { body, tenantId, settingId });
107
+ };
108
+
109
+ export const computeAmortizationTable = async (
110
+ tenantId: string,
111
+ input: AmortizationTableRequest
112
+ ): Promise<AmortizationTable | undefined> => {
113
+ const auth = await getAdminAuth();
114
+ const response: Response = await fetch(
115
+ `${process.env.CORE_API_ORIGIN}/api/leasing/amortization-table`,
116
+ {
117
+ method: 'POST',
118
+ headers: cleanHeaders({
119
+ Authorization: auth,
120
+ 'Content-Type': 'application/json',
121
+ [TENANT_ID_ADMIN_HEADER]: tenantId,
122
+ }),
123
+ body: JSON.stringify(input),
124
+ }
125
+ );
126
+
127
+ if (response.ok) {
128
+ return response.status === 200 ? response.json() : undefined;
129
+ }
130
+
131
+ const body = await response.text();
132
+ throw new XError(response.statusText, { body, tenantId, input });
133
+ };
@@ -0,0 +1,50 @@
1
+ import { XError } from '@bisondesk/commons-sdk/lib/errors';
2
+ import { Option } from '@bisondesk/commons-sdk/lib/types';
3
+ import fetch, { Response } from 'node-fetch';
4
+ import {
5
+ AdHocPicklistRequest,
6
+ AdHocPicklistValueDisplay,
7
+ PicklistOptionsRequest,
8
+ } from '../types/picklists';
9
+ import { cleanHeaders, getAdminAuth } from './utils';
10
+
11
+ export const getMultiplePicklistValues = async (
12
+ request: AdHocPicklistRequest
13
+ ): Promise<AdHocPicklistValueDisplay> => {
14
+ const auth = await getAdminAuth();
15
+ const response: Response = await fetch(`${process.env.CORE_API_ORIGIN}/api/picklists/bulk/read`, {
16
+ method: 'POST',
17
+ headers: {
18
+ Authorization: auth,
19
+ 'Content-Type': 'application/json',
20
+ },
21
+ body: JSON.stringify(request),
22
+ });
23
+
24
+ if (response.ok) {
25
+ return response.json() as any;
26
+ }
27
+
28
+ const body = await response.text();
29
+ throw new XError(response.statusText, { body });
30
+ };
31
+
32
+ export const getPicklistOptions = async (req: PicklistOptionsRequest): Promise<Option[]> => {
33
+ const auth = await getAdminAuth();
34
+
35
+ const response: Response = await fetch(`${process.env.CORE_API_ORIGIN}/api/picklists/options`, {
36
+ method: 'POST',
37
+ headers: cleanHeaders({
38
+ Authorization: auth,
39
+ 'Content-Type': 'application/json',
40
+ }),
41
+ body: JSON.stringify(req),
42
+ });
43
+
44
+ if (response.ok) {
45
+ return response.json() as any;
46
+ }
47
+
48
+ const body = await response.text();
49
+ throw new XError(response.statusText, { body, req });
50
+ };
@@ -0,0 +1,100 @@
1
+ import { XError } from '@bisondesk/commons-sdk/lib/errors';
2
+ import fetch, { Response } from 'node-fetch';
3
+ import { Tenant, TenantSyncSettings } from '../types/tenants';
4
+ import { getAdminAuth } from './utils';
5
+
6
+ export const listTenants = async (): Promise<Tenant[]> => {
7
+ const auth = await getAdminAuth();
8
+ const response: Response = await fetch(`${process.env.CORE_API_ORIGIN}/admin/tenants`, {
9
+ headers: {
10
+ Authorization: auth,
11
+ },
12
+ });
13
+
14
+ if (response.ok) {
15
+ return response.json() as any;
16
+ }
17
+
18
+ const body = await response.text();
19
+ throw new XError(response.statusText, { body });
20
+ };
21
+
22
+ export const listTenantsSyncSettings = async (): Promise<TenantSyncSettings[]> => {
23
+ const auth = await getAdminAuth();
24
+ const response: Response = await fetch(
25
+ `${process.env.CORE_API_ORIGIN}/admin/tenants/settings/sync`,
26
+ {
27
+ headers: {
28
+ Authorization: auth,
29
+ },
30
+ }
31
+ );
32
+
33
+ if (response.ok) {
34
+ return response.json() as any;
35
+ }
36
+
37
+ const body = await response.text();
38
+ throw new XError(response.statusText, { body });
39
+ };
40
+
41
+ export const getTenant = async (tenantId: string): Promise<Tenant | undefined> => {
42
+ const auth = await getAdminAuth();
43
+ const response: Response = await fetch(
44
+ `${process.env.CORE_API_ORIGIN}/admin/tenants/${tenantId}`,
45
+ {
46
+ headers: {
47
+ Authorization: auth,
48
+ },
49
+ }
50
+ );
51
+
52
+ if (response.ok) {
53
+ return response.status === 200 ? (response.json() as any) : undefined;
54
+ }
55
+
56
+ const body = await response.text();
57
+ throw new XError(response.statusText, { body });
58
+ };
59
+
60
+ export const getTenantByHyperdmsId = async (
61
+ hyperdmsTenantId: string
62
+ ): Promise<Tenant | undefined> => {
63
+ const auth = await getAdminAuth();
64
+ const response: Response = await fetch(
65
+ `${process.env.CORE_API_ORIGIN}/admin/tenants/hyperdms/${hyperdmsTenantId}`,
66
+ {
67
+ headers: {
68
+ Authorization: auth,
69
+ },
70
+ }
71
+ );
72
+
73
+ if (response.ok) {
74
+ return response.status === 200 ? (response.json() as any) : undefined;
75
+ }
76
+
77
+ const body = await response.text();
78
+ throw new XError(response.statusText, { body });
79
+ };
80
+
81
+ export const getTenantByHyperportalId = async (
82
+ hyperportalTenantId: string
83
+ ): Promise<Tenant | undefined> => {
84
+ const auth = await getAdminAuth();
85
+ const response: Response = await fetch(
86
+ `${process.env.CORE_API_ORIGIN}/admin/tenants/hyperportal/${hyperportalTenantId}`,
87
+ {
88
+ headers: {
89
+ Authorization: auth,
90
+ },
91
+ }
92
+ );
93
+
94
+ if (response.ok) {
95
+ return response.status === 200 ? (response.json() as any) : undefined;
96
+ }
97
+
98
+ const body = await response.text();
99
+ throw new XError(response.statusText, { body });
100
+ };
@@ -0,0 +1,22 @@
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 { User } from '../types/users';
5
+ import { cleanHeaders, getAdminAuth } from './utils';
6
+
7
+ export const getUser = async (tenantId: string, userId: string): Promise<User | undefined> => {
8
+ const auth = await getAdminAuth();
9
+ const response: Response = await fetch(`${process.env.CORE_API_ORIGIN}/api/users/${userId}`, {
10
+ headers: cleanHeaders({
11
+ Authorization: auth,
12
+ [TENANT_ID_ADMIN_HEADER]: tenantId,
13
+ }),
14
+ });
15
+
16
+ if (response.ok) {
17
+ return response.status === 200 ? (response.json() as any) : undefined;
18
+ }
19
+
20
+ const body = await response.text();
21
+ throw new XError(response.statusText, { body, tenantId, userId });
22
+ };
@@ -0,0 +1,23 @@
1
+ import { TENANT_ID_ADMIN_HEADER } from '@bisondesk/commons-sdk/lib/constants';
2
+ import { InvalidArgumentsError } from '@bisondesk/commons-sdk/lib/errors';
3
+ import type { HeadersInit } from 'node-fetch';
4
+
5
+ export const getAdminAuth = async (): Promise<string> => {
6
+ if (!process.env.ADMIN_KEY) {
7
+ return '';
8
+ }
9
+
10
+ return `AdminKey ${process.env.ADMIN_KEY}`;
11
+ };
12
+
13
+ export const getTenantIdHeader = (headers: { [name: string]: unknown }): string => {
14
+ const tenantId = headers[TENANT_ID_ADMIN_HEADER];
15
+ if (!tenantId || typeof tenantId !== 'string') {
16
+ throw new InvalidArgumentsError('Missing or invalid tenantId');
17
+ }
18
+
19
+ return tenantId;
20
+ };
21
+
22
+ export const cleanHeaders = (headers: { [key: string]: string | undefined }): HeadersInit =>
23
+ Object.entries(headers).filter((h): h is [string, string] => h[1] != null);
@@ -0,0 +1,229 @@
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 { DataRecord } from '../types/utils';
8
+ import { NewVehicle, Vehicle, VehicleMarketing, VehiclePurchase } from '../types/vehicles';
9
+ import { cleanHeaders, getAdminAuth } from './utils';
10
+
11
+ export const getVehicle = async (
12
+ tenantId: string,
13
+ vehicleId: string
14
+ ): Promise<DataRecord<Vehicle> | undefined> => {
15
+ const auth = await getAdminAuth();
16
+ const response: Response = await fetch(
17
+ `${process.env.CORE_API_ORIGIN}/api/vehicles/${vehicleId}`,
18
+ {
19
+ headers: {
20
+ Authorization: auth,
21
+ [TENANT_ID_ADMIN_HEADER]: tenantId,
22
+ },
23
+ }
24
+ );
25
+
26
+ if (response.ok) {
27
+ return response.status === 200 ? (response.json() as any) : undefined;
28
+ }
29
+
30
+ const body = await response.text();
31
+ throw new XError(response.statusText, { body });
32
+ };
33
+
34
+ export const getVehiclePurchase = async (
35
+ tenantId: string,
36
+ vehicleId: string
37
+ ): Promise<DataRecord<VehiclePurchase> | undefined> => {
38
+ const auth = await getAdminAuth();
39
+ const response: Response = await fetch(
40
+ `${process.env.CORE_API_ORIGIN}/api/vehicles/${vehicleId}/purchase`,
41
+ {
42
+ headers: {
43
+ Authorization: auth,
44
+ [TENANT_ID_ADMIN_HEADER]: tenantId,
45
+ },
46
+ }
47
+ );
48
+
49
+ if (response.ok) {
50
+ return response.status === 200 ? (response.json() as any) : undefined;
51
+ }
52
+
53
+ const body = await response.text();
54
+ throw new XError(response.statusText, { body });
55
+ };
56
+
57
+ export const getVehicleByStockNumber = async (
58
+ tenantId: string,
59
+ stockNumber: string
60
+ ): Promise<Vehicle | undefined> => {
61
+ const auth = await getAdminAuth();
62
+ const response: Response = await fetch(
63
+ `${process.env.CORE_API_ORIGIN}/api/vehicles/stock-number/${stockNumber}`,
64
+ {
65
+ headers: {
66
+ Authorization: auth,
67
+ [TENANT_ID_ADMIN_HEADER]: tenantId,
68
+ },
69
+ }
70
+ );
71
+
72
+ if (response.ok) {
73
+ return response.status === 200 ? (response.json() as any) : undefined;
74
+ }
75
+
76
+ const body = await response.text();
77
+ throw new XError(response.statusText, { body });
78
+ };
79
+
80
+ export const getVehicleByPurchaseStockNumber = async (
81
+ tenantId: string,
82
+ stockNumber: string
83
+ ): Promise<Vehicle | undefined> => {
84
+ const auth = await getAdminAuth();
85
+ const response: Response = await fetch(
86
+ `${process.env.CORE_API_ORIGIN}/api/vehicles/purchase-stock-number/${stockNumber}`,
87
+ {
88
+ headers: {
89
+ Authorization: auth,
90
+ [TENANT_ID_ADMIN_HEADER]: tenantId,
91
+ },
92
+ }
93
+ );
94
+
95
+ if (response.ok) {
96
+ return response.status === 200 ? (response.json() as any) : undefined;
97
+ }
98
+
99
+ const body = await response.text();
100
+ throw new XError(response.statusText, { body });
101
+ };
102
+
103
+ export const getVehicleMarketing = async (
104
+ tenantId: string,
105
+ vehicleId: string
106
+ ): Promise<VehicleMarketing | undefined> => {
107
+ const auth = await getAdminAuth();
108
+ const response: Response = await fetch(
109
+ `${process.env.CORE_API_ORIGIN}/api/vehicles/${vehicleId}/marketing`,
110
+ {
111
+ headers: {
112
+ Authorization: auth,
113
+ [TENANT_ID_ADMIN_HEADER]: tenantId,
114
+ },
115
+ }
116
+ );
117
+
118
+ if (response.ok) {
119
+ return response.status === 200 ? (response.json() as any) : undefined;
120
+ }
121
+
122
+ const body = await response.text();
123
+ throw new XError(response.statusText, { body });
124
+ };
125
+
126
+ export const saveMarketing = async (
127
+ tenantId: string,
128
+ vehicleId: string,
129
+ marketing: VehicleMarketing
130
+ ): Promise<VehicleMarketing> => {
131
+ const auth = await getAdminAuth();
132
+
133
+ const response: Response = await fetch(
134
+ `${process.env.CORE_API_ORIGIN}/api/vehicles/${vehicleId}/marketing`,
135
+ {
136
+ method: 'POST',
137
+ headers: cleanHeaders({
138
+ Authorization: auth,
139
+ 'Content-Type': 'application/json',
140
+ [TENANT_ID_ADMIN_HEADER]: tenantId,
141
+ }),
142
+ body: JSON.stringify(marketing),
143
+ }
144
+ );
145
+
146
+ if (response.ok) {
147
+ return response.json() as any;
148
+ }
149
+ const body = await response.text();
150
+ throw new XError(response.statusText, { body, tenantId, vehicleId, marketing });
151
+ };
152
+
153
+ export const saveVehiclePurchase = async (
154
+ tenantId: string,
155
+ vehicleId: string,
156
+ purchase: VehiclePurchase,
157
+ opts?: { serviceId: string }
158
+ ): Promise<Vehicle> => {
159
+ const auth = await getAdminAuth();
160
+ const response: Response = await fetch(
161
+ `${process.env.CORE_API_ORIGIN}/api/vehicles/${vehicleId}/purchase`,
162
+ {
163
+ method: 'POST',
164
+ headers: cleanHeaders({
165
+ Authorization: auth,
166
+ 'Content-Type': 'application/json',
167
+ [TENANT_ID_ADMIN_HEADER]: tenantId,
168
+ [SERVICE_ID_ADMIN_HEADER]: opts?.serviceId,
169
+ }),
170
+ body: JSON.stringify(purchase),
171
+ }
172
+ );
173
+
174
+ if (response.ok) {
175
+ return response.json() as any;
176
+ }
177
+ const body = await response.text();
178
+ throw new XError(response.statusText, { body, tenantId, vehicleId, purchase, opts });
179
+ };
180
+
181
+ export const saveVehicle = async (
182
+ tenantId: string,
183
+ vehicle: NewVehicle | Vehicle,
184
+ opts?: { serviceId: string }
185
+ ): Promise<Vehicle> => {
186
+ const auth = await getAdminAuth();
187
+ const response: Response = await fetch(`${process.env.CORE_API_ORIGIN}/api/vehicles`, {
188
+ method: 'POST',
189
+ headers: cleanHeaders({
190
+ Authorization: auth,
191
+ 'Content-Type': 'application/json',
192
+ [TENANT_ID_ADMIN_HEADER]: tenantId,
193
+ [SERVICE_ID_ADMIN_HEADER]: opts?.serviceId,
194
+ }),
195
+ body: JSON.stringify(vehicle),
196
+ });
197
+
198
+ if (response.ok) {
199
+ return response.json() as any;
200
+ }
201
+ const body = await response.text();
202
+ throw new XError(response.statusText, { body, tenantId, vehicle, opts });
203
+ };
204
+
205
+ export const deleteVehicle = async (
206
+ tenantId: string,
207
+ vehicleId: string,
208
+ opts?: { serviceId: string }
209
+ ): Promise<void> => {
210
+ const auth = await getAdminAuth();
211
+ const response: Response = await fetch(
212
+ `${process.env.CORE_API_ORIGIN}/api/vehicles/${vehicleId}`,
213
+ {
214
+ method: 'DELETE',
215
+ headers: cleanHeaders({
216
+ Authorization: auth,
217
+ [TENANT_ID_ADMIN_HEADER]: tenantId,
218
+ [SERVICE_ID_ADMIN_HEADER]: opts?.serviceId,
219
+ }),
220
+ }
221
+ );
222
+
223
+ if (response.ok) {
224
+ return;
225
+ }
226
+
227
+ const body = await response.text();
228
+ throw new XError(response.statusText, { body, tenantId, vehicleId, opts });
229
+ };