Advertisement
krle997

Untitled

Jul 19th, 2024
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. exercise.delete("/cant-do-exercises", verifyJwt, async (req, res) => {
  2.   const {exerciseId, userId} = req.body;
  3.   const currentUserId = req.userId;
  4.  
  5.   if (!exerciseId || !userId) {
  6.     res.statusCode = 422;
  7.     return res.json({
  8.       data: null,
  9.       error: {message: "Required query parameter [exerciseId|userId] is missing."}
  10.     });
  11.   }
  12.  
  13.   if (currentUserId !== userId) {
  14.     try {
  15.       if (!await trainerHasClient(currentUserId, userId)) {
  16.         return res.json({
  17.           data: null,
  18.           error: {message: "No authorization to do this."}
  19.         });
  20.       }
  21.     } catch (error) {
  22.       return res.json({
  23.         data: null,
  24.         error: {message: error.message}
  25.       });
  26.     }
  27.   }
  28.  
  29.   try {
  30.     await knexDb("cant_do_exercises")
  31.       .where("exerciseId", exerciseId)
  32.       .andWhere("userId", userId)
  33.       .delete();
  34.  
  35.     return res.json({
  36.       data: {exerciseId},
  37.       error: null
  38.     });
  39.   } catch (error) {
  40.     return res.json({
  41.       data: null,
  42.       error: {message: error.message}
  43.     });
  44.   }
  45. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement