import { Ctx, Field, ObjectType, UseMiddleware } from 'type-graphql'; import { Entity, Index, OneToMany } from 'typeorm'; import { ContextType } from '@/types'; import { getFilterRetailersUsingWhitelistMiddleware, RETAILER_DOMAIN_WHITELIST_SET, } from '@/utils/retailer'; import { AggregateProductVariant, RetailerProduct } from '..'; import { AbstractProductData } from '../abstract/AbstractProductData'; @Entity({ name: 'aggregate_products' }) @ObjectType({ implements: AbstractProductData.objectTypeImplements, }) @Index('category_index', ['category'], { background: true, }) export default class AggregateProduct extends AbstractProductData { @OneToMany(() => AggregateProductVariant, (variant) => variant.product) variants!: AggregateProductVariant[]; @Field(() => [AggregateProductVariant], { name: 'variants' }) async variantsResolver( @Ctx() { dbDataLoader }: ContextType ): Promise { const aggregateProductVariants = await dbDataLoader.aggregateProductVariantsByAggregateProductId.load( this.id ); const aggregateProductVariantsWithImages = aggregateProductVariants.filter( (variant) => variant.imageUrls.length ); return aggregateProductVariantsWithImages; } @OneToMany( () => RetailerProduct, (retailerProduct) => retailerProduct.aggregateProduct ) retailerProducts!: RetailerProduct[]; @Field(() => [RetailerProduct], { name: 'retailerProducts' }) @UseMiddleware( getFilterRetailersUsingWhitelistMiddleware(RETAILER_DOMAIN_WHITELIST_SET) ) async retailerProductsResolver( @Ctx() { dbDataLoader }: ContextType ): Promise { return dbDataLoader.retailerProductsByAggregateProductId.load(this.id); } }