-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add console.log to create + update order endpoints
- Loading branch information
Showing
2 changed files
with
120 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,59 @@ | ||
import shopify from '../../../shopify.js'; | ||
import getSession from '../../../utils/getShopifySession.js'; | ||
import { extractOrderAndLinesAndSalesSession, createDfcOrderFromShopify } from '../dfc/dfc-order.js'; | ||
import { | ||
extractOrderAndLinesAndSalesSession, | ||
createDfcOrderFromShopify | ||
} from '../dfc/dfc-order.js'; | ||
import { findCustomer } from './shopify/customer.js'; | ||
import * as orders from './shopify/orders.js'; | ||
import * as ids from './shopify/ids.js'; | ||
import { persistLineIdMappings } from './lineItemMappings.js' | ||
import { createDraftOrder } from '../../../database/orders/orders.js' | ||
import { createSalesSession } from '../../../database/sales_sessions/salesSessions.js' | ||
import { persistLineIdMappings } from './lineItemMappings.js'; | ||
import { createDraftOrder } from '../../../database/orders/orders.js'; | ||
import { createSalesSession } from '../../../database/sales_sessions/salesSessions.js'; | ||
|
||
const createOrder = async (req, res) => { | ||
try { | ||
const session = await getSession(`${req.params.EnterpriseName}.myshopify.com`) | ||
const client = new shopify.api.clients.Graphql({ session }); | ||
try { | ||
console.log('Creating order with', req.body); | ||
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 customerId = await findCustomer(client, req.user.id); | ||
|
||
const { order, saleSession } = await extractOrderAndLinesAndSalesSession(req.body); | ||
const { order, saleSession } = await extractOrderAndLinesAndSalesSession( | ||
req.body | ||
); | ||
|
||
const shopifyLines = await Promise.all((await order.getLines()).map(orders.dfcLineToShopifyLine)) | ||
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)); | ||
await createSalesSession(ids.extract(shopifyDraftOrder.id), saleSession.getEndDate()); | ||
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(); | ||
} | ||
} | ||
await createDraftOrder(ids.extract(shopifyDraftOrder.id)); | ||
await createSalesSession( | ||
ids.extract(shopifyDraftOrder.id), | ||
saleSession.getEndDate() | ||
); | ||
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; | ||
export default createOrder; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,96 @@ | ||
import loadConnectorWithResources from '../../../connector/index.js'; | ||
import * as database from '../../../database/orders/orders.js'; | ||
import {loadSalesSession} from '../../../database/sales_sessions/salesSessions.js'; | ||
import { loadSalesSession } from '../../../database/sales_sessions/salesSessions.js'; | ||
import shopify from '../../../shopify.js'; | ||
import getSession from '../../../utils/getShopifySession.js'; | ||
import { createDfcOrderFromShopify, extractOrderAndLines } from '../dfc/dfc-order.js'; | ||
import { | ||
createDfcOrderFromShopify, | ||
extractOrderAndLines | ||
} from '../dfc/dfc-order.js'; | ||
import { persistLineIdMappings } from './lineItemMappings.js'; | ||
import * as ids from './shopify/ids.js'; | ||
import * as shopifyOrders from './shopify/orders.js'; | ||
|
||
//todo: transaction | ||
const updateOrder = async (req, res) => { | ||
try { | ||
const session = await getSession(`${req.params.EnterpriseName}.myshopify.com`) | ||
const client = new shopify.api.clients.Graphql({ session }); | ||
try { | ||
console.log('updating order with :>> ', req.body); | ||
const session = await getSession( | ||
`${req.params.EnterpriseName}.myshopify.com` | ||
); | ||
const client = new shopify.api.clients.Graphql({ session }); | ||
|
||
const order = await extractOrderAndLines(req.body) | ||
const order = await extractOrderAndLines(req.body); | ||
|
||
if (ids.extract(await order.getSemanticId()) !== req.params.id) { | ||
return res.status(400).send('ID does not match payload'); | ||
} | ||
if (ids.extract(await order.getSemanticId()) !== req.params.id) { | ||
return res.status(400).send('ID does not match payload'); | ||
} | ||
|
||
const {order: shopifyOrder} = await shopifyOrders.findOrder(client, req.params.id, {}); | ||
const { order: shopifyOrder } = await shopifyOrders.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 salesSession = await loadSalesSession(req.params.id); | ||
const salesSession = await loadSalesSession(req.params.id); | ||
|
||
if (!salesSession) { | ||
return res.status(500).send('Unable to find sales session'); | ||
} | ||
if (!salesSession) { | ||
return res.status(500).send('Unable to find sales session'); | ||
} | ||
|
||
const shopifyDraftOrder = await updateShopifyDraftOrder(client, order, new Date(salesSession.reservationDate)); | ||
const shopifyDraftOrder = await updateShopifyDraftOrder( | ||
client, | ||
order, | ||
new Date(salesSession.reservationDate) | ||
); | ||
|
||
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(); | ||
} | ||
} | ||
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(); | ||
} | ||
}; | ||
|
||
async function updateShopifyDraftOrder(client, order, reservationDate) { | ||
const dfcLines = await order.getLines(); | ||
|
||
const shopifyLines = (await Promise.all(dfcLines.map(shopifyOrders.dfcLineToShopifyLine))).filter(({ quantity }) => quantity > 0); | ||
const shopifyDraftOrder = await shopifyOrders.updateOrder(client, ids.extract(await order.getSemanticId()), reservationDate, shopifyLines) | ||
const connector = await loadConnectorWithResources(); | ||
if ((await order.getOrderStatus()) === connector.VOCABULARY.STATES.ORDERSTATE.COMPLETE) { | ||
const completedOrder = await shopifyOrders.completeDraftOrder(client, ids.extract(await order.getSemanticId())); | ||
await database.completeDraftOrder(ids.extract(completedOrder.id), ids.extract(completedOrder.order.id)) | ||
return completedOrder; | ||
} else { | ||
return shopifyDraftOrder; | ||
} | ||
const dfcLines = await order.getLines(); | ||
|
||
const shopifyLines = ( | ||
await Promise.all(dfcLines.map(shopifyOrders.dfcLineToShopifyLine)) | ||
).filter(({ quantity }) => quantity > 0); | ||
const shopifyDraftOrder = await shopifyOrders.updateOrder( | ||
client, | ||
ids.extract(await order.getSemanticId()), | ||
reservationDate, | ||
shopifyLines | ||
); | ||
const connector = await loadConnectorWithResources(); | ||
if ( | ||
(await order.getOrderStatus()) === | ||
connector.VOCABULARY.STATES.ORDERSTATE.COMPLETE | ||
) { | ||
const completedOrder = await shopifyOrders.completeDraftOrder( | ||
client, | ||
ids.extract(await order.getSemanticId()) | ||
); | ||
await database.completeDraftOrder( | ||
ids.extract(completedOrder.id), | ||
ids.extract(completedOrder.order.id) | ||
); | ||
return completedOrder; | ||
} else { | ||
return shopifyDraftOrder; | ||
} | ||
} | ||
|
||
export default updateOrder | ||
export default updateOrder; |