🚩 create basic flow for fetching user data
This commit is contained in:
11
src/helpers/http/userHeader/query/parseArrayQuery.ts
Normal file
11
src/helpers/http/userHeader/query/parseArrayQuery.ts
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import { Context } from "elysia";
|
||||||
|
|
||||||
|
export const parseArrayQuery = <T extends string = string>(
|
||||||
|
query: Context["query"],
|
||||||
|
key: string
|
||||||
|
): T[] => {
|
||||||
|
const raw = query[key];
|
||||||
|
if (!raw) return [];
|
||||||
|
|
||||||
|
return raw.split(",").map((s) => s.trim()) as T[];
|
||||||
|
};
|
||||||
@ -4,11 +4,12 @@ import { mainErrorHandler } from "../../../helpers/error/handler";
|
|||||||
import { getUserByEmailService } from "../services/http/getUserByEmail.service";
|
import { getUserByEmailService } from "../services/http/getUserByEmail.service";
|
||||||
import { returnReadResponse } from "../../../helpers/callback/httpResponse";
|
import { returnReadResponse } from "../../../helpers/callback/httpResponse";
|
||||||
import { getUserOptionsSchema } from "../schemas/getUserOptions.schema";
|
import { getUserOptionsSchema } from "../schemas/getUserOptions.schema";
|
||||||
|
import { getUserDataOptions } from "../user.types";
|
||||||
|
|
||||||
export const getUserByEmailController = async (ctx: Context) => {
|
export const getUserByEmailController = async (ctx: Context) => {
|
||||||
try {
|
try {
|
||||||
const params = getUserByIdSchema.parse(ctx.params);
|
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);
|
const getUserByEmail = await getUserByEmailService(params.email, options);
|
||||||
|
|
||||||
return returnReadResponse(ctx.set, 200, "User data found", getUserByEmail);
|
return returnReadResponse(ctx.set, 200, "User data found", getUserByEmail);
|
||||||
|
|||||||
@ -9,6 +9,34 @@ export const findUserByEmailRepository = async (
|
|||||||
where: {
|
where: {
|
||||||
email,
|
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";
|
import z from "zod";
|
||||||
|
|
||||||
const includeEnum = z.enum(
|
const includeOptions = ["preference", "roles"] as const;
|
||||||
["preference", "roles"],
|
|
||||||
"option: include value didn't match with enum types"
|
|
||||||
);
|
|
||||||
|
|
||||||
export const getUserOptionsSchema = z.object({
|
export const getUserOptionsSchema = z.object({
|
||||||
verbosity: z
|
verbosity: z
|
||||||
@ -12,9 +9,12 @@ export const getUserOptionsSchema = z.object({
|
|||||||
"option: verbosity value didn't match with enum types"
|
"option: verbosity value didn't match with enum types"
|
||||||
)
|
)
|
||||||
.optional(),
|
.optional(),
|
||||||
include: z.preprocess((val) => {
|
include: z
|
||||||
if (Array.isArray(val)) return val;
|
.string()
|
||||||
if (typeof val === "string") return [val];
|
.optional()
|
||||||
return [];
|
.transform((val) => val?.split(",") ?? [])
|
||||||
}, z.array(includeEnum).optional()),
|
.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";
|
import { getUserDataOptions } from "../../user.types";
|
||||||
|
|
||||||
export const getUserByEmailService = async (
|
export const getUserByEmailService = async (
|
||||||
email: string,
|
email: string,
|
||||||
options: getUserDataOptions
|
options: getUserDataOptions
|
||||||
) => {
|
) => {
|
||||||
return email;
|
try {
|
||||||
|
return await findUserByEmailRepository(email, options);
|
||||||
|
} catch (error) {
|
||||||
|
ErrorForwarder(error);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user