Skip to content

Commit

Permalink
Merge pull request #56 from openfoodfacts:issues/55
Browse files Browse the repository at this point in the history
fix: Prevent errors when there are duplicate product codes
  • Loading branch information
john-gom authored Jul 19, 2024
2 parents 7edc56f + 6f0cb9b commit 0e49150
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
28 changes: 28 additions & 0 deletions src/domain/services/import.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { SettingsService } from './settings.service';
import { createClient } from 'redis';
import { GenericContainer } from 'testcontainers';
import { setTimeout } from 'timers/promises';
import { ProductIngredient } from '../entities/product-ingredient';

const lastModified = 1692032161;

Expand Down Expand Up @@ -301,6 +302,33 @@ describe('importFromMongo', () => {
expect(warnSpy).toHaveBeenCalledTimes(1);
});
});

it('should cope with duplicate product codes', async () => {
await createTestingModule([DomainModule], async (app) => {
// WHEN: Importing data containing nul characters
const { productIdNew } = testProducts();
const productWithIngredients = {
code: productIdNew,
ingredients: [{ ingredient_text: 'test' }],
};
const duplicateProducts = [
productWithIngredients,
productWithIngredients,
];
mockMongoDB(duplicateProducts);
await app.get(ImportService).importFromMongo();

// THEN: Product should be loaded with no duplicates
const ingredientsNew = await app
.get(EntityManager)
.find(ProductIngredient, {
product: { code: productIdNew },
});

expect(ingredientsNew).toHaveLength(1);
expect(ingredientsNew[0].ingredientText).toBe('test');
});
});
});

describe('scheduledImportFromMongo', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/domain/services/import.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export class ImportService {

// Now using postgres to help with transactions
const connection = await sql.reserve();
await connection`CREATE TEMP TABLE product_temp (id int, last_modified timestamptz, data jsonb)`;
await connection`CREATE TEMP TABLE product_temp (id int PRIMARY KEY, last_modified timestamptz, data jsonb)`;
// let sql: string;
// const vars = [];
for (const collection of Object.values(collections)) {
Expand Down Expand Up @@ -166,7 +166,7 @@ export class ImportService {
results =
await connection`insert into product_temp (id, last_modified, data) values (${id}, ${lastModified}, ${
data as unknown as SerializableParameter
})`;
}) ON CONFLICT DO NOTHING`;

latestModified = Math.max(latestModified, lastModified?.getTime() ?? 0);

Expand Down

0 comments on commit 0e49150

Please sign in to comment.