Skip to content

Commit f539023

Browse files
committed
fix(js-api-client): Convert dates and enums according to API types
1 parent 0df32cf commit f539023

File tree

4 files changed

+120
-21
lines changed

4 files changed

+120
-21
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@crystallize/js-api-client",
33
"license": "MIT",
4-
"version": "1.5.2",
4+
"version": "1.6.0",
55
"author": "Crystallize <[email protected]> (https://crystallize.com)",
66
"contributors": [
77
"Sébastien Morel <[email protected]>"
@@ -27,7 +27,7 @@
2727
},
2828
"dependencies": {
2929
"dotenv": "^16.0.0",
30-
"json-to-graphql-query": "^2.2.3",
30+
"json-to-graphql-query": "^2.2.4",
3131
"node-fetch": "^2",
3232
"tiny-invariant": "^1.2.0",
3333
"typescript": "^4.6.3",

src/core/customer.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@ import {
77
} from '../types/customer';
88
import { ClientInterface } from './client';
99

10+
function convertDates(intent: CreateCustomerInputRequest | UpdateCustomerInputRequest) {
11+
if (!intent.birthDate) {
12+
return {
13+
...intent,
14+
};
15+
}
16+
return {
17+
...intent,
18+
birthDate: intent.birthDate.toISOString(),
19+
};
20+
}
21+
1022
export function createCustomerManager(apiClient: ClientInterface) {
1123
const create = async (intentCustomer: CreateCustomerInputRequest, extraResultQuery?: any): Promise<any> => {
1224
const intent = createCustomerInputRequest.parse(intentCustomer);
@@ -18,7 +30,7 @@ export function createCustomerManager(apiClient: ClientInterface) {
1830
create: {
1931
__args: {
2032
input: {
21-
...intent,
33+
...convertDates(intent),
2234
tenantId: apiClient.config.tenantId || intent.tenantId || '',
2335
},
2436
},
@@ -46,7 +58,7 @@ export function createCustomerManager(apiClient: ClientInterface) {
4658
update: {
4759
__args: {
4860
identifier,
49-
input: intent,
61+
input: convertDates(intent),
5062
tenantId: apiClient.config.tenantId || '',
5163
},
5264
identifier: true,

src/core/order.ts

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,32 @@ export function createOrderFetcher(apiClient: ClientInterface) {
155155
};
156156
}
157157

158+
function convertDates(intent: CreateOrderInputRequest | UpdateOrderInputRequest) {
159+
if (!intent.cart) {
160+
return {
161+
...intent,
162+
};
163+
}
164+
return {
165+
...intent,
166+
cart: intent.cart.map((item) => {
167+
if (!item.subscription) {
168+
return {
169+
...item,
170+
};
171+
}
172+
return {
173+
...item,
174+
subscription: {
175+
...item.subscription,
176+
start: item.subscription.start?.toISOString(),
177+
end: item.subscription.end?.toISOString(),
178+
},
179+
};
180+
}),
181+
};
182+
}
183+
158184
export function createOrderPusher(apiClient: ClientInterface) {
159185
return async function pushOrder(intentOrder: CreateOrderInputRequest): Promise<OrderCreatedConfirmation> {
160186
const intent = createOrderInputRequest.parse(intentOrder);
@@ -166,7 +192,8 @@ export function createOrderPusher(apiClient: ClientInterface) {
166192
create: {
167193
__args: {
168194
input: {
169-
...intent,
195+
...convertDates(intent),
196+
createdAt: intent.createdAt?.toISOString() ?? new Date().toISOString(),
170197
},
171198
},
172199
id: true,
@@ -196,9 +223,7 @@ export function createOrderPaymentUpdater(apiClient: ClientInterface) {
196223
update: {
197224
__args: {
198225
id: orderId,
199-
input: {
200-
...intent,
201-
},
226+
input: convertDates(intent),
202227
},
203228
id: true,
204229
updatedAt: true,

src/core/subscription.ts

Lines changed: 75 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,91 @@ import {
2020
import { catalogueFetcherGraphqlBuilder, createCatalogueFetcher } from './catalogue';
2121
import { ClientInterface } from './client';
2222

23+
function convertDates(intent: CreateSubscriptionContractInputRequest | UpdateSubscriptionContractInputRequest) {
24+
if (!intent.status) {
25+
return {
26+
...intent,
27+
};
28+
}
29+
30+
let results: any = {
31+
...intent,
32+
};
33+
34+
if (intent.status.renewAt) {
35+
results = {
36+
...results,
37+
status: {
38+
...results.status,
39+
renewAt: intent.status.renewAt.toISOString(),
40+
},
41+
};
42+
}
43+
44+
if (intent.status.activeUntil) {
45+
results = {
46+
...results,
47+
status: {
48+
...results.status,
49+
activeUntil: intent.status.activeUntil.toISOString(),
50+
},
51+
};
52+
}
53+
return results;
54+
}
55+
56+
function convertEnums(intent: CreateSubscriptionContractInputRequest | UpdateSubscriptionContractInputRequest) {
57+
let results: any = {
58+
...intent,
59+
};
60+
61+
if (intent.initial && intent.initial.meteredVariables) {
62+
results = {
63+
...results,
64+
initial: {
65+
...intent.initial,
66+
meteredVariables: intent.initial.meteredVariables.map((variable: any) => {
67+
return {
68+
...variable,
69+
tierType: typeof variable.tierType === 'string' ? variable.tierType : variable.tierType.value,
70+
};
71+
}),
72+
},
73+
};
74+
}
75+
76+
if (intent.recurring && intent.recurring.meteredVariables) {
77+
results = {
78+
...results,
79+
recurring: {
80+
...intent.recurring,
81+
meteredVariables: intent.recurring.meteredVariables.map((variable: any) => {
82+
return {
83+
...variable,
84+
tierType: typeof variable.tierType === 'string' ? variable.tierType : variable.tierType.value,
85+
};
86+
}),
87+
},
88+
};
89+
}
90+
91+
return results;
92+
}
93+
2394
export function createSubscriptionContractManager(apiClient: ClientInterface) {
2495
const create = async (
2596
intentSubsctiptionContract: CreateSubscriptionContractInputRequest,
2697
extraResultQuery?: any,
2798
): Promise<any> => {
28-
const intent = createSubscriptionContractInputRequest.parse(intentSubsctiptionContract);
99+
const intent = createSubscriptionContractInputRequest.parse(convertEnums(intentSubsctiptionContract));
29100
const api = apiClient.pimApi;
30101

31102
const mutation = {
32103
mutation: {
33104
subscriptionContract: {
34105
create: {
35106
__args: {
36-
input: {
37-
...intent,
38-
status: {
39-
...intent.status,
40-
renewAt: intent.status.renewAt.toISOString(),
41-
activeUntil: intent.status.activeUntil.toISOString(),
42-
},
43-
},
107+
input: convertDates(intent),
44108
},
45109
id: true,
46110
createdAt: true,
@@ -58,7 +122,7 @@ export function createSubscriptionContractManager(apiClient: ClientInterface) {
58122
intentSubsctiptionContract: UpdateSubscriptionContractInputRequest,
59123
extraResultQuery?: any,
60124
): Promise<any> => {
61-
const intent = updateSubscriptionContractInputRequest.parse(intentSubsctiptionContract);
125+
const intent = updateSubscriptionContractInputRequest.parse(convertEnums(intentSubsctiptionContract));
62126
const api = apiClient.pimApi;
63127

64128
const mutation = {
@@ -67,9 +131,7 @@ export function createSubscriptionContractManager(apiClient: ClientInterface) {
67131
update: {
68132
__args: {
69133
id,
70-
input: {
71-
...intent,
72-
},
134+
input: convertDates(intent),
73135
},
74136
id: true,
75137
updatedAt: true,

0 commit comments

Comments
 (0)