feat: order get requests

This commit is contained in:
2025-11-11 23:58:03 -07:00
parent 5ddedadd24
commit b6a08573f3
9 changed files with 382 additions and 22 deletions

View File

@@ -71,7 +71,7 @@ SELECT
iph.price AS unit_price
FROM
order_item oi
INNER JOIN item i ON oi.item_id = i.init_db_pool
INNER JOIN item i ON oi.item_id = i.id
AND oi.order_id = $1
INNER JOIN item_price_history iph ON iph.item_id = i.id
AND iph.valid_from <= oi.created_at

View File

@@ -4,24 +4,6 @@ const CREATE_ORDER = `
INSERT INTO order_record(user_id, orderer, date_due, date_placed) VALUES ($1, $2, $3, $4);
`
const GET_ORDER_BY_ORDER_INFORMATION = `
SELECT
id,
user_id,
orderer,
date_due,
date_placed,
amount_paid,
filled,
delivered
FROM
order_record
WHERE
user_id = $1
AND orderer = $2
AND date_placed = $3;
`
const SET_ORDER_FILLED = `
UPDATE order_record SET filled = $1 WHERE id = $2
`
@@ -37,6 +19,7 @@ UPDATE order_record SET amount_paid = $1 WHERE id = $2
const GET_ORDER_TOTAL_AND_BALANCE = `
SELECT
orec.id,
order_total,
order_total - orec.amount_paid AS balance
FROM
order_record orec
@@ -48,10 +31,135 @@ FROM
order_record orec
INNER JOIN order_item oi ON oi.order_id = orec.id
INNER JOIN item i ON oi.item_id = i.id
AND oi.order_id = 1
AND oi.order_id = $1
INNER JOIN item_price_history iph ON iph.item_id = i.id
AND iph.valid_from <= oi.created_at
AND (iph.valid_to IS NULL OR iph.valid_to > oi.created_at)
GROUP BY orec.id
) totals ON orec.id = totals.id
`
const GET_TOTAL_ORDER_FROM_ORDER_INFORMATION = `
WITH order_bill AS (
SELECT
orec.id,
COALESCE(order_total, 0) AS order_total,
COALESCE(order_total - orec.amount_paid, 0) AS balance
FROM
order_record orec
INNER JOIN (
SELECT
orec.id,
SUM(quantity * price) AS order_total
FROM
order_record orec
LEFT JOIN order_item oi ON oi.order_id = orec.id
LEFT JOIN item i ON oi.item_id = i.id
LEFT JOIN item_price_history iph ON iph.item_id = i.id
AND iph.valid_from <= oi.created_at
AND (iph.valid_to IS NULL OR iph.valid_to > oi.created_at)
WHERE
orec.user_id = $1
AND orec.orderer = $2
AND orec.date_place = $3
GROUP BY orec.id
) totals ON orec.id = totals.id
)
SELECT
orec.id,
user_id,
orderer,
date_due,
date_placed,
amount_paid,
order_total,
balance,
filled,
delivered
FROM
order_record orec
INNER JOIN order_bill ON order_bill.id = orec.id;
`
const GET_TOTAL_ORDER_FROM_ORDER_ID = `
WITH order_bill AS (
SELECT
orec.id,
COALESCE(order_total, 0) AS order_total,
COALESCE(order_total - orec.amount_paid, 0) AS balance
FROM
order_record orec
INNER JOIN (
SELECT
orec.id,
SUM(quantity * price) AS order_total
FROM
order_record orec
LEFT JOIN order_item oi ON oi.order_id = orec.id
LEFT JOIN item i ON oi.item_id = i.id
LEFT JOIN item_price_history iph ON iph.item_id = i.id
AND iph.valid_from <= oi.created_at
AND (iph.valid_to IS NULL OR iph.valid_to > oi.created_at)
WHERE orec.id = $1
GROUP BY orec.id
) totals ON orec.id = totals.id
)
SELECT
orec.id,
user_id,
orderer,
date_due,
date_placed,
amount_paid,
order_total,
balance,
filled,
delivered
FROM
order_record orec
INNER JOIN order_bill ON order_bill.id = orec.id;
`
const GET_ORDER_TABLE = `
WITH order_bill AS (
SELECT
orec.id,
COALESCE(order_total, 0) AS order_total,
COALESCE(order_total - orec.amount_paid, 0) AS balance
FROM
order_record orec
INNER JOIN (
SELECT
orec.id,
SUM(quantity * price) AS order_total
FROM
order_record orec
LEFT JOIN order_item oi ON oi.order_id = orec.id
LEFT JOIN item i ON oi.item_id = i.id
LEFT JOIN item_price_history iph ON iph.item_id = i.id
AND iph.valid_from <= oi.created_at
AND (iph.valid_to IS NULL OR iph.valid_to > oi.created_at)
GROUP BY orec.id
) totals ON orec.id = totals.id
)
SELECT
orec.id,
user_id,
orderer,
date_due,
date_placed,
amount_paid,
order_total,
balance,
filled,
delivered
FROM
order_record orec
INNER JOIN order_bill ON order_bill.id = orec.id
ORDER BY date_due DESC
OFFSET $1
LIMIT $2;
`