Skip to content

Commit

Permalink
Keep obsolete on tags in sync with product
Browse files Browse the repository at this point in the history
Signed-off-by: John Gomersall <[email protected]>
  • Loading branch information
john-gom committed Sep 13, 2024
1 parent 1021201 commit a9fe059
Showing 1 changed file with 30 additions and 11 deletions.
41 changes: 30 additions & 11 deletions src/domain/services/import.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,14 +201,10 @@ export class ImportService {
WHERE code IN ${sql(missingProducts)}
RETURNING id`;

const deletedProductIds = deletedProducts.map((p) => p.id);
for (const entity of Object.values(ProductTagMap.MAPPED_TAGS)) {
const tableName = this.em.getMetadata(entity).tableName;
await connection`UPDATE ${sql(tableName)} SET obsolete = NULL
where product_id in ${sql(deletedProductIds)}`;
}
await connection`UPDATE product_ingredient SET obsolete = NULL
where product_id in ${sql(deletedProductIds)}`;
await this.deleteProductTags(
connection,
deletedProducts.map((p) => p.id),
);
}
}

Expand Down Expand Up @@ -360,9 +356,32 @@ export class ImportService {
}

async deleteOtherProducts(connection: ReservedSql, updateId: string) {
const deleted =
await connection`delete from product where last_update_id != ${updateId} OR last_update_id IS NULL`;
this.logger.debug(`${deleted.count} Products deleted`);
const deletedProducts = await connection`UPDATE product SET
obsolete = NULL,
last_update_id = ${updateId},
last_updated = ${new Date()},
source = ${ProductSource.FULL_LOAD}
WHERE last_update_id != ${updateId} OR last_update_id IS NULL
RETURNING id`;
this.logger.debug(`${deletedProducts.count} Products deleted`);

await this.deleteProductTags(
connection,
deletedProducts.map((p) => p.id),
);
}

private async deleteProductTags(
connection: ReservedSql,
deletedProductIds: any[],
) {
for (const entity of Object.values(ProductTagMap.MAPPED_TAGS)) {
const tableName = this.em.getMetadata(entity).tableName;
await connection`UPDATE ${sql(tableName)} SET obsolete = NULL
where product_id in ${sql(deletedProductIds)}`;
}
await connection`UPDATE product_ingredient SET obsolete = NULL
where product_id in ${sql(deletedProductIds)}`;
}

// Make sure to pause redis before calling this
Expand Down

0 comments on commit a9fe059

Please sign in to comment.