backend/lib/seed/graphql/baseResolvers/BasePublicResolver.ts
2025-05-14 21:45:16 +02:00

144 lines
7.9 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 { totalPublic } from '../Middleware';
export const createBasePublicResolver = <T extends ClassType<any>, ARGS extends ClassType<any>>(
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<T> {
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<T[]> {
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<number> {
const model = new objectTypeCls();
return getCountGeneric(model, args, ctx);
}
}
return BasePublicResolver;
};
export const createBaseMyResolver = <T extends ClassType<any>, ARGS extends ClassType<any>>(
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<T> {
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<T[]> {
const model = new objectTypeCls();
return getManyGenericWithArgs(model, args, ctx);
}
}
return BaseMyResolver;
};
export const createBaseReadOnlyResolver = <T extends ClassType<any>, ARGS extends ClassType<any>>(
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<T> {
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<T[]> {
const model = new objectTypeCls();
return getManyGenericWithArgs(model, args, ctx);
}
}
return BaseReadOnlyResolver;
};