import { getCustomRepository } from 'typeorm'; import { AggregateProduct, AggregateProductVariant, RetailerProduct, RetailerProductVariant, } from '@/entities'; import { RetailerProductSource } from '@/entities/types'; import { RetailerProductRepository } from '@/repositories/RetailerProductRepository'; import { RetailerKind } from '@/types'; import RetailerProductVariantRepository from '../RetailerProductVariantRepository'; describe('RetailerProductVariantRepository', () => { let retailerProductRepo: RetailerProductRepository; let retailerProductVariantRepo: RetailerProductVariantRepository; beforeEach(async () => { retailerProductRepo = getCustomRepository(RetailerProductRepository); retailerProductVariantRepo = getCustomRepository( RetailerProductVariantRepository ); }); describe('CRUD', () => { it('should be able to create new populated retailer product', async () => { const { retailerProductId } = await retailerProductRepo.upsert({ retailer: RetailerKind.PERIGOLD, productIdentifiers: ['test'], source: RetailerProductSource.GOOGLE_SHOPPING, data: { asin: 'asin', colorCode: 'colorCode', annotations: 'annotation', price: 100, description: 'description', dimensions: 'dimensions', discoverable: true, distributor: 'distributor', distributorSKU: 'distributor', imageUrls: ['http://test.com/test.jpg'], isAvailable: true, name: 'test', url: 'https://www.amazon.com/Haussmann-Twist-Stool-High-Walnut/dp/B00KRY0SY2', }, }); const retailerProduct = await RetailerProduct.findOneOrFail( retailerProductId ); expect(retailerProduct).toMatchObject({ id: expect.any(String), identifiers: ['test'], retailer: RetailerKind.PERIGOLD, asin: 'asin', colorCode: 'colorCode', annotations: 'annotation', price: 100, description: 'description', dimensions: 'dimensions', discoverable: true, distributor: 'distributor', distributorSKU: 'distributor', imageUrls: ['http://test.com/test.jpg'], isAvailable: true, name: 'test', source: RetailerProductSource.GOOGLE_SHOPPING, url: 'https://www.amazon.com/Haussmann-Twist-Stool-High-Walnut/dp/B00KRY0SY2', }); const { retailerProductVariantId } = await retailerProductVariantRepo.upsert({ source: RetailerProductSource.GOOGLE_SHOPPING, retailer: RetailerKind.PERIGOLD, productIdentifiers: ['test'], variantIdentifiers: ['test1-x'], data: { asin: 'asin', colorCode: 'colorCode', annotations: 'annotation', price: 100, description: 'description', dimensions: 'dimensions', discoverable: true, distributor: 'distributor', distributorSKU: 'distributor', imageUrls: ['http://test.com/test.jpg'], isAvailable: true, name: 'test', url: 'https://www.amazon.com/Haussmann-Twist-Stool-High-Walnut/dp/B00KRY0SY2', }, }); const retailerProductVariant = await RetailerProductVariant.findOneOrFail( retailerProductVariantId ); expect(retailerProductVariant).toMatchObject({ id: expect.any(String), source: RetailerProductSource.GOOGLE_SHOPPING, identifiers: ['test1-x'], retailer: RetailerKind.PERIGOLD, asin: 'asin', colorCode: 'colorCode', annotations: 'annotation', price: 100, description: 'description', dimensions: 'dimensions', discoverable: true, distributor: 'distributor', distributorSKU: 'distributor', imageUrls: ['http://test.com/test.jpg'], isAvailable: true, name: 'test', url: 'https://www.amazon.com/Haussmann-Twist-Stool-High-Walnut/dp/B00KRY0SY2', }); }); }); it('handles bulk upserting multiple products and variants', async () => { await retailerProductVariantRepo.bulkUpsert({ data: [ { retailer: RetailerKind.LULU_AND_GEORGIA, productIdentifiers: ['randomid'], variantIdentifiers: ['394902368420831'], data: { asin: 'asin', colorCode: 'colorCode', annotations: 'annotation', price: 100, description: 'description', dimensions: 'dimensions', discoverable: true, distributor: 'distributor', distributorSKU: 'distributor', imageUrls: [ 'http://test.com/test.jpg', 'http://test.com/test2.jpg', ], isAvailable: true, name: 'test', url: 'https://www.luluandgeorgia.com/products/cylia-counter-stool?variant=394902368420831', }, }, { retailer: RetailerKind.LULU_AND_GEORGIA, productIdentifiers: ['randomid'], variantIdentifiers: ['394902368420831'], data: { asin: 'asin', colorCode: 'colorCode', annotations: 'annotation', price: 100, description: 'description', dimensions: 'dimensions', discoverable: true, distributor: 'distributor', distributorSKU: 'distributor', imageUrls: [ 'http://test.com/test.jpg', 'http://test.com/test2.jpg', ], isAvailable: true, name: 'test', url: 'https://www.luluandgeorgia.com/products/cylia-counter-stool?variant=394902368420831', // duplicate }, }, { retailer: RetailerKind.LULU_AND_GEORGIA, productIdentifiers: ['randomid'], variantIdentifiers: ['394902368420831hihi'], data: { asin: 'asin', colorCode: 'colorCode', annotations: 'annotation', price: 100, description: 'description', dimensions: 'dimensions', discoverable: true, distributor: 'distributor', distributorSKU: 'distributor', imageUrls: ['http://test.com/test.jpg'], isAvailable: true, name: 'test', url: 'https://www.luluandgeorgia.com/products/cylia-counter-stool?variant=39490236842083hihi', // different variant of above product }, }, { retailer: RetailerKind.LULU_AND_GEORGIA, productIdentifiers: ['randomid12'], variantIdentifiers: ['3949023684208asdfasdf'], data: { asin: 'asin', colorCode: 'colorCode', annotations: 'annotation', price: 100, description: 'description', dimensions: 'dimensions', discoverable: true, distributor: 'distributor', distributorSKU: 'distributor', imageUrls: ['http://test.com/test.jpg'], isAvailable: true, name: 'test', url: 'https://www.luluandgeorgia.com/products/cylia-counter-table?variant=3949023684208asdfasdf', // different product }, }, ], }); expect(await AggregateProduct.count()).toBe(2); expect(await AggregateProductVariant.count()).toBe(3); expect(await RetailerProduct.count()).toBe(2); expect(await RetailerProductVariant.count()).toBe(3); const aggregateProducts = await AggregateProduct.find(); expect(aggregateProducts).toContainEqual({ id: expect.any(String), createdAt: expect.any(Date), updatedAt: expect.any(Date), deletedAt: null, identifiers: ['randomid'], discoverable: true, asin: 'asin', dimensions: 'dimensions', colorCode: 'colorCode', annotations: 'annotation', description: 'description', name: 'test', price: 100, isAvailable: true, featured: false, distributor: 'distributor', distributorSKU: 'distributor', category: null, imageUrls: ['http://test.com/test.jpg', 'http://test.com/test2.jpg'], }); expect(aggregateProducts).toContainEqual({ id: expect.any(String), createdAt: expect.any(Date), updatedAt: expect.any(Date), deletedAt: null, identifiers: ['randomid12'], discoverable: true, asin: 'asin', dimensions: 'dimensions', colorCode: 'colorCode', annotations: 'annotation', description: 'description', name: 'test', price: 100, isAvailable: true, featured: false, distributor: 'distributor', distributorSKU: 'distributor', category: null, imageUrls: ['http://test.com/test.jpg'], }); const aggregateProductVariants = await AggregateProductVariant.find(); expect(aggregateProductVariants).toContainEqual({ id: expect.any(String), createdAt: expect.any(Date), updatedAt: expect.any(Date), deletedAt: null, identifiers: ['394902368420831'], discoverable: true, asin: 'asin', dimensions: 'dimensions', colorCode: 'colorCode', annotations: 'annotation', description: 'description', name: 'test', price: 100, isAvailable: true, featured: false, distributor: 'distributor', distributorSKU: 'distributor', category: null, imageUrls: ['http://test.com/test.jpg', 'http://test.com/test2.jpg'], productId: expect.any(String), }); expect(aggregateProductVariants).toContainEqual({ id: expect.any(String), createdAt: expect.any(Date), updatedAt: expect.any(Date), deletedAt: null, identifiers: ['394902368420831hihi'], discoverable: true, asin: 'asin', dimensions: 'dimensions', colorCode: 'colorCode', annotations: 'annotation', description: 'description', name: 'test', price: 100, isAvailable: true, featured: false, distributor: 'distributor', distributorSKU: 'distributor', category: null, imageUrls: ['http://test.com/test.jpg'], productId: expect.any(String), }); expect(aggregateProductVariants).toContainEqual({ id: expect.any(String), createdAt: expect.any(Date), updatedAt: expect.any(Date), deletedAt: null, identifiers: ['3949023684208asdfasdf'], discoverable: true, asin: 'asin', dimensions: 'dimensions', colorCode: 'colorCode', annotations: 'annotation', description: 'description', name: 'test', price: 100, isAvailable: true, featured: false, distributor: 'distributor', distributorSKU: 'distributor', category: null, imageUrls: ['http://test.com/test.jpg'], productId: expect.any(String), }); const retailerProducts = await RetailerProduct.find(); expect(retailerProducts).toContainEqual({ id: expect.any(String), createdAt: expect.any(Date), updatedAt: expect.any(Date), deletedAt: null, identifiers: ['randomid'], discoverable: true, asin: 'asin', dimensions: 'dimensions', colorCode: 'colorCode', annotations: 'annotation', description: 'description', name: 'test', price: 100, isAvailable: true, featured: false, distributor: 'distributor', distributorSKU: 'distributor', category: null, imageUrls: ['http://test.com/test.jpg', 'http://test.com/test2.jpg'], retailer: 'LULU_AND_GEORGIA', source: null, url: expect.stringContaining( 'https://www.luluandgeorgia.com/products/cylia-counter-stool?variant=' ), aggregateProductId: expect.any(String), }); expect(retailerProducts).toContainEqual({ id: expect.any(String), createdAt: expect.any(Date), updatedAt: expect.any(Date), deletedAt: null, identifiers: ['randomid12'], discoverable: true, asin: 'asin', dimensions: 'dimensions', colorCode: 'colorCode', annotations: 'annotation', description: 'description', name: 'test', price: 100, isAvailable: true, featured: false, distributor: 'distributor', distributorSKU: 'distributor', category: null, imageUrls: ['http://test.com/test.jpg'], retailer: 'LULU_AND_GEORGIA', source: null, url: 'https://www.luluandgeorgia.com/products/cylia-counter-table?variant=3949023684208asdfasdf', aggregateProductId: expect.any(String), }); const retailerProductVariants = await RetailerProductVariant.find(); expect(retailerProductVariants).toContainEqual({ id: expect.any(String), createdAt: expect.any(Date), updatedAt: expect.any(Date), deletedAt: null, identifiers: ['394902368420831'], discoverable: true, asin: 'asin', dimensions: 'dimensions', colorCode: 'colorCode', annotations: 'annotation', description: 'description', name: 'test', price: 100, isAvailable: true, featured: false, distributor: 'distributor', distributorSKU: 'distributor', category: null, imageUrls: ['http://test.com/test.jpg', 'http://test.com/test2.jpg'], retailer: 'LULU_AND_GEORGIA', source: null, url: 'https://www.luluandgeorgia.com/products/cylia-counter-stool?variant=394902368420831', productId: expect.any(String), variantId: expect.any(String), }); expect(retailerProductVariants).toContainEqual({ id: expect.any(String), createdAt: expect.any(Date), updatedAt: expect.any(Date), deletedAt: null, identifiers: ['394902368420831hihi'], discoverable: true, asin: 'asin', dimensions: 'dimensions', colorCode: 'colorCode', annotations: 'annotation', description: 'description', name: 'test', price: 100, isAvailable: true, featured: false, distributor: 'distributor', distributorSKU: 'distributor', category: null, imageUrls: ['http://test.com/test.jpg'], retailer: 'LULU_AND_GEORGIA', source: null, url: 'https://www.luluandgeorgia.com/products/cylia-counter-stool?variant=39490236842083hihi', productId: expect.any(String), variantId: expect.any(String), }); expect(retailerProductVariants).toContainEqual({ id: expect.any(String), createdAt: expect.any(Date), updatedAt: expect.any(Date), deletedAt: null, identifiers: ['3949023684208asdfasdf'], discoverable: true, asin: 'asin', dimensions: 'dimensions', colorCode: 'colorCode', annotations: 'annotation', description: 'description', name: 'test', price: 100, isAvailable: true, featured: false, distributor: 'distributor', distributorSKU: 'distributor', category: null, imageUrls: ['http://test.com/test.jpg'], retailer: 'LULU_AND_GEORGIA', source: null, url: 'https://www.luluandgeorgia.com/products/cylia-counter-table?variant=3949023684208asdfasdf', productId: expect.any(String), variantId: expect.any(String), }); }); });