fix(orchestration,link-modules,pricing,types): fix shippingprofile errror outside of core + change link alias name (#5025)

* fix(orchestration,link-modules,pricing,types): fix shippingprofile error outside of core + change link alias name

* chore: scope relationships and move condition inside

* chore: remove islist

* chore: fix merge conflict

* chore: reverted serviceName scoping

* chore: change shape to make methodOverride compatible

* chore: added methodOverride to remote query

* chore: revert override
This commit is contained in:
Riqwan Thamir authored and GitHub committed 2023-09-13 12:16:00 +02:00
1 parent 7231f65833
commit 3d68be2b6b
10 files changed
+47 -19

No files matched your search

+8
View File
@@ -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
@@ -38,7 +38,6 @@ export const ProductShippingProfile: ModuleJoinerConfig = {
}, },
relationship: { relationship: {
serviceName: LINKS.ProductShippingProfile, serviceName: LINKS.ProductShippingProfile,
isInternalService: true,
primaryKey: "product_id", primaryKey: "product_id",
foreignKey: "id", foreignKey: "id",
alias: "shipping_profile", alias: "shipping_profile",
@@ -43,8 +43,7 @@ export const ProductVariantPriceSet: ModuleJoinerConfig = {
serviceName: LINKS.ProductVariantPriceSet, serviceName: LINKS.ProductVariantPriceSet,
primaryKey: "variant_id", primaryKey: "variant_id",
foreignKey: "id", foreignKey: "id",
alias: "prices", alias: "price",
isList: false,
}, },
}, },
{ {
-1
View File
@@ -157,7 +157,6 @@ export class RemoteQuery {
"skip", "skip",
"take", "take",
"limit", "limit",
"order",
"offset", "offset",
"cursor", "cursor",
"sort", "sort",
@@ -197,6 +197,16 @@ export class RemoteJoiner {
{ fieldAlias, relationships }, { fieldAlias, relationships },
] of expandedRelationships) { ] of expandedRelationships) {
if (!this.serviceConfigCache.has(serviceName)) { 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`) throw new Error(`Service "${serviceName}" was not found`)
} }
@@ -72,7 +72,7 @@ describe("PricingModule Service - PriceSet", () => {
describe("calculatePrices", () => { describe("calculatePrices", () => {
it("retrieves the calculated prices when no context is set", async () => { it("retrieves the calculated prices when no context is set", async () => {
const priceSetsResult = await service.calculatePrices( const priceSetsResult = await service.calculatePrices(
["price-set-1", "price-set-2"], { id: ["price-set-1", "price-set-2"] },
{} {}
) )
@@ -96,9 +96,11 @@ describe("PricingModule Service - PriceSet", () => {
it("retrieves the calculated prices when a context is set", async () => { it("retrieves the calculated prices when a context is set", async () => {
const priceSetsResult = await service.calculatePrices( const priceSetsResult = await service.calculatePrices(
["price-set-1", "price-set-2"], { id: ["price-set-1", "price-set-2"] },
{ {
context: {
currency_code: "USD", currency_code: "USD",
},
} }
) )
@@ -122,9 +124,9 @@ describe("PricingModule Service - PriceSet", () => {
it("retrieves the calculated prices only when id exists in the database", async () => { it("retrieves the calculated prices only when id exists in the database", async () => {
const priceSetsResult = await service.calculatePrices( 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" },
} }
) )
@@ -4,6 +4,8 @@ import {
FindConfig, FindConfig,
InternalModuleDeclaration, InternalModuleDeclaration,
ModuleJoinerConfig, ModuleJoinerConfig,
PricingContext,
PricingFilters,
PricingTypes, PricingTypes,
} from "@medusajs/types" } from "@medusajs/types"
import { Currency, MoneyAmount, PriceSet } from "@models" import { Currency, MoneyAmount, PriceSet } from "@models"
@@ -25,10 +27,6 @@ type InjectedDependencies = {
priceSetService: PriceSetService<any> priceSetService: PriceSetService<any>
} }
type PricingContext = {
currency_code?: string
}
export default class PricingModuleService< export default class PricingModuleService<
TPriceSet extends PriceSet = PriceSet, TPriceSet extends PriceSet = PriceSet,
TMoneyAmount extends MoneyAmount = MoneyAmount, TMoneyAmount extends MoneyAmount = MoneyAmount,
@@ -61,14 +59,15 @@ export default class PricingModuleService<
@InjectManager("baseRepository_") @InjectManager("baseRepository_")
async calculatePrices( async calculatePrices(
priceSetIds: string[], pricingFilters: PricingFilters,
pricingContext: PricingContext, pricingContext: PricingContext = { context: {} },
@MedusaContext() sharedContext: Context = {} @MedusaContext() sharedContext: Context = {}
): Promise<PricingTypes.CalculatedPriceSetDTO> { ): Promise<PricingTypes.CalculatedPriceSetDTO> {
// Keeping this whole logic raw in here for now as they will undergo // 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 // some changes, will abstract them out once we have a final version
const context = pricingContext.context || {}
const priceSetFilters: PricingTypes.FilterablePriceSetProps = { const priceSetFilters: PricingTypes.FilterablePriceSetProps = {
id: priceSetIds, id: pricingFilters.id,
} }
const priceSets = await this.list( const priceSets = await this.list(
@@ -95,8 +94,7 @@ export default class PricingModuleService<
// When no price is set, return null values for all cases // When no price is set, return null values for all cases
const selectedMoneyAmount = priceSet.money_amounts?.find( const selectedMoneyAmount = priceSet.money_amounts?.find(
(ma) => (ma) =>
pricingContext.currency_code && context.currency_code && ma.currency_code === context.currency_code
ma.currency_code === pricingContext.currency_code
) )
return { return {
+5
View File
@@ -3,6 +3,11 @@ export type JoinerRelationship = {
foreignKey: string foreignKey: string
primaryKey: string primaryKey: string
serviceName: 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 * In an inverted relationship the foreign key is on the other service and the primary key is on the current service
*/ */
@@ -2,8 +2,15 @@ import { BaseFilterable } from "../../dal"
import { FilterableMoneyAmountProps, MoneyAmountDTO } from "./money-amount" import { FilterableMoneyAmountProps, MoneyAmountDTO } from "./money-amount"
export interface PricingContext { export interface PricingContext {
context?: {
currency_code?: string currency_code?: string
}
} }
export interface PricingFilters {
id: string[]
}
export interface PriceSetDTO { export interface PriceSetDTO {
id: string id: string
money_amounts?: MoneyAmountDTO[] money_amounts?: MoneyAmountDTO[]
+3 -2
View File
@@ -13,6 +13,7 @@ import {
MoneyAmountDTO, MoneyAmountDTO,
PriceSetDTO, PriceSetDTO,
PricingContext, PricingContext,
PricingFilters,
UpdateCurrencyDTO, UpdateCurrencyDTO,
UpdateMoneyAmountDTO, UpdateMoneyAmountDTO,
UpdatePriceSetDTO, UpdatePriceSetDTO,
@@ -22,8 +23,8 @@ export interface IPricingModuleService {
__joinerConfig(): ModuleJoinerConfig __joinerConfig(): ModuleJoinerConfig
calculatePrices( calculatePrices(
priceSetIds: string[], filters: PricingFilters,
pricingContext: PricingContext, context?: PricingContext,
sharedContext?: Context sharedContext?: Context
): Promise<CalculatedPriceSetDTO> ): Promise<CalculatedPriceSetDTO>