-
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.
Merge pull request #40 from yalla-coop/179-more-order-work
179 more order work
- Loading branch information
Showing
21 changed files
with
482 additions
and
109 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
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
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,31 +1,40 @@ | ||
import {pool} from '../connect.js' | ||
export const createLineItems = async (orderId, lineItems) => { | ||
import { pool } from '../connect.js' | ||
|
||
export const createOrUpdateLineItems = async (orderId, lineItems) => { | ||
const parameters = lineItems.map(line => ({ orderId, id: line.id, variantId: line.variantId })); | ||
try { | ||
const result = await pool.query(` | ||
const result = await pool.query(` | ||
INSERT INTO line_items (shopify_id, order_id, variant_id) | ||
(SELECT * | ||
FROM json_to_recordset($1) | ||
AS x("id" bigint, "orderId" bigint, "variantId" bigint)) | ||
on CONFLICT(variant_id) | ||
on CONFLICT(order_id, variant_id) | ||
DO UPDATE SET | ||
shopify_id = EXCLUDED.shopify_id | ||
RETURNING *; | ||
`, | ||
|
||
[JSON.stringify(parameters)] | ||
); | ||
return result.rows; | ||
} catch (err) { | ||
throw new Error(err); | ||
} | ||
[JSON.stringify(parameters)] | ||
); | ||
return result.rows; | ||
}; | ||
|
||
export const getLineItems = async (orderId) => { | ||
return (await pool.query(`SELECT external_id as "externalId", shopify_id as "shopifyId", variant_id as "variantId" FROM line_items where order_id = $1`, [orderId])).rows; | ||
}; | ||
|
||
export const getAllLineItems = async () => { | ||
const lineItems = (await pool.query(`SELECT order_id as "draftOrderId", external_id as "externalId", shopify_id as "shopifyId", variant_id as "variantId" FROM line_items order by order_id`, [])).rows; | ||
return lineItems.reduce((accumulator, lineItem) => { | ||
const [lastOrder, ...others] = accumulator | ||
if (lastOrder?.draftOrderId === lineItem.draftOrderId) { | ||
return [{draftOrderId: lineItem.draftOrderId, lineItems: {[lineItem.shopifyId]: lineItem.externalId, ...lastOrder.lineItems}}, ...others]; | ||
} else { | ||
return [{draftOrderId: lineItem.draftOrderId, lineItems: {[lineItem.shopifyId]: lineItem.externalId}}, ...accumulator] | ||
} | ||
}, []).reverse(); | ||
} | ||
|
||
export const getLineItemIdMappings = async (orderId) => { | ||
return (await getLineItems(orderId)) | ||
.reduce((mappings, mapping) => ({...mappings, [mapping.shopifyId]: mapping.externalId}), {}) | ||
.reduce((mappings, mapping) => ({ ...mappings, [mapping.shopifyId]: mapping.externalId }), {}) | ||
} |
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
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
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { pool } from '../connect.js' | ||
|
||
export const createDraftOrder = async (draftOrderId) => { | ||
const result = await pool.query(` | ||
INSERT INTO orders (draft_order_id) | ||
VALUES ($1) | ||
RETURNING *; | ||
`, [draftOrderId]); | ||
return result.rows; | ||
}; | ||
|
||
export const completeDraftOrder = async (draftOrderId, completedOrderId) => { | ||
const result = await pool.query(` | ||
UPDATE orders set completed_order_id = $2 | ||
WHERE draft_order_id = $1 | ||
RETURNING *; | ||
`, [draftOrderId, completedOrderId]); | ||
return result.rows; | ||
}; | ||
|
||
export const getOrders = async () => { | ||
const result = await pool.query(`SELECT draft_order_id as "draftOrderId", completed_order_id as "completedOrderId" from orders order by draft_order_id`, []); | ||
return result.rows; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { pool } from '../connect'; | ||
import {createDraftOrder, completeDraftOrder, getOrders} from './orders' | ||
|
||
describe('orders', () => { | ||
beforeAll(async () => { | ||
await pool.query(`truncate table orders restart identity`); | ||
}); | ||
|
||
it('Order can be created', async () => { | ||
await createDraftOrder(55); | ||
await createDraftOrder(56); | ||
const result = await getOrders(); | ||
expect(result).toStrictEqual([{draftOrderId: "55", completedOrderId: null}, {draftOrderId: "56", completedOrderId: null}]) | ||
}); | ||
|
||
it('Order can be completed', async () => { | ||
await completeDraftOrder(55, 654); | ||
const result = await getOrders(); | ||
expect(result).toStrictEqual([{draftOrderId: "55", completedOrderId: "654"}, {draftOrderId: "56", completedOrderId: null}]) | ||
}) | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
BEGIN; | ||
DROP TABLE IF EXISTS "orders" CASCADE; | ||
CREATE TABLE IF NOT EXISTS "orders" ( | ||
"draft_order_id" bigint PRIMARY KEY, | ||
"completed_order_id" bigint NULL, | ||
"created_at" TIMESTAMP NOT NULL DEFAULT NOW(), | ||
"updated_at" TIMESTAMP NOT NULL DEFAULT NOW() | ||
); | ||
CREATE TRIGGER set_timestamp BEFORE | ||
UPDATE ON "orders" FOR EACH ROW EXECUTE PROCEDURE trigger_set_timestamp(); | ||
COMMIT; |
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
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
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { getAllLineItems } from '../../../database/line_items/lineItems.js'; | ||
import shopify from '../../../shopify.js'; | ||
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 draftOrdersWithLineItemMappings = await getAllLineItems(); | ||
|
||
const shopifyOrders = await findOrders(client, draftOrdersWithLineItemMappings.map(({draftOrderId}) => draftOrderId)); | ||
|
||
const allDfcOrders = await createBulkDfcOrderFromShopify(shopifyOrders, draftOrdersWithLineItemMappings, req.params.EnterpriseName); | ||
|
||
res.type('application/json') | ||
res.send(allDfcOrders); | ||
} | ||
|
||
export default getAllOrders |
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
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
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
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,12 +1,15 @@ | ||
import {createLineItems, getLineItemIdMappings} from '../../../database/line_items/lineItems.js' | ||
import {createOrUpdateLineItems, getLineItemIdMappings} from '../../../database/line_items/lineItems.js' | ||
import * as ids from '../controllers/shopify/ids.js' | ||
|
||
export async function persistLineIdMappings(shopifyDraftOrder) { | ||
|
||
const draftOrderId = ids.extract(shopifyDraftOrder.id); | ||
|
||
const mappings = shopifyDraftOrder.lineItems.edges.map(({node: lineItem}) => ({ | ||
id: lineItem.id, | ||
variantId: lineItem.variant.id | ||
id: ids.extract(lineItem.id), | ||
variantId: ids.extract(lineItem.variant.id) | ||
})); | ||
|
||
await createLineItems(shopifyDraftOrder.id, mappings); | ||
return await getLineItemIdMappings(shopifyDraftOrder.id); | ||
} | ||
await createOrUpdateLineItems(draftOrderId, mappings); | ||
return await getLineItemIdMappings(draftOrderId); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export function extract(shopifyId) { | ||
return shopifyId.substring(shopifyId.lastIndexOf('/') + 1); | ||
} | ||
|
||
export function variant(id) { | ||
return `gid://shopify/ProductVariant/${id}` | ||
} | ||
|
||
export function draftOrder(id){ | ||
return `gid://shopify/DraftOrder/${id}` | ||
} |
Oops, something went wrong.