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(medusa, utils): fix the way selects are consumed alongside the relations #4389

Merged
merged 22 commits into from
Jun 29, 2023
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
12 changes: 12 additions & 0 deletions .changeset/great-panthers-call.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
"@medusajs/medusa": patch
"@medusajs/admin-ui": patch
"@medusajs/admin": patch
"medusa-plugin-brightpearl": patch
"medusa-plugin-segment": patch
"medusa-plugin-sendgrid": patch
"medusa-plugin-slack-notification": patch
"@medusajs/utils": patch
---

fix(medusa, utils): fix the way selects are consumed alongside the relations
2 changes: 0 additions & 2 deletions integration-tests/api/__tests__/batch-jobs/order/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,6 @@ describe("Batchjob with type order-export", () => {

expect(batchJob.status).toBe("completed")

expect(batchJob.status).toBe("completed")

exportFilePath = path.resolve(__dirname, batchJob.result.file_key)
const isFileExists = (await fs.stat(exportFilePath)).isFile()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ describe("Batch job of product-export type", () => {
const db = useDb()
await db.teardown()

// @ts-ignore
try {
const isFileExists = (await fs.stat(exportFilePath))?.isFile()

Expand All @@ -74,8 +75,8 @@ describe("Batch job of product-export type", () => {

await fs.unlink(exportFilePath)
}
} catch (e) {
console.log(e)
} catch (err) {
// noop
}
})

Expand Down
2 changes: 1 addition & 1 deletion integration-tests/api/__tests__/database/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe("Database options", () => {

// Idle time is 1000 ms so this should timeout
await new Promise((resolve) =>
setTimeout(() => resolve(console.log("")), 2000)
setTimeout(() => resolve(undefined), 2000)
)

// This query should fail with a QueryRunnerAlreadyReleasedError
Expand Down
23 changes: 13 additions & 10 deletions integration-tests/api/__tests__/store/customer.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,16 +293,19 @@ describe("/store/customers", () => {
})

expect(response.status).toEqual(200)
expect(response.data.orders).toEqual([
expect.objectContaining({
display_id: 3,
status: "canceled",
}),
expect.objectContaining({
display_id: 1,
status: "completed",
}),
])
expect(response.data.orders.length).toEqual(2)
expect(response.data.orders).toEqual(
expect.arrayContaining([
expect.objectContaining({
display_id: 3,
status: "canceled",
}),
expect.objectContaining({
display_id: 1,
status: "completed",
}),
])
)
expect(response.data.orders.length).toEqual(2)
})
})
Expand Down
33 changes: 21 additions & 12 deletions integration-tests/api/__tests__/store/products.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ const {

const productSeeder = require("../../helpers/store-product-seeder")
const adminSeeder = require("../../helpers/admin-seeder")
const {
allowedStoreProductsFields,
defaultStoreProductsRelations,
} = require("@medusajs/medusa/dist")

jest.setTimeout(30000)

Expand Down Expand Up @@ -988,23 +992,28 @@ describe("/store/products", () => {
it("response contains only fields defined with `fields` param", async () => {
const api = useApi()

const fields = allowedStoreProductsFields

const response = await api.get(
"/store/products/test-product?fields=handle"
`/store/products/test-product?fields=${fields.join(",")}`
)

expect(response.status).toEqual(200)

expect(Object.keys(response.data.product)).toEqual([
// fields
"handle",
// relations
"variants",
"options",
"images",
"tags",
"collection",
"type",
])
const expectedProperties = [...fields, ...defaultStoreProductsRelations]
const actualProperties = [
...Object.keys(response.data.product),
...Object.keys(response.data.product.variants[0]).map(
(key) => `variants.${key}`
),
"variants.prices.amount",
"options.values",
]

expect(Object.keys(response.data.product).length).toEqual(31)
expect(actualProperties).toEqual(
expect.arrayContaining(expectedProperties)
)
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const { initDb, useDb } = require("../../../../helpers/use-db")
const { setPort, useApi } = require("../../../../helpers/use-api")

const adminSeeder = require("../../../helpers/admin-seeder")
const cartSeeder = require("../../../helpers/cart-seeder")
const {
simpleProductFactory,
simpleCustomerFactory,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const orderRelations = [
"discounts",
"discounts.rule",
"shipping_methods",
"shipping_methods.shipping_option",
"payments",
"items",
"fulfillments",
Expand Down
10 changes: 4 additions & 6 deletions packages/medusa-plugin-brightpearl/src/services/brightpearl.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { MedusaError, humanizeAmount } from "medusa-core-utils"
import {
ReservationType,
updateInventoryAndReservations,
} from "@medusajs/medusa"
import { humanizeAmount, MedusaError } from "medusa-core-utils"
import { updateInventoryAndReservations } from "@medusajs/medusa"

import { BaseService } from "medusa-interfaces"
import Brightpearl from "../utils/brightpearl"
Expand Down Expand Up @@ -111,7 +108,7 @@ class BrightpearlService extends BaseService {
httpMethod: "POST",
uriTemplate: `${this.options.backend_url}/brightpearl/goods-out`,
bodyTemplate:
"{\"account\": \"${account-code}\", \"lifecycle_event\": \"${lifecycle-event}\", \"resource_type\": \"${resource-type}\", \"id\": \"${resource-id}\" }",
'{"account": "${account-code}", "lifecycle_event": "${lifecycle-event}", "resource_type": "${resource-type}", "id": "${resource-id}" }',
contentType: "application/json",
idSetAccepted: false,
},
Expand Down Expand Up @@ -1008,6 +1005,7 @@ class BrightpearlService extends BaseService {
"shipping_address",
"billing_address",
"shipping_methods",
"shipping_methods.shipping_option",
"payments",
"sales_channel",
],
Expand Down
2 changes: 2 additions & 0 deletions packages/medusa-plugin-brightpearl/src/subscribers/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ class OrderSubscriber {
"additional_items.tax_lines",
"shipping_address",
"shipping_methods",
"shipping_methods.shipping_option",
"shipping_methods.tax_lines",
],
})
Expand Down Expand Up @@ -148,6 +149,7 @@ class OrderSubscriber {
"additional_items.variant.product",
"additional_items.tax_lines",
"shipping_address",
"shipping_methods.shipping_option",
"shipping_methods",
],
})
Expand Down
6 changes: 6 additions & 0 deletions packages/medusa-plugin-segment/src/subscribers/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class OrderSubscriber {
"discounts",
"discounts.rule",
"shipping_methods",
"shipping_methods.shipping_option",
"payments",
"fulfillments",
"returns",
Expand All @@ -56,6 +57,7 @@ class OrderSubscriber {
"swaps.return_order",
"swaps.payment",
"swaps.shipping_methods",
"swaps.shipping_methods.shipping_option",
"swaps.shipping_address",
"swaps.additional_items",
"swaps.fulfillments",
Expand Down Expand Up @@ -149,6 +151,7 @@ class OrderSubscriber {
"discounts",
"discounts.rule",
"shipping_methods",
"shipping_methods.shipping_option",
"payments",
"fulfillments",
"returns",
Expand All @@ -159,6 +162,7 @@ class OrderSubscriber {
"swaps.return_order",
"swaps.payment",
"swaps.shipping_methods",
"swaps.shipping_methods.shipping_option",
"swaps.shipping_address",
"swaps.additional_items",
"swaps.fulfillments",
Expand Down Expand Up @@ -257,6 +261,7 @@ class OrderSubscriber {
"discounts",
"discounts.rule",
"shipping_methods",
"shipping_methods.shipping_option",
"payments",
"fulfillments",
"items",
Expand All @@ -267,6 +272,7 @@ class OrderSubscriber {
"swaps.return_order",
"swaps.payment",
"swaps.shipping_methods",
"swaps.shipping_methods.shipping_option",
"swaps.shipping_address",
"swaps.additional_items",
"swaps.fulfillments",
Expand Down
15 changes: 7 additions & 8 deletions packages/medusa-plugin-sendgrid/src/services/sendgrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -850,7 +850,9 @@ class SendGridService extends NotificationService {
}

async swapCreatedData({ id }) {
const store = await this.storeService_.retrieve({ where: { id: Not(IsNull()) } })
const store = await this.storeService_.retrieve({
where: { id: Not(IsNull()) },
})
const swap = await this.swapService_.retrieve(id, {
relations: [
"additional_items",
Expand Down Expand Up @@ -913,7 +915,7 @@ class SendGridService extends NotificationService {
"shipping_total",
"subtotal",
],
relations: ["items", "items.variant", "items.variant.product"]
relations: ["items", "items.variant", "items.variant.product"],
})
const currencyCode = order.currency_code.toUpperCase()

Expand Down Expand Up @@ -993,6 +995,7 @@ class SendGridService extends NotificationService {
relations: [
"shipping_address",
"shipping_methods",
"shipping_methods.shipping_option",
"shipping_methods.tax_lines",
"additional_items",
"additional_items.variant",
Expand Down Expand Up @@ -1028,11 +1031,7 @@ class SendGridService extends NotificationService {
"shipping_total",
"subtotal",
],
relations: [
"items",
"items.variant",
"items.variant.product",
]
relations: ["items", "items.variant", "items.variant.product"],
})

const returnRequest = swap.return_order
Expand Down Expand Up @@ -1152,7 +1151,7 @@ class SendGridService extends NotificationService {
"order.items",
"order.items.variant",
"order.items.variant.product",
"order.shipping_address"
"order.shipping_address",
],
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class SlackService extends BaseService {
"discounts",
"discounts.rule",
"shipping_methods",
"shipping_methods.shipping_option",
"payments",
"fulfillments",
"returns",
Expand All @@ -51,6 +52,7 @@ class SlackService extends BaseService {
"swaps.return_order",
"swaps.payment",
"swaps.shipping_methods",
"swaps.shipping_methods.shipping_option",
"swaps.shipping_address",
"swaps.additional_items",
"swaps.fulfillments",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const defaultRelations = [
"return_order",
"shipping_address",
"shipping_methods",
"shipping_methods.shipping_option",
]

const defaultFields = [
Expand Down
1 change: 1 addition & 0 deletions packages/medusa/src/api/routes/admin/swaps/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const defaultAdminSwapRelations = [
"return_order",
"shipping_address",
"shipping_methods",
"shipping_methods.shipping_option",
]

export const defaultAdminSwapFields = [
Expand Down
1 change: 1 addition & 0 deletions packages/medusa/src/api/routes/store/orders/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export const defaultStoreOrdersRelations = [
"items",
"items.variant",
"shipping_methods",
"shipping_methods.shipping_option",
"discounts",
"discounts.rule",
"customer",
Expand Down
2 changes: 0 additions & 2 deletions packages/medusa/src/api/routes/store/products/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,7 @@ export const allowedStoreProductsFields = [

export const allowedStoreProductsRelations = [
...defaultStoreProductsRelations,
"variants.title",
"variants.inventory_items",
"variants.prices.amount",
"sales_channels",
]

Expand Down
2 changes: 2 additions & 0 deletions packages/medusa/src/api/routes/store/swaps/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ export const defaultStoreSwapRelations = [
"additional_items.variant",
"return_order",
"return_order.shipping_method",
"return_order.shipping_method.shipping_option",
"fulfillments",
"payment",
"shipping_address",
"shipping_methods",
"shipping_methods.shipping_option",
"cart",
]
export const defaultStoreSwapFields: FindConfig<Swap>["select"] = [
Expand Down
2 changes: 1 addition & 1 deletion packages/medusa/src/models/shipping-method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export class ShippingMethod {
@JoinColumn({ name: "return_id" })
return_order: Return

@ManyToOne(() => ShippingOption, { eager: true })
@ManyToOne(() => ShippingOption)
@JoinColumn({ name: "shipping_option_id" })
shipping_option: ShippingOption

Expand Down
Loading