backend/lib/seed/engine/utils/service.utils.ts
2025-05-14 21:45:16 +02:00

119 lines
4.5 KiB
TypeScript

import { GetArgs } from '@lib/seed/graphql/Request';
import { ApolloContext } from '@lib/seed/interfaces/context';
import { baseSearchFunction } from '@lib/seed/services/database/DBRequestService';
import { EngineModel } from '../EngineModel';
/*
██████╗ ██╗ ██╗███████╗██████╗ ██╗ ██╗
██╔═══██╗██║ ██║██╔════╝██╔══██╗╚██╗ ██╔╝
██║ ██║██║ ██║█████╗ ██████╔╝ ╚████╔╝
██║▄▄ ██║██║ ██║██╔══╝ ██╔══██╗ ╚██╔╝
╚██████╔╝╚██████╔╝███████╗██║ ██║ ██║
╚══▀▀═╝ ╚═════╝ ╚══════╝╚═╝ ╚═╝ ╚═╝
*/
export const getOneGeneric = async (model: any, id: string, ctx: ApolloContext): Promise<any> => {
try {
return await (model as EngineModel<any, any, any>).getOne({ query: { _id: id }, ctx });
} catch (error) {
throw error;
}
};
export const getManyGeneric = async (model: any, query: any, pagination: GetArgs | undefined, ctx: ApolloContext | null): Promise<any> => {
return baseSearchFunction({
model: model,
query: query,
pagination: pagination,
engine: true,
ctx: ctx,
});
};
export const getCountGeneric = async (model: any, baseArguments: any, ctx: ApolloContext): Promise<any> => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { pagination, ...args } = baseArguments;
const result = await baseSearchFunction({
model: model,
query: args,
count: true,
engine: true,
ctx: ctx,
});
return result;
};
export const getManyGenericWithArgs = async (model: any, baseArguments: any, ctx: ApolloContext | null): Promise<any> => {
try {
const { pagination, ...args } = baseArguments;
return baseSearchFunction({
model: model,
query: args,
pagination: pagination,
engine: true,
ctx: ctx,
});
} catch (error) {
throw error;
}
};
// export const editOneGeneric = async <T>(model: T, id: string, body: Partial<T>, ctx: ApolloContext): Promise<T> => {
// try {
// await model as EngineModel<any,any,any>).updateOne({ _id: id } as any, body, ctx);
// return model as EngineModel<any,any,any>).get();
// } catch (error) {
// throw error;
// }
// };
// export const deleteOneGeneric = async <T>(model: T, id: string, ctx: ApolloContext): Promise<T> => {
// try {
// await model as EngineModel<any,any,any>).deleteOne({ _id: id } as any, ctx);
// return model as EngineModel<any,any,any>).get();
// } catch (error) {
// throw error;
// }
// };
// export const permissionsAddOne = async (model: any, id: string, input: NewPermissionInput, ctx: ApolloContext): Promise<any> => {
// try {
// await model as EngineModel<any,any,any>).updateOneCustom({ _id: id }, { $addToSet: { [`${input.permissionType}`]: input.permission } }, ctx);
// return model as EngineModel<any,any,any>).get();
// } catch (error) {
// throw error;
// }
// };
// export const permissionsRemoveOne = async (model: any, id: string, input: NewPermissionInput, ctx: ApolloContext): Promise<any> => {
// try {
// await model as EngineModel<any,any,any>).updateOneCustom({ _id: id }, { $pull: { [`${input.permissionType}`]: input.permission } }, ctx);
// return model as EngineModel<any,any,any>).get();
// } catch (error) {
// throw error;
// }
// };
// export const publishOneGeneric = async <T>(model: T, id: string, ctx: ApolloContext): Promise<any> => {
// try {
// const missingInputs: string[] = [];
// // TODO THE CHECK OF THE INPUTS
// if (missingInputs.length > 0) throw newError('missingInput', '400', missingInputs);
// return editOneGeneric(model, id, { published: true, publicationDate: new Date() } as any, ctx);
// } catch (error) {
// throw error;
// }
// };
// export const unpublishOneGeneric = async <T>(model: T, id: string, ctx: ApolloContext): Promise<any> => {
// try {
// return editOneGeneric(model, id, { published: false } as any, ctx);
// } catch (error) {
// throw error;
// }
// };