← back to AbramsOS

routes/prescriptions.js

35 lines

// Prescriptions — pharmacy FILL HISTORY viewer (read-only). Rows come from prescription_fill,
// imported from a pharmacy "PAT PROFILE FOR TAX" summary. Health data: display only.
const express = require('express');
const db = require('../lib/db');

const router = express.Router();
const DEV_USER_ID = 'user_steve';

async function load(userId) {
  const fills = (await db.query(
    `SELECT pf.*, p.full_name AS person_name
       FROM prescription_fill pf LEFT JOIN person p ON p.id = pf.person_id
      WHERE pf.user_id = $1
      ORDER BY pf.fill_date DESC NULLS LAST, pf.drug_name ASC`, [userId])).rows;
  const totals = (await db.query(
    `SELECT coalesce(p.full_name,'—') AS person, count(*)::int AS fills,
            coalesce(sum(pf.plan_paid),0)::numeric(12,2)    AS plan_paid,
            coalesce(sum(pf.patient_paid),0)::numeric(12,2) AS patient_paid,
            min(pf.fill_date) AS first_fill, max(pf.fill_date) AS last_fill
       FROM prescription_fill pf LEFT JOIN person p ON p.id = pf.person_id
      WHERE pf.user_id = $1
      GROUP BY p.full_name ORDER BY 1`, [userId])).rows;
  return { fills, totals };
}

router.get('/prescriptions', async (_req, res) => {
  res.render('prescriptions', await load(DEV_USER_ID));
});

router.get('/api/prescriptions', async (_req, res) => {
  res.json(await load(DEV_USER_ID));
});

module.exports = router;