Advertisement
codhank

Untitled

May 28th, 2023
602
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import {
  2.   User,
  3.   Role,
  4.   UserInformation,
  5.   UserPreference,
  6. } from "../database/sequelize/index.js";
  7.  
  8. export default {
  9.   async getCurrentUser(req, res, next) {
  10.     try {
  11.       const user = await User.findOne({
  12.         where: { id: req.userId },
  13.         include: [
  14.           { model: Role },
  15.           { model: UserInformation, as: "userInformation" },
  16.           { model: UserPreference, as: "userPreference" },
  17.         ],
  18.       });
  19.  
  20.       console.log("User found in userController:", user);
  21.       if (!user) {
  22.         return res.status(404).json({ message: "User not found" });
  23.       }
  24.  
  25.       res.status(200).json(user);
  26.     } catch (error) {
  27.       console.error(error);
  28.       next(error);
  29.     }
  30.   },
  31.  
  32.  
  33.   async updateUserProfilePic(req, res, next) {
  34.     try {
  35.       const user = await User.findOne({
  36.         where: { id: req.userId },
  37.       });
  38.  
  39.       const userInformation = await UserInformation.findOne({
  40.         where: { userId: req.userId },
  41.       });
  42.  
  43.       if (!user || !userInformation) {
  44.         return res.status(404).send({ message: "User not found" });
  45.       }
  46.  
  47.       userInformation.profilePicture = `/uploads/${req.file.filename}`;
  48.  
  49.       await userInformation.save();
  50.  
  51.       res.status(200).json({
  52.         message: "Profile picture updated successfully",
  53.         user: user,
  54.       });
  55.     } catch (error) {
  56.       console.error(error);
  57.       next(error);
  58.     }
  59.   },
  60.  
  61.   async updateUserInfo(req, res, next) {
  62.     try {
  63.       const { username, email, address, phoneNumber, profilePicture } =
  64.         req.body;
  65.  
  66.       // Mettre à jour l'utilisateur
  67.       const user = await User.findOne({
  68.         where: { id: req.userId },
  69.       });
  70.  
  71.       if (!user) {
  72.         return res.status(404).send({ message: "User not found" });
  73.       }
  74.  
  75.       user.username = username;
  76.       user.email = email;
  77.  
  78.       await user.save();
  79.  
  80.       // Mettre à jour les informations utilisateur
  81.       const userInformation = await UserInformation.findOne({
  82.         where: { userId: req.userId },
  83.       });
  84.  
  85.       if (!userInformation) {
  86.         return res.status(404).send({ message: "User information not found" });
  87.       }
  88.  
  89.       userInformation.address = address;
  90.       userInformation.phoneNumber = phoneNumber;
  91.       userInformation.profilePicture = profilePicture;
  92.  
  93.       await userInformation.save();
  94.  
  95.       // Récupérer l'utilisateur avec les informations mises à jour
  96.       const updatedUser = await User.findOne({
  97.         where: { id: req.userId },
  98.         include: [
  99.           { model: Role },
  100.           { model: UserInformation, as: "userInformation" },
  101.         ],
  102.       });
  103.  
  104.       res.status(200).json({
  105.         message: "User information updated successfully",
  106.         user: updatedUser,
  107.       });
  108.     } catch (error) {
  109.       console.error(error);
  110.       next(error);
  111.     }
  112.   },
  113.  
  114.   // Récupere les preferences de l'utilisateur
  115.   async getUserPreferences(req, res, next) {
  116.     try {
  117.       const userPreference = await UserPreference.findOne({
  118.         where: { userId: req.userId },
  119.       });
  120.  
  121.       if (!userPreference) {
  122.         return res.status(404).json({ message: "User preferences not found" });
  123.       }
  124.  
  125.       res.status(200).json(userPreference);
  126.     } catch (error) {
  127.       console.error(error);
  128.       next(error);
  129.     }
  130.   },
  131.  
  132.  
  133.   async updateUserPreferences(req, res, next) {
  134.     console.log("Request reached to updateUserPreferences controller");
  135.     //TODO: gérer region, fishingType, et favouriteSpecies
  136.     try {
  137.       const { fishingLevel, hasFishingCard } = req.body;
  138.  
  139.       // Trouver ou créer les préférences utilisateur
  140.       let [userPreference, created] = await UserPreference.findOrCreate({
  141.         where: { userId: req.userId },
  142.         defaults: {
  143.           fishingLevel: fishingLevel,
  144.           hasFishingCard: hasFishingCard,
  145.         },
  146.       });
  147.  
  148.       // Si les préférences de l'utilisateur existent déjà, mettez-les à jour
  149.       if (!created) {
  150.         userPreference.fishingLevel = fishingLevel;
  151.         userPreference.hasFishingCard = hasFishingCard;
  152.  
  153.         await userPreference.save();
  154.       }
  155.  
  156.       res.status(200).json({
  157.         message: "User preferences updated successfully",
  158.         userPreference: userPreference,
  159.       });
  160.     } catch (error) {
  161.       console.error(error);
  162.       next(error);
  163.     }
  164.   },  
  165. };
  166.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement