2025-05-14 21:45:16 +02:00

74 lines
3.9 KiB
TypeScript

import { Arg, Authorized, Ctx, Mutation, Query, Resolver } from 'type-graphql';
import CountryModel from '../country.model';
import { createEngineQueryResolver, createEngineMutationResolver } from '@seed/engine/genericResolvers/BaseEngineResolver';
import { CountryArgsSchema, CountryEditInputSchema, CountryNewInputSchema } from '../schemas/country.input';
import { CountrySchema } from '../schemas/country.schema';
import { ApolloContext } from '@seed/interfaces/context';
import { AccountTypeEnum } from '@src/accounts/account.components';
import { importFromJSON } from '../country.service';
import { SimpleResult } from '@seed/interfaces/components';
const CountryGenericQueryResolver = createEngineQueryResolver({
domain: 'country',
schemaName: CountrySchema,
modelName: CountryModel,
argsType: CountryArgsSchema,
engineMiddleware: {
noPermissionCheck: false,
authorization: [AccountTypeEnum.admin],
},
});
const CountryGenericMutationResolver = createEngineMutationResolver({
domain: 'country',
schemaName: CountrySchema,
modelName: CountryModel,
editInput: CountryEditInputSchema,
newInput: CountryNewInputSchema,
engineMiddleware: {
noPermissionCheck: false,
authorization: [AccountTypeEnum.admin],
},
});
@Resolver(CountrySchema)
export class CountryQueryResolver extends CountryGenericQueryResolver {
/*
██████╗ ██╗ ██╗███████╗██████╗ ██╗ ██╗
██╔═══██╗██║ ██║██╔════╝██╔══██╗╚██╗ ██╔╝
██║ ██║██║ ██║█████╗ ██████╔╝ ╚████╔╝
██║▄▄ ██║██║ ██║██╔══╝ ██╔══██╗ ╚██╔╝
╚██████╔╝╚██████╔╝███████╗██║ ██║ ██║
╚══▀▀═╝ ╚═════╝ ╚══════╝╚═╝ ╚═╝ ╚═╝
*/
}
@Resolver(CountrySchema)
export class CountryMutationResolver extends CountryGenericMutationResolver {
/*
███╗ ███╗██╗ ██╗████████╗ █████╗ ████████╗ ██████╗ ██████╗ ███████╗
████╗ ████║██║ ██║╚══██╔══╝██╔══██╗╚══██╔══╝██╔═══██╗██╔══██╗██╔════╝
██╔████╔██║██║ ██║ ██║ ███████║ ██║ ██║ ██║██████╔╝███████╗
██║╚██╔╝██║██║ ██║ ██║ ██╔══██║ ██║ ██║ ██║██╔══██╗╚════██║
██║ ╚═╝ ██║╚██████╔╝ ██║ ██║ ██║ ██║ ╚██████╔╝██║ ██║███████║
╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝╚══════╝
*/
@Mutation(() => SimpleResult)
@Authorized(AccountTypeEnum.admin)
async countryImportMany(@Ctx() ctx: ApolloContext): Promise<SimpleResult> {
const dataToImport: CountrySchema[] = importFromJSON(ctx);
const model = new CountryModel();
try {
await (await model.db()).insertMany(dataToImport, { ordered: false });
return { message: 'imported' };
} catch (error) {
throw error;
}
}
}
export const CountryAdminResolvers = [CountryQueryResolver, CountryMutationResolver];