import { EDBEntity , GraphQLPaths , TAuthRole , TFilteredProductList , TPagedList , TProduct , TProductCategory , TProductRating , TProductReview , } from '@cromwell/core' ; import { AttributeInstance , CreateProduct , DeleteManyInput , entityMetaRepository , FilteredProduct , PagedParamsInput , PagedProduct , PagedProductReview , Product , ProductCategory , ProductCategoryRepository , ProductFilterInput , ProductRating , ProductRepository , ProductVariant , UpdateProduct , } 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 categoriesKey = 'categories' ; const ratingKey = 'rating' ; const reviewsKey = 'reviews' ; const viewsKey = 'views' ; const attributesKey = 'attributes' ; const variantsKey = 'variants' ; const getOneBySlugPath = GraphQLPaths . Product . getOneBySlug ; const getOneByIdPath = GraphQLPaths . Product . getOneById ; const getManyPath = GraphQLPaths . Product . getMany ; const createPath = GraphQLPaths . Product . create ; const updatePath = GraphQLPaths . Product . update ; const deletePath = GraphQLPaths . Product . delete ; const deleteManyPath = GraphQLPaths . Product . deleteMany ; const deleteManyFilteredPath = GraphQLPaths . Product . deleteManyFiltered ; const getFromCategoryPath = GraphQLPaths . Product . getFromCategory ; const getFilteredPath = GraphQLPaths . Product . getFiltered ; @ Resolver ( Product ) export class ProductResolver { private repository = getCustomRepository ( ProductRepository ) ; @ Query ( ( ) => PagedProduct ) async [ getManyPath ] ( @ Arg ( "pagedParams" , { nullable : true } ) pagedParams ? ) { return this . repository . getProducts ( pagedParams ) ; } @ Query ( ( ) => Product , { nullable : true } ) async [ getOneBySlugPath ] ( @ Arg ( "slug" ) slug ) { return this . repository . getProductBySlug ( slug , { withRating : true } ) ; } @ Query ( ( ) => Product , { nullable : true } ) async [ getOneByIdPath ] ( @ Arg ( "id" , ( ) => Int ) id ) { return this . repository . getProductById ( id , { withRating : true } ) ; } @ Authorized < TAuthRole > ( "administrator" ) @ Mutation ( ( ) => Product ) async [ createPath ] ( @ Arg ( "data" ) data ) { const product = await this . repository . createProduct ( data ) ; serverFireAction ( 'create_product' , product ) ; resetAllPagesCache ( ) ; return product ; } @ Authorized < TAuthRole > ( "administrator" ) @ Mutation ( ( ) => Product ) async [ updatePath ] ( @ Arg ( "id" , ( ) => Int ) id , @ Arg ( "data" ) data ) { const product = await this . repository . updateProduct ( id , data ) ; serverFireAction ( 'update_product' , product ) ; resetAllPagesCache ( ) ; return product ; } @ Authorized < TAuthRole > ( "administrator" ) @ Mutation ( ( ) => Boolean ) async [ deletePath ] ( @ Arg ( "id" , ( ) => Int ) id ) { const product = await this . repository . deleteProduct ( id ) ; serverFireAction ( 'update_product' , { id } ) ; resetAllPagesCache ( ) ; return product ; } @ Authorized < TAuthRole > ( "administrator" ) @ Mutation ( ( ) => Boolean ) async [ deleteManyPath ] ( @ Arg ( "data" ) data ) { const res = await this . repository . deleteMany ( data ) ; resetAllPagesCache ( ) ; return res ; } @ Authorized < TAuthRole > ( "administrator" ) @ Mutation ( ( ) => Boolean ) async [ deleteManyFilteredPath ] ( @ Arg ( "input" ) input , @ Arg ( "filterParams" , { nullable : true } ) filterParams ? ) { const res = await this . repository . deleteManyFilteredProducts ( input , filterParams ) ; resetAllPagesCache ( ) ; return res ; } @ Query ( ( ) => PagedProduct ) async [ getFromCategoryPath ] ( @ Arg ( "categoryId" , ( ) => Int ) categoryId , @ Arg ( "pagedParams" ) pagedParams ) { return this . repository . getProductsFromCategory ( categoryId , pagedParams ) ; } @ Query ( ( ) => FilteredProduct ) async [ getFilteredPath ] ( @ Arg ( "pagedParams" , { nullable : true } ) pagedParams ? , @ Arg ( "filterParams" , { nullable : true } ) filterParams ? ) { return this . repository . getFilteredProducts ( pagedParams , filterParams ) ; } @ FieldResolver ( ( ) => [ ProductCategory ] , { nullable : true } ) async [ categoriesKey ] ( @ Root ( ) product , @ Arg ( "pagedParams" ) pagedParams ) { return getCustomRepository ( ProductCategoryRepository ) . getCategoriesOfProduct ( product . id , pagedParams ) ; } @ FieldResolver ( ( ) => PagedProductReview ) async [ reviewsKey ] ( @ Root ( ) product , @ Arg ( "pagedParams" ) pagedParams ) { return this . repository . getReviewsOfProduct ( product . id , pagedParams ) ; } @ FieldResolver ( ( ) => ProductRating ) async [ ratingKey ] ( @ Root ( ) product ) { return { reviewsNumber : product . reviewsCount , average : product . averageRating , } ; } @ FieldResolver ( ( ) => GraphQLJSONObject , { nullable : true } ) async customMeta ( @ Root ( ) entity , @ Arg ( "keys" , ( ) => [ String ] ) fields ) { return entityMetaRepository . getEntityMetaByKeys ( EDBEntity . Product , entity . id , fields ) ; } @ FieldResolver ( ( ) => Int , { nullable : true } ) async [ viewsKey ] ( @ Root ( ) product ) { return this . repository . getEntityViews ( product . id , EDBEntity . Product ) ; } @ FieldResolver ( ( ) => [ AttributeInstance ] , { nullable : true } ) async [ attributesKey ] ( @ Root ( ) product ) { return this . repository . getProductAttributes ( product . id ) ; } @ FieldResolver ( ( ) => [ ProductVariant ] , { nullable : true } ) async [ variantsKey ] ( @ Root ( ) product ) { return this . repository . getProductVariantsOfProduct ( product . id ) ; } }