Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import {
- User,
- Role,
- UserInformation,
- UserPreference,
- } from "../database/sequelize/index.js";
- export default {
- async getCurrentUser(req, res, next) {
- try {
- const user = await User.findOne({
- where: { id: req.userId },
- include: [
- { model: Role },
- { model: UserInformation, as: "userInformation" },
- { model: UserPreference, as: "userPreference" },
- ],
- });
- console.log("User found in userController:", user);
- if (!user) {
- return res.status(404).json({ message: "User not found" });
- }
- res.status(200).json(user);
- } catch (error) {
- console.error(error);
- next(error);
- }
- },
- async updateUserProfilePic(req, res, next) {
- try {
- const user = await User.findOne({
- where: { id: req.userId },
- });
- const userInformation = await UserInformation.findOne({
- where: { userId: req.userId },
- });
- if (!user || !userInformation) {
- return res.status(404).send({ message: "User not found" });
- }
- userInformation.profilePicture = `/uploads/${req.file.filename}`;
- await userInformation.save();
- res.status(200).json({
- message: "Profile picture updated successfully",
- user: user,
- });
- } catch (error) {
- console.error(error);
- next(error);
- }
- },
- async updateUserInfo(req, res, next) {
- try {
- const { username, email, address, phoneNumber, profilePicture } =
- req.body;
- // Mettre à jour l'utilisateur
- const user = await User.findOne({
- where: { id: req.userId },
- });
- if (!user) {
- return res.status(404).send({ message: "User not found" });
- }
- user.username = username;
- user.email = email;
- await user.save();
- // Mettre à jour les informations utilisateur
- const userInformation = await UserInformation.findOne({
- where: { userId: req.userId },
- });
- if (!userInformation) {
- return res.status(404).send({ message: "User information not found" });
- }
- userInformation.address = address;
- userInformation.phoneNumber = phoneNumber;
- userInformation.profilePicture = profilePicture;
- await userInformation.save();
- // Récupérer l'utilisateur avec les informations mises à jour
- const updatedUser = await User.findOne({
- where: { id: req.userId },
- include: [
- { model: Role },
- { model: UserInformation, as: "userInformation" },
- ],
- });
- res.status(200).json({
- message: "User information updated successfully",
- user: updatedUser,
- });
- } catch (error) {
- console.error(error);
- next(error);
- }
- },
- // Récupere les preferences de l'utilisateur
- async getUserPreferences(req, res, next) {
- try {
- const userPreference = await UserPreference.findOne({
- where: { userId: req.userId },
- });
- if (!userPreference) {
- return res.status(404).json({ message: "User preferences not found" });
- }
- res.status(200).json(userPreference);
- } catch (error) {
- console.error(error);
- next(error);
- }
- },
- async updateUserPreferences(req, res, next) {
- console.log("Request reached to updateUserPreferences controller");
- //TODO: gérer region, fishingType, et favouriteSpecies
- try {
- const { fishingLevel, hasFishingCard } = req.body;
- // Trouver ou créer les préférences utilisateur
- let [userPreference, created] = await UserPreference.findOrCreate({
- where: { userId: req.userId },
- defaults: {
- fishingLevel: fishingLevel,
- hasFishingCard: hasFishingCard,
- },
- });
- // Si les préférences de l'utilisateur existent déjà, mettez-les à jour
- if (!created) {
- userPreference.fishingLevel = fishingLevel;
- userPreference.hasFishingCard = hasFishingCard;
- await userPreference.save();
- }
- res.status(200).json({
- message: "User preferences updated successfully",
- userPreference: userPreference,
- });
- } catch (error) {
- console.error(error);
- next(error);
- }
- },
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement