import _ from 'lodash'; import { ApolloError } from 'apollo-server-lambda'; import { GetArgs } from './Request'; import { ApolloContext } from '@seed/interfaces/context'; import { BaseGraphModel } from './BaseGraphModel'; import { NewPermissionInput } from '@seed/interfaces/components'; import { baseSearchFunction } from '@seed/services/database/DBRequestService'; import { newError } from '@seed/helpers/Error'; export const getOneGeneric = async (model: T, id: string, ctx: ApolloContext): Promise => { try { await model.getOne({ _id: id } as any, ctx); return model.get(); } catch (error) { throw error; } }; export const addOneGeneric = async (model: T, body: Partial, ctx: ApolloContext): Promise => { try { await model.saveOne(body, ctx); return model.get(); } catch (error) { throw error; } }; export const editOneGeneric = async (model: T, id: string, body: Partial, ctx: ApolloContext): Promise => { try { await model.updateOne({ _id: id } as any, body, ctx); return model.get(); } catch (error) { throw error; } }; export const deleteOneGeneric = async (model: T, id: string, ctx: ApolloContext): Promise => { try { await model.deleteOne({ _id: id } as any, ctx); return model.get(); } catch (error) { throw error; } }; export const getManyGeneric = async (model: T, query: any, pagination: GetArgs, ctx: ApolloContext): Promise => { return baseSearchFunction({ model: model, query: query, pagination: pagination, ctx: ctx, }); }; export const getCountGeneric = async (model: T, baseArguments: any, ctx: ApolloContext): Promise => { // eslint-disable-next-line @typescript-eslint/no-unused-vars const { pagination, ...args } = baseArguments; const result = await baseSearchFunction({ model: model, query: args, count: true, ctx: ctx, }); return result.count || 0; }; export const getManyGenericWithArgs = async (model: T, baseArguments: any, ctx: ApolloContext): Promise => { try { const { pagination, ...args } = baseArguments; return baseSearchFunction({ model: model, query: args, pagination: pagination, ctx: ctx, }); } catch (error) { throw error; } }; export const permissionsAddOne = async (model: any, id: string, input: NewPermissionInput, ctx: ApolloContext): Promise => { try { await model.updateOneCustom({ _id: id }, { $addToSet: { [`${input.permissionType}`]: input.permission } }, ctx); return model.get(); } catch (error) { throw error; } }; export const permissionsRemoveOne = async (model: any, id: string, input: NewPermissionInput, ctx: ApolloContext): Promise => { try { await model.updateOneCustom({ _id: id }, { $pull: { [`${input.permissionType}`]: input.permission } }, ctx); return model.get(); } catch (error) { throw error; } }; export const publishOneGeneric = async (model: T, id: string, ctx: ApolloContext): Promise => { try { const missingInputs: string[] = []; // TODO THE CHECK OF THE INPUTS if (missingInputs.length > 0) throw newError(3000, missingInputs); return editOneGeneric(model, id, { published: true, publicationDate: new Date() } as any, ctx); } catch (error) { throw error; } }; export const unpublishOneGeneric = async (model: T, id: string, ctx: ApolloContext): Promise => { try { return editOneGeneric(model, id, { published: false } as any, ctx); } catch (error) { throw error; } };