add:module:user:*update | add all service in module user for updating data
This commit is contained in:
@ -1,24 +1,7 @@
|
|||||||
import { Context } from "elysia";
|
import { Context } from "elysia";
|
||||||
import { mainErrorHandler } from "../../helpers/error/handler";
|
|
||||||
import { debugService } from "./debug.service";
|
|
||||||
import { returnWriteResponse } from "../../helpers/callback/httpResponse";
|
|
||||||
import { getCookie } from "../../helpers/http/userHeader/cookies/getCookies";
|
|
||||||
import { checkUserEmailAndUsernameAvailabillity } from "../user/repositories/checkUserEmailAndUsernameAvailability.repository";
|
|
||||||
import { jwtDecode } from "../../helpers/http/jwt/decode";
|
|
||||||
|
|
||||||
export const debugController = async (ctx: Context) => {
|
export const debugController = async (ctx: Context) => {
|
||||||
try {
|
return "OK";
|
||||||
const userCookie = getCookie(ctx);
|
|
||||||
const jwtSession = jwtDecode(userCookie.auth_token!);
|
|
||||||
jwtSession.user.email = ctx.params.email;
|
|
||||||
jwtSession.user.username = ctx.params.username;
|
|
||||||
const checkAvailabillity = await checkUserEmailAndUsernameAvailabillity(
|
|
||||||
jwtSession.user
|
|
||||||
);
|
|
||||||
return checkAvailabillity;
|
|
||||||
} catch (error) {
|
|
||||||
return mainErrorHandler(ctx.set, error);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// buat debug untuk date to number (second)
|
// buat debug untuk date to number (second)
|
||||||
|
|||||||
@ -10,20 +10,16 @@ import { getCookie } from "../../../helpers/http/userHeader/cookies/getCookies";
|
|||||||
|
|
||||||
export const editUserController = async (
|
export const editUserController = async (
|
||||||
ctx: Context & {
|
ctx: Context & {
|
||||||
params: { username: string };
|
|
||||||
body: Prisma.UserUncheckedCreateInput;
|
body: Prisma.UserUncheckedCreateInput;
|
||||||
}
|
}
|
||||||
) => {
|
) => {
|
||||||
try {
|
try {
|
||||||
const userCookie = getCookie(ctx);
|
const userCookie = getCookie(ctx);
|
||||||
if (!userCookie.auth_token)
|
const auth_token = userCookie.auth_token;
|
||||||
|
if (!auth_token)
|
||||||
return returnErrorResponse(ctx.set, 401, "User Unauthenticated");
|
return returnErrorResponse(ctx.set, 401, "User Unauthenticated");
|
||||||
|
|
||||||
const editUser = await editUserService(
|
const editUser = await editUserService(auth_token, ctx.body);
|
||||||
ctx.params.username,
|
|
||||||
userCookie.auth_token,
|
|
||||||
ctx.body
|
|
||||||
);
|
|
||||||
return editUser;
|
return editUser;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return mainErrorHandler(ctx.set, error);
|
return mainErrorHandler(ctx.set, error);
|
||||||
|
|||||||
@ -6,4 +6,4 @@ import { editUserController } from "./controller/editUser.controller";
|
|||||||
export const userModule = new Elysia({ prefix: "/users" })
|
export const userModule = new Elysia({ prefix: "/users" })
|
||||||
.get("/", getAllUserController)
|
.get("/", getAllUserController)
|
||||||
.post("/", createUserController)
|
.post("/", createUserController)
|
||||||
.put("/:username", editUserController);
|
.put("/", editUserController);
|
||||||
|
|||||||
@ -1,18 +1,19 @@
|
|||||||
import { Prisma } from "@prisma/client";
|
|
||||||
import { userModel } from "../user.model";
|
import { userModel } from "../user.model";
|
||||||
|
|
||||||
export const checkUserEmailAndUsernameAvailabillity = async (
|
export const checkUserEmailAndUsernameAvailabillityRepo = async (
|
||||||
payload: Partial<Prisma.UserGetPayload<Record<string, never>>>
|
id: string,
|
||||||
|
username: string,
|
||||||
|
email: string
|
||||||
) => {
|
) => {
|
||||||
try {
|
try {
|
||||||
const checkUsernameAndEmailAvailabillity = await userModel.findFirst({
|
const checkUsernameAndEmailAvailabillity = await userModel.findFirst({
|
||||||
where: {
|
where: {
|
||||||
OR: [
|
OR: [
|
||||||
{ username: payload.username ?? undefined },
|
{ username: username ?? undefined },
|
||||||
{ email: payload.email ?? undefined },
|
{ email: email ?? undefined },
|
||||||
],
|
],
|
||||||
NOT: {
|
NOT: {
|
||||||
id: payload.id,
|
id: id,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
@ -1,20 +1,17 @@
|
|||||||
import { Prisma } from "@prisma/client";
|
import { Prisma } from "@prisma/client";
|
||||||
import { userModel } from "../user.model";
|
import { userModel } from "../user.model";
|
||||||
|
import { JWTSessionPayload } from "../../auth/auth.types";
|
||||||
|
|
||||||
export const updateUserRepo = async (
|
export const updateUserRepo = async (
|
||||||
identifier: string,
|
username: string,
|
||||||
payload: Prisma.UserUncheckedCreateInput
|
payload: Prisma.UserUpdateInput
|
||||||
) => {
|
) => {
|
||||||
try {
|
try {
|
||||||
const userData = await userModel.update({
|
const userData = await userModel.update({
|
||||||
where: {
|
where: {
|
||||||
username: identifier,
|
username,
|
||||||
},
|
|
||||||
data: {
|
|
||||||
username: payload.username,
|
|
||||||
name: payload.name,
|
|
||||||
birthDate: payload.name,
|
|
||||||
},
|
},
|
||||||
|
data: payload,
|
||||||
});
|
});
|
||||||
return userData;
|
return userData;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
@ -0,0 +1,20 @@
|
|||||||
|
import { Prisma } from "@prisma/client";
|
||||||
|
import { ErrorForwarder } from "../../../helpers/error/instances/forwarder";
|
||||||
|
import { checkUserEmailAndUsernameAvailabillityRepo } from "../repositories/checkUserEmailAndUsernameAvailabillity.repository";
|
||||||
|
|
||||||
|
export const checkUserEmailAndUsernameAvailabillityService = async (
|
||||||
|
payload: Prisma.UserUpdateInput,
|
||||||
|
idException: string
|
||||||
|
) => {
|
||||||
|
try {
|
||||||
|
const usernameAndEmailAvailabillity =
|
||||||
|
checkUserEmailAndUsernameAvailabillityRepo(
|
||||||
|
idException!,
|
||||||
|
payload.username as string,
|
||||||
|
payload.email as string
|
||||||
|
);
|
||||||
|
return usernameAndEmailAvailabillity;
|
||||||
|
} catch (error) {
|
||||||
|
ErrorForwarder(error);
|
||||||
|
}
|
||||||
|
};
|
||||||
@ -3,20 +3,60 @@ import { jwtDecode } from "../../../helpers/http/jwt/decode";
|
|||||||
import { AppError } from "../../../helpers/error/instances/app";
|
import { AppError } from "../../../helpers/error/instances/app";
|
||||||
import { ErrorForwarder } from "../../../helpers/error/instances/forwarder";
|
import { ErrorForwarder } from "../../../helpers/error/instances/forwarder";
|
||||||
import { updateUserRepo } from "../repositories/updateUser.repository";
|
import { updateUserRepo } from "../repositories/updateUser.repository";
|
||||||
|
import { checkUserEmailAndUsernameAvailabillityService } from "./checkUserEmailAndUsernameAvailabillity.service";
|
||||||
|
|
||||||
export const editUserService = async (
|
export const editUserService = async (
|
||||||
identifier: string,
|
|
||||||
cookie: string,
|
cookie: string,
|
||||||
payload: Prisma.UserUncheckedCreateInput
|
payload: Prisma.UserUpdateInput
|
||||||
) => {
|
) => {
|
||||||
try {
|
try {
|
||||||
// Decode the JWT token and verify the user, if the user is not the same as the identifier, throw an error
|
// Decode the JWT token and verify the user, if the user is not the same as the identifier, throw an error
|
||||||
const jwtSession = jwtDecode(cookie);
|
const jwtSession = jwtDecode(cookie);
|
||||||
if (jwtSession.user.username !== identifier) {
|
|
||||||
throw new AppError(401, "Unauthorized");
|
|
||||||
}
|
|
||||||
|
|
||||||
const updateUser = updateUserRepo(identifier, payload);
|
// Check if the username or email is being taken by another user, if so, throw an error
|
||||||
|
const isUsernameOrEmailIsBeingTaken =
|
||||||
|
await checkUserEmailAndUsernameAvailabillityService(
|
||||||
|
payload,
|
||||||
|
jwtSession.userId
|
||||||
|
);
|
||||||
|
if (isUsernameOrEmailIsBeingTaken)
|
||||||
|
throw new AppError(
|
||||||
|
409,
|
||||||
|
"The username or email has already taken by another user."
|
||||||
|
);
|
||||||
|
|
||||||
|
const fieldsToUpdate: Partial<Prisma.UserUpdateInput> = {
|
||||||
|
...(payload.username && payload.username !== jwtSession.user.username
|
||||||
|
? { username: payload.username }
|
||||||
|
: {}),
|
||||||
|
|
||||||
|
...(payload.name !== undefined ? { name: payload.name } : {}),
|
||||||
|
...(payload.birthDate !== undefined
|
||||||
|
? { birthDate: payload.birthDate }
|
||||||
|
: {}),
|
||||||
|
...(payload.gender !== undefined ? { gender: payload.gender } : {}),
|
||||||
|
...(payload.phoneCC !== undefined ? { phoneCC: payload.phoneCC } : {}),
|
||||||
|
...(payload.phoneNumber !== undefined
|
||||||
|
? { phoneNumber: payload.phoneNumber }
|
||||||
|
: {}),
|
||||||
|
...(payload.bioProfile !== undefined
|
||||||
|
? { bioProfile: payload.bioProfile }
|
||||||
|
: {}),
|
||||||
|
...(payload.profilePicture !== undefined
|
||||||
|
? { profilePicture: payload.profilePicture }
|
||||||
|
: {}),
|
||||||
|
...(payload.commentPicture !== undefined
|
||||||
|
? { commentPicture: payload.commentPicture }
|
||||||
|
: {}),
|
||||||
|
...(payload.deletedAt !== undefined
|
||||||
|
? { deletedAt: payload.deletedAt }
|
||||||
|
: {}),
|
||||||
|
};
|
||||||
|
|
||||||
|
const updateUser = await updateUserRepo(
|
||||||
|
jwtSession.user.username,
|
||||||
|
fieldsToUpdate
|
||||||
|
);
|
||||||
return updateUser;
|
return updateUser;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
ErrorForwarder(error, 500, "Internal server error");
|
ErrorForwarder(error, 500, "Internal server error");
|
||||||
|
|||||||
Reference in New Issue
Block a user