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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | const { sequelize, models } = require("../config/sequelize");
const { ApiError } = require("../utils/errorHandler");
const bcrypt = require("bcrypt");
const jwt = require("jsonwebtoken");
const config = require("../config/config");
const logger = require("../utils/logger");
/**
* Create User Service
*/
exports.createUser = async (
first_name,
last_name,
email,
phone_number,
company_name,
password,
) => {
const existingUser = await models.User.findOne({
where: { email },
paranoid: false,
});
Eif (existingUser) {
throw new ApiError(
existingUser.deleted_at
? "This email is associated with a deleted account. Please contact support."
: "User already exists with this email",
400,
);
}
return models.User.create({
first_name,
last_name,
phone_number,
email,
password,
company_name,
});
};
exports.getUserProfile = async (userId) => {
const user = await models.User.findByPk(userId, {
attributes: {
exclude: ["password"], // Exclude sensitive information
},
});
if (!user) {
throw new ApiError("User not found", 404);
}
return user;
};
exports.updateUserProfile = async (userId, updatedData = {}) => {
if (typeof userId !== "number" || isNaN(userId)) {
throw new ApiError("Invalid user ID", 400);
}
const ALLOWED_FIELDS = Object.freeze([
"first_name",
"last_name",
"phone_number",
"company_name",
"profile",
"email",
]);
const filteredUpdates = {};
for (const key of ALLOWED_FIELDS) {
if (updatedData[key] !== undefined) {
filteredUpdates[key] = updatedData[key];
}
}
if (Object.keys(filteredUpdates).length === 0) {
throw new ApiError("No valid fields provided for update", 400);
}
try {
await models.User.update(filteredUpdates, {
where: { id: userId },
});
const updatedUser = await models.User.findByPk(userId, {
attributes: { exclude: ["password"] },
});
if (!updatedUser) {
throw new ApiError("User not found", 404);
}
return updatedUser.get({ plain: true });
} catch (error) {
if (error.name === "SequelizeValidationError") {
throw new ApiError(`Validation error: ${error.message}`, 400);
}
throw error;
}
};
exports.changeUserPassword = async (userId, currentPassword, newPassword) => {
try {
const user = await models.User.findByPk(userId);
if (!user) {
throw new ApiError("User not found", 404);
}
const isMatch = await bcrypt.compare(currentPassword, user.password);
if (!isMatch) {
throw new ApiError("Current password is incorrect", 401);
}
const salt = await bcrypt.genSalt(10);
const hashedPassword = await bcrypt.hash(newPassword, salt);
await models.User.update(
{ password: hashedPassword },
{ where: { id: userId } },
);
const token = jwt.sign({ userId: user.id }, config.jwt.secret, {
expiresIn: config.jwt.expiration,
});
return {
message: "Password changed successfully",
token,
};
} catch (error) {
throw error;
}
};
exports.deleteUserAccount = async (userId) => {
const transaction = await sequelize.transaction();
try {
const user = await models.User.findByPk(userId, { transaction });
if (!user) {
throw new ApiError("User not found", 404);
}
await models.ApiKey.destroy({ where: { user_id: userId }, transaction });
await models.User.destroy({ where: { id: userId }, transaction });
await transaction.commit();
return {
userId,
email: user.email,
deletedAt: new Date(),
};
} catch (error) {
await transaction.rollback();
throw error;
}
};
|