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 { totalPublic } from '../Middleware'; export const createBasePublicResolver = , ARGS extends ClassType>( domain: string, objectTypeCls: T, argsType: ARGS, pub = false, ): any => { @Resolver({ isAbstract: true }) abstract class BasePublicResolver { /* ███████╗██╗███████╗██╗ ██████╗ ███████╗ ██╔════╝██║██╔════╝██║ ██╔══██╗██╔════╝ █████╗ ██║█████╗ ██║ ██║ ██║███████╗ ██╔══╝ ██║██╔══╝ ██║ ██║ ██║╚════██║ ██║ ██║███████╗███████╗██████╔╝███████║ ╚═╝ ╚═╝╚══════╝╚══════╝╚═════╝ ╚══════╝ */ /* ██████╗ ██╗ ██╗███████╗██████╗ ██╗ ██╗ ██╔═══██╗██║ ██║██╔════╝██╔══██╗╚██╗ ██╔╝ ██║ ██║██║ ██║█████╗ ██████╔╝ ╚████╔╝ ██║▄▄ ██║██║ ██║██╔══╝ ██╔══██╗ ╚██╔╝ ╚██████╔╝╚██████╔╝███████╗██║ ██║ ██║ ╚══▀▀═╝ ╚═════╝ ╚══════╝╚═╝ ╚═╝ ╚═╝ */ @Query(() => objectTypeCls, { name: `${domain}GetOne` }) @totalPublic(pub) async getOne(@Arg('id') id: string, @Ctx() ctx: ApolloContext): Promise { const model = new objectTypeCls(); return getOneGeneric(model, id, ctx); } @Query(() => [objectTypeCls], { name: `${domain}GetMany` }) @totalPublic(pub) async getMany(@Args((type) => argsType) args: ARGS, @Ctx() ctx: ApolloContext): Promise { const model = new objectTypeCls(); return getManyGenericWithArgs(model, args, ctx); } @Query(() => Number, { name: `${domain}GetCount` }) @totalPublic(pub) async getCount(@Args((type) => argsType) args: ARGS, @Ctx() ctx: ApolloContext): Promise { const model = new objectTypeCls(); return getCountGeneric(model, args, ctx); } } return BasePublicResolver; }; export const createBaseMyResolver = , ARGS extends ClassType>( domain: string, objectTypeCls: T, argsType: ARGS, ): any => { @Resolver({ isAbstract: true }) abstract class BaseMyResolver { /* ███████╗██╗███████╗██╗ ██████╗ ███████╗ ██╔════╝██║██╔════╝██║ ██╔══██╗██╔════╝ █████╗ ██║█████╗ ██║ ██║ ██║███████╗ ██╔══╝ ██║██╔══╝ ██║ ██║ ██║╚════██║ ██║ ██║███████╗███████╗██████╔╝███████║ ╚═╝ ╚═╝╚══════╝╚══════╝╚═════╝ ╚══════╝ */ /* ██████╗ ██╗ ██╗███████╗██████╗ ██╗ ██╗ ██╔═══██╗██║ ██║██╔════╝██╔══██╗╚██╗ ██╔╝ ██║ ██║██║ ██║█████╗ ██████╔╝ ╚████╔╝ ██║▄▄ ██║██║ ██║██╔══╝ ██╔══██╗ ╚██╔╝ ╚██████╔╝╚██████╔╝███████╗██║ ██║ ██║ ╚══▀▀═╝ ╚═════╝ ╚══════╝╚═╝ ╚═╝ ╚═╝ */ @Query(() => objectTypeCls, { name: `${domain}GetOneMine` }) @Authorized() async getMineOne(@Arg('id') id: string, @Ctx() ctx: ApolloContext): Promise { const model = new objectTypeCls(); return getOneGeneric(model, id, ctx); } @Query(() => [objectTypeCls], { name: `${domain}GetManyMine` }) @Authorized() async getMine(@Args((type) => argsType) args: ARGS, @Ctx() ctx: ApolloContext): Promise { const model = new objectTypeCls(); return getManyGenericWithArgs(model, args, ctx); } } return BaseMyResolver; }; export const createBaseReadOnlyResolver = , ARGS extends ClassType>( domain: string, objectTypeCls: T, argsType: ARGS, auth: AccountTypeEnum[], ): any => { @Resolver({ isAbstract: true }) abstract class BaseReadOnlyResolver { /* ███████╗██╗███████╗██╗ ██████╗ ███████╗ ██╔════╝██║██╔════╝██║ ██╔══██╗██╔════╝ █████╗ ██║█████╗ ██║ ██║ ██║███████╗ ██╔══╝ ██║██╔══╝ ██║ ██║ ██║╚════██║ ██║ ██║███████╗███████╗██████╔╝███████║ ╚═╝ ╚═╝╚══════╝╚══════╝╚═════╝ ╚══════╝ */ /* ██████╗ ██╗ ██╗███████╗██████╗ ██╗ ██╗ ██╔═══██╗██║ ██║██╔════╝██╔══██╗╚██╗ ██╔╝ ██║ ██║██║ ██║█████╗ ██████╔╝ ╚████╔╝ ██║▄▄ ██║██║ ██║██╔══╝ ██╔══██╗ ╚██╔╝ ╚██████╔╝╚██████╔╝███████╗██║ ██║ ██║ ╚══▀▀═╝ ╚═════╝ ╚══════╝╚═╝ ╚═╝ ╚═╝ */ @Query(() => objectTypeCls, { name: `${domain}GetOne` }) @Authorized(auth) async getOne(@Arg('id') id: string, @Ctx() ctx: ApolloContext): Promise { const model = new objectTypeCls(); return getOneGeneric(model, id, ctx); } @Query(() => [objectTypeCls], { name: `${domain}GetMany` }) @Authorized(auth) async getMany(@Args((type) => argsType) args: ARGS, @Ctx() ctx: ApolloContext): Promise { const model = new objectTypeCls(); return getManyGenericWithArgs(model, args, ctx); } } return BaseReadOnlyResolver; };