fix: send push notification when stock changes from 0 to available #52
@@ -2055,6 +2055,12 @@ app.put('/api/admin/pharmacy-medicines/:id', requireAdmin, async (req, res) => {
|
||||
const id = parseInt(req.params.id);
|
||||
const { price, stock } = req.body;
|
||||
|
||||
// Get current state before update to detect stock becoming available
|
||||
const current = await userDbGet(
|
||||
'SELECT * FROM pharmacy_medicines WHERE id = ?',
|
||||
[id]
|
||||
);
|
||||
|
||||
await userDbRun(
|
||||
'UPDATE pharmacy_medicines SET price = ?, stock = ? WHERE id = ?',
|
||||
[price || null, stock || 0, id]
|
||||
@@ -2069,6 +2075,16 @@ app.put('/api/admin/pharmacy-medicines/:id', requireAdmin, async (req, res) => {
|
||||
return res.status(404).json({ error: 'Relationship not found' });
|
||||
}
|
||||
|
||||
// Send push if stock went from 0 to available
|
||||
if (current && (current.stock || 0) === 0 && (stock || 0) > 0) {
|
||||
const pharmacy = await userDbGet('SELECT id, name FROM pharmacies WHERE id = ?', [updated.pharmacy_id]);
|
||||
sendPushForMedicine({
|
||||
medicine_nregistro: updated.medicine_nregistro,
|
||||
medicine_name: updated.medicine_name,
|
||||
pharmacy,
|
||||
}).catch(err => console.error('[push] stock-change fanout error:', err));
|
||||
}
|
||||
|
||||
res.json(updated);
|
||||
appMetrics.pharmacyMedicineLinkTotal.add(1, { op: 'update' });
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user