This commit is contained in:
Jake Kasper
2025-09-05 11:11:53 -04:00
parent 7789eadb61
commit c517b28f51
2 changed files with 61 additions and 9 deletions

View File

@@ -184,13 +184,27 @@ router.put('/points/:id', async (req, res, next) => {
router.delete('/points/:id', async (req,res,next)=>{
try {
const pointId = req.params.id;
const own = await pool.query(
`SELECT wpp.id FROM watering_plan_points wpp
// Verify and fetch plan id for resequencing
const chk = await pool.query(
`SELECT wpp.plan_id FROM watering_plan_points wpp
JOIN watering_plans wp ON wpp.plan_id = wp.id
WHERE wpp.id=$1 AND wp.user_id=$2`, [pointId, req.user.id]
);
if (own.rows.length === 0) throw new AppError('Point not found', 404);
if (chk.rows.length === 0) throw new AppError('Point not found', 404);
const planId = chk.rows[0].plan_id;
await pool.query('DELETE FROM watering_plan_points WHERE id=$1', [pointId]);
// Resequence remaining points for the plan
await pool.query(
`WITH ordered AS (
SELECT id, ROW_NUMBER() OVER (ORDER BY sequence, id) AS rn
FROM watering_plan_points WHERE plan_id=$1
)
UPDATE watering_plan_points w
SET sequence = o.rn
FROM ordered o
WHERE w.id = o.id`,
[planId]
);
res.json({ success:true });
} catch (e) { next(e); }
});