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 { 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) => {
|
||||
try {
|
||||
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);
|
||||
}
|
||||
return "OK";
|
||||
};
|
||||
|
||||
// buat debug untuk date to number (second)
|
||||
|
||||
@ -10,20 +10,16 @@ import { getCookie } from "../../../helpers/http/userHeader/cookies/getCookies";
|
||||
|
||||
export const editUserController = async (
|
||||
ctx: Context & {
|
||||
params: { username: string };
|
||||
body: Prisma.UserUncheckedCreateInput;
|
||||
}
|
||||
) => {
|
||||
try {
|
||||
const userCookie = getCookie(ctx);
|
||||
if (!userCookie.auth_token)
|
||||
const auth_token = userCookie.auth_token;
|
||||
if (!auth_token)
|
||||
return returnErrorResponse(ctx.set, 401, "User Unauthenticated");
|
||||
|
||||
const editUser = await editUserService(
|
||||
ctx.params.username,
|
||||
userCookie.auth_token,
|
||||
ctx.body
|
||||
);
|
||||
const editUser = await editUserService(auth_token, ctx.body);
|
||||
return editUser;
|
||||
} catch (error) {
|
||||
return mainErrorHandler(ctx.set, error);
|
||||
|
||||
@ -6,4 +6,4 @@ import { editUserController } from "./controller/editUser.controller";
|
||||
export const userModule = new Elysia({ prefix: "/users" })
|
||||
.get("/", getAllUserController)
|
||||
.post("/", createUserController)
|
||||
.put("/:username", editUserController);
|
||||
.put("/", editUserController);
|
||||
|
||||
@ -1,18 +1,19 @@
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { userModel } from "../user.model";
|
||||
|
||||
export const checkUserEmailAndUsernameAvailabillity = async (
|
||||
payload: Partial<Prisma.UserGetPayload<Record<string, never>>>
|
||||
export const checkUserEmailAndUsernameAvailabillityRepo = async (
|
||||
id: string,
|
||||
username: string,
|
||||
email: string
|
||||
) => {
|
||||
try {
|
||||
const checkUsernameAndEmailAvailabillity = await userModel.findFirst({
|
||||
where: {
|
||||
OR: [
|
||||
{ username: payload.username ?? undefined },
|
||||
{ email: payload.email ?? undefined },
|
||||
{ username: username ?? undefined },
|
||||
{ email: email ?? undefined },
|
||||
],
|
||||
NOT: {
|
||||
id: payload.id,
|
||||
id: id,
|
||||
},
|
||||
},
|
||||
});
|
||||
@ -1,20 +1,17 @@
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { userModel } from "../user.model";
|
||||
import { JWTSessionPayload } from "../../auth/auth.types";
|
||||
|
||||
export const updateUserRepo = async (
|
||||
identifier: string,
|
||||
payload: Prisma.UserUncheckedCreateInput
|
||||
username: string,
|
||||
payload: Prisma.UserUpdateInput
|
||||
) => {
|
||||
try {
|
||||
const userData = await userModel.update({
|
||||
where: {
|
||||
username: identifier,
|
||||
},
|
||||
data: {
|
||||
username: payload.username,
|
||||
name: payload.name,
|
||||
birthDate: payload.name,
|
||||
username,
|
||||
},
|
||||
data: payload,
|
||||
});
|
||||
return userData;
|
||||
} 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 { ErrorForwarder } from "../../../helpers/error/instances/forwarder";
|
||||
import { updateUserRepo } from "../repositories/updateUser.repository";
|
||||
import { checkUserEmailAndUsernameAvailabillityService } from "./checkUserEmailAndUsernameAvailabillity.service";
|
||||
|
||||
export const editUserService = async (
|
||||
identifier: string,
|
||||
cookie: string,
|
||||
payload: Prisma.UserUncheckedCreateInput
|
||||
payload: Prisma.UserUpdateInput
|
||||
) => {
|
||||
try {
|
||||
// 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);
|
||||
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;
|
||||
} catch (error) {
|
||||
ErrorForwarder(error, 500, "Internal server error");
|
||||
|
||||
Reference in New Issue
Block a user