Skip to content

Commit

Permalink
feat(api): add endpoint DELETE /users/:userId/profile
Browse files Browse the repository at this point in the history
  • Loading branch information
armandwipangestu committed Jun 6, 2024
1 parent dff6dd6 commit 02a4f6d
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 2 deletions.
45 changes: 43 additions & 2 deletions src/controller/users.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
getUserById,
getUserProfileByUserId,
removeUserById,
removeUserProfileByUserId,
} from "../service/users.service.js";
import { findUserProfileByUserId } from "../repository/users.repository.js";
import {
Expand Down Expand Up @@ -158,11 +159,51 @@ export const editUserProfilePartialFieldByUserIdHandler = async (req, res) => {
}
};

export const removeUserProfileByUserIdHandler = async (req, res) => {
try {
const userId = req.params.userId;

const userProfile = await getUserProfileByUserId(userId);
const userAvatar = userProfile?.avatar;
const user = await getUserById(userId, res);
const userGender = user.gender === "Laki_laki" ? "male" : "female";
const userGenderAvatarUrl = getPublicUrl(`${userGender}.png`);

const fileName = getFileNameFromUrl(userAvatar);
const maleAvatarUrl = getPublicUrl("male.png");
const femaleAvatarUrl = getPublicUrl("female.png");

if (
userAvatar &&
userAvatar !== maleAvatarUrl &&
userAvatar !== femaleAvatarUrl
) {
await deleteFileFromBucket(fileName);
}

await removeUserProfileByUserId(userId, res);
await editAvatarUser(userId, userGenderAvatarUrl);

res.status(200).send({
status: "success",
message: "Successfully delete user profile data",
data: {
id: userId,
},
});
} catch (error) {
if (error instanceof NotFoundError) {
return;
}
handleServerError(error, res);
}
};

export const removeUserByIdHandler = async (req, res) => {
try {
const userId = req.params.userId;

const userProfile = await findUserProfileByUserId(userId);
const userProfile = await getUserProfileByUserId(userId);
const userAvatar = userProfile?.avatar;

const fileName = getFileNameFromUrl(userAvatar);
Expand Down Expand Up @@ -224,7 +265,7 @@ export const editAvatarUserHandler = async (req, res) => {
imageUrl = req.file.cloudStoragePublicUrl;
}

const userProfile = await findUserProfileByUserId(userId);
const userProfile = await getUserProfileByUserId(userId);
const oldAvatar = userProfile?.avatar;

const fileName = getFileNameFromUrl(oldAvatar);
Expand Down
16 changes: 16 additions & 0 deletions src/repository/users.repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,22 @@ export const updateUserProfileByUserId = async (userId, userData) => {
return newUserProfile;
};

export const deleteUserProfileByUserId = async (userId) => {
const userProfile = await prisma.profile.update({
where: {
userId,
},
data: {
maritalStatus: null,
certifiedStatus: null,
workId: null,
educationId: null,
},
});

return userProfile;
};

export const createBlankProfileWithUserId = async (userId) => {
const user = await prisma.profile.create({
data: {
Expand Down
6 changes: 6 additions & 0 deletions src/routes/router/users.router.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
getUserByIdHandler,
getUserProfileByUserIdHandler,
removeUserByIdHandler,
removeUserProfileByUserIdHandler,
} from "../../controller/users.controller.js";
import {
uploadAvatar,
Expand Down Expand Up @@ -44,6 +45,11 @@ router.patch(
verifyAccessTokenHandler,
editUserProfilePartialFieldByUserIdHandler
);
router.delete(
"/users/:userId/profile",
verifyAccessTokenHandler,
removeUserProfileByUserIdHandler
);
router.put(
"/users/:userId/profile/avatar",
verifyAccessTokenHandler,
Expand Down
8 changes: 8 additions & 0 deletions src/service/users.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { handleNotFoundError } from "../exceptions/client.exception.js";
import {
createBlankProfileWithUserId,
deleteUserById,
deleteUserProfileByUserId,
findUserByEmail,
findUserById,
findUserByRefreshToken,
Expand Down Expand Up @@ -76,6 +77,13 @@ export const editUserProfileByUserId = async (userId, userData, res) => {
return newUserProfile;
};

export const removeUserProfileByUserId = async (userId, res) => {
await getUserById(userId, res);
const profile = await deleteUserProfileByUserId(userId);

return profile;
};

export const editAvatarUser = async (userId, imageUrl, res) => {
await getUserById(userId, res);
const userProfile = await findUserProfileByUserId(userId);
Expand Down

0 comments on commit 02a4f6d

Please sign in to comment.