Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compare graphql #456

Draft
wants to merge 3 commits into
base: development
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 29 additions & 21 deletions packages/headless/src/database/mongodb/compare/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,40 @@ export class CompareDatabase implements ICompareDatabase {
return compareList ? await this.mappedProductDetails(compareList) : null;
}

async addItemToCompare(
userId: string,
productId: CompareItems,
): Promise<Compare | null> {
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,
},
async addItemToCompare(userId: string, productId: CompareItems): Promise<Compare | null> {
try{
const productExists = await ProductModel.findOne({id: productId.productId});
if(!productExists){
return null;
}
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();

return compareList ? await this.mappedProductDetails(compareList) : null;
{ new: true },
).lean();

return compareList ? await this.mappedProductDetails(compareList) : null;
}
}catch(err){
console.log(err);
return err;
}
return null;
return null;
}

async getProductDetails(productId: string): Promise<CompareItems[] | null> {

const productDetails = await ProductModel.find({
id: productId,
}).select('info meta.friendlyPageName photos id -_id');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand All @@ -25,7 +26,7 @@ export const ResolveGraphqlModule = () => {
CategoryModule,
CustomerAuthModule,
MediaModule,
// CompareModule,
CompareModule,
WishListModule,
TagsModule,
CustomerModule,
Expand Down
37 changes: 0 additions & 37 deletions packages/headless/src/modules/compare/graphql/compare.graphql

This file was deleted.

103 changes: 103 additions & 0 deletions packages/headless/src/modules/compare/graphql/compare.model.ts
Original file line number Diff line number Diff line change
@@ -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[];
}




34 changes: 23 additions & 11 deletions packages/headless/src/modules/compare/graphql/compare.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,23 @@ 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) {}

/**
* 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,
Expand All @@ -35,36 +36,47 @@ 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,
) {
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,
) {
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
*/
Expand Down