feat: Most of Item and Order controllers

This commit is contained in:
2025-11-10 20:32:23 -07:00
parent 6313a85e96
commit c81b181515
12 changed files with 421 additions and 28 deletions

View File

@@ -10,13 +10,23 @@ INSERT INTO item_price_history(item_id, price) VALUES ($1, $2);
const SET_ITEM_PRICE_VALID_TO_DATE = `
UPDATE item_price_history SET valid_to = now()
FROM (
SELECT item_id, MAX(valid_from) FROM item_price_history WHERE item_id = $1
) as o
WHERE item_id IN o.item_id
WHERE item_id IN (
SELECT item_id FROM item_price_history WHERE item_id = $1 ORDER BY valid_from DESC LIMIT 1
)
`
const GET_ITEM_PRICE = `
const GET_CURRENT_ITEM_PRICE = `
SELECT
i.id,
i.item_name,
iph.price AS unit_price
FROM item i
INNER JOIN item_price_history iph ON iph.item_id = i.id
AND i.id = $1
ORDER BY iph.valid_from DESC
LIMIT 1;
`
const GET_ORDER_ITEM_PRICE = `
SELECT
oi.item_id,
oi.order_id,

View File

@@ -4,6 +4,24 @@ 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
`
@@ -17,15 +35,23 @@ UPDATE order_record SET amount_paid = $1 WHERE id = $2
`
const GET_ORDER_TOTAL_AND_BALANCE = `
SELECT
SUM(oi.quantity * iph.price) AS order_total,
SELECT
orec.id,
order_total - orec.amount_paid AS balance
FROM
order_item oi
INNER JOIN item i ON oi.item_id = i.init_db_pool
AND oi.order_id = $1
INNER JOIN order_record orec ON oi.order_id = orec.order_id
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);
order_record orec
INNER JOIN (
SELECT
orec.id,
SUM(quantity * price) AS order_total
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
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
`

View File

@@ -41,8 +41,8 @@ SELECT
ordr_user.id,
user_name,
position_name,
active,
is_admin
active::boolean,
is_admin::boolean
FROM
ordr_user
INNER JOIN ordr_position