131 lines
3.9 KiB
JavaScript
131 lines
3.9 KiB
JavaScript
const pool = require("../db/pool");
|
|
|
|
|
|
exports.getUnboughtItems = async () => {
|
|
const result = await pool.query(
|
|
`SELECT
|
|
gl.id,
|
|
LOWER(gl.item_name) AS item_name,
|
|
gl.quantity,
|
|
gl.bought,
|
|
ENCODE(gl.item_image, 'base64') as item_image,
|
|
gl.image_mime_type,
|
|
ARRAY_AGG(DISTINCT COALESCE(gh_user.name, creator.name)) FILTER (WHERE COALESCE(gh_user.name, creator.name) IS NOT NULL) as added_by_users,
|
|
gl.modified_on as last_added_on
|
|
FROM grocery_list gl
|
|
LEFT JOIN users creator ON gl.added_by = creator.id
|
|
LEFT JOIN grocery_history gh ON gl.id = gh.list_item_id
|
|
LEFT JOIN users gh_user ON gh.added_by = gh_user.id
|
|
WHERE gl.bought = FALSE
|
|
GROUP BY gl.id, gl.item_name, gl.quantity, gl.bought, gl.item_image, gl.image_mime_type, gl.modified_on
|
|
ORDER BY gl.id ASC`
|
|
);
|
|
return result.rows;
|
|
};
|
|
|
|
exports.getItemByName = async (itemName) => {
|
|
const result = await pool.query(
|
|
"SELECT * FROM grocery_list WHERE item_name ILIKE $1",
|
|
[itemName]
|
|
);
|
|
|
|
return result.rows[0] || null;
|
|
};
|
|
|
|
|
|
exports.addOrUpdateItem = async (itemName, quantity, userId, imageBuffer = null, mimeType = null) => {
|
|
const result = await pool.query(
|
|
"SELECT id, bought FROM grocery_list WHERE item_name ILIKE $1",
|
|
[itemName]
|
|
);
|
|
|
|
if (result.rowCount > 0) {
|
|
// Update existing item
|
|
if (imageBuffer && mimeType) {
|
|
await pool.query(
|
|
`UPDATE grocery_list
|
|
SET quantity = $1,
|
|
bought = FALSE,
|
|
item_image = $3,
|
|
image_mime_type = $4,
|
|
modified_on = NOW()
|
|
WHERE id = $2`,
|
|
[quantity, result.rows[0].id, imageBuffer, mimeType]
|
|
);
|
|
} else {
|
|
await pool.query(
|
|
`UPDATE grocery_list
|
|
SET quantity = $1,
|
|
bought = FALSE,
|
|
modified_on = NOW()
|
|
WHERE id = $2`,
|
|
[quantity, result.rows[0].id]
|
|
);
|
|
}
|
|
return result.rows[0].id;
|
|
} else {
|
|
// Insert new item
|
|
const insert = await pool.query(
|
|
`INSERT INTO grocery_list
|
|
(item_name, quantity, added_by, item_image, image_mime_type)
|
|
VALUES ($1, $2, $3, $4, $5) RETURNING id`,
|
|
[itemName, quantity, userId, imageBuffer, mimeType]
|
|
);
|
|
return insert.rows[0].id;
|
|
}
|
|
};
|
|
|
|
|
|
exports.setBought = async (id, userId) => {
|
|
await pool.query(
|
|
"UPDATE grocery_list SET bought = TRUE, modified_on = NOW() WHERE id = $1",
|
|
[id]
|
|
);
|
|
};
|
|
|
|
|
|
exports.addHistoryRecord = async (itemId, quantity, userId) => {
|
|
await pool.query(
|
|
`INSERT INTO grocery_history (list_item_id, quantity, added_by, added_on)
|
|
VALUES ($1, $2, $3, NOW())`,
|
|
[itemId, quantity, userId]
|
|
);
|
|
};
|
|
|
|
|
|
exports.getSuggestions = async (query) => {
|
|
const result = await pool.query(
|
|
`SELECT DISTINCT item_name
|
|
FROM grocery_list
|
|
WHERE item_name ILIKE $1
|
|
LIMIT 10`,
|
|
[`%${query}%`]
|
|
);
|
|
res = result.rows;
|
|
return result.rows;
|
|
};
|
|
|
|
exports.getRecentlyBoughtItems = async () => {
|
|
const result = await pool.query(
|
|
`SELECT
|
|
gl.id,
|
|
LOWER(gl.item_name) AS item_name,
|
|
gl.quantity,
|
|
gl.bought,
|
|
ENCODE(gl.item_image, 'base64') as item_image,
|
|
gl.image_mime_type,
|
|
ARRAY_AGG(DISTINCT COALESCE(gh_user.name, creator.name)) FILTER (WHERE COALESCE(gh_user.name, creator.name) IS NOT NULL) as added_by_users,
|
|
gl.modified_on as last_added_on
|
|
FROM grocery_list gl
|
|
LEFT JOIN users creator ON gl.added_by = creator.id
|
|
LEFT JOIN grocery_history gh ON gl.id = gh.list_item_id
|
|
LEFT JOIN users gh_user ON gh.added_by = gh_user.id
|
|
WHERE gl.bought = TRUE
|
|
AND gl.modified_on >= NOW() - INTERVAL '24 hours'
|
|
GROUP BY gl.id, gl.item_name, gl.quantity, gl.bought, gl.item_image, gl.image_mime_type, gl.modified_on
|
|
ORDER BY gl.modified_on DESC`
|
|
);
|
|
return result.rows;
|
|
};
|
|
|