🚩 complete get user method
adding find user by emial, username, and ID
This commit is contained in:
@ -1,14 +1,14 @@
|
||||
import { Context } from "elysia";
|
||||
import { getUserByIdSchema } from "../schemas/getUserById.schema";
|
||||
import { mainErrorHandler } from "../../../helpers/error/handler";
|
||||
import { getUserByEmailService } from "../services/http/getUserByEmail.service";
|
||||
import { returnReadResponse } from "../../../helpers/callback/httpResponse";
|
||||
import { getUserOptionsSchema } from "../schemas/getUserOptions.schema";
|
||||
import { getUserDataOptions } from "../user.types";
|
||||
import { getUserByEmailSchema } from "../schemas/getUserByEmail.schema";
|
||||
|
||||
export const getUserByEmailController = async (ctx: Context) => {
|
||||
try {
|
||||
const params = getUserByIdSchema.parse(ctx.params);
|
||||
const params = getUserByEmailSchema.parse(ctx.params);
|
||||
const options = getUserOptionsSchema.parse(ctx.query) as getUserDataOptions;
|
||||
const getUserByEmail = await getUserByEmailService(params.email, options);
|
||||
|
||||
|
||||
19
src/modules/user/controller/getUserById.controller.ts
Normal file
19
src/modules/user/controller/getUserById.controller.ts
Normal file
@ -0,0 +1,19 @@
|
||||
import { Context } from "elysia";
|
||||
import { getUserByIdSchema } from "../schemas/getUserById.schema";
|
||||
import { mainErrorHandler } from "../../../helpers/error/handler";
|
||||
import { returnReadResponse } from "../../../helpers/callback/httpResponse";
|
||||
import { getUserOptionsSchema } from "../schemas/getUserOptions.schema";
|
||||
import { getUserDataOptions } from "../user.types";
|
||||
import { getUserByIdService } from "../services/http/getUserById.service";
|
||||
|
||||
export const getUserByIdController = async (ctx: Context) => {
|
||||
try {
|
||||
const params = getUserByIdSchema.parse(ctx.params);
|
||||
const options = getUserOptionsSchema.parse(ctx.query) as getUserDataOptions;
|
||||
const getUserByEmail = await getUserByIdService(params.id, options);
|
||||
|
||||
return returnReadResponse(ctx.set, 200, "User data found", getUserByEmail);
|
||||
} catch (error) {
|
||||
return mainErrorHandler(ctx.set, error);
|
||||
}
|
||||
};
|
||||
22
src/modules/user/controller/getUserByUsername.controller.ts
Normal file
22
src/modules/user/controller/getUserByUsername.controller.ts
Normal file
@ -0,0 +1,22 @@
|
||||
import { Context } from "elysia";
|
||||
import { mainErrorHandler } from "../../../helpers/error/handler";
|
||||
import { returnReadResponse } from "../../../helpers/callback/httpResponse";
|
||||
import { getUserOptionsSchema } from "../schemas/getUserOptions.schema";
|
||||
import { getUserDataOptions } from "../user.types";
|
||||
import { getUserByUsernameSchema } from "../schemas/getUserByUsername.schema";
|
||||
import { getUserByUsernameService } from "../services/http/getUserByUsername.service";
|
||||
|
||||
export const getUserByUsernameController = async (ctx: Context) => {
|
||||
try {
|
||||
const params = getUserByUsernameSchema.parse(ctx.params);
|
||||
const options = getUserOptionsSchema.parse(ctx.query) as getUserDataOptions;
|
||||
const getUserByEmail = await getUserByUsernameService(
|
||||
params.username,
|
||||
options
|
||||
);
|
||||
|
||||
return returnReadResponse(ctx.set, 200, "User data found", getUserByEmail);
|
||||
} catch (error) {
|
||||
return mainErrorHandler(ctx.set, error);
|
||||
}
|
||||
};
|
||||
@ -1,7 +1,11 @@
|
||||
import Elysia from "elysia";
|
||||
import { createUserViaRegisterController } from "./controller/createUserViaRegister.controller";
|
||||
import { getUserByEmailController } from "./controller/getUserByEmail.controller";
|
||||
import { getUserByUsernameController } from "./controller/getUserByUsername.controller";
|
||||
import { getUserByIdController } from "./controller/getUserById.controller";
|
||||
|
||||
export const userModule = new Elysia({ prefix: "/users" })
|
||||
.post("/", createUserViaRegisterController)
|
||||
.get("/e/:email", getUserByEmailController);
|
||||
.get("/e/:email", getUserByEmailController)
|
||||
.get("/u/:username", getUserByUsernameController)
|
||||
.get("/id/:id", getUserByIdController);
|
||||
|
||||
@ -1,42 +1,9 @@
|
||||
import { userModel } from "../../user.model";
|
||||
import { getUserDataOptions } from "../../user.types";
|
||||
|
||||
export const findUserByEmailRepository = async (
|
||||
email: string,
|
||||
options: getUserDataOptions
|
||||
) => {
|
||||
export const findUserByEmailRepository = async (email: string) => {
|
||||
return await userModel.findUnique({
|
||||
where: {
|
||||
email,
|
||||
},
|
||||
select: {
|
||||
id: options.verbosity?.includes("full"),
|
||||
name: ["full", "basic"].some((level) =>
|
||||
options.verbosity?.includes(level)
|
||||
),
|
||||
username: ["full", "basic"].some((level) =>
|
||||
options.verbosity?.includes(level)
|
||||
),
|
||||
email: options.verbosity?.includes("full"),
|
||||
password: options.verbosity?.includes("full"),
|
||||
birthDate: options.verbosity?.includes("full"),
|
||||
gender: options.verbosity?.includes("full"),
|
||||
phoneCC: options.verbosity?.includes("full"),
|
||||
phoneNumber: options.verbosity?.includes("full"),
|
||||
bioProfile: ["full", "basic"].some((level) =>
|
||||
options.verbosity?.includes(level)
|
||||
),
|
||||
avatar: ["full", "basic"].some((level) =>
|
||||
options.verbosity?.includes(level)
|
||||
),
|
||||
commentBackground: ["full", "basic"].some((level) =>
|
||||
options.verbosity?.includes(level)
|
||||
),
|
||||
preferenceId: options.verbosity?.includes("full"),
|
||||
verifiedAt: options.verbosity?.includes("full"),
|
||||
disabledAt: options.verbosity?.includes("full"),
|
||||
createdAt: options.verbosity?.includes("full"),
|
||||
updatedAt: options.verbosity?.includes("full"),
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
@ -0,0 +1,9 @@
|
||||
import { userModel } from "../../user.model";
|
||||
|
||||
export const findUserByIdRepository = async (id: string) => {
|
||||
return await userModel.findUnique({
|
||||
where: {
|
||||
id,
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
@ -0,0 +1,9 @@
|
||||
import { userModel } from "../../user.model";
|
||||
|
||||
export const findUserByUsernameRepository = async (username: string) => {
|
||||
return await userModel.findUnique({
|
||||
where: {
|
||||
username,
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
5
src/modules/user/schemas/getUserByEmail.schema.ts
Normal file
5
src/modules/user/schemas/getUserByEmail.schema.ts
Normal file
@ -0,0 +1,5 @@
|
||||
import z from "zod";
|
||||
|
||||
export const getUserByEmailSchema = z.object({
|
||||
email: z.email(),
|
||||
});
|
||||
@ -1,5 +1,5 @@
|
||||
import z from "zod";
|
||||
|
||||
export const getUserByIdSchema = z.object({
|
||||
email: z.email(),
|
||||
id: z.string(),
|
||||
});
|
||||
|
||||
5
src/modules/user/schemas/getUserByUsername.schema.ts
Normal file
5
src/modules/user/schemas/getUserByUsername.schema.ts
Normal file
@ -0,0 +1,5 @@
|
||||
import z from "zod";
|
||||
|
||||
export const getUserByUsernameSchema = z.object({
|
||||
username: z.string().min(4),
|
||||
});
|
||||
@ -3,12 +3,10 @@ import z from "zod";
|
||||
const includeOptions = ["preference", "roles"] as const;
|
||||
|
||||
export const getUserOptionsSchema = z.object({
|
||||
verbosity: z
|
||||
.enum(
|
||||
["exists", "basic", "full"],
|
||||
"option: verbosity value didn't match with enum types"
|
||||
)
|
||||
.optional(),
|
||||
verbosity: z.enum(
|
||||
["exists", "basic", "full"],
|
||||
"option: verbosity value must match with enum types"
|
||||
),
|
||||
include: z
|
||||
.string()
|
||||
.optional()
|
||||
|
||||
@ -1,13 +1,17 @@
|
||||
import { ErrorForwarder } from "../../../../helpers/error/instances/forwarder";
|
||||
import { findUserByEmailRepository } from "../../repositories/read/findUserByEmail.repository";
|
||||
import { getUserDataOptions } from "../../user.types";
|
||||
import { findUserService } from "../internal/findUser.service";
|
||||
|
||||
export const getUserByEmailService = async (
|
||||
email: string,
|
||||
options: getUserDataOptions
|
||||
) => {
|
||||
try {
|
||||
return await findUserByEmailRepository(email, options);
|
||||
return await findUserService({
|
||||
identifier: email,
|
||||
queryTarget: "email",
|
||||
options,
|
||||
});
|
||||
} catch (error) {
|
||||
ErrorForwarder(error);
|
||||
}
|
||||
|
||||
18
src/modules/user/services/http/getUserById.service.ts
Normal file
18
src/modules/user/services/http/getUserById.service.ts
Normal file
@ -0,0 +1,18 @@
|
||||
import { ErrorForwarder } from "../../../../helpers/error/instances/forwarder";
|
||||
import { getUserDataOptions } from "../../user.types";
|
||||
import { findUserService } from "../internal/findUser.service";
|
||||
|
||||
export const getUserByIdService = async (
|
||||
id: string,
|
||||
options: getUserDataOptions
|
||||
) => {
|
||||
try {
|
||||
return await findUserService({
|
||||
identifier: id,
|
||||
queryTarget: "id",
|
||||
options,
|
||||
});
|
||||
} catch (error) {
|
||||
ErrorForwarder(error);
|
||||
}
|
||||
};
|
||||
18
src/modules/user/services/http/getUserByUsername.service.ts
Normal file
18
src/modules/user/services/http/getUserByUsername.service.ts
Normal file
@ -0,0 +1,18 @@
|
||||
import { ErrorForwarder } from "../../../../helpers/error/instances/forwarder";
|
||||
import { getUserDataOptions } from "../../user.types";
|
||||
import { findUserService } from "../internal/findUser.service";
|
||||
|
||||
export const getUserByUsernameService = async (
|
||||
username: string,
|
||||
options: getUserDataOptions
|
||||
) => {
|
||||
try {
|
||||
return await findUserService({
|
||||
identifier: username,
|
||||
queryTarget: "username",
|
||||
options,
|
||||
});
|
||||
} catch (error) {
|
||||
ErrorForwarder(error);
|
||||
}
|
||||
};
|
||||
@ -0,0 +1,23 @@
|
||||
import { User } from "@prisma/client";
|
||||
import { findUserByEmailRepository } from "../../repositories/read/findUserByEmail.repository";
|
||||
import { getUserDataService } from "../../user.types";
|
||||
import { AppError } from "../../../../helpers/error/instances/app";
|
||||
import { findUserByIdRepository } from "../../repositories/read/findUserById.repository";
|
||||
import { findUserByUsernameRepository } from "../../repositories/read/findUserByUsername.repository";
|
||||
|
||||
export const findUserService = async (payload: getUserDataService) => {
|
||||
let userData: User | null = null;
|
||||
if (payload.queryTarget === "email") {
|
||||
userData = await findUserByEmailRepository(payload.identifier);
|
||||
}
|
||||
if (payload.queryTarget === "id") {
|
||||
userData = await findUserByIdRepository(payload.identifier);
|
||||
}
|
||||
if (payload.queryTarget === "username") {
|
||||
userData = await findUserByUsernameRepository(payload.identifier);
|
||||
}
|
||||
|
||||
if (userData === null) throw new AppError(404, "User not found");
|
||||
|
||||
return userData;
|
||||
};
|
||||
|
||||
@ -1,10 +1,10 @@
|
||||
export interface getUserDataService {
|
||||
identifier: string;
|
||||
queryTarget: "id" | "email" | "username" | "email_username";
|
||||
options?: getUserDataOptions;
|
||||
options: getUserDataOptions;
|
||||
}
|
||||
export interface getUserDataOptions {
|
||||
verbosity?: "exists" | "basic" | "full";
|
||||
verbosity: "exists" | "basic" | "full";
|
||||
include?: ("preference" | "roles")[];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user