@reactionary/hcl 0.9.13 → 0.9.15

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.
@@ -0,0 +1,307 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __decorateClass = (decorators, target, key, kind) => {
4
+ var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
5
+ for (var i = decorators.length - 1, decorator; i >= 0; i--)
6
+ if (decorator = decorators[i])
7
+ result = (kind ? decorator(target, key, result) : decorator(result)) || result;
8
+ if (kind && result)
9
+ __defProp(target, key, result);
10
+ return result;
11
+ };
12
+ import {
13
+ CompanyCapability,
14
+ CompanyMutationAddShippingAddressSchema,
15
+ CompanyMutationMakeShippingAddressDefaultSchema,
16
+ CompanyMutationRemoveShippingAddressSchema,
17
+ CompanyMutationUpdateShippingAddressSchema,
18
+ CompanyPaginatedListSchema,
19
+ CompanyQueryByIdSchema,
20
+ CompanyQueryListSchema,
21
+ Reactionary,
22
+ error,
23
+ success
24
+ } from "@reactionary/core";
25
+ import createDebug from "debug";
26
+ const debug = createDebug("reactionary:hcl:company");
27
+ class HclCompanyCapability extends CompanyCapability {
28
+ config;
29
+ client;
30
+ factory;
31
+ constructor(cache, context, config, client, factory) {
32
+ super(cache, context);
33
+ this.config = config;
34
+ this.client = client;
35
+ this.factory = factory;
36
+ }
37
+ async getById(payload) {
38
+ debug("getById %s", payload.identifier.taxIdentifier);
39
+ const raw = await this.client.callGet(
40
+ this.getOrganizationUrl(payload.identifier.taxIdentifier),
41
+ this.getOrganizationDetailsParams(),
42
+ { allowUndefined: true }
43
+ );
44
+ if (!raw) {
45
+ return error({
46
+ type: "NotFound",
47
+ identifier: payload.identifier
48
+ });
49
+ }
50
+ return success(
51
+ this.factory.parseCompany(
52
+ this.context,
53
+ raw
54
+ )
55
+ );
56
+ }
57
+ async listCompanies(payload) {
58
+ debug(
59
+ "listCompanies page=%d size=%d",
60
+ payload.search.paginationOptions.pageNumber,
61
+ payload.search.paginationOptions.pageSize
62
+ );
63
+ const response = await this.client.callGet(
64
+ this.getOrganizationListUrl(),
65
+ this.getOrganizationListParams(payload)
66
+ );
67
+ return success(
68
+ this.factory.parseCompanyPaginatedList(this.context, response, payload)
69
+ );
70
+ }
71
+ async addShippingAddress(payload) {
72
+ debug(
73
+ "addShippingAddress %s \u2192 %s",
74
+ payload.company.taxIdentifier,
75
+ payload.address.identifier.nickName
76
+ );
77
+ const org = await this.fetchOrganization(payload.company.taxIdentifier);
78
+ if (!org) {
79
+ return error({
80
+ type: "NotFound",
81
+ identifier: payload.company
82
+ });
83
+ }
84
+ const addressBook = [
85
+ ...org.addressBook ?? [],
86
+ this.toOrgContact(payload.address)
87
+ ];
88
+ await this.client.callPut(
89
+ this.getUpdateOrganizationUrl(payload.company.taxIdentifier),
90
+ this.getUpdateOrganizationBody(org, addressBook)
91
+ );
92
+ return success(
93
+ this.factory.parseCompany(
94
+ this.context,
95
+ await this.fetchOrganizationOrThrow(payload.company.taxIdentifier)
96
+ )
97
+ );
98
+ }
99
+ async updateShippingAddress(payload) {
100
+ debug(
101
+ "updateShippingAddress %s \u2192 %s",
102
+ payload.company.taxIdentifier,
103
+ payload.address.identifier.nickName
104
+ );
105
+ const org = await this.fetchOrganization(payload.company.taxIdentifier);
106
+ if (!org) {
107
+ return error({
108
+ type: "NotFound",
109
+ identifier: payload.company
110
+ });
111
+ }
112
+ const existing = (org.addressBook ?? []).find(
113
+ (a) => a.nickName === payload.address.identifier.nickName
114
+ );
115
+ if (!existing) {
116
+ return error({
117
+ type: "NotFound",
118
+ identifier: payload.address.identifier
119
+ });
120
+ }
121
+ const addressBook = (org.addressBook ?? []).map(
122
+ (a) => a.nickName === payload.address.identifier.nickName ? { ...this.toOrgContact(payload.address), primary: existing.primary } : a
123
+ );
124
+ await this.client.callPut(
125
+ this.getUpdateOrganizationUrl(payload.company.taxIdentifier),
126
+ this.getUpdateOrganizationBody(org, addressBook)
127
+ );
128
+ return success(
129
+ this.factory.parseCompany(
130
+ this.context,
131
+ await this.fetchOrganizationOrThrow(payload.company.taxIdentifier)
132
+ )
133
+ );
134
+ }
135
+ async removeShippingAddress(payload) {
136
+ debug(
137
+ "removeShippingAddress %s \u2192 %s",
138
+ payload.company.taxIdentifier,
139
+ payload.addressIdentifier.nickName
140
+ );
141
+ const org = await this.fetchOrganization(payload.company.taxIdentifier);
142
+ if (!org) {
143
+ return error({
144
+ type: "NotFound",
145
+ identifier: payload.company
146
+ });
147
+ }
148
+ const existing = (org.addressBook ?? []).find(
149
+ (a) => a.nickName === payload.addressIdentifier.nickName
150
+ );
151
+ if (!existing) {
152
+ return error({
153
+ type: "NotFound",
154
+ identifier: payload.addressIdentifier
155
+ });
156
+ }
157
+ const addressBook = (org.addressBook ?? []).filter(
158
+ (a) => a.nickName !== payload.addressIdentifier.nickName
159
+ );
160
+ await this.client.callPut(
161
+ this.getUpdateOrganizationUrl(payload.company.taxIdentifier),
162
+ this.getUpdateOrganizationBody(org, addressBook)
163
+ );
164
+ return success(
165
+ this.factory.parseCompany(
166
+ this.context,
167
+ await this.fetchOrganizationOrThrow(payload.company.taxIdentifier)
168
+ )
169
+ );
170
+ }
171
+ async makeShippingAddressDefault(payload) {
172
+ debug(
173
+ "makeShippingAddressDefault %s \u2192 %s",
174
+ payload.company.taxIdentifier,
175
+ payload.addressIdentifier.nickName
176
+ );
177
+ const org = await this.fetchOrganization(payload.company.taxIdentifier);
178
+ if (!org) {
179
+ return error({
180
+ type: "NotFound",
181
+ identifier: payload.company
182
+ });
183
+ }
184
+ const existing = (org.addressBook ?? []).find(
185
+ (a) => a.nickName === payload.addressIdentifier.nickName
186
+ );
187
+ if (!existing) {
188
+ return error({
189
+ type: "NotFound",
190
+ identifier: payload.addressIdentifier
191
+ });
192
+ }
193
+ const addressBook = (org.addressBook ?? []).map((a) => ({
194
+ ...a,
195
+ primary: a.nickName === payload.addressIdentifier.nickName ? "1" : void 0
196
+ }));
197
+ await this.client.callPut(
198
+ this.getUpdateOrganizationUrl(payload.company.taxIdentifier),
199
+ this.getUpdateOrganizationBody(org, addressBook)
200
+ );
201
+ return success(
202
+ this.factory.parseCompany(
203
+ this.context,
204
+ await this.fetchOrganizationOrThrow(payload.company.taxIdentifier)
205
+ )
206
+ );
207
+ }
208
+ // ---------------------------------------------------------------------------
209
+ // Extension points
210
+ // ---------------------------------------------------------------------------
211
+ getOrganizationUrl(orgId) {
212
+ return `${this.client.transactionBaseUrl}/organization/${encodeURIComponent(orgId)}`;
213
+ }
214
+ getOrganizationDetailsParams() {
215
+ const params = new URLSearchParams();
216
+ params.set("profileName", "IBM_Organization_Details");
217
+ return params;
218
+ }
219
+ getOrganizationListUrl() {
220
+ return `${this.client.transactionBaseUrl}/organization`;
221
+ }
222
+ getOrganizationListParams(query) {
223
+ const { pageSize, pageNumber } = query.search.paginationOptions;
224
+ const params = new URLSearchParams();
225
+ params.set("q", "organizationsICanAdmin");
226
+ params.set("profileName", "IBM_Organization_List_Summary");
227
+ params.set("maxResults", String(pageSize));
228
+ params.set("startIndex", String((pageNumber - 1) * pageSize));
229
+ return params;
230
+ }
231
+ getUpdateOrganizationUrl(orgId) {
232
+ return `${this.client.transactionBaseUrl}/organization/${encodeURIComponent(orgId)}`;
233
+ }
234
+ getUpdateOrganizationBody(org, addressBook) {
235
+ return { addressBook };
236
+ }
237
+ // ---------------------------------------------------------------------------
238
+ // Internal helpers
239
+ // ---------------------------------------------------------------------------
240
+ async fetchOrganization(orgId) {
241
+ return this.client.callGet(
242
+ this.getOrganizationUrl(orgId),
243
+ this.getOrganizationDetailsParams(),
244
+ { allowUndefined: true }
245
+ );
246
+ }
247
+ async fetchOrganizationOrThrow(orgId) {
248
+ const org = await this.fetchOrganization(orgId);
249
+ if (!org)
250
+ throw new Error(`Organization not found: ${orgId}`);
251
+ return org;
252
+ }
253
+ toOrgContact(address) {
254
+ const lines = [address.streetAddress, address.streetNumber].filter(
255
+ Boolean
256
+ );
257
+ return {
258
+ nickName: address.identifier.nickName,
259
+ firstName: address.firstName,
260
+ lastName: address.lastName,
261
+ addressLine: lines.length ? lines : void 0,
262
+ city: address.city,
263
+ stateOrProvinceName: address.region,
264
+ zipCode: address.postalCode,
265
+ country: address.countryCode
266
+ };
267
+ }
268
+ }
269
+ __decorateClass([
270
+ Reactionary({
271
+ inputSchema: CompanyQueryByIdSchema,
272
+ outputSchema: CompanyPaginatedListSchema
273
+ })
274
+ ], HclCompanyCapability.prototype, "getById", 1);
275
+ __decorateClass([
276
+ Reactionary({
277
+ inputSchema: CompanyQueryListSchema,
278
+ outputSchema: CompanyPaginatedListSchema
279
+ })
280
+ ], HclCompanyCapability.prototype, "listCompanies", 1);
281
+ __decorateClass([
282
+ Reactionary({
283
+ inputSchema: CompanyMutationAddShippingAddressSchema,
284
+ outputSchema: CompanyPaginatedListSchema
285
+ })
286
+ ], HclCompanyCapability.prototype, "addShippingAddress", 1);
287
+ __decorateClass([
288
+ Reactionary({
289
+ inputSchema: CompanyMutationUpdateShippingAddressSchema,
290
+ outputSchema: CompanyPaginatedListSchema
291
+ })
292
+ ], HclCompanyCapability.prototype, "updateShippingAddress", 1);
293
+ __decorateClass([
294
+ Reactionary({
295
+ inputSchema: CompanyMutationRemoveShippingAddressSchema,
296
+ outputSchema: CompanyPaginatedListSchema
297
+ })
298
+ ], HclCompanyCapability.prototype, "removeShippingAddress", 1);
299
+ __decorateClass([
300
+ Reactionary({
301
+ inputSchema: CompanyMutationMakeShippingAddressDefaultSchema,
302
+ outputSchema: CompanyPaginatedListSchema
303
+ })
304
+ ], HclCompanyCapability.prototype, "makeShippingAddressDefault", 1);
305
+ export {
306
+ HclCompanyCapability
307
+ };