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(orchestration,link-modules,pricing,types): fix shippingprofile errror outside of core + change link alias name #5025

Merged
merged 12 commits into from
Sep 13, 2023
Merged
8 changes: 8 additions & 0 deletions .changeset/shy-students-look.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@medusajs/orchestration": patch
"@medusajs/link-modules": patch
"@medusajs/pricing": patch
"@medusajs/types": patch
---

fix(orchestration,link-modules,pricing,types): fix shippingprofile error outside of core + change link alias name
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ export const ProductShippingProfile: ModuleJoinerConfig = {
},
relationship: {
serviceName: LINKS.ProductShippingProfile,
isInternalService: true,
primaryKey: "product_id",
foreignKey: "id",
alias: "shipping_profile",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ export const ProductVariantPriceSet: ModuleJoinerConfig = {
serviceName: LINKS.ProductVariantPriceSet,
primaryKey: "variant_id",
foreignKey: "id",
alias: "prices",
isList: false,
alias: "price",
},
},
{
Expand Down
1 change: 0 additions & 1 deletion packages/modules-sdk/src/remote-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ export class RemoteQuery {
"skip",
"take",
"limit",
"order",
"offset",
"cursor",
"sort",
Expand Down
10 changes: 10 additions & 0 deletions packages/orchestration/src/joiner/remote-joiner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,16 @@ export class RemoteJoiner {
{ fieldAlias, relationships },
] of expandedRelationships) {
if (!this.serviceConfigCache.has(serviceName)) {
// If true, the relationship is an internal service from the medusa core
// If modules are being used ouside of the core, we should not be throwing
// errors when the core services are not found in cache.
// TODO: Remove when there are no more "internal" services
const isInternalServicePresent = relationships.some(
(rel) => rel.isInternalService === true
)

if (isInternalServicePresent) continue

throw new Error(`Service "${serviceName}" was not found`)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ describe("PricingModule Service - PriceSet", () => {
describe("calculatePrices", () => {
it("retrieves the calculated prices when no context is set", async () => {
const priceSetsResult = await service.calculatePrices(
["price-set-1", "price-set-2"],
{ id: ["price-set-1", "price-set-2"] },
{}
)

Expand All @@ -96,9 +96,11 @@ describe("PricingModule Service - PriceSet", () => {

it("retrieves the calculated prices when a context is set", async () => {
const priceSetsResult = await service.calculatePrices(
["price-set-1", "price-set-2"],
{ id: ["price-set-1", "price-set-2"] },
{
currency_code: "USD",
context: {
currency_code: "USD",
},
}
)

Expand All @@ -122,9 +124,9 @@ describe("PricingModule Service - PriceSet", () => {

it("retrieves the calculated prices only when id exists in the database", async () => {
const priceSetsResult = await service.calculatePrices(
["price-set-doesnotexist", "price-set-1"],
{ id: ["price-set-doesnotexist", "price-set-1"] },
{
currency_code: "USD",
context: { currency_code: "USD" },
}
)

Expand Down
16 changes: 7 additions & 9 deletions packages/pricing/src/services/pricing-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
FindConfig,
InternalModuleDeclaration,
ModuleJoinerConfig,
PricingContext,
PricingFilters,
PricingTypes,
} from "@medusajs/types"
import { Currency, MoneyAmount, PriceSet } from "@models"
Expand All @@ -25,10 +27,6 @@ type InjectedDependencies = {
priceSetService: PriceSetService<any>
}

type PricingContext = {
currency_code?: string
}

export default class PricingModuleService<
TPriceSet extends PriceSet = PriceSet,
TMoneyAmount extends MoneyAmount = MoneyAmount,
Expand Down Expand Up @@ -61,14 +59,15 @@ export default class PricingModuleService<

@InjectManager("baseRepository_")
async calculatePrices(
priceSetIds: string[],
pricingContext: PricingContext,
pricingFilters: PricingFilters,
pricingContext: PricingContext = { context: {} },
@MedusaContext() sharedContext: Context = {}
): Promise<PricingTypes.CalculatedPriceSetDTO> {
// Keeping this whole logic raw in here for now as they will undergo
// some changes, will abstract them out once we have a final version
const context = pricingContext.context || {}
const priceSetFilters: PricingTypes.FilterablePriceSetProps = {
id: priceSetIds,
id: pricingFilters.id,
}

const priceSets = await this.list(
Expand All @@ -95,8 +94,7 @@ export default class PricingModuleService<
// When no price is set, return null values for all cases
const selectedMoneyAmount = priceSet.money_amounts?.find(
(ma) =>
pricingContext.currency_code &&
ma.currency_code === pricingContext.currency_code
context.currency_code && ma.currency_code === context.currency_code
)

return {
Expand Down
5 changes: 5 additions & 0 deletions packages/types/src/joiner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ export type JoinerRelationship = {
foreignKey: string
primaryKey: string
serviceName: string
/**
* If true, the relationship is an internal service from the medusa core
* TODO: Remove when there are no more "internal" services
*/
isInternalService?: boolean
/**
* In an inverted relationship the foreign key is on the other service and the primary key is on the current service
*/
Expand Down
9 changes: 8 additions & 1 deletion packages/types/src/pricing/common/price-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@ import { BaseFilterable } from "../../dal"
import { FilterableMoneyAmountProps, MoneyAmountDTO } from "./money-amount"

export interface PricingContext {
currency_code?: string
context?: {
currency_code?: string
}
}

export interface PricingFilters {
id: string[]
}

export interface PriceSetDTO {
id: string
money_amounts?: MoneyAmountDTO[]
Expand Down
5 changes: 3 additions & 2 deletions packages/types/src/pricing/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
MoneyAmountDTO,
PriceSetDTO,
PricingContext,
PricingFilters,
UpdateCurrencyDTO,
UpdateMoneyAmountDTO,
UpdatePriceSetDTO,
Expand All @@ -22,8 +23,8 @@ export interface IPricingModuleService {
__joinerConfig(): ModuleJoinerConfig

calculatePrices(
priceSetIds: string[],
pricingContext: PricingContext,
filters: PricingFilters,
context?: PricingContext,
sharedContext?: Context
): Promise<CalculatedPriceSetDTO>

Expand Down