All files / postcardotp-backend/src/controllers userController.js

23.8% Statements 10/42
0% Branches 0/14
0% Functions 0/4
23.8% Lines 10/42

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 891x 1x 1x 1x 1x   1x   1x                           1x                                                 1x                                                   1x                              
const successResponse = require("../utils/successResponse");
const userService = require("../services/userService");
const { ApiError } = require("../utils/errorHandler");
const { models } = require("../config/sequelize");
const logger = require("../utils/logger");
 
require("dotenv").config();
 
exports.getProfile = async (req, res, next) => {
  try {
    const userProfile = await userService.getUserProfile(req.userId);
    successResponse(
      res,
      userProfile,
      "User profile retrieved successfully",
      200,
    );
  } catch (error) {
    next(error);
  }
};
 
exports.updateProfile = async (req, res, next) => {
  try {
    const userId = req.userId;
    const { first_name, last_name, email, company_name } = req.body || {};
 
    let updatedData = { first_name, last_name, email, company_name };
    // Handle file upload if present
    if (req.file?.path) {
      updatedData.profile = `${process.env.STATIC_FILES_URL}/${req.file.originalname}`;
    }
 
    if (Object.keys(updatedData).length === 0) {
      throw new ApiError("No data provided for update", 400);
    }
 
    const updatedProfile = await userService.updateUserProfile(
      userId,
      updatedData,
    );
    successResponse(res, updatedProfile, "Profile updated successfully", 200);
  } catch (error) {
    next(error);
  }
};
 
exports.changePassword = async (req, res, next) => {
  try {
    const { currentPassword, newPassword } = req.body;
 
    if (!currentPassword || !newPassword) {
      throw new ApiError("Both current and new password are required", 400);
    }
 
    if (currentPassword === newPassword) {
      throw new ApiError(
        "New password must be different from current password",
        400,
      );
    }
    const passwordReset = await userService.changeUserPassword(
      req.userId,
      currentPassword,
      newPassword,
    );
 
    successResponse(res, passwordReset, "Password changed successfully", 200);
  } catch (error) {
    next(error);
  }
};
 
exports.deleteAccount = async (req, res, next) => {
  try {
    const userId = req.userId;
    // Verify user exists first
    const user = await models.User.findByPk(userId);
    if (!user) {
      throw new ApiError("User not found", 404);
    }
    const userdetails = await userService.deleteUserAccount(userId);
    successResponse(res, userdetails, "Account deleted successfully", 200);
  } catch (error) {
    //logger.error(`Account deletion failed for user ${userId}: ${error.message}`);
    next(error);
  }
};