Files

22 lines
678 B
JavaScript

const { pool } = require('./src/db/pool');
async function test() {
const query = `
SELECT
p.id as product_id,
p.brand_name,
d.generic_name,
(SELECT COALESCE(SUM(qty_on_hand), 0) FROM batches WHERE product_id = p.id AND expiry > CURRENT_DATE AND tenant_id = $2) as total_stock
FROM products p
JOIN drugs d ON p.drug_id = d.id
WHERE p.tenant_id = $2 OR EXISTS (SELECT 1 FROM batches WHERE product_id = p.id AND tenant_id = $2) OR p.tenant_id IS NULL
ORDER BY p.brand_name ASC
LIMIT $1
`;
const { rows } = await pool.query(query, [50, 'act_admin']);
console.log(rows);
process.exit();
}
test();