import { getCustomRepository } from 'typeorm'; import { AggregateProduct, AggregateProductVariant, RetailerProduct, RetailerProductVariant, } from '@/entities'; import { TypeormDataLoader } from '@/entities/utils'; import { RawRetailerProductRepository, RetailerProductVariantRepository, } from '@/repositories'; import { getAlgoliaProduct } from '.'; describe('algolia', () => { it('gets product', async () => { // first retailer, first variant await getCustomRepository(RawRetailerProductRepository).createFromData({ data: { price: 100, annotations: 'annotations', colorCode: 'blue', description: 'Blue couch', dimensions: '100"x100"', distributor: 'CostCo', distributorSKU: '1290-12', imageUrls: ['https://example.com/test1.png'], isAvailable: true, featured: true, name: 'Couch', url: 'https://www.luluandgeorgia.com/products/cylia-counter-stool?variant=39490236842083', category: 'couch', }, ingestionID: 'test12', productIds: ['product1'], }); // first retailer, second variant await getCustomRepository(RawRetailerProductRepository).createFromData({ data: { price: 200, annotations: 'annotations', colorCode: 'green', description: 'Green couch', dimensions: '100"x100"', distributor: 'CostCo', distributorSKU: '1290-12', imageUrls: ['https://example.com/test2.png'], isAvailable: true, featured: true, name: 'Couch', url: 'https://www.luluandgeorgia.com/products/cylia-counter-stool?variant=39490236842083555', }, ingestionID: 'test123', productIds: ['product1'], }); // second retailer, first variant await getCustomRepository(RawRetailerProductRepository).createFromData({ data: { price: 150, annotations: 'annotations', colorCode: 'blue', description: 'Blue couch', dimensions: '100"x100"', distributor: 'CostCo', distributorSKU: '1290-12', imageUrls: ['https://example.com/test1.png'], isAvailable: true, featured: true, name: 'Couch', url: 'https://www.wayfair.com/products/cylia-counter-stool?piid=39490236842083abc', category: 'couch', }, ingestionID: 'test12', productIds: ['product1'], }); expect(await AggregateProduct.count()).toBe(1); expect(await RetailerProduct.count()).toBe(2); expect(await AggregateProductVariant.count()).toBe(2); expect(await RetailerProductVariant.count()).toBe(3); const aggregateProduct = await AggregateProduct.findOneOrFail(); const aggregateProductVariant = await AggregateProductVariant.findOneOrFail( { where: { colorCode: 'blue' }, } ); const dbDataLoader = new TypeormDataLoader(); const algoliaProduct = await getAlgoliaProduct({ id: aggregateProduct.id, dbDataLoader, }); expect(algoliaProduct).toEqual({ objectID: aggregateProduct.id, variantID: aggregateProductVariant.id, name: 'Couch', imgUrl: 'https://example.com/test1.png', isAvailable: true, featured: true, variantsCount: 2, price: 100, lowestPrice: 100, highestPrice: 150, savingRatio: (150 - 100) / 150, retailers: ['LULU_AND_GEORGIA', 'WAYFAIR'], priceByRetailer: { LULU_AND_GEORGIA: 100, WAYFAIR: 150 }, lastModified: expect.any(Number), category: 'couch', }); }); it('returns algolia product if retailer is in whitelist', async () => { await getCustomRepository(RawRetailerProductRepository).createFromData({ data: { price: 100, annotations: 'annotations', colorCode: 'blue', description: 'Blue couch', dimensions: '100"x100"', distributor: 'CostCo', distributorSKU: '1290-12', imageUrls: ['https://example.com/test1.png'], isAvailable: true, name: 'Couch', url: 'https://www.luluandgeorgia.com/products/cylia-counter-stool?variant=39490236842083', category: 'couch', }, ingestionID: 'test12', productIds: ['product1'], }); await getCustomRepository(RawRetailerProductRepository).createFromData({ data: { price: 150, annotations: 'annotations', colorCode: 'blue', description: 'Blue couch', dimensions: '100"x100"', distributor: 'CostCo', distributorSKU: '1290-12', imageUrls: ['https://example.com/test1.png'], isAvailable: true, name: 'Couch', url: 'https://www.wayfair.com/products/cylia-counter-stool?piid=39490236842083abc', category: 'couch', }, ingestionID: 'test12', productIds: ['product1'], }); expect(await AggregateProduct.count()).toBe(1); expect(await RetailerProduct.count()).toBe(2); expect(await AggregateProductVariant.count()).toBe(1); expect(await RetailerProductVariant.count()).toBe(2); const aggregateProduct = await AggregateProduct.findOneOrFail(); const dbDataLoader = new TypeormDataLoader(); const algoliaProduct = await getAlgoliaProduct({ id: aggregateProduct.id, dbDataLoader, }); /** * luluandgeorgia.com is in the whitelist */ expect(algoliaProduct).not.toBe(null); }); it('returns null if retailer is not in whitelist', async () => { await getCustomRepository(RetailerProductVariantRepository).upsert({ data: { price: 100, annotations: 'annotations', colorCode: 'blue', description: 'Blue couch', dimensions: '100"x100"', distributor: 'CostCo', distributorSKU: '1290-12', imageUrls: ['https://example.com/test1.png'], isAvailable: true, name: 'Couch', url: 'https://www.discountbandit.com/products/cylia-counter-stool?variant=39490236842083', category: 'couch', }, productIdentifiers: ['prod-id-1'], variantIdentifiers: ['var-id-1'], retailer: 'DISCOUNT_BANDIT', }); await getCustomRepository(RetailerProductVariantRepository).upsert({ data: { price: 100, annotations: 'annotations', colorCode: 'blue', description: 'Blue couch', dimensions: '100"x100"', distributor: 'CostCo', distributorSKU: '1290-12', imageUrls: ['https://example.com/test1.png'], isAvailable: true, name: 'Couch', url: 'https://www.overpricedbandit.com/products/cylia-counter-stool?variant=39490236842083', category: 'couch', }, productIdentifiers: ['prod-id-1'], variantIdentifiers: ['var-id-1'], retailer: 'OVERPRICED_BANDIT', }); expect(await AggregateProduct.count()).toBe(1); expect(await RetailerProduct.count()).toBe(2); expect(await AggregateProductVariant.count()).toBe(1); expect(await RetailerProductVariant.count()).toBe(2); const aggregateProduct = await AggregateProduct.findOneOrFail(); const dbDataLoader = new TypeormDataLoader(); const algoliaProduct = await getAlgoliaProduct({ id: aggregateProduct.id, dbDataLoader, }); /** * discountbandit.com and overpricedbandit.com are not in the whitelist */ expect(algoliaProduct).toBe(null); }); it('returns null if product has only 1 retailer', async () => { await getCustomRepository(RawRetailerProductRepository).createFromData({ data: { price: 100, annotations: 'annotations', colorCode: 'blue', description: 'Blue couch', dimensions: '100"x100"', distributor: 'CostCo', distributorSKU: '1290-12', imageUrls: ['https://example.com/test1.png'], isAvailable: true, name: 'Couch', url: 'https://www.luluandgeorgia.com/products/cylia-counter-stool?variant=39490236842083', category: 'couch', }, ingestionID: 'test12', productIds: ['product1'], }); expect(await AggregateProduct.count()).toBe(1); expect(await RetailerProduct.count()).toBe(1); expect(await AggregateProductVariant.count()).toBe(1); expect(await RetailerProductVariant.count()).toBe(1); const aggregateProduct = await AggregateProduct.findOneOrFail(); const dbDataLoader = new TypeormDataLoader(); const algoliaProduct = await getAlgoliaProduct({ id: aggregateProduct.id, dbDataLoader, }); expect(algoliaProduct).toBe(null); }); });