👔 add include option on find user repository

add include option for spicify which associated data that want to retrive along with user data response
This commit is contained in:
Rafi Arrafif
2025-07-20 15:06:38 +07:00
parent 6de6028a40
commit 5c6e072622
6 changed files with 51 additions and 7 deletions

View File

@ -1,9 +1,22 @@
import { userModel } from "../../user.model";
import {
getUserDataIncludeOptions,
getUserDataOptions,
} from "../../user.types";
export const findUserByEmailRepository = async (email: string) => {
export const findUserByEmailRepository = async (
email: string,
include?: getUserDataOptions["include"]
) => {
return await userModel.findUnique({
where: {
email,
},
include: include
? (Object.fromEntries(include.map((key) => [key, true])) as Record<
getUserDataIncludeOptions,
true
>)
: undefined,
});
};

View File

@ -1,9 +1,22 @@
import { userModel } from "../../user.model";
import {
getUserDataIncludeOptions,
getUserDataOptions,
} from "../../user.types";
export const findUserByIdRepository = async (id: string) => {
export const findUserByIdRepository = async (
id: string,
include?: getUserDataOptions["include"]
) => {
return await userModel.findUnique({
where: {
id,
},
include: include
? (Object.fromEntries(include.map((key) => [key, true])) as Record<
getUserDataIncludeOptions,
true
>)
: undefined,
});
};

View File

@ -1,9 +1,22 @@
import { userModel } from "../../user.model";
import {
getUserDataIncludeOptions,
getUserDataOptions,
} from "../../user.types";
export const findUserByUsernameRepository = async (username: string) => {
export const findUserByUsernameRepository = async (
username: string,
include?: getUserDataOptions["include"]
) => {
return await userModel.findUnique({
where: {
username,
},
include: include
? (Object.fromEntries(include.map((key) => [key, true])) as Record<
getUserDataIncludeOptions,
true
>)
: undefined,
});
};

View File

@ -19,7 +19,7 @@ export const findUserService = async (payload: getUserDataService) => {
if (!repoFn) throw new AppError(503, "Repository handler not found");
// Retrieving user data using the associated repository, if user not found return 404 response
const userData = await repoFn(payload.identifier);
const userData = await repoFn(payload.identifier, payload.options.include);
if (!userData) throw new AppError(404, "User not found");
// Define verbosity levels

View File

@ -5,8 +5,9 @@ export interface getUserDataService {
}
export interface getUserDataOptions {
verbosity: "exists" | "basic" | "full";
include?: ("preference" | "roles")[];
include?: getUserDataIncludeOptions[];
}
export type getUserDataIncludeOptions = "preference" | "roles";
export interface createUserViaRegisterInput {
name: string;