From c28d0db1647a5c8edaf0ba0faba6426e8a740399 Mon Sep 17 00:00:00 2001 From: Kasper Fabricius Kristensen <45367945+kasperkristensen@users.noreply.github.com> Date: Mon, 25 Nov 2024 11:44:22 +0100 Subject: [PATCH] fix(product, dashboard): manual cleanup of uploaded images (#10254) * manual cleanup * add changeset * remove thumbnail on deletion --- .changeset/shy-gorillas-allow.md | 6 ++++++ .../edit-product-media-form.tsx | 4 ++-- .../product/src/migrations/Migration20241122120331.ts | 6 ++++++ .../product/src/services/product-module-service.ts | 10 ++++++++++ 4 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 .changeset/shy-gorillas-allow.md diff --git a/.changeset/shy-gorillas-allow.md b/.changeset/shy-gorillas-allow.md new file mode 100644 index 0000000000..1c94c4a148 --- /dev/null +++ b/.changeset/shy-gorillas-allow.md @@ -0,0 +1,6 @@ +--- +"@medusajs/dashboard": patch +"@medusajs/product": patch +--- + +fix(product,dashboard): Avoid duplicating images diff --git a/packages/admin/dashboard/src/routes/products/product-media/components/edit-product-media-form/edit-product-media-form.tsx b/packages/admin/dashboard/src/routes/products/product-media/components/edit-product-media-form/edit-product-media-form.tsx index 11ab0f7487..e8b1c4df6f 100644 --- a/packages/admin/dashboard/src/routes/products/product-media/components/edit-product-media-form/edit-product-media-form.tsx +++ b/packages/admin/dashboard/src/routes/products/product-media/components/edit-product-media-form/edit-product-media-form.tsx @@ -132,8 +132,8 @@ export const EditProductMediaForm = ({ product }: ProductMediaViewProps) => { await mutateAsync( { - images: withUpdatedUrls.map((file) => ({ url: file.url })), - thumbnail: thumbnail, + images: withUpdatedUrls.map((file) => ({ url: file.url, id: file.id })), + thumbnail: thumbnail || null, }, { onSuccess: () => { diff --git a/packages/modules/product/src/migrations/Migration20241122120331.ts b/packages/modules/product/src/migrations/Migration20241122120331.ts index d8585ef729..ff324f3e1e 100644 --- a/packages/modules/product/src/migrations/Migration20241122120331.ts +++ b/packages/modules/product/src/migrations/Migration20241122120331.ts @@ -19,6 +19,12 @@ export class Migration20241122120331 extends Migration { where pi.image_id = i.id; `); + // Delete orphaned images + this.addSql(` + delete from "image" + where product_id is null; + `); + this.addSql('alter table if exists "image" alter column "product_id" set not null;'); this.addSql('alter table if exists "image" add constraint "image_product_id_foreign" foreign key ("product_id") references "product" ("id") on update cascade on delete cascade;'); this.addSql('drop table if exists "product_images" cascade;'); diff --git a/packages/modules/product/src/services/product-module-service.ts b/packages/modules/product/src/services/product-module-service.ts index 777fb9fc09..623eb8b870 100644 --- a/packages/modules/product/src/services/product-module-service.ts +++ b/packages/modules/product/src/services/product-module-service.ts @@ -1690,6 +1690,16 @@ export default class ProductModuleService sharedContext ) upsertedProduct.images = productImages + + await this.productImageService_.delete( + { + product_id: upsertedProduct.id, + id: { + $nin: productImages.map(({ id }) => id), + }, + }, + sharedContext + ) } else { await this.productImageService_.delete( { product_id: upsertedProduct.id },