Files
AI_Pharmacy_Api/src/modules/inventory/supplierRouter.ts
T
2026-08-03 10:22:42 +05:30

39 lines
1.3 KiB
TypeScript

import { Router } from 'express';
import { requireAuth } from '../../middleware/auth';
import { pool } from '../../db/pool';
const router = Router();
// GET /v1/inventory/suppliers
router.get('/suppliers', requireAuth, async (req, res, next) => {
try {
const { rows } = await pool.query('SELECT * FROM suppliers WHERE tenant_id = $1 ORDER BY name ASC', [req.actorId]);
res.status(200).json({ suppliers: rows });
} catch (error) {
next(error);
}
});
// POST /v1/inventory/suppliers
router.post('/suppliers', requireAuth, async (req, res, next) => {
try {
const { name, contact_number, email, gstin, dl_number, address, contact_person } = req.body;
if (!name) {
return res.status(400).json({ error: 'Name is required' });
}
const id = 'sup_' + Date.now() + Math.floor(Math.random() * 1000);
await pool.query(`
INSERT INTO suppliers (id, tenant_id, name, contact_number, email, gstin, dl_number, address, contact_person)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
`, [id, req.actorId, name, contact_number, email, gstin, dl_number, address, contact_person]);
res.status(201).json({ id, name, contact_number, email, gstin, dl_number, address, contact_person });
} catch (error) {
next(error);
}
});
export default router;