123 lines
3.9 KiB
TypeScript
123 lines
3.9 KiB
TypeScript
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 <T extends BaseGraphModel>(model: T, id: string, ctx: ApolloContext): Promise<any> => {
|
|
try {
|
|
await model.getOne({ _id: id } as any, ctx);
|
|
return model.get();
|
|
} catch (error) {
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
export const addOneGeneric = async <T extends BaseGraphModel>(model: T, body: Partial<T>, ctx: ApolloContext): Promise<T> => {
|
|
try {
|
|
await model.saveOne(body, ctx);
|
|
return model.get();
|
|
} catch (error) {
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
export const editOneGeneric = async <T extends BaseGraphModel>(model: T, id: string, body: Partial<T>, ctx: ApolloContext): Promise<T> => {
|
|
try {
|
|
await model.updateOne({ _id: id } as any, body, ctx);
|
|
return model.get();
|
|
} catch (error) {
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
export const deleteOneGeneric = async <T extends BaseGraphModel>(model: T, id: string, ctx: ApolloContext): Promise<T> => {
|
|
try {
|
|
await model.deleteOne({ _id: id } as any, ctx);
|
|
return model.get();
|
|
} catch (error) {
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
export const getManyGeneric = async <T extends BaseGraphModel>(model: T, query: any, pagination: GetArgs, ctx: ApolloContext): Promise<any> => {
|
|
return baseSearchFunction({
|
|
model: model,
|
|
query: query,
|
|
pagination: pagination,
|
|
ctx: ctx,
|
|
});
|
|
};
|
|
|
|
export const getCountGeneric = async <T extends BaseGraphModel>(model: T, baseArguments: any, ctx: ApolloContext): Promise<number> => {
|
|
// 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 <T extends BaseGraphModel>(model: T, baseArguments: any, ctx: ApolloContext): Promise<any> => {
|
|
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<any> => {
|
|
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<any> => {
|
|
try {
|
|
await model.updateOneCustom({ _id: id }, { $pull: { [`${input.permissionType}`]: input.permission } }, ctx);
|
|
return model.get();
|
|
} catch (error) {
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
export const publishOneGeneric = async <T extends BaseGraphModel>(model: T, id: string, ctx: ApolloContext): Promise<any> => {
|
|
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 <T extends BaseGraphModel>(model: T, id: string, ctx: ApolloContext): Promise<any> => {
|
|
try {
|
|
return editOneGeneric(model, id, { published: false } as any, ctx);
|
|
} catch (error) {
|
|
throw error;
|
|
}
|
|
};
|