import { EDBEntity , GraphQLPaths , TAuthRole , TPagedList , TProduct , TProductCategory } from '@cromwell/core' ; import { CreateProductCategory , DeleteManyInput , entityMetaRepository , PagedParamsInput , PagedProduct , PagedProductCategory , ProductCategory , ProductCategoryFilterInput , ProductCategoryRepository , ProductRepository , UpdateProductCategory , } from '@cromwell/core-backend' ; import { GraphQLJSONObject } from 'graphql-type-json' ; import { Arg , Authorized , FieldResolver , Int , Mutation , Query , Resolver , Root } from 'type-graphql' ; import { getCustomRepository } from 'typeorm' ; import { resetAllPagesCache } from '../helpers/reset-page' ; import { serverFireAction } from '../helpers/server-fire-action' ; const getOneBySlugPath = GraphQLPaths . ProductCategory . getOneBySlug ; const getOneByIdPath = GraphQLPaths . ProductCategory . getOneById ; const getManyPath = GraphQLPaths . ProductCategory . getMany ; const createPath = GraphQLPaths . ProductCategory . create ; const updatePath = GraphQLPaths . ProductCategory . update ; const deletePath = GraphQLPaths . ProductCategory . delete ; const deleteManyPath = GraphQLPaths . ProductCategory . deleteMany ; const deleteManyFilteredPath = GraphQLPaths . ProductCategory . deleteManyFiltered ; const getRootCategoriesPath = GraphQLPaths . ProductCategory . getRootCategories ; const getFilteredPath = GraphQLPaths . ProductCategory . getFiltered ; const productsKey = 'products' ; const parentKey = 'parent' ; const childrenKey = 'children' ; const viewsKey = 'views' ; @ Resolver ( ProductCategory ) export class ProductCategoryResolver { private repository = getCustomRepository ( ProductCategoryRepository ) ; private productRepository = getCustomRepository ( ProductRepository ) ; @ Query ( ( ) => PagedProductCategory ) async [ getManyPath ] ( @ Arg ( "pagedParams" ) pagedParams ) { return await this . repository . getProductCategories ( pagedParams ) ; } @ Query ( ( ) => ProductCategory ) async [ getOneBySlugPath ] ( @ Arg ( "slug" ) slug ) { return await this . repository . getProductCategoryBySlug ( slug ) ; } @ Query ( ( ) => ProductCategory ) async [ getOneByIdPath ] ( @ Arg ( "id" , ( ) => Int ) id ) { return await this . repository . getProductCategoryById ( id ) ; } @ Authorized < TAuthRole > ( "administrator" ) @ Mutation ( ( ) => ProductCategory ) async [ createPath ] ( @ Arg ( "data" ) data ) { const category = await this . repository . createProductCategory ( data ) ; serverFireAction ( 'create_product_category' , category ) ; resetAllPagesCache ( ) ; return category ; } @ Authorized < TAuthRole > ( "administrator" ) @ Mutation ( ( ) => ProductCategory ) async [ updatePath ] ( @ Arg ( "id" , ( ) => Int ) id , @ Arg ( "data" ) data ) { const category = await this . repository . updateProductCategory ( id , data ) ; serverFireAction ( 'update_product_category' , category ) ; resetAllPagesCache ( ) ; return category ; } @ Authorized < TAuthRole > ( "administrator" ) @ Mutation ( ( ) => Boolean ) async [ deletePath ] ( @ Arg ( "id" , ( ) => Int ) id ) { const category = await this . repository . deleteProductCategory ( id ) ; serverFireAction ( 'delete_product_category' , { id } ) ; resetAllPagesCache ( ) ; return category ; } @ Authorized < TAuthRole > ( "administrator" ) @ Mutation ( ( ) => Boolean ) async [ deleteManyPath ] ( @ Arg ( "data" ) data ) { return this . repository . deleteManyCategories ( data ) ; } @ Authorized < TAuthRole > ( "administrator" ) @ Mutation ( ( ) => Boolean ) async [ deleteManyFilteredPath ] ( @ Arg ( "input" ) input , @ Arg ( "filterParams" , { nullable : true } ) filterParams ? ) { const res = await this . repository . deleteManyCategories ( input , filterParams ) ; resetAllPagesCache ( ) ; return res ; } @ Query ( ( ) => PagedProductCategory ) async [ getRootCategoriesPath ] ( ) { return this . repository . getRootCategories ( ) ; } @ Query ( ( ) => PagedProductCategory ) async [ getFilteredPath ] ( @ Arg ( "pagedParams" , { nullable : true } ) pagedParams ? , @ Arg ( "filterParams" , { nullable : true } ) filterParams ? ) { return this . repository . getFilteredCategories ( pagedParams , filterParams ) ; } @ FieldResolver ( ( ) => PagedProduct ) async [ productsKey ] ( @ Root ( ) productCategory , @ Arg ( "pagedParams" ) pagedParams ) { return await this . productRepository . getProductsFromCategory ( productCategory . id , pagedParams ) ; } @ FieldResolver ( ( ) => ProductCategory , { nullable : true } ) async [ parentKey ] ( @ Root ( ) productCategory ) { return await this . repository . getParentCategory ( productCategory ) ; } @ FieldResolver ( ( ) => [ ProductCategory ] ) async [ childrenKey ] ( @ Root ( ) productCategory ) { return await this . repository . getChildCategories ( productCategory ) ; } @ FieldResolver ( ( ) => GraphQLJSONObject , { nullable : true } ) async customMeta ( @ Root ( ) entity , @ Arg ( "keys" , ( ) => [ String ] ) fields ) { return entityMetaRepository . getEntityMetaByKeys ( EDBEntity . ProductCategory , entity . id , fields ) ; } @ FieldResolver ( ( ) => Int , { nullable : true } ) async [ viewsKey ] ( @ Root ( ) entity ) { return this . repository . getEntityViews ( entity . id , EDBEntity . ProductCategory ) ; } }