101 lines
5.8 KiB
TypeScript
101 lines
5.8 KiB
TypeScript
import { Query, Arg, Int, Resolver, Authorized, Ctx, Args, Mutation, ClassType } from 'type-graphql';
|
|
import { ApolloContext } from '@seed/interfaces/context';
|
|
import { getOneGeneric, getManyGenericWithArgs, addOneGeneric, editOneGeneric, deleteOneGeneric, getCountGeneric } from '../BaseService';
|
|
import { AccountTypeEnum } from '@src/accounts/account.components';
|
|
import { checkOrganisation as CheckOrganisation } from '../Middleware';
|
|
|
|
export const createBaseResolver = <T extends ClassType<any>, ARGS extends ClassType<any>, NEW extends ClassType<any>, EDIT extends ClassType<any>>(
|
|
domain: string,
|
|
objectTypeCls: T,
|
|
argsType: ARGS,
|
|
newInput: NEW,
|
|
editInput: EDIT,
|
|
auth: AccountTypeEnum[],
|
|
organisationCheck = false,
|
|
): any => {
|
|
@Resolver({ isAbstract: true })
|
|
abstract class BaseResolver {
|
|
/*
|
|
███████╗██╗███████╗██╗ ██████╗ ███████╗
|
|
██╔════╝██║██╔════╝██║ ██╔══██╗██╔════╝
|
|
█████╗ ██║█████╗ ██║ ██║ ██║███████╗
|
|
██╔══╝ ██║██╔══╝ ██║ ██║ ██║╚════██║
|
|
██║ ██║███████╗███████╗██████╔╝███████║
|
|
╚═╝ ╚═╝╚══════╝╚══════╝╚═════╝ ╚══════╝
|
|
*/
|
|
|
|
/*
|
|
██████╗ ██╗ ██╗███████╗██████╗ ██╗ ██╗
|
|
██╔═══██╗██║ ██║██╔════╝██╔══██╗╚██╗ ██╔╝
|
|
██║ ██║██║ ██║█████╗ ██████╔╝ ╚████╔╝
|
|
██║▄▄ ██║██║ ██║██╔══╝ ██╔══██╗ ╚██╔╝
|
|
╚██████╔╝╚██████╔╝███████╗██║ ██║ ██║
|
|
╚══▀▀═╝ ╚═════╝ ╚══════╝╚═╝ ╚═╝ ╚═╝
|
|
*/
|
|
|
|
@Query(() => objectTypeCls, { name: `${domain}GetOne` })
|
|
@Authorized(auth)
|
|
@CheckOrganisation(organisationCheck)
|
|
async getOne(@Arg('id') id: string, @Ctx() ctx: ApolloContext): Promise<T> {
|
|
const model = new objectTypeCls();
|
|
return getOneGeneric(model, id, ctx);
|
|
}
|
|
|
|
@Query(() => [objectTypeCls], { name: `${domain}GetMany` })
|
|
@Authorized(auth)
|
|
@CheckOrganisation(organisationCheck)
|
|
async getMany(@Args((type) => argsType) args: ARGS, @Ctx() ctx: ApolloContext): Promise<T[]> {
|
|
const model = new objectTypeCls();
|
|
return getManyGenericWithArgs(model, args, ctx);
|
|
}
|
|
|
|
@Query(() => Number, { name: `${domain}GetCount` })
|
|
@Authorized(auth)
|
|
@CheckOrganisation(organisationCheck)
|
|
async getCount(@Args((type) => argsType) args: ARGS, @Ctx() ctx: ApolloContext): Promise<number> {
|
|
const model = new objectTypeCls();
|
|
return getCountGeneric(model, args, ctx);
|
|
}
|
|
|
|
/*
|
|
███╗ ███╗██╗ ██╗████████╗ █████╗ ████████╗ ██████╗ ██████╗ ███████╗
|
|
████╗ ████║██║ ██║╚══██╔══╝██╔══██╗╚══██╔══╝██╔═══██╗██╔══██╗██╔════╝
|
|
██╔████╔██║██║ ██║ ██║ ███████║ ██║ ██║ ██║██████╔╝███████╗
|
|
██║╚██╔╝██║██║ ██║ ██║ ██╔══██║ ██║ ██║ ██║██╔══██╗╚════██║
|
|
██║ ╚═╝ ██║╚██████╔╝ ██║ ██║ ██║ ██║ ╚██████╔╝██║ ██║███████║
|
|
╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝╚══════╝
|
|
*/
|
|
@Mutation(() => objectTypeCls, { name: `${domain}AddOne` })
|
|
@Authorized(auth)
|
|
@CheckOrganisation(organisationCheck)
|
|
async addOne(@Arg('input', (type) => newInput) input: NEW, @Ctx() ctx: ApolloContext): Promise<T> {
|
|
const model = new objectTypeCls();
|
|
if (model.onCreate) await model.onCreate(input);
|
|
|
|
return addOneGeneric(model, input, ctx);
|
|
}
|
|
|
|
@Mutation(() => objectTypeCls, { name: `${domain}EditOne` })
|
|
@Authorized(auth)
|
|
@CheckOrganisation(organisationCheck)
|
|
async editOne(@Arg('id') id: string, @Arg('input', (type) => editInput) input: EDIT, @Ctx() ctx: ApolloContext): Promise<T> {
|
|
const model = new objectTypeCls();
|
|
if (model.onUpdate) await model.onUpdate(input);
|
|
|
|
return editOneGeneric(model, id, input, ctx);
|
|
}
|
|
|
|
@Mutation(() => objectTypeCls, { name: `${domain}DeleteOne` })
|
|
@Authorized(auth)
|
|
@CheckOrganisation(organisationCheck)
|
|
async deleteOne(@Arg('id') id: string, @Ctx() ctx: ApolloContext): Promise<T> {
|
|
const model = new objectTypeCls();
|
|
if (model.onDelete) await model.onDelete();
|
|
|
|
return deleteOneGeneric(model, id, ctx);
|
|
}
|
|
}
|
|
|
|
return BaseResolver;
|
|
};
|