From c03e6fe788fcfc0df8aabe21080c4280e069af87 Mon Sep 17 00:00:00 2001 From: tahsinZilani Date: Thu, 15 Sep 2022 00:27:59 +0600 Subject: [PATCH 1/2] compare graphql --- .../src/database/mongodb/compare/index.ts | 46 +++++--- .../graphql/graphql.module.resolver.ts | 2 +- .../modules/compare/graphql/compare.graphql | 37 ------- .../modules/compare/graphql/compare.model.ts | 103 ++++++++++++++++++ .../compare/graphql/compare.resolver.ts | 34 ++++-- 5 files changed, 156 insertions(+), 66 deletions(-) delete mode 100644 packages/headless/src/modules/compare/graphql/compare.graphql create mode 100644 packages/headless/src/modules/compare/graphql/compare.model.ts diff --git a/packages/headless/src/database/mongodb/compare/index.ts b/packages/headless/src/database/mongodb/compare/index.ts index 1fb802767..060764d26 100644 --- a/packages/headless/src/database/mongodb/compare/index.ts +++ b/packages/headless/src/database/mongodb/compare/index.ts @@ -21,34 +21,46 @@ export class CompareDatabase implements ICompareDatabase { } async addItemToCompare(userId: string, productId: CompareItems): Promise { - const isExist = await CompareModel.findOne({items: productId}); - if(!isExist){ - const compareList = await CompareModel.findOneAndUpdate( - { userId: userId }, - { - $push: { - items: { - $each: [productId], - $sort: { created_at: 1}, - $slice: -3 - } - } - }, - { new: true }, - ).lean(); + try{ + const productExists = await ProductModel.findOne({id: productId.productId}); + if(!productExists){ + return null; + } + const isExist = await CompareModel.findOne({items: productId}); - return compareList ? await this.mappedProductDetails(compareList) : null; + if(!isExist){ + const compareList = await CompareModel.findOneAndUpdate( + { userId: userId }, + { + $push: { + items: { + $each: [productId], + $sort: { created_at: 1}, + $slice: -3 + } + } + }, + { new: true }, + ).lean(); + + return compareList ? await this.mappedProductDetails(compareList) : null; + } + }catch(err){ + console.log(err); + return err; } return null; } async getProductDetails(productId: string): Promise { + const productDetails = await ProductModel.find({ id: productId }).select('info meta.friendlyPageName photos id -_id'); if(!productDetails) return null; - const mappedItems = productDetails.map((e) => { + + const mappedItems = productDetails.map((e) => { const { name, price, shortDescription, fullDescription, oldPrice } = e.info; return { productId: e.id, diff --git a/packages/headless/src/internal/graphql/graphql.module.resolver.ts b/packages/headless/src/internal/graphql/graphql.module.resolver.ts index 8f2b0cb0a..25eaa13df 100644 --- a/packages/headless/src/internal/graphql/graphql.module.resolver.ts +++ b/packages/headless/src/internal/graphql/graphql.module.resolver.ts @@ -26,7 +26,7 @@ export const ResolveGraphqlModule = () => { CategoryModule, CustomerAuthModule, MediaModule, - // CompareModule, + CompareModule, WishListModule, TagsModule, CustomerModule, diff --git a/packages/headless/src/modules/compare/graphql/compare.graphql b/packages/headless/src/modules/compare/graphql/compare.graphql deleted file mode 100644 index 84e43d84a..000000000 --- a/packages/headless/src/modules/compare/graphql/compare.graphql +++ /dev/null @@ -1,37 +0,0 @@ -type Comapre { - id: String! - userId: String! - items: [String!]! -} - -type DeleteResponse { - success: Boolean -} - -input ProductIdInput { - productId: String! -} - -type ComapreResponse { - error: String - code: Int! - data: Comapre -} - -type DeleteCompareByIdResponse { - error: String - code: Int! - data: DeleteResponse -} - -type Query { - getCompareByUserId: ComapreResponse! - getCompareById(compareId: String!): ComapreResponse! -} - -type Mutation { - addItemToCompare(body: ProductIdInput!): ComapreResponse! - deleteCompareById(compareId: String!): DeleteCompareByIdResponse - deleteItemByProductId(productId: String!): ComapreResponse! - deleteAllItemByUserId: ComapreResponse! -} diff --git a/packages/headless/src/modules/compare/graphql/compare.model.ts b/packages/headless/src/modules/compare/graphql/compare.model.ts new file mode 100644 index 000000000..63f58b390 --- /dev/null +++ b/packages/headless/src/modules/compare/graphql/compare.model.ts @@ -0,0 +1,103 @@ +import { ObjectType, Field, Int, InputType } from '@nestjs/graphql'; +import { IsNotEmpty } from 'class-validator'; +import { + AddCompareItem, + CompareData, + ICompareItems, + IProductDetails, + IProductInfo, + IProductMeta +} from '@bs-commerce/models'; +import { UseGuards } from '@nestjs/common'; +import { RolesGuard } from 'src/guards/auth.guard'; + +@ObjectType({ description: 'Product Info Model'}) +export class ProductInfoModel implements IProductInfo { + @Field({ nullable: false}) + name: string; + + @Field({ nullable: false}) + price: number; + + @Field({ nullable: false}) + shortDescription: string; + + @Field({ nullable: false}) + fullDescription: string; + + @Field({ nullable: false}) + oldPrice: number; +} + +@ObjectType({ description: 'Product Meta Model'}) +export class ProductMetaModel implements IProductMeta { + @Field({ nullable: true}) + friendlyPageName?: string; +} + +@ObjectType({ description: 'Commpare Products Details Model'}) +export class ProductDetailsModel implements IProductDetails{ + @Field(() => ProductInfoModel,{ nullable: false}) + info: IProductInfo; + + @Field(() => ProductMetaModel,{ nullable: false}) + meta: IProductMeta; + + @Field(() => [String],{ nullable: false}) + photos: string[]; +} + +@ObjectType({ description: 'Commpare Items Model'}) +export class CompareItemsModel implements ICompareItems{ + @Field({ nullable: false}) + productId: string; + + @Field((type) => ProductDetailsModel,{ nullable: false}) + productDetails?: ProductDetailsModel; +} + +@ObjectType({ description: 'Compare Model'}) +export class CompareDataModel implements CompareData{ + @Field({ nullable: false}) + id: string; + + @Field({ nullable: false}) + userId: string; + + @Field((type)=> [CompareItemsModel]) + items: CompareItemsModel[] +} + +@InputType({ description: 'Add Compare Item Input Model'}) +export class AddCompareItemModel implements AddCompareItem { + @Field({ nullable: false}) + productId: string; +} + +@ObjectType({ description: 'Compare Response' }) +export class CompareResponseModel { + @Field({ nullable: true }) + error?: string; + + @Field((type) => Int, { nullable: false }) + code: number; + + @Field((type) => CompareDataModel, { nullable: true }) + data?: CompareDataModel; +} + +@ObjectType({ description: 'Compare Public Response' }) +export class ComparePublicResponseModel { + @Field({ nullable: true }) + error?: string; + + @Field((type) => Int, { nullable: false }) + code: number; + + @Field((type) => [CompareItemsModel], { nullable: true }) + data?: CompareItemsModel[]; +} + + + + diff --git a/packages/headless/src/modules/compare/graphql/compare.resolver.ts b/packages/headless/src/modules/compare/graphql/compare.resolver.ts index 8096ef85d..be9759134 100644 --- a/packages/headless/src/modules/compare/graphql/compare.resolver.ts +++ b/packages/headless/src/modules/compare/graphql/compare.resolver.ts @@ -5,8 +5,8 @@ import { User } from 'src/entity/user'; import { RolesGuard } from 'src/guards/auth.guard'; import { User as UserInfo } from 'src/decorators/auth.decorator'; import { CompareService } from '../services'; - -@UseGuards(new RolesGuard(['customer'])) +import { AddCompareItemModel, ComparePublicResponseModel, CompareResponseModel } from './compare.model'; +import { AddCompareItem } from '@bs-commerce/models'; @Resolver() export class GqlCompareResolver { constructor(private compareService: CompareService) {} @@ -14,13 +14,14 @@ export class GqlCompareResolver { /** * Query Start */ - - @Query() + @UseGuards(new RolesGuard(['customer'])) + @Query(returns => CompareResponseModel) async getCompareByUserId(@UserInfo() user: User) { return await this.compareService.getCompareByUserId(user.id); } - @Query() + @UseGuards(new RolesGuard(['customer'])) + @Query(returns => CompareResponseModel) async getCompareById( @Args('compareId') compareId: string, @UserInfo() user: User, @@ -35,16 +36,18 @@ export class GqlCompareResolver { /** * Mutation Start */ - - @Mutation() + @UseGuards(new RolesGuard(['customer'])) + @Mutation(returns => CompareResponseModel) async addItemToCompare( @UserInfo() user: User, - @Args('body') body: CompareItems, + @Args('body') body: AddCompareItemModel, ) { + console.log('sds') return await this.compareService.addItemToCompare(user.id, body.productId); } - @Mutation() + @UseGuards(new RolesGuard(['customer'])) + @Mutation(returns => CompareResponseModel) async deleteCompareById( @UserInfo() user: User, @Args('compareId') compareId: string, @@ -52,7 +55,8 @@ export class GqlCompareResolver { return await this.compareService.deleteCompareById(user.id, compareId); } - @Mutation() + @UseGuards(new RolesGuard(['customer'])) + @Mutation(returns => CompareResponseModel) async deleteItemByProductId( @UserInfo() user: User, @Args('productId') productId: string, @@ -60,11 +64,19 @@ export class GqlCompareResolver { return await this.compareService.deleteItemByProductId(user.id, productId); } - @Mutation() + @UseGuards(new RolesGuard(['customer'])) + @Mutation(returns => CompareResponseModel) async deleteAllItemByUserId(@UserInfo() user: User) { return await this.compareService.deleteAllItemByUserId(user.id); } + @Mutation(returns => ComparePublicResponseModel) + async getCompareProduct( + @Args('body') body: AddCompareItemModel + ) { + return await this.compareService.getProductDetails(body.productId); + } + /** * Mutation End */ From 28809a4b96553a33972bd6e1278a3501ec5f3a22 Mon Sep 17 00:00:00 2001 From: tahsinZilani Date: Thu, 15 Sep 2022 12:43:14 +0600 Subject: [PATCH 2/2] compare import addded in grapghql --- .../headless/src/internal/graphql/graphql.module.resolver.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/headless/src/internal/graphql/graphql.module.resolver.ts b/packages/headless/src/internal/graphql/graphql.module.resolver.ts index 8ee5fbe69..59b3a41a3 100644 --- a/packages/headless/src/internal/graphql/graphql.module.resolver.ts +++ b/packages/headless/src/internal/graphql/graphql.module.resolver.ts @@ -12,6 +12,7 @@ import { WishListModule } from 'src/modules/wishlist/wishlist.graphql.module'; import { TagsModule } from 'src/modules/tags/tags.graphql.module'; import { CustomerModule } from 'src/modules/customer/customer.graphql.module'; import { OrderModule } from 'src/modules/order/order.graphql.module'; +import { CompareModule } from 'src/modules/compare/compare.graphql.module'; export const ResolveGraphqlModule = () => { return [