Skip to content

Commit

Permalink
Merge pull request #44 from yalla-coop/fixes
Browse files Browse the repository at this point in the history
Improve error handling. Fix deprecation warnings on shopify
  • Loading branch information
ajluker authored Jul 24, 2024
2 parents a33103e + 9d502dd commit 45590c6
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 124 deletions.
35 changes: 20 additions & 15 deletions web/fdc-modules/orders/controllers/create-or-update-order-line.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
import shopify from '../../../shopify.js';
import getSession from '../../../utils/getShopifySession.js';
import { extractOrderLine, createDfcOrderLineFromShopify } from '../dfc/dfc-order.js';
import {persistLineIdMappings} from './lineItemMappings.js'
import { persistLineIdMappings } from './lineItemMappings.js'
import * as orders from './shopify/orders.js';

// transaction
const createOrUpdateOrderLine = async (req, res) => {
const session = await getSession(`${req.params.EnterpriseName}.myshopify.com`)
const client = new shopify.api.clients.Graphql({ session });
const createOrUpdateOrderLine = async (req, res) => {
try {
const session = await getSession(`${req.params.EnterpriseName}.myshopify.com`)
const client = new shopify.api.clients.Graphql({ session });

const orderLine = extractOrderLine(req.body)
const orderLine = extractOrderLine(req.body)

const shopifyOrder = await orders.findOrder(client, req.params.id);
const shopifyOrder = await orders.findOrder(client, req.params.id);

if (!shopifyOrder) {
return res.status(404).send('Unable to find order');
}
if (!shopifyOrder) {
return res.status(404).send('Unable to find order');
}

const updatedLines = await orders.createUpdatedShopifyLines(shopifyOrder, orderLine);
const updatedShopifyDraftOrder = await orders.updateOrder(client, req.params.id, updatedLines);
const lineItemIdMappings = await persistLineIdMappings(updatedShopifyDraftOrder)
const dfcOrder = await createDfcOrderLineFromShopify(updatedShopifyDraftOrder, req.params.lineId, lineItemIdMappings, req.params.EnterpriseName, req.params.id);
res.type('application/json')
res.send(dfcOrder);
const updatedLines = await orders.createUpdatedShopifyLines(shopifyOrder, orderLine);
const updatedShopifyDraftOrder = await orders.updateOrder(client, req.params.id, updatedLines);
const lineItemIdMappings = await persistLineIdMappings(updatedShopifyDraftOrder)
const dfcOrder = await createDfcOrderLineFromShopify(updatedShopifyDraftOrder, req.params.lineId, lineItemIdMappings, req.params.EnterpriseName, req.params.id);
res.type('application/json')
res.send(dfcOrder);
} catch (error) {
console.error(error);
res.status(500).end();
}
}

export default createOrUpdateOrderLine
21 changes: 13 additions & 8 deletions web/fdc-modules/orders/controllers/create-order.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,27 @@ import { persistLineIdMappings } from './lineItemMappings.js'
import { createDraftOrder } from '../../../database/orders/orders.js'

const createOrder = async (req, res) => {
const session = await getSession(`${req.params.EnterpriseName}.myshopify.com`)
const client = new shopify.api.clients.Graphql({ session });
try {
const session = await getSession(`${req.params.EnterpriseName}.myshopify.com`)
const client = new shopify.api.clients.Graphql({ session });

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

const { order, saleSession } = await extractOrderAndLinesAndSalesSession(req.body);

const shopifyLines = await Promise.all((await order.getLines()).map(orders.dfcLineToShopifyLine))

const shopifyDraftOrder = await orders.createShopifyOrder(client, customerId, req.user.email, new Date(saleSession.getEndDate()), shopifyLines);
const shopifyDraftOrder = await orders.createShopifyOrder(client, customerId, req.user.email, new Date(saleSession.getEndDate()), shopifyLines);

await createDraftOrder(ids.extract(shopifyDraftOrder.id));
const lineItemIdMappings = await persistLineIdMappings(shopifyDraftOrder)
const dfcOrder = await createDfcOrderFromShopify(shopifyDraftOrder, lineItemIdMappings, req.params.EnterpriseName);
res.type('application/json')
res.send(dfcOrder);
await createDraftOrder(ids.extract(shopifyDraftOrder.id));
const lineItemIdMappings = await persistLineIdMappings(shopifyDraftOrder)
const dfcOrder = await createDfcOrderFromShopify(shopifyDraftOrder, lineItemIdMappings, req.params.EnterpriseName);
res.type('application/json')
res.send(dfcOrder);
} catch (error) {
console.error(error);
res.status(500).end();
}
}

export default createOrder;
21 changes: 13 additions & 8 deletions web/fdc-modules/orders/controllers/get-all-orders.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,23 @@ import getSession from '../../../utils/getShopifySession.js';
import { createBulkDfcOrderFromShopify } from '../dfc/dfc-order.js';
import { findOrders } from './shopify/orders.js';

const getAllOrders = async (req, res) => {
const session = await getSession(`${req.params.EnterpriseName}.myshopify.com`)
const client = new shopify.api.clients.Graphql({ session });
const getAllOrders = async (req, res) => {
try {
const session = await getSession(`${req.params.EnterpriseName}.myshopify.com`)
const client = new shopify.api.clients.Graphql({ session });

const draftOrdersWithLineItemMappings = await getAllLineItems();
const draftOrdersWithLineItemMappings = await getAllLineItems();

const shopifyOrders = await findOrders(client);
const shopifyOrders = await findOrders(client);

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

res.type('application/json')
res.send(allDfcOrders);
res.type('application/json')
res.send(allDfcOrders);
} catch (error) {
console.error(error);
res.status(500).end();
}
}

export default getAllOrders
29 changes: 17 additions & 12 deletions web/fdc-modules/orders/controllers/get-order-line.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,28 @@ import getSession from '../../../utils/getShopifySession.js';
import { createDfcOrderLineFromShopify } from '../dfc/dfc-order.js';
import { findOrder } from './shopify/orders.js';

const getOrderLine = async (req, res) => {
const session = await getSession(`${req.params.EnterpriseName}.myshopify.com`)
const client = new shopify.api.clients.Graphql({ session });
const getOrderLine = async (req, res) => {
try {
const session = await getSession(`${req.params.EnterpriseName}.myshopify.com`)
const client = new shopify.api.clients.Graphql({ session });

const shopifyOrder = await findOrder(client, req.params.id);
const shopifyOrder = await findOrder(client, req.params.id);

if (!shopifyOrder) {
return res.status(404).send('Unable to find order');
}
if (!shopifyOrder) {
return res.status(404).send('Unable to find order');
}

const dfcOrder = await createDfcOrderLineFromShopify(shopifyOrder, req.params.lineId, await getLineItemIdMappings(shopifyOrder.id), req.params.EnterpriseName, req.params.id);
const dfcOrder = await createDfcOrderLineFromShopify(shopifyOrder, req.params.lineId, await getLineItemIdMappings(shopifyOrder.id), req.params.EnterpriseName, req.params.id);

if (!dfcOrder) {
res.status(404).send('Order ine not found');
if (!dfcOrder) {
res.status(404).send('Order ine not found');
}
res.type('application/json')
res.send(dfcOrder);
} catch (error) {
console.error(error);
res.status(500).end();
}
res.type('application/json')
res.send(dfcOrder);
}

export default getOrderLine
25 changes: 15 additions & 10 deletions web/fdc-modules/orders/controllers/get-order-lines.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,24 @@ import getSession from '../../../utils/getShopifySession.js';
import { createDfcOrderLinesFromShopify } from '../dfc/dfc-order.js';
import { findOrder } from './shopify/orders.js';

const getOrderLines = async (req, res) => {
const session = await getSession(`${req.params.EnterpriseName}.myshopify.com`)
const client = new shopify.api.clients.Graphql({ session });
const getOrderLines = async (req, res) => {
try {
const session = await getSession(`${req.params.EnterpriseName}.myshopify.com`)
const client = new shopify.api.clients.Graphql({ session });

const shopifyOrder = await findOrder(client, req.params.id);
const shopifyOrder = await findOrder(client, req.params.id);

if (!shopifyOrder) {
return res.status(404).send('Unable to find order');
}
if (!shopifyOrder) {
return res.status(404).send('Unable to find order');
}

const dfcOrder = await createDfcOrderLinesFromShopify(shopifyOrder, await getLineItemIdMappings(shopifyOrder.id), req.params.EnterpriseName, req.params.id);
res.type('application/json')
res.send(dfcOrder);
const dfcOrder = await createDfcOrderLinesFromShopify(shopifyOrder, await getLineItemIdMappings(shopifyOrder.id), req.params.EnterpriseName, req.params.id);
res.type('application/json')
res.send(dfcOrder);
} catch (error) {
console.error(error);
res.status(500).end();
}
}

export default getOrderLines
25 changes: 15 additions & 10 deletions web/fdc-modules/orders/controllers/get-order.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,24 @@ import { createDfcOrderFromShopify } from '../dfc/dfc-order.js';
import { findOrder } from './shopify/orders.js';
import { getLineItemIdMappings } from '../../../database/line_items/lineItems.js'

const getOrder = async (req, res) => {
const session = await getSession(`${req.params.EnterpriseName}.myshopify.com`)
const client = new shopify.api.clients.Graphql({ session });
const getOrder = async (req, res) => {
try {
const session = await getSession(`${req.params.EnterpriseName}.myshopify.com`)
const client = new shopify.api.clients.Graphql({ session });

const shopifyOrder = await findOrder(client, req.params.id);
const shopifyOrder = await findOrder(client, req.params.id);

if (!shopifyOrder) {
return res.status(404).send('Unable to find order');
}
if (!shopifyOrder) {
return res.status(404).send('Unable to find order');
}

const dfcOrder = await createDfcOrderFromShopify(shopifyOrder, await getLineItemIdMappings(shopifyOrder.id), req.params.EnterpriseName);
res.type('application/json')
res.send(dfcOrder);
const dfcOrder = await createDfcOrderFromShopify(shopifyOrder, await getLineItemIdMappings(shopifyOrder.id), req.params.EnterpriseName);
res.type('application/json')
res.send(dfcOrder);
} catch (error) {
console.error(error);
res.status(500).end();
}
}

export default getOrder
88 changes: 42 additions & 46 deletions web/fdc-modules/orders/controllers/shopify/orders.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export async function findOrder(client, orderId) {
console.error('Failed to load Order', JSON.stringify(response.errors));
throw new Error('Failed to load Order');
}

return response.data.draftOrder;
}

Expand All @@ -49,7 +49,7 @@ export async function findOrders(client) {
query findDraftOrders {
draftOrders(first: 250, query: "tag:fdc") {
edges {
nodes {
node {
id
status
order {
Expand Down Expand Up @@ -81,18 +81,14 @@ export async function findOrders(client) {
}
}
`;
const response = await client.query({
data: {
query,
}
});
const response = await client.request(query, {});

if (response.errors) {
console.error('Failed to load Orders', JSON.stringify(response.errors));
throw new Error('Failed to load Orders');
}

return response.data.draftOrders;
return response.data.draftOrders.edges.map(({ node }) => node);
}

export async function createShopifyOrder(client, customerId, customerEmail, reservationDate, lines) {
Expand Down Expand Up @@ -224,48 +220,48 @@ export async function updateOrder(client, orderId, lines) {
}

export async function completeDraftOrder(client, orderId) {
const response = await client.query({
data: {
"query": `mutation CompleteDraftOrder($id: ID!) {
draftOrderComplete(id: $id) {
userErrors {
field
message
}
draftOrder {
const query = `mutation CompleteDraftOrder($id: ID!) {
draftOrderComplete(id: $id) {
userErrors {
field
message
}
draftOrder {
id
status
order {
id
displayFulfillmentStatus
cancelledAt
closed
fullyPaid
}
lineItems(first: 250) {
edges {
node {
id
status
order {
id
displayFulfillmentStatus
cancelledAt
closed
fullyPaid
}
lineItems(first: 250) {
edges {
node {
id
quantity
originalUnitPriceSet {
shopMoney {
amount
currencyCode
}
}
variant {
id
title
}
quantity
originalUnitPriceSet {
shopMoney {
amount
currencyCode
}
}
}
}
variant {
id
title
}
}
}`,
"variables": {
"id": ids.draftOrder(orderId)
},
}
}
}
}
}`;


const response = await client.request(query, {
variables: {
"id": ids.draftOrder(orderId)
},
});

Expand Down
Loading

0 comments on commit 45590c6

Please sign in to comment.