From d42c85004481194a933cb44bee064551f1e99b2b Mon Sep 17 00:00:00 2001 From: Kasper Fabricius Kristensen <45367945+kasperkristensen@users.noreply.github.com> Date: Mon, 25 Nov 2024 11:04:06 +0100 Subject: [PATCH 1/3] manual cleanup --- .../edit-product-media-form.tsx | 2 +- .../product/src/migrations/Migration20241122120331.ts | 6 ++++++ .../product/src/services/product-module-service.ts | 10 ++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) 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 11ab0f748746a..6eb54e92a8195 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,7 +132,7 @@ export const EditProductMediaForm = ({ product }: ProductMediaViewProps) => { await mutateAsync( { - images: withUpdatedUrls.map((file) => ({ url: file.url })), + images: withUpdatedUrls.map((file) => ({ url: file.url, id: file.id })), thumbnail: thumbnail, }, { diff --git a/packages/modules/product/src/migrations/Migration20241122120331.ts b/packages/modules/product/src/migrations/Migration20241122120331.ts index d8585ef7295e8..ff324f3e1ec7d 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 777fb9fc091ef..623eb8b8700f5 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 }, From 07525be5e23960a1371de28254bd1b7401afaf37 Mon Sep 17 00:00:00 2001 From: Kasper Fabricius Kristensen <45367945+kasperkristensen@users.noreply.github.com> Date: Mon, 25 Nov 2024 11:07:49 +0100 Subject: [PATCH 2/3] add changeset --- .changeset/shy-gorillas-allow.md | 6 ++++++ 1 file changed, 6 insertions(+) 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 0000000000000..1c94c4a148d0d --- /dev/null +++ b/.changeset/shy-gorillas-allow.md @@ -0,0 +1,6 @@ +--- +"@medusajs/dashboard": patch +"@medusajs/product": patch +--- + +fix(product,dashboard): Avoid duplicating images From 6b0202c8d729fda4bd1791a1976d6146cfb79671 Mon Sep 17 00:00:00 2001 From: Kasper Fabricius Kristensen <45367945+kasperkristensen@users.noreply.github.com> Date: Mon, 25 Nov 2024 11:22:58 +0100 Subject: [PATCH 3/3] remove thumbnail on deletion --- .../edit-product-media-form/edit-product-media-form.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 6eb54e92a8195..e8b1c4df6fb08 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 @@ -133,7 +133,7 @@ export const EditProductMediaForm = ({ product }: ProductMediaViewProps) => { await mutateAsync( { images: withUpdatedUrls.map((file) => ({ url: file.url, id: file.id })), - thumbnail: thumbnail, + thumbnail: thumbnail || null, }, { onSuccess: () => {