Skip to content

Commit

Permalink
Improve error handling when customer does not exist in shop
Browse files Browse the repository at this point in the history
  • Loading branch information
ajluker committed Sep 17, 2024
1 parent 7e6a873 commit cd6e49c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
4 changes: 4 additions & 0 deletions web/fdc-modules/orders/controllers/create-order.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ const createOrder = async (req, res) => {

const customerId = await findCustomer(client, req.user.id);

if (!customerId) {
return res.status(403).send(`Customer with email matching ${req.user.id} must exist in shopify for you to create an order`);
}

const { order, saleSession } = await extractOrderAndLinesAndSalesSession(
req.body
);
Expand Down
16 changes: 13 additions & 3 deletions web/fdc-modules/orders/controllers/get-all-orders.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ const getAllOrders = async (req, res) => {

const customerId = await findCustomer(client, req.user.id);

if (!customerId) {
return respond(res, await createBulkDfcOrderFromShopify([], [], req.params.EnterpriseName), null);
}

const {before, after, first, last} = req.query;

if ((before && after) || (before && first) || (after && last) && (before && !last) && (after && !first)){
Expand All @@ -24,13 +28,19 @@ const getAllOrders = async (req, res) => {

const allDfcOrders = await createBulkDfcOrderFromShopify(orders, draftOrdersWithLineItemMappings, req.params.EnterpriseName);

res.type('application/json');
res.set("pageInfo", JSON.stringify(pageInfo));
res.send(allDfcOrders);
return respond(res, allDfcOrders, pageInfo);
} catch (error) {
console.error(error);
res.status(500).end();
}
}

function respond(res, graph, pageInfo) {
res.type('application/json');
if (pageInfo){
res.set("pageInfo", JSON.stringify(pageInfo));
}
res.send(graph);
}

export default getAllOrders
2 changes: 1 addition & 1 deletion web/fdc-modules/orders/controllers/shopify/customer.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export async function findCustomer(client, customerEmail) {
}

if (response.data.customers.nodes.length < 1) {
throw new Error('Unable to find customer with email', customerEmail);
return null;
}

return response.data.customers.nodes[0].id
Expand Down

0 comments on commit cd6e49c

Please sign in to comment.