🚩 create basic flow for fetching user data
This commit is contained in:
@ -4,11 +4,12 @@ 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";
|
||||
|
||||
export const getUserByEmailController = async (ctx: Context) => {
|
||||
try {
|
||||
const params = getUserByIdSchema.parse(ctx.params);
|
||||
const options = getUserOptionsSchema.parse(ctx.query);
|
||||
const options = getUserOptionsSchema.parse(ctx.query) as getUserDataOptions;
|
||||
const getUserByEmail = await getUserByEmailService(params.email, options);
|
||||
|
||||
return returnReadResponse(ctx.set, 200, "User data found", getUserByEmail);
|
||||
|
||||
@ -9,6 +9,34 @@ export const findUserByEmailRepository = async (
|
||||
where: {
|
||||
email,
|
||||
},
|
||||
omit: {},
|
||||
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"),
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
@ -1,9 +1,6 @@
|
||||
import z from "zod";
|
||||
|
||||
const includeEnum = z.enum(
|
||||
["preference", "roles"],
|
||||
"option: include value didn't match with enum types"
|
||||
);
|
||||
const includeOptions = ["preference", "roles"] as const;
|
||||
|
||||
export const getUserOptionsSchema = z.object({
|
||||
verbosity: z
|
||||
@ -12,9 +9,12 @@ export const getUserOptionsSchema = z.object({
|
||||
"option: verbosity value didn't match with enum types"
|
||||
)
|
||||
.optional(),
|
||||
include: z.preprocess((val) => {
|
||||
if (Array.isArray(val)) return val;
|
||||
if (typeof val === "string") return [val];
|
||||
return [];
|
||||
}, z.array(includeEnum).optional()),
|
||||
include: z
|
||||
.string()
|
||||
.optional()
|
||||
.transform((val) => val?.split(",") ?? [])
|
||||
.refine(
|
||||
(arr) => arr.every((val) => includeOptions.includes(val.trim() as any)),
|
||||
"option: include value didn't match with enum types"
|
||||
),
|
||||
});
|
||||
|
||||
@ -1,8 +1,14 @@
|
||||
import { ErrorForwarder } from "../../../../helpers/error/instances/forwarder";
|
||||
import { findUserByEmailRepository } from "../../repositories/read/findUserByEmail.repository";
|
||||
import { getUserDataOptions } from "../../user.types";
|
||||
|
||||
export const getUserByEmailService = async (
|
||||
email: string,
|
||||
options: getUserDataOptions
|
||||
) => {
|
||||
return email;
|
||||
try {
|
||||
return await findUserByEmailRepository(email, options);
|
||||
} catch (error) {
|
||||
ErrorForwarder(error);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user