Skip to content

Commit 831a0f4

Browse files
committed
Finalize the Order Fetcher, adapt React Hooks according, and some renaming
1 parent 63edbc1 commit 831a0f4

File tree

4 files changed

+111
-17
lines changed

4 files changed

+111
-17
lines changed

src/core/order.ts

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,92 @@ import {
1010
} from '../types/order.types';
1111
import { jsonToGraphQLQuery } from 'json-to-graphql-query';
1212

13+
function buildQuery(onCustomer?: any, onOrderItem?: any, extraQuery?: any) {
14+
return {
15+
id: true,
16+
createdAt: true,
17+
updatedAt: true,
18+
customer: {
19+
identifier: true,
20+
...(onCustomer !== undefined ? onCustomer : {})
21+
},
22+
cart: {
23+
name: true,
24+
sku: true,
25+
imageUrl: true,
26+
quantity: true,
27+
...(onOrderItem !== undefined ? onOrderItem : {}),
28+
price: {
29+
gross: true,
30+
net: true,
31+
discounts: {
32+
percent: true
33+
}
34+
}
35+
},
36+
total: {
37+
gross: true,
38+
net: true,
39+
currency: true,
40+
discounts: {
41+
percent: true
42+
},
43+
tax: {
44+
name: true,
45+
percent: true
46+
}
47+
},
48+
...(extraQuery !== undefined ? extraQuery : {})
49+
};
50+
}
51+
1352
export function createOrderFetcher(apiClient: ClientInterface) {
53+
// we don't provide the current cursor for each Order.
54+
const fetchPaginatedOrdersByCustomerIdentifier = async (
55+
customerIdentifier: string,
56+
extraQueryArgs?: any,
57+
onCustomer?: any,
58+
onOrderItem?: any,
59+
extraQuery?: any
60+
): Promise<{
61+
pageInfo: {
62+
hasNextPage: boolean;
63+
hasPreviousPage: boolean;
64+
startCursor: string;
65+
endCursor: string;
66+
totalNodes: number;
67+
};
68+
orders: Order[];
69+
}> => {
70+
const orderApi = apiClient.orderApi;
71+
const query = {
72+
orders: {
73+
getAll: {
74+
__args: {
75+
customerIdentifier: customerIdentifier,
76+
...(extraQueryArgs !== undefined ? extraQueryArgs : {})
77+
},
78+
pageInfo: {
79+
hasPreviousPage: true,
80+
hasNextPage: true,
81+
startCursor: true,
82+
endCursor: true,
83+
totalNodes: true
84+
},
85+
edges: {
86+
cursor: true,
87+
node: buildQuery(onCustomer, onOrderItem, extraQuery)
88+
}
89+
}
90+
}
91+
};
92+
const response = await orderApi(jsonToGraphQLQuery({ query }));
93+
return {
94+
pageInfo: response.orders.getAll.pageInfo,
95+
orders: response.orders.getAll.edges.map((edge: any) => edge.node)
96+
};
97+
};
98+
1499
const fetchOrderById = async (
15100
orderId: string,
16101
onCustomer?: any,
@@ -65,7 +150,8 @@ export function createOrderFetcher(apiClient: ClientInterface) {
65150
};
66151

67152
return {
68-
byId: fetchOrderById
153+
byId: fetchOrderById,
154+
byCustomerIdentifier: fetchPaginatedOrdersByCustomerIdentifier
69155
};
70156
}
71157

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,4 @@ export const CrystallizeCatalogueFetcher = createCatalogueFetcher(CrystallizeCli
3535

3636
const orderFetcher = createOrderFetcher(CrystallizeClient);
3737
export const CrystallizeOrderFetcherById = orderFetcher.byId;
38+
export const CrystallizeOrderFetcherByCustomerIdentifier = orderFetcher.byCustomerIdentifier;

tests/order.fetch.test.js

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
1-
const { CrystallizeOrderFetcherById } = require('../dist/index.js');
1+
const { CrystallizeOrderFetcherById, CrystallizeOrderFetcherByCustomerIdentifier } = require('../dist/index.js');
22

3-
test('Hydrate Paths', async () => {
3+
test('Oder By ID', async () => {
44
const fetcher = CrystallizeOrderFetcherById;
5-
const order = await fetcher('62557a65be73e0f09258ef69');
6-
console.log(order);
7-
// const hydrater = createProductHydraterByPaths(CrystallizeClient);
8-
// const response = await hydrater(
9-
// [
10-
// '/shop/bathroom-fitting/large-mounted-cabinet-in-treated-wood',
11-
// '/shop/bathroom-fitting/mounted-bathroom-counter-with-shelf'
12-
// ],
13-
// 'en'
14-
// );
5+
try {
6+
// it has to fail with 404 because we don't have any credentials
7+
const order = await fetcher('62557a65be73e0f09258ef69');
8+
expect(order.cart[0].name).toBe('sofa');
9+
} catch (exception) {
10+
expect(exception.code).toBe(404);
11+
}
12+
});
1513

16-
// expect(response.product0.path).toBe('/shop/bathroom-fitting/large-mounted-cabinet-in-treated-wood');
17-
// expect(response.product1.path).toBe('/shop/bathroom-fitting/mounted-bathroom-counter-with-shelf');
14+
test('Oder By Customer ID', async () => {
15+
const fetcher = CrystallizeOrderFetcherByCustomerIdentifier;
16+
try {
17+
// it has to fail with 404 because we don't have any credentials
18+
const pagination = await fetcher('GillesC.');
19+
expect(pagination.orders[0].cart[0].name).toBe('iteasadadsdm1');
20+
expect(pagination.orders[1].cart[0].name).toBe('item1');
21+
expect(pagination.orders[1].cart[0].quantity).toBe(1);
22+
} catch (exception) {
23+
expect(exception.code).toBe(404);
24+
}
1825
});

tests/order.update.payment.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ test('pimAPi: Update Order Payment: Test Validation', async () => {
1515
value: 'Plopix Coin'
1616
},
1717
{
18-
property: 'amountt',
18+
property: 'amount',
1919
value: '112358'
2020
}
2121
]
@@ -30,7 +30,7 @@ test('pimAPi: Update Order Payment: Test Validation', async () => {
3030
value: 'Plopix Coin 2'
3131
},
3232
{
33-
property: 'amountt',
33+
property: 'amount',
3434
value: '90'
3535
}
3636
]

0 commit comments

Comments
 (0)