Skip to content

Commit

Permalink
Merge pull request #12 from Finboost/dev-arman
Browse files Browse the repository at this point in the history
[API]: Finish /users/:userId/profile
  • Loading branch information
armandwipangestu authored Jun 6, 2024
2 parents eedef2c + 02a4f6d commit 0ac03d7
Show file tree
Hide file tree
Showing 6 changed files with 200 additions and 186 deletions.
184 changes: 2 additions & 182 deletions openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -1201,188 +1201,6 @@
}
]
},
"post": {
"tags": ["User", "Profile"],
"summary": "Insert user profile data by user id",
"description": "Insert specific user profile by user id. Requires valid access token",
"parameters": [
{
"$ref": "#/components/parameters/UserId"
}
],
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/InsertAllFieldProfile"
},
"examples": {
"User 1": {
"description": "Example request to insert user profile",
"value": {
"maritalStatus": "Menikah",
"certifiedStatus": null,
"workId": "319defd8-1836-4b9d-af2d-655a43a60bde",
"educationId": "7fe46a1f-fb32-4e36-9741-d96f22cf4b4b"
}
},
"User 2": {
"description": "Example request to insert user profile",
"value": {
"maritalStatus": "Cerai",
"certifiedStatus": "Certified Financial Planner",
"workId": "dc4aa7d6-7cbf-4789-a7a6-5fdc9c1ca642",
"educationId": "b5fbeaee-812f-4ec3-b807-22dd2794e09a"
}
}
}
}
}
},
"responses": {
"201": {
"description": "Successfully updated user profile data",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/SuccessSignupOrRegisUser"
},
"examples": {
"User 1": {
"description": "Successfully updated user data",
"value": {
"status": "success",
"message": "User profile data updated successfully",
"data": {
"id": "d969ee14-3362-46cd-ace9-af4d7b131733"
}
}
},
"User 2": {
"description": "Successfully updated user data",
"value": {
"status": "success",
"message": "User profile data updated successfully",
"data": {
"id": "f5e05c30-99ff-4693-a0aa-bc1e869dd3e5"
}
}
}
}
}
}
},
"401": {
"$ref": "#/components/responses/MiddlewareUnauthorizedError"
},
"403": {
"$ref": "#/components/responses/MiddlewareForbiddenError"
},
"404": {
"$ref": "#/components/responses/ErrorUserNotFound"
},
"500": {
"$ref": "#/components/responses/InternalServerError"
}
},
"security": [
{
"bearerAuth": []
}
]
},
"put": {
"tags": ["User", "Profile"],
"summary": "Update user profile data by user id",
"description": "Update specific user profile by user id. Requires valid access token",
"parameters": [
{
"$ref": "#/components/parameters/UserId"
}
],
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/UpdateAllFieldProfile"
},
"examples": {
"User 1": {
"description": "Example request to update user profile",
"value": {
"maritalStatus": "Menikah",
"certifiedStatus": null,
"workId": "72608273-18c1-4924-b67f-d67225e73faa",
"educationId": "b5fbeaee-812f-4ec3-b807-22dd2794e09a"
}
},
"User 2": {
"description": "Example request to update user profile",
"value": {
"maritalStatus": "Cerai",
"certifiedStatus": "Certified Financial Planner",
"workId": "dc4aa7d6-7cbf-4789-a7a6-5fdc9c1ca642",
"educationId": "bd2bb42f-d42e-4b7d-a3f4-221a867d1836"
}
}
}
}
}
},
"responses": {
"200": {
"description": "Successfully updated user profile data",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/SuccessSignupOrRegisUser"
},
"examples": {
"User 1": {
"description": "Successfully updated user data",
"value": {
"status": "success",
"message": "User profile data updated successfully",
"data": {
"id": "d969ee14-3362-46cd-ace9-af4d7b131733"
}
}
},
"User 2": {
"description": "Successfully updated user data",
"value": {
"status": "success",
"message": "User profile data updated successfully",
"data": {
"id": "f5e05c30-99ff-4693-a0aa-bc1e869dd3e5"
}
}
}
}
}
}
},
"401": {
"$ref": "#/components/responses/MiddlewareUnauthorizedError"
},
"403": {
"$ref": "#/components/responses/MiddlewareForbiddenError"
},
"404": {
"$ref": "#/components/responses/ErrorUserNotFound"
},
"500": {
"$ref": "#/components/responses/InternalServerError"
}
},
"security": [
{
"bearerAuth": []
}
]
},
"patch": {
"tags": ["User", "Profile"],
"summary": "Partial update user profile data by user id",
Expand All @@ -1404,6 +1222,7 @@
"description": "Example request to partially update user profile",
"value": {
"maritalStatus": "Lajang",
"certifiedStatus": null,
"workId": "319defd8-1836-4b9d-af2d-655a43a60bde",
"educationId": "7fe46a1f-fb32-4e36-9741-d96f22cf4b4b"
}
Expand All @@ -1412,6 +1231,7 @@
"description": "Example request to update user profile",
"value": {
"maritalStatus": "Menikah",
"certifiedStatus": "Certified Financial Planner (CFP)",
"workId": "dc4aa7d6-7cbf-4789-a7a6-5fdc9c1ca642",
"educationId": "b5fbeaee-812f-4ec3-b807-22dd2794e09a"
}
Expand Down
98 changes: 96 additions & 2 deletions src/controller/users.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ import { handleServerError } from "../exceptions/server.exception.js";
import { handleZodError } from "../exceptions/zod.exception.js";
import {
UpdateAllFieldUserSchema,
UpdatePartialFieldUserProfileSchema,
UpdatePartialFieldUserSchema,
} from "../schema/users.schema.js";
import {
editAvatarUser,
editUserById,
editUserProfileByUserId,
getAllUsers,
getUserById,
getUserProfileByUserId,
removeUserById,
removeUserProfileByUserId,
} from "../service/users.service.js";
import { findUserProfileByUserId } from "../repository/users.repository.js";
import {
Expand Down Expand Up @@ -126,11 +130,80 @@ export const editUserPartialFieldByIdHandler = async (req, res) => {
}
};

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

const validateData = UpdatePartialFieldUserProfileSchema.parse(
req.body
);

const user = await editUserProfileByUserId(userId, validateData, res);

res.status(200).send({
status: "success",
message: "User profile data updated successfully",
data: {
id: user.id,
},
});
} catch (error) {
try {
handleZodError(error, res);
} catch (err) {
if (err instanceof NotFoundError) {
return;
}
handleServerError(err, 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 @@ -162,6 +235,27 @@ export const removeUserByIdHandler = async (req, res) => {
}
};

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

const profile = await getUserProfileByUserId(userId, res);

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

export const editAvatarUserHandler = async (req, res) => {
try {
const userId = req.params.userId;
Expand All @@ -171,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
Loading

0 comments on commit 0ac03d7

Please sign in to comment.