Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(product, dashboard): manual cleanup of uploaded images #10254

Merged
merged 3 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/shy-gorillas-allow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@medusajs/dashboard": patch
"@medusajs/product": patch
---

fix(product,dashboard): Avoid duplicating images
Original file line number Diff line number Diff line change
Expand Up @@ -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: () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;');
Expand Down
10 changes: 10 additions & 0 deletions packages/modules/product/src/services/product-module-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down
Loading