asdfasdf
This commit is contained in:
@@ -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); }
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user