Skip to content

Commit

Permalink
✨ add instant state update in decoy list
Browse files Browse the repository at this point in the history
  • Loading branch information
TarradeMarc committed Dec 17, 2024
1 parent 54f0bb9 commit c7c7c2c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
10 changes: 10 additions & 0 deletions controlpanel/api/routes/decoy.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,14 @@ router.put('/:id', async (req, res) => {
}
});

router.patch('/state', async (req, res) => {
try {
const result = await decoyService.updateDecoyState(req.body);
return res.status(result.code).send(result);
} catch(e) {
console.error(e);
return { type: 'error', code: 500, message: "Server error", data: e };
}
});

module.exports = router;
17 changes: 17 additions & 0 deletions controlpanel/api/services/decoy.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,22 @@ module.exports = {
} catch(e) {
throw e;
}
},
/**
* @param {JSON} decoyData
*/
updateDecoyState: async (decoyData) => {
try {
if (typeof decoyData != 'object') return { type: 'error', code: 422, message: 'Payload should be a json' };
if (!decoyData.id) return { type: 'error', code: 422, message: 'No decoy id provided' }
if (!isUUID(decoyData.id)) return { type: 'error', code: 400, message: 'Invalid decoy id supplied' };
if (!decoyData.state) return { type: 'error', code: 422, message: 'No state provided' };
if (!['active', 'inactive', 'error'].includes(decoyData.state)) return { type: 'error', code: 422, message: 'Invalid state provided' };
const updatedDecoy = await Decoy.update({ state: decoyData.state }, { where: { id: decoyData.id }});
if (!updatedDecoy['0']) return { type: 'success', code: 404, message: 'Decoy not found' };
return { type: 'success', code: 200, message: 'Successful operation' };
} catch(e) {
throw e;
}
}
}

0 comments on commit c7c7c2c

Please sign in to comment.