fix(dashboard, product): product attributes update with a relation update (#13019)
* fix(dashboard, product): product attributes update with a relation update * fix: rm log * chore: refactor
This commit is contained in:
1 parent
c71ace0be2
commit
439c711845
5 files changed
+129
-8
No files matched your search
@@ -0,0 +1,6 @@
|
||||
---
|
||||
"@medusajs/dashboard": patch
|
||||
"@medusajs/product": patch
|
||||
---
|
||||
|
||||
fix(dashboard, product): update product attributes
|
||||
@@ -1813,6 +1813,110 @@ medusaIntegrationTestRunner({
|
||||
)
|
||||
})
|
||||
|
||||
it("updates products relations and attributes", async () => {
|
||||
const shortsCategory = (
|
||||
await api.post(
|
||||
"/admin/product-categories",
|
||||
{ name: "Shorts", is_internal: false, is_active: true },
|
||||
adminHeaders
|
||||
)
|
||||
).data.product_category
|
||||
|
||||
const pantsCategory = (
|
||||
await api.post(
|
||||
"/admin/product-categories",
|
||||
{ name: "Pants", is_internal: false, is_active: true },
|
||||
adminHeaders
|
||||
)
|
||||
).data.product_category
|
||||
|
||||
const payload = {
|
||||
title: "Test an update",
|
||||
weight: 100,
|
||||
length: 100,
|
||||
width: 100,
|
||||
height: 100,
|
||||
options: [{ title: "size", values: ["large", "small"] }],
|
||||
variants: [
|
||||
{
|
||||
options: { size: "large" },
|
||||
title: "New variant",
|
||||
prices: [
|
||||
{
|
||||
currency_code: "usd",
|
||||
amount: 200,
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
const createdProduct = (
|
||||
await api.post("/admin/products", payload, adminHeaders)
|
||||
).data.product
|
||||
|
||||
let updatedProduct = (
|
||||
await api.post(
|
||||
`/admin/products/${createdProduct.id}`,
|
||||
{ weight: 20, length: null },
|
||||
adminHeaders
|
||||
)
|
||||
).data.product
|
||||
|
||||
expect(updatedProduct).toEqual(
|
||||
expect.objectContaining({
|
||||
weight: "20",
|
||||
length: null,
|
||||
width: "100",
|
||||
height: "100",
|
||||
})
|
||||
)
|
||||
|
||||
updatedProduct = (
|
||||
await api.post(
|
||||
`/admin/products/${createdProduct.id}?fields=+categories.id`,
|
||||
{ categories: [{ id: pantsCategory.id }] },
|
||||
adminHeaders
|
||||
)
|
||||
).data.product
|
||||
|
||||
expect(updatedProduct).toEqual(
|
||||
expect.objectContaining({
|
||||
weight: "20",
|
||||
length: null,
|
||||
width: "100",
|
||||
height: "100",
|
||||
categories: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
id: pantsCategory.id,
|
||||
}),
|
||||
]),
|
||||
})
|
||||
)
|
||||
|
||||
updatedProduct = (
|
||||
await api.post(
|
||||
`/admin/products/${createdProduct.id}?fields=+categories.id`,
|
||||
{ weight: null, length: 20, width: 50 },
|
||||
adminHeaders
|
||||
)
|
||||
).data.product
|
||||
|
||||
expect(updatedProduct).toEqual(
|
||||
expect.objectContaining({
|
||||
weight: null,
|
||||
length: "20",
|
||||
width: "50",
|
||||
height: "100",
|
||||
categories: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
id: pantsCategory.id,
|
||||
}),
|
||||
]),
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("updates a product (update prices, tags, update status, delete collection, delete type, replaces images)", async () => {
|
||||
const payload = {
|
||||
collection_id: null,
|
||||
|
||||
+4
-4
@@ -68,10 +68,10 @@ export const ProductAttributesForm = ({
|
||||
const handleSubmit = form.handleSubmit(async (data) => {
|
||||
await mutateAsync(
|
||||
{
|
||||
weight: data.weight ? data.weight : undefined,
|
||||
length: data.length ? data.length : undefined,
|
||||
width: data.width ? data.width : undefined,
|
||||
height: data.height ? data.height : undefined,
|
||||
weight: data.weight ? data.weight : null,
|
||||
length: data.length ? data.length : null,
|
||||
width: data.width ? data.width : null,
|
||||
height: data.height ? data.height : null,
|
||||
mid_code: data.mid_code,
|
||||
hs_code: data.hs_code,
|
||||
origin_country: data.origin_country,
|
||||
|
||||
@@ -53,9 +53,7 @@ export type CancelReturnValidateOrderInput = {
|
||||
*/
|
||||
export const cancelReturnValidateOrder = createStep(
|
||||
"validate-return",
|
||||
({
|
||||
orderReturn,
|
||||
}: CancelReturnValidateOrderInput) => {
|
||||
({ orderReturn }: CancelReturnValidateOrderInput) => {
|
||||
const orderReturn_ = orderReturn as ReturnDTO & {
|
||||
payment_collections: PaymentCollectionDTO[]
|
||||
fulfillments: FulfillmentDTO[]
|
||||
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
MedusaError,
|
||||
isPresent,
|
||||
mergeMetadata,
|
||||
isDefined,
|
||||
} from "@medusajs/framework/utils"
|
||||
import { SqlEntityManager, wrap } from "@mikro-orm/postgresql"
|
||||
|
||||
@@ -57,11 +58,19 @@ export class ProductRepository extends DALUtils.mikroOrmBaseRepositoryFactory(
|
||||
height?: string | number
|
||||
width?: string | number
|
||||
}) {
|
||||
if (isDefined(productToUpdate.weight)) {
|
||||
productToUpdate.weight = productToUpdate.weight?.toString()
|
||||
}
|
||||
if (isDefined(productToUpdate.length)) {
|
||||
productToUpdate.length = productToUpdate.length?.toString()
|
||||
}
|
||||
if (isDefined(productToUpdate.height)) {
|
||||
productToUpdate.height = productToUpdate.height?.toString()
|
||||
}
|
||||
if (isDefined(productToUpdate.width)) {
|
||||
productToUpdate.width = productToUpdate.width?.toString()
|
||||
}
|
||||
}
|
||||
|
||||
async deepUpdate(
|
||||
productsToUpdate: ({ id: string } & any)[],
|
||||
@@ -72,6 +81,7 @@ export class ProductRepository extends DALUtils.mikroOrmBaseRepositoryFactory(
|
||||
context: Context = {}
|
||||
): Promise<InferEntityType<typeof Product>[]> {
|
||||
const productIdsToUpdate: string[] = []
|
||||
|
||||
productsToUpdate.forEach((productToUpdate) => {
|
||||
ProductRepository.#correctUpdateDTOTypes(productToUpdate)
|
||||
productIdsToUpdate.push(productToUpdate.id)
|
||||
@@ -151,7 +161,10 @@ export class ProductRepository extends DALUtils.mikroOrmBaseRepositoryFactory(
|
||||
}
|
||||
|
||||
if (isPresent(productToUpdate.metadata)) {
|
||||
productToUpdate.metadata = mergeMetadata(product.metadata ?? {}, productToUpdate.metadata)
|
||||
productToUpdate.metadata = mergeMetadata(
|
||||
product.metadata ?? {},
|
||||
productToUpdate.metadata
|
||||
)
|
||||
}
|
||||
|
||||
wrappedProduct.assign(productToUpdate)
|
||||
|
||||
Reference in new issue
Block a user