fix: fix weight/length/height/width types in updates (#12500)
This commit is contained in:
@@ -3043,12 +3043,20 @@ medusaIntegrationTestRunner({
|
|||||||
describe("batch methods", () => {
|
describe("batch methods", () => {
|
||||||
it("successfully creates, updates, and deletes products", async () => {
|
it("successfully creates, updates, and deletes products", async () => {
|
||||||
const createPayload = getProductFixture({
|
const createPayload = getProductFixture({
|
||||||
|
weight: 100,
|
||||||
|
length: 200,
|
||||||
|
height: 300,
|
||||||
|
width: 400,
|
||||||
title: "Test batch create",
|
title: "Test batch create",
|
||||||
handle: "test-batch-create",
|
handle: "test-batch-create",
|
||||||
shipping_profile_id: shippingProfile.id,
|
shipping_profile_id: shippingProfile.id,
|
||||||
})
|
})
|
||||||
|
|
||||||
const updatePayload = {
|
const updatePayload = {
|
||||||
|
weight: 101,
|
||||||
|
length: 201,
|
||||||
|
height: 301,
|
||||||
|
width: 401,
|
||||||
id: publishedProduct.id,
|
id: publishedProduct.id,
|
||||||
title: "Test batch update",
|
title: "Test batch update",
|
||||||
}
|
}
|
||||||
|
|||||||
+15
@@ -33,6 +33,7 @@ import {
|
|||||||
jest.setTimeout(300000)
|
jest.setTimeout(300000)
|
||||||
|
|
||||||
moduleIntegrationTestRunner<IProductModuleService>({
|
moduleIntegrationTestRunner<IProductModuleService>({
|
||||||
|
debug: true,
|
||||||
moduleName: Modules.PRODUCT,
|
moduleName: Modules.PRODUCT,
|
||||||
injectedDependencies: {
|
injectedDependencies: {
|
||||||
[Modules.EVENT_BUS]: new MockEventBusService(),
|
[Modules.EVENT_BUS]: new MockEventBusService(),
|
||||||
@@ -127,6 +128,10 @@ moduleIntegrationTestRunner<IProductModuleService>({
|
|||||||
productOne = service.createProducts({
|
productOne = service.createProducts({
|
||||||
title: "product 1",
|
title: "product 1",
|
||||||
status: ProductStatus.PUBLISHED,
|
status: ProductStatus.PUBLISHED,
|
||||||
|
weight: 100,
|
||||||
|
length: 200,
|
||||||
|
height: 300,
|
||||||
|
width: 400,
|
||||||
options: [
|
options: [
|
||||||
{
|
{
|
||||||
title: "opt-title",
|
title: "opt-title",
|
||||||
@@ -236,6 +241,11 @@ moduleIntegrationTestRunner<IProductModuleService>({
|
|||||||
productBefore.images = data.images
|
productBefore.images = data.images
|
||||||
productBefore.thumbnail = data.thumbnail
|
productBefore.thumbnail = data.thumbnail
|
||||||
productBefore.tag_ids = data.tag_ids
|
productBefore.tag_ids = data.tag_ids
|
||||||
|
// Update the weight/length/height/width to ensure we are compensating the type mismatch with the DB
|
||||||
|
productBefore.weight = 101
|
||||||
|
productBefore.length = 201
|
||||||
|
productBefore.height = 301
|
||||||
|
productBefore.width = 401
|
||||||
const updatedProducts = await service.upsertProducts([productBefore])
|
const updatedProducts = await service.upsertProducts([productBefore])
|
||||||
expect(updatedProducts).toHaveLength(1)
|
expect(updatedProducts).toHaveLength(1)
|
||||||
|
|
||||||
@@ -270,6 +280,11 @@ moduleIntegrationTestRunner<IProductModuleService>({
|
|||||||
discountable: productBefore.discountable,
|
discountable: productBefore.discountable,
|
||||||
thumbnail: images[0].url,
|
thumbnail: images[0].url,
|
||||||
status: productBefore.status,
|
status: productBefore.status,
|
||||||
|
// TODO: Notice how the weight/length/height/width are strings, not respecting the ProductDTO typings
|
||||||
|
weight: "101",
|
||||||
|
length: "201",
|
||||||
|
height: "301",
|
||||||
|
width: "401",
|
||||||
images: expect.arrayContaining([
|
images: expect.arrayContaining([
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
id: expect.any(String),
|
id: expect.any(String),
|
||||||
|
|||||||
@@ -21,6 +21,8 @@ export class ProductRepository extends DALUtils.mikroOrmBaseRepositoryFactory(
|
|||||||
) => void,
|
) => void,
|
||||||
context: Context = {}
|
context: Context = {}
|
||||||
): Promise<InferEntityType<typeof Product>[]> {
|
): Promise<InferEntityType<typeof Product>[]> {
|
||||||
|
updates.forEach((update) => this.correctUpdateDTOTypes(update))
|
||||||
|
|
||||||
const products = await this.find(
|
const products = await this.find(
|
||||||
buildQuery({ id: updates.map((p) => p.id) }, { relations: ["*"] }),
|
buildQuery({ id: updates.map((p) => p.id) }, { relations: ["*"] }),
|
||||||
context
|
context
|
||||||
@@ -80,6 +82,15 @@ export class ProductRepository extends DALUtils.mikroOrmBaseRepositoryFactory(
|
|||||||
.filter((product) => product !== undefined)
|
.filter((product) => product !== undefined)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// We should probably fix the column types in the database to avoid this
|
||||||
|
// It would also match the types in ProductVariant, which are already numbers
|
||||||
|
protected correctUpdateDTOTypes(update: any) {
|
||||||
|
update.weight = update.weight?.toString()
|
||||||
|
update.length = update.length?.toString()
|
||||||
|
update.height = update.height?.toString()
|
||||||
|
update.width = update.width?.toString()
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* In order to be able to have a strict not in categories, and prevent a product
|
* In order to be able to have a strict not in categories, and prevent a product
|
||||||
* to be return in the case it also belongs to other categories, we need to
|
* to be return in the case it also belongs to other categories, we need to
|
||||||
|
|||||||
Reference in New Issue
Block a user