feat(medusa): Performance improvements of Carts domain (#2648)
**What** I have created a new method on the cart service which is `addLineItems`, allowing a user to add one or multiple items in an optimized way. Also updated the `generate` method from the line item service which now also accept a object data or a collection of data which. Various places have been optimized and cache support has been added to the price selection strategy. The overall optimization allows to reach another 9000% improvement in the response time as a median (Creating a cart with 6 items): | | Min (ms) | Median (ms) | Max (ms) | Median Improvement (%) |---|:-:|---|---|---| | Before optimisation | 1200 | 9999 | 12698 | N/A | After optimisation | 63 | 252 | 500 | 39x | After re optimisation | 56 | 82 | 399 | 121x | After including addressed feedback | 65 | 202 | 495 | 49x FIXES CORE-722
This commit is contained in:
@@ -98,19 +98,24 @@ describe("POST /store/carts", () => {
|
||||
})
|
||||
|
||||
it("calls line item generate", () => {
|
||||
expect(CartServiceMock.addOrUpdateLineItems).toHaveBeenCalledTimes(1)
|
||||
|
||||
expect(LineItemServiceMock.generate).toHaveBeenCalledWith(
|
||||
IdMap.getId("testVariant"),
|
||||
IdMap.getId("testRegion"),
|
||||
3,
|
||||
{ customer_id: undefined }
|
||||
[
|
||||
{
|
||||
variantId: IdMap.getId("testVariant"),
|
||||
quantity: 3,
|
||||
},
|
||||
{
|
||||
variantId: IdMap.getId("testVariant1"),
|
||||
quantity: 1,
|
||||
},
|
||||
],
|
||||
{
|
||||
region_id: IdMap.getId("testRegion"),
|
||||
customer_id: undefined,
|
||||
}
|
||||
)
|
||||
expect(LineItemServiceMock.generate).toHaveBeenCalledWith(
|
||||
IdMap.getId("testVariant1"),
|
||||
IdMap.getId("testRegion"),
|
||||
1,
|
||||
{ customer_id: undefined }
|
||||
)
|
||||
expect(CartServiceMock.addLineItem).toHaveBeenCalledTimes(2)
|
||||
})
|
||||
|
||||
it("returns cart", () => {
|
||||
|
||||
@@ -17,7 +17,7 @@ import {
|
||||
RegionService,
|
||||
} from "../../../../services"
|
||||
import { defaultStoreCartFields, defaultStoreCartRelations } from "."
|
||||
import { Cart } from "../../../../models"
|
||||
import { Cart, LineItem } from "../../../../models"
|
||||
import { FeatureFlagDecorators } from "../../../../utils/feature-flag-decorators"
|
||||
import { FlagRouter } from "../../../../utils/flag-router"
|
||||
import SalesChannelFeatureFlag from "../../../../loaders/feature-flags/sales-channels"
|
||||
@@ -179,24 +179,30 @@ export default async (req, res) => {
|
||||
|
||||
let cart: Cart
|
||||
await entityManager.transaction(async (manager) => {
|
||||
cart = await cartService.withTransaction(manager).create(toCreate)
|
||||
const cartServiceTx = cartService.withTransaction(manager)
|
||||
const lineItemServiceTx = lineItemService.withTransaction(manager)
|
||||
|
||||
if (validated.items) {
|
||||
await Promise.all(
|
||||
validated.items.map(async (i) => {
|
||||
const lineItem = await lineItemService
|
||||
.withTransaction(manager)
|
||||
.generate(i.variant_id, regionId, i.quantity, {
|
||||
customer_id: req.user?.customer_id,
|
||||
})
|
||||
return await cartService
|
||||
.withTransaction(manager)
|
||||
.addLineItem(cart.id, lineItem, {
|
||||
validateSalesChannels:
|
||||
featureFlagRouter.isFeatureEnabled("sales_channels"),
|
||||
})
|
||||
})
|
||||
cart = await cartServiceTx.create(toCreate)
|
||||
|
||||
if (validated.items?.length) {
|
||||
const generateInputData = validated.items.map((item) => {
|
||||
return {
|
||||
variantId: item.variant_id,
|
||||
quantity: item.quantity,
|
||||
}
|
||||
})
|
||||
const generatedLineItems: LineItem[] = await lineItemServiceTx.generate(
|
||||
generateInputData,
|
||||
{
|
||||
region_id: regionId,
|
||||
customer_id: req.user?.customer_id,
|
||||
}
|
||||
)
|
||||
|
||||
await cartServiceTx.addOrUpdateLineItems(cart.id, generatedLineItems, {
|
||||
validateSalesChannels:
|
||||
featureFlagRouter.isFeatureEnabled("sales_channels"),
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
@@ -10,3 +10,4 @@ export * from "./models/base-entity"
|
||||
export * from "./models/soft-deletable-entity"
|
||||
export * from "./search-service"
|
||||
export * from "./payment-service"
|
||||
export * from "./services"
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
export interface ICacheService {
|
||||
get<T>(key: string): Promise<T | null>
|
||||
|
||||
set(key: string, data: unknown, ttl?: number): Promise<void>
|
||||
|
||||
invalidate(key: string): Promise<void>
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export * from "./cache"
|
||||
@@ -172,11 +172,19 @@ export class MoneyAmountRepository extends Repository<MoneyAmount> {
|
||||
}
|
||||
if (region_id || currency_code) {
|
||||
qb.andWhere(
|
||||
new Brackets((qb) =>
|
||||
qb
|
||||
.where({ region_id: region_id })
|
||||
.orWhere({ currency_code: currency_code })
|
||||
)
|
||||
new Brackets((qb) => {
|
||||
if (region_id && !currency_code) {
|
||||
qb.where({ region_id: region_id })
|
||||
}
|
||||
if (!region_id && currency_code) {
|
||||
qb.where({ currency_code: currency_code })
|
||||
}
|
||||
if (currency_code && region_id) {
|
||||
qb.where({ region_id: region_id }).orWhere({
|
||||
currency_code: currency_code,
|
||||
})
|
||||
}
|
||||
})
|
||||
)
|
||||
} else if (!customer_id && !include_discount_prices) {
|
||||
qb.andWhere("price_list.id IS null")
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
export const cacheServiceMock = {
|
||||
set: jest.fn().mockImplementation(async () => void 0),
|
||||
get: jest.fn().mockImplementation(async () => null),
|
||||
invalidate: jest.fn().mockImplementation(async () => void 0),
|
||||
}
|
||||
|
||||
const mock = jest.fn().mockImplementation(() => {
|
||||
return cacheServiceMock
|
||||
})
|
||||
|
||||
export default mock
|
||||
@@ -319,6 +319,9 @@ export const CartServiceMock = {
|
||||
addLineItem: jest.fn().mockImplementation((cartId, lineItem) => {
|
||||
return Promise.resolve()
|
||||
}),
|
||||
addOrUpdateLineItems: jest.fn().mockImplementation((cartId, lineItem) => {
|
||||
return Promise.resolve()
|
||||
}),
|
||||
setPaymentMethod: jest.fn().mockImplementation((cartId, method) => {
|
||||
if (method.provider_id === "default_provider") {
|
||||
return Promise.resolve(carts.cartWithPaySessions)
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import { IdMap, MockManager, MockRepository } from "medusa-test-utils"
|
||||
import { FlagRouter } from "../../utils/flag-router"
|
||||
import DiscountService from "../discount"
|
||||
import { TotalsServiceMock } from "../__mocks__/totals"
|
||||
import { newTotalsServiceMock } from "../__mocks__/new-totals"
|
||||
|
||||
const featureFlagRouter = new FlagRouter({})
|
||||
|
||||
@@ -601,15 +603,28 @@ describe("DiscountService", () => {
|
||||
})
|
||||
|
||||
const totalsService = {
|
||||
getSubtotal: () => {
|
||||
...TotalsServiceMock,
|
||||
getSubtotal: async () => {
|
||||
return 1100
|
||||
},
|
||||
}
|
||||
|
||||
const newTotalsService = {
|
||||
...newTotalsServiceMock,
|
||||
getLineItemTotals: async () => {
|
||||
return [
|
||||
{
|
||||
subtotal: 1100,
|
||||
},
|
||||
]
|
||||
},
|
||||
}
|
||||
|
||||
const discountService = new DiscountService({
|
||||
manager: MockManager,
|
||||
discountRepository,
|
||||
totalsService,
|
||||
newTotalsService,
|
||||
featureFlagRouter,
|
||||
})
|
||||
|
||||
@@ -631,21 +646,31 @@ describe("DiscountService", () => {
|
||||
})
|
||||
|
||||
it("correctly calculates fixed + total discount", async () => {
|
||||
let item = {
|
||||
unit_price: 400,
|
||||
quantity: 2,
|
||||
allow_discounts: true,
|
||||
}
|
||||
|
||||
const adjustment1 = await discountService.calculateDiscountForLineItem(
|
||||
"disc_fixed_total",
|
||||
item,
|
||||
{
|
||||
unit_price: 400,
|
||||
quantity: 2,
|
||||
allow_discounts: true,
|
||||
items: [item],
|
||||
}
|
||||
)
|
||||
|
||||
item = {
|
||||
unit_price: 300,
|
||||
quantity: 1,
|
||||
allow_discounts: true,
|
||||
}
|
||||
|
||||
const adjustment2 = await discountService.calculateDiscountForLineItem(
|
||||
"disc_fixed_total",
|
||||
item,
|
||||
{
|
||||
unit_price: 300,
|
||||
quantity: 1,
|
||||
allow_discounts: true,
|
||||
items: [item],
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@@ -32,9 +32,7 @@ import { RegionServiceMock } from "../__mocks__/region"
|
||||
}
|
||||
|
||||
const productVariantService = {
|
||||
withTransaction: function () {
|
||||
return this
|
||||
},
|
||||
...ProductVariantServiceMock,
|
||||
retrieve: (query) => {
|
||||
if (query === IdMap.getId("test-giftcard")) {
|
||||
return {
|
||||
@@ -58,15 +56,25 @@ import { RegionServiceMock } from "../__mocks__/region"
|
||||
}
|
||||
},
|
||||
getRegionPrice: () => 100,
|
||||
list: jest.fn().mockImplementation(async (selector) => {
|
||||
return (selector.id || []).map((id) => ({
|
||||
id,
|
||||
title: "Test variant",
|
||||
product: {
|
||||
title: "Test product",
|
||||
thumbnail: "",
|
||||
discountable: false,
|
||||
is_giftcard: true,
|
||||
},
|
||||
}))
|
||||
}),
|
||||
}
|
||||
|
||||
const pricingService = {
|
||||
withTransaction: function () {
|
||||
return this
|
||||
},
|
||||
getProductVariantPricingById: () => {
|
||||
...PricingServiceMock,
|
||||
getProductVariantsPricing: () => {
|
||||
return {
|
||||
calculated_price: 100,
|
||||
[IdMap.getId("test-giftcard")]: { calculated_price: 100 },
|
||||
}
|
||||
},
|
||||
getProductVariantPricing: () => {
|
||||
@@ -106,15 +114,17 @@ import { RegionServiceMock } from "../__mocks__/region"
|
||||
})
|
||||
|
||||
expect(lineItemRepository.create).toHaveBeenCalledTimes(1)
|
||||
expect(lineItemRepository.create).toHaveBeenCalledWith({
|
||||
variant_id: IdMap.getId("test-variant"),
|
||||
cart_id: IdMap.getId("test-cart"),
|
||||
title: "Test product",
|
||||
description: "Test variant",
|
||||
thumbnail: "",
|
||||
unit_price: 100,
|
||||
quantity: 1,
|
||||
})
|
||||
expect(lineItemRepository.create).toHaveBeenCalledWith([
|
||||
{
|
||||
variant_id: IdMap.getId("test-variant"),
|
||||
cart_id: IdMap.getId("test-cart"),
|
||||
title: "Test product",
|
||||
description: "Test variant",
|
||||
thumbnail: "",
|
||||
unit_price: 100,
|
||||
quantity: 1,
|
||||
},
|
||||
])
|
||||
})
|
||||
|
||||
it("successfully create a line item with price and quantity", async () => {
|
||||
@@ -126,12 +136,14 @@ import { RegionServiceMock } from "../__mocks__/region"
|
||||
})
|
||||
|
||||
expect(lineItemRepository.create).toHaveBeenCalledTimes(1)
|
||||
expect(lineItemRepository.create).toHaveBeenCalledWith({
|
||||
variant_id: IdMap.getId("test-variant"),
|
||||
cart_id: IdMap.getId("test-cart"),
|
||||
unit_price: 50,
|
||||
quantity: 2,
|
||||
})
|
||||
expect(lineItemRepository.create).toHaveBeenCalledWith([
|
||||
{
|
||||
variant_id: IdMap.getId("test-variant"),
|
||||
cart_id: IdMap.getId("test-cart"),
|
||||
unit_price: 50,
|
||||
quantity: 2,
|
||||
},
|
||||
])
|
||||
})
|
||||
|
||||
it("successfully create a line item giftcard", async () => {
|
||||
@@ -147,8 +159,7 @@ import { RegionServiceMock } from "../__mocks__/region"
|
||||
})
|
||||
|
||||
expect(lineItemRepository.create).toHaveBeenCalledTimes(2)
|
||||
expect(lineItemRepository.create).toHaveBeenNthCalledWith(
|
||||
2,
|
||||
expect(lineItemRepository.create).toHaveBeenNthCalledWith(2, [
|
||||
expect.objectContaining({
|
||||
allow_discounts: false,
|
||||
variant_id: IdMap.getId("test-giftcard"),
|
||||
@@ -161,8 +172,8 @@ import { RegionServiceMock } from "../__mocks__/region"
|
||||
is_giftcard: true,
|
||||
should_merge: true,
|
||||
metadata: {},
|
||||
})
|
||||
)
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
@@ -190,6 +201,7 @@ import { RegionServiceMock } from "../__mocks__/region"
|
||||
const lineItemService = new LineItemService({
|
||||
manager: MockManager,
|
||||
lineItemRepository,
|
||||
productVariantService: ProductVariantServiceMock,
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
@@ -329,9 +341,7 @@ describe("LineItemService", () => {
|
||||
}
|
||||
|
||||
const productVariantService = {
|
||||
withTransaction: function () {
|
||||
return this
|
||||
},
|
||||
...ProductVariantServiceMock,
|
||||
retrieve: (query) => {
|
||||
if (query === IdMap.getId("test-giftcard")) {
|
||||
return {
|
||||
@@ -355,16 +365,26 @@ describe("LineItemService", () => {
|
||||
}
|
||||
},
|
||||
getRegionPrice: () => 100,
|
||||
list: jest.fn().mockImplementation(async (selector) => {
|
||||
return (selector.id || []).map((id) => ({
|
||||
id,
|
||||
title: "Test variant",
|
||||
product: {
|
||||
title: "Test product",
|
||||
thumbnail: "",
|
||||
},
|
||||
}))
|
||||
}),
|
||||
}
|
||||
|
||||
const pricingService = {
|
||||
withTransaction: function () {
|
||||
return this
|
||||
},
|
||||
getProductVariantPricingById: () => {
|
||||
...PricingServiceMock,
|
||||
getProductVariantsPricing: () => {
|
||||
return {
|
||||
calculated_price: 100,
|
||||
calculated_price_includes_tax: true,
|
||||
[IdMap.getId("test-variant")]: {
|
||||
calculated_price: 100,
|
||||
calculated_price_includes_tax: true,
|
||||
},
|
||||
}
|
||||
},
|
||||
getProductVariantPricing: () => {
|
||||
@@ -423,6 +443,41 @@ describe("LineItemService", () => {
|
||||
}),
|
||||
})
|
||||
})
|
||||
|
||||
it("successfully create a line item with tax inclusive set to true by passing an object", async () => {
|
||||
await lineItemService.generate(
|
||||
{
|
||||
variantId: IdMap.getId("test-variant"),
|
||||
quantity: 1,
|
||||
},
|
||||
{
|
||||
region_id: IdMap.getId("test-region"),
|
||||
}
|
||||
)
|
||||
|
||||
expect(lineItemRepository.create).toHaveBeenCalledTimes(1)
|
||||
expect(lineItemRepository.create).toHaveBeenCalledWith({
|
||||
unit_price: 100,
|
||||
title: "Test product",
|
||||
description: "Test variant",
|
||||
thumbnail: "",
|
||||
variant_id: IdMap.getId("test-variant"),
|
||||
quantity: 1,
|
||||
allow_discounts: undefined,
|
||||
is_giftcard: undefined,
|
||||
metadata: {},
|
||||
should_merge: true,
|
||||
includes_tax: true,
|
||||
variant: expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
product: expect.objectContaining({
|
||||
thumbnail: "",
|
||||
title: "Test product",
|
||||
}),
|
||||
title: "Test variant",
|
||||
}),
|
||||
})
|
||||
})
|
||||
})
|
||||
describe("generate", () => {
|
||||
const lineItemRepository = MockRepository({
|
||||
@@ -448,9 +503,7 @@ describe("LineItemService", () => {
|
||||
}
|
||||
|
||||
const productVariantService = {
|
||||
withTransaction: function () {
|
||||
return this
|
||||
},
|
||||
...ProductVariantServiceMock,
|
||||
retrieve: (query) => {
|
||||
if (query === IdMap.getId("test-giftcard")) {
|
||||
return {
|
||||
@@ -464,26 +517,30 @@ describe("LineItemService", () => {
|
||||
},
|
||||
}
|
||||
}
|
||||
return {
|
||||
id: IdMap.getId("test-variant"),
|
||||
title: "Test variant",
|
||||
product: {
|
||||
title: "Test product",
|
||||
thumbnail: "",
|
||||
},
|
||||
}
|
||||
},
|
||||
getRegionPrice: () => 100,
|
||||
list: jest.fn().mockImplementation(async (selector) => {
|
||||
return (selector.id || []).map((id) => {
|
||||
return {
|
||||
id,
|
||||
title: "Test variant",
|
||||
product: {
|
||||
title: "Test product",
|
||||
thumbnail: "",
|
||||
},
|
||||
}
|
||||
})
|
||||
}),
|
||||
}
|
||||
|
||||
const pricingService = {
|
||||
withTransaction: function () {
|
||||
return this
|
||||
},
|
||||
getProductVariantPricingById: () => {
|
||||
...PricingServiceMock,
|
||||
getProductVariantsPricing: () => {
|
||||
return {
|
||||
calculated_price: 100,
|
||||
calculated_price_includes_tax: false,
|
||||
[IdMap.getId("test-variant")]: {
|
||||
calculated_price: 100,
|
||||
calculated_price_includes_tax: false,
|
||||
},
|
||||
}
|
||||
},
|
||||
getProductVariantPricing: () => {
|
||||
@@ -542,6 +599,41 @@ describe("LineItemService", () => {
|
||||
}),
|
||||
})
|
||||
})
|
||||
|
||||
it("successfully create a line item with tax inclusive set to false by passing an object", async () => {
|
||||
await lineItemService.generate(
|
||||
{
|
||||
variantId: IdMap.getId("test-variant"),
|
||||
quantity: 1,
|
||||
},
|
||||
{
|
||||
region_id: IdMap.getId("test-region"),
|
||||
}
|
||||
)
|
||||
|
||||
expect(lineItemRepository.create).toHaveBeenCalledTimes(1)
|
||||
expect(lineItemRepository.create).toHaveBeenCalledWith({
|
||||
unit_price: 100,
|
||||
title: "Test product",
|
||||
description: "Test variant",
|
||||
thumbnail: "",
|
||||
variant_id: IdMap.getId("test-variant"),
|
||||
quantity: 1,
|
||||
allow_discounts: undefined,
|
||||
is_giftcard: undefined,
|
||||
metadata: {},
|
||||
should_merge: true,
|
||||
includes_tax: false,
|
||||
variant: expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
product: expect.objectContaining({
|
||||
thumbnail: "",
|
||||
title: "Test product",
|
||||
}),
|
||||
title: "Test variant",
|
||||
}),
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("clone", () => {
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
import { Redis } from "ioredis"
|
||||
import { ICacheService } from "../interfaces"
|
||||
|
||||
const DEFAULT_CACHE_TIME = 30 // 30 seconds
|
||||
const EXPIRY_MODE = "EX" // "EX" stands for an expiry time in second
|
||||
|
||||
export default class CacheService implements ICacheService {
|
||||
protected readonly redis_: Redis
|
||||
|
||||
constructor({ redisClient }) {
|
||||
this.redis_ = redisClient
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a key/value pair to the cache.
|
||||
* It is also possible to manage the ttl through environment variable using CACHE_TTL. If the ttl is 0 it will
|
||||
* act like the value should not be cached at all.
|
||||
* @param key
|
||||
* @param data
|
||||
* @param ttl
|
||||
*/
|
||||
async set(
|
||||
key: string,
|
||||
data: Record<string, unknown>,
|
||||
ttl: number = DEFAULT_CACHE_TIME
|
||||
): Promise<void> {
|
||||
ttl = Number(process.env.CACHE_TTL ?? ttl)
|
||||
if (ttl === 0) {
|
||||
// No need to call redis set without expiry time
|
||||
return
|
||||
}
|
||||
|
||||
await this.redis_.set(key, JSON.stringify(data), EXPIRY_MODE, ttl)
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a cached value belonging to the given key.
|
||||
* @param cacheKey
|
||||
*/
|
||||
async get<T>(cacheKey: string): Promise<T | null> {
|
||||
try {
|
||||
const cached = await this.redis_.get(cacheKey)
|
||||
if (cached) {
|
||||
return JSON.parse(cached)
|
||||
}
|
||||
} catch (err) {
|
||||
await this.redis_.del(cacheKey)
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* Invalidate cache for a specific key. a key can be either a specific key or more global such as "ps:*".
|
||||
* @param key
|
||||
*/
|
||||
async invalidate(key: string): Promise<void> {
|
||||
await this.redis_.del()
|
||||
const keys = await this.redis_.keys(key)
|
||||
const pipeline = this.redis_.pipeline()
|
||||
|
||||
keys.forEach(function (key) {
|
||||
pipeline.del(key)
|
||||
})
|
||||
|
||||
await pipeline.exec()
|
||||
}
|
||||
}
|
||||
@@ -26,6 +26,7 @@ import {
|
||||
FilterableCartProps,
|
||||
isCart,
|
||||
LineItemUpdate,
|
||||
LineItemValidateData,
|
||||
} from "../types/cart"
|
||||
import {
|
||||
AddressPayload,
|
||||
@@ -331,7 +332,7 @@ class CartService extends TransactionBaseService {
|
||||
rawCart.email = customer.email
|
||||
}
|
||||
|
||||
if (!data.region_id) {
|
||||
if (!data.region_id && !data.region) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_DATA,
|
||||
`A region_id must be provided when creating a cart`
|
||||
@@ -339,11 +340,13 @@ class CartService extends TransactionBaseService {
|
||||
}
|
||||
|
||||
rawCart.region_id = data.region_id
|
||||
const region = await this.regionService_
|
||||
.withTransaction(transactionManager)
|
||||
.retrieve(data.region_id, {
|
||||
relations: ["countries"],
|
||||
})
|
||||
const region = data.region
|
||||
? data.region
|
||||
: await this.regionService_
|
||||
.withTransaction(transactionManager)
|
||||
.retrieve(data.region_id!, {
|
||||
relations: ["countries"],
|
||||
})
|
||||
const regCountries = region.countries.map(({ iso_2 }) => iso_2)
|
||||
|
||||
if (!data.shipping_address && !data.shipping_address_id) {
|
||||
@@ -555,15 +558,17 @@ class CartService extends TransactionBaseService {
|
||||
*/
|
||||
protected async validateLineItem(
|
||||
{ sales_channel_id }: { sales_channel_id: string | null },
|
||||
lineItem: LineItem
|
||||
lineItem: LineItemValidateData
|
||||
): Promise<boolean> {
|
||||
if (!sales_channel_id) {
|
||||
return true
|
||||
}
|
||||
|
||||
const lineItemVariant = await this.productVariantService_
|
||||
.withTransaction(this.manager_)
|
||||
.retrieve(lineItem.variant_id)
|
||||
const lineItemVariant = lineItem.variant?.product_id
|
||||
? lineItem.variant
|
||||
: await this.productVariantService_
|
||||
.withTransaction(this.manager_)
|
||||
.retrieve(lineItem.variant_id, { select: ["id", "product_id"] })
|
||||
|
||||
return !!(
|
||||
await this.productService_
|
||||
@@ -583,6 +588,7 @@ class CartService extends TransactionBaseService {
|
||||
* validateSalesChannels - should check if product belongs to the same sales chanel as cart
|
||||
* (if cart has associated sales channel)
|
||||
* @return the result of the update operation
|
||||
* @deprecated Use {@link addOrUpdateLineItems} instead.
|
||||
*/
|
||||
async addLineItem(
|
||||
cartId: string,
|
||||
@@ -659,7 +665,166 @@ class CartService extends TransactionBaseService {
|
||||
{ cart_id: cartId, has_shipping: true },
|
||||
{ has_shipping: false }
|
||||
)
|
||||
.catch(() => void 0)
|
||||
.catch((err: Error | MedusaError) => {
|
||||
// We only want to catch the errors related to not found items since we don't care if there is not item to update
|
||||
if ("type" in err && err.type === MedusaError.Types.NOT_FOUND) {
|
||||
return
|
||||
}
|
||||
throw err
|
||||
})
|
||||
|
||||
cart = await this.retrieve(cart.id, {
|
||||
relations: ["items", "discounts", "discounts.rule", "region"],
|
||||
})
|
||||
|
||||
await this.refreshAdjustments_(cart)
|
||||
|
||||
await this.eventBus_
|
||||
.withTransaction(transactionManager)
|
||||
.emit(CartService.Events.UPDATED, { id: cart.id })
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds or update one or multiple line items to the cart. It also update all existing items in the cart
|
||||
* to have has_shipping to false. Finally, the adjustments will be updated.
|
||||
* @param cartId - the id of the cart that we will add to
|
||||
* @param lineItems - the line items to add.
|
||||
* @param config
|
||||
* validateSalesChannels - should check if product belongs to the same sales chanel as cart
|
||||
* (if cart has associated sales channel)
|
||||
* @return the result of the update operation
|
||||
*/
|
||||
async addOrUpdateLineItems(
|
||||
cartId: string,
|
||||
lineItems: LineItem | LineItem[],
|
||||
config = { validateSalesChannels: true }
|
||||
): Promise<void> {
|
||||
const items: LineItem[] = Array.isArray(lineItems) ? lineItems : [lineItems]
|
||||
|
||||
const select: (keyof Cart)[] = ["id"]
|
||||
|
||||
if (this.featureFlagRouter_.isFeatureEnabled("sales_channels")) {
|
||||
select.push("sales_channel_id")
|
||||
}
|
||||
|
||||
return await this.atomicPhase_(
|
||||
async (transactionManager: EntityManager) => {
|
||||
let cart = await this.retrieve(cartId, { select })
|
||||
|
||||
if (this.featureFlagRouter_.isFeatureEnabled("sales_channels")) {
|
||||
if (config.validateSalesChannels) {
|
||||
const areValid = await Promise.all(
|
||||
items.map(async (item) => {
|
||||
return await this.validateLineItem(cart, item)
|
||||
})
|
||||
)
|
||||
|
||||
const invalidProducts = areValid
|
||||
.map((valid, index) => {
|
||||
return !valid ? { title: items[index].title } : undefined
|
||||
})
|
||||
.filter((v): v is { title: string } => !!v)
|
||||
|
||||
if (invalidProducts.length) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_DATA,
|
||||
`The products [${invalidProducts
|
||||
.map((item) => item.title)
|
||||
.join(
|
||||
" - "
|
||||
)}] must belongs to the sales channel on which the cart has been created.`
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const lineItemServiceTx =
|
||||
this.lineItemService_.withTransaction(transactionManager)
|
||||
const inventoryServiceTx =
|
||||
this.inventoryService_.withTransaction(transactionManager)
|
||||
|
||||
const existingItems = await lineItemServiceTx.list(
|
||||
{
|
||||
cart_id: cart.id,
|
||||
variant_id: In([items.map((item) => item.variant_id)]),
|
||||
should_merge: true,
|
||||
},
|
||||
{ select: ["id", "metadata", "quantity"] }
|
||||
)
|
||||
|
||||
const existingItemsVariantMap = new Map()
|
||||
existingItems.forEach((item) => {
|
||||
existingItemsVariantMap.set(item.variant_id, item)
|
||||
})
|
||||
|
||||
const lineItemsToCreate: LineItem[] = []
|
||||
const lineItemsToUpdate: { [id: string]: LineItem }[] = []
|
||||
for (const item of items) {
|
||||
let currentItem: LineItem | undefined
|
||||
|
||||
const existingItem = existingItemsVariantMap.get(item.variant_id)
|
||||
if (item.should_merge) {
|
||||
if (existingItem && isEqual(existingItem.metadata, item.metadata)) {
|
||||
currentItem = existingItem
|
||||
}
|
||||
}
|
||||
|
||||
// If content matches one of the line items currently in the cart we can
|
||||
// simply update the quantity of the existing line item
|
||||
item.quantity = currentItem
|
||||
? (currentItem.quantity += item.quantity)
|
||||
: item.quantity
|
||||
|
||||
await inventoryServiceTx.confirmInventory(
|
||||
item.variant_id,
|
||||
item.quantity
|
||||
)
|
||||
|
||||
if (currentItem) {
|
||||
lineItemsToUpdate[currentItem.id] = {
|
||||
quantity: item.quantity,
|
||||
has_shipping: false,
|
||||
}
|
||||
} else {
|
||||
// Since the variant is eager loaded, we are removing it before the line item is being created.
|
||||
delete (item as Partial<LineItem>).variant
|
||||
item.has_shipping = false
|
||||
item.cart_id = cart.id
|
||||
lineItemsToCreate.push(item)
|
||||
}
|
||||
}
|
||||
|
||||
const itemKeysToUpdate = Object.keys(lineItemsToUpdate)
|
||||
|
||||
// Update all items that needs to be updated
|
||||
if (itemKeysToUpdate.length) {
|
||||
await Promise.all(
|
||||
itemKeysToUpdate.map(async (id) => {
|
||||
return await lineItemServiceTx.update(id, lineItemsToUpdate[id])
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
// Create all items that needs to be created
|
||||
await lineItemServiceTx.create(lineItemsToCreate)
|
||||
|
||||
await lineItemServiceTx
|
||||
.update(
|
||||
{
|
||||
cart_id: cartId,
|
||||
has_shipping: true,
|
||||
},
|
||||
{ has_shipping: false }
|
||||
)
|
||||
.catch((err: Error | MedusaError) => {
|
||||
// We only want to catch the errors related to not found items since we don't care if there is not item to update
|
||||
if ("type" in err && err.type === MedusaError.Types.NOT_FOUND) {
|
||||
return
|
||||
}
|
||||
throw err
|
||||
})
|
||||
|
||||
cart = await this.retrieve(cart.id, {
|
||||
relations: ["items", "discounts", "discounts.rule", "region"],
|
||||
|
||||
@@ -41,6 +41,7 @@ import { isFuture, isPast } from "../utils/date-helpers"
|
||||
import { FlagRouter } from "../utils/flag-router"
|
||||
import CustomerService from "./customer"
|
||||
import DiscountConditionService from "./discount-condition"
|
||||
import { CalculationContextData } from "../types/totals"
|
||||
|
||||
/**
|
||||
* Provides layer to manipulate discounts.
|
||||
@@ -573,7 +574,7 @@ class DiscountService extends TransactionBaseService {
|
||||
async calculateDiscountForLineItem(
|
||||
discountId: string,
|
||||
lineItem: LineItem,
|
||||
cart: Cart
|
||||
calculationContextData: CalculationContextData
|
||||
): Promise<number> {
|
||||
return await this.atomicPhase_(async (transactionManager) => {
|
||||
let adjustment = 0
|
||||
@@ -586,6 +587,12 @@ class DiscountService extends TransactionBaseService {
|
||||
|
||||
const { type, value, allocation } = discount.rule
|
||||
|
||||
const calculationContext = await this.totalsService_
|
||||
.withTransaction(transactionManager)
|
||||
.getCalculationContext(calculationContextData, {
|
||||
exclude_shipping: true,
|
||||
})
|
||||
|
||||
let fullItemPrice = lineItem.unit_price * lineItem.quantity
|
||||
if (
|
||||
this.featureFlagRouter_.isFeatureEnabled(
|
||||
@@ -593,11 +600,6 @@ class DiscountService extends TransactionBaseService {
|
||||
) &&
|
||||
lineItem.includes_tax
|
||||
) {
|
||||
const calculationContext = await this.totalsService_
|
||||
.withTransaction(transactionManager)
|
||||
.getCalculationContext(cart, {
|
||||
exclude_shipping: true,
|
||||
})
|
||||
const lineItemTotals = await this.newTotalsService_
|
||||
.withTransaction(transactionManager)
|
||||
.getLineItemTotals([lineItem], {
|
||||
@@ -616,15 +618,26 @@ class DiscountService extends TransactionBaseService {
|
||||
// when a fixed discount should be applied to the total,
|
||||
// we create line adjustments for each item with an amount
|
||||
// relative to the subtotal
|
||||
const subtotal = await this.totalsService_.getSubtotal(cart, {
|
||||
excludeNonDiscounts: true,
|
||||
})
|
||||
const discountedItems = calculationContextData.items.filter(
|
||||
(item) => item.allow_discounts
|
||||
)
|
||||
const totals = await this.newTotalsService_.getLineItemTotals(
|
||||
discountedItems,
|
||||
{
|
||||
calculationContext,
|
||||
}
|
||||
)
|
||||
const subtotal = Object.values(totals).reduce((subtotal, total) => {
|
||||
subtotal += total.subtotal
|
||||
return subtotal
|
||||
}, 0)
|
||||
const nominator = Math.min(value, subtotal)
|
||||
const totalItemPercentage = fullItemPrice / subtotal
|
||||
adjustment = Math.round(nominator * totalItemPercentage)
|
||||
} else {
|
||||
adjustment = value * lineItem.quantity
|
||||
}
|
||||
|
||||
// if the amount of the discount exceeds the total price of the item,
|
||||
// we return the total item price, else the fixed amount
|
||||
return adjustment >= fullItemPrice ? fullItemPrice : adjustment
|
||||
|
||||
@@ -55,28 +55,28 @@ export default class EventBusService {
|
||||
config: ConfigModule,
|
||||
singleton = true
|
||||
) {
|
||||
const opts = {
|
||||
createClient: (type: string): Redis.Redis => {
|
||||
switch (type) {
|
||||
case "client":
|
||||
return redisClient
|
||||
case "subscriber":
|
||||
return redisSubscriber
|
||||
default:
|
||||
if (config.projectConfig.redis_url) {
|
||||
return new Redis(config.projectConfig.redis_url)
|
||||
}
|
||||
return redisClient
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
this.config_ = config
|
||||
this.manager_ = manager
|
||||
this.logger_ = logger
|
||||
this.stagedJobRepository_ = stagedJobRepository
|
||||
|
||||
if (singleton) {
|
||||
const opts = {
|
||||
createClient: (type: string): Redis.Redis => {
|
||||
switch (type) {
|
||||
case "client":
|
||||
return redisClient
|
||||
case "subscriber":
|
||||
return redisSubscriber
|
||||
default:
|
||||
if (config.projectConfig.redis_url) {
|
||||
return new Redis(config.projectConfig.redis_url)
|
||||
}
|
||||
return redisClient
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
this.observers_ = new Map()
|
||||
this.queue_ = new Bull(`${this.constructor.name}:queue`, opts)
|
||||
this.cronHandlers_ = new Map()
|
||||
|
||||
@@ -2,6 +2,7 @@ export { default as AnalyticsConfigService } from "./analytics-config"
|
||||
export { default as AuthService } from "./auth"
|
||||
export { default as BatchJobService } from "./batch-job"
|
||||
export { default as CartService } from "./cart"
|
||||
export { default as CacheService } from "./cache"
|
||||
export { default as ClaimService } from "./claim"
|
||||
export { default as ClaimItemService } from "./claim-item"
|
||||
export { default as CurrencyService } from "./currency"
|
||||
@@ -35,7 +36,7 @@ export { default as ProductService } from "./product"
|
||||
export { default as ProductCollectionService } from "./product-collection"
|
||||
export { default as ProductTypeService } from "./product-type"
|
||||
export { default as ProductVariantService } from "./product-variant"
|
||||
import { default as PublishableApiKey } from "./publishable-api-key"
|
||||
|
||||
export { default as RegionService } from "./region"
|
||||
export { default as ReturnService } from "./return"
|
||||
export { default as ReturnReasonService } from "./return-reason"
|
||||
|
||||
@@ -3,6 +3,7 @@ import { TransactionBaseService } from "../interfaces"
|
||||
import { EntityManager } from "typeorm"
|
||||
import ProductVariantService from "./product-variant"
|
||||
import { ProductVariant } from "../models"
|
||||
import { isDefined } from "../utils"
|
||||
|
||||
type InventoryServiceProps = {
|
||||
manager: EntityManager
|
||||
@@ -62,22 +63,29 @@ class InventoryService extends TransactionBaseService {
|
||||
* @return true if the inventory covers the quantity
|
||||
*/
|
||||
async confirmInventory(
|
||||
variantId: string | undefined | null,
|
||||
variantId: string | null | undefined,
|
||||
quantity: number
|
||||
): Promise<boolean> {
|
||||
// if variantId is undefined then confirm inventory as it
|
||||
// is a custom item that is not managed
|
||||
if (typeof variantId === "undefined" || variantId === null) {
|
||||
if (!isDefined(variantId) || variantId === null) {
|
||||
return true
|
||||
}
|
||||
|
||||
const variant = await this.productVariantService_
|
||||
.withTransaction(this.manager_)
|
||||
.retrieve(variantId)
|
||||
.retrieve(variantId, {
|
||||
select: [
|
||||
"id",
|
||||
"inventory_quantity",
|
||||
"allow_backorder",
|
||||
"manage_inventory",
|
||||
],
|
||||
})
|
||||
|
||||
const { inventory_quantity, allow_backorder, manage_inventory } = variant
|
||||
const isCovered =
|
||||
!manage_inventory || allow_backorder || inventory_quantity >= quantity
|
||||
|
||||
if (!isCovered) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_ALLOWED,
|
||||
|
||||
@@ -1,19 +1,14 @@
|
||||
import { MedusaError } from "medusa-core-utils"
|
||||
import { EntityManager, In } from "typeorm"
|
||||
|
||||
import {
|
||||
Cart,
|
||||
DiscountRuleType,
|
||||
LineItem,
|
||||
LineItemAdjustment,
|
||||
ProductVariant,
|
||||
} from "../models"
|
||||
import { Cart, DiscountRuleType, LineItem, LineItemAdjustment } from "../models"
|
||||
import { LineItemAdjustmentRepository } from "../repositories/line-item-adjustment"
|
||||
import { FindConfig } from "../types/common"
|
||||
import { FilterableLineItemAdjustmentProps } from "../types/line-item-adjustment"
|
||||
import DiscountService from "./discount"
|
||||
import { TransactionBaseService } from "../interfaces"
|
||||
import { buildQuery, setMetadata } from "../utils"
|
||||
import { CalculationContextData } from "../types/totals"
|
||||
|
||||
type LineItemAdjustmentServiceProps = {
|
||||
manager: EntityManager
|
||||
@@ -22,7 +17,7 @@ type LineItemAdjustmentServiceProps = {
|
||||
}
|
||||
|
||||
type AdjustmentContext = {
|
||||
variant: ProductVariant
|
||||
variant: { product_id: string }
|
||||
}
|
||||
|
||||
type GeneratedAdjustment = {
|
||||
@@ -174,13 +169,13 @@ class LineItemAdjustmentService extends TransactionBaseService {
|
||||
|
||||
/**
|
||||
* Creates adjustment for a line item
|
||||
* @param cart - the cart object holding discounts
|
||||
* @param calculationContextData - the calculationContextData object holding discounts
|
||||
* @param generatedLineItem - the line item for which a line item adjustment might be created
|
||||
* @param context - the line item for which a line item adjustment might be created
|
||||
* @return a line item adjustment or undefined if no adjustment was created
|
||||
*/
|
||||
async generateAdjustments(
|
||||
cart: Cart,
|
||||
calculationContextData: CalculationContextData,
|
||||
generatedLineItem: LineItem,
|
||||
context: AdjustmentContext
|
||||
): Promise<GeneratedAdjustment[]> {
|
||||
@@ -196,12 +191,12 @@ class LineItemAdjustmentService extends TransactionBaseService {
|
||||
if (
|
||||
!lineItem.allow_discounts ||
|
||||
lineItem.is_return ||
|
||||
!cart?.discounts?.length
|
||||
!calculationContextData?.discounts?.length
|
||||
) {
|
||||
return []
|
||||
}
|
||||
|
||||
const [discount] = cart.discounts.filter(
|
||||
const [discount] = calculationContextData.discounts.filter(
|
||||
(d) => d.rule.type !== DiscountRuleType.FREE_SHIPPING
|
||||
)
|
||||
|
||||
@@ -226,7 +221,7 @@ class LineItemAdjustmentService extends TransactionBaseService {
|
||||
const amount = await this.discountService.calculateDiscountForLineItem(
|
||||
discount.id,
|
||||
lineItem,
|
||||
cart
|
||||
calculationContextData
|
||||
)
|
||||
|
||||
// if discounted amount is 0, then do nothing
|
||||
@@ -234,15 +229,13 @@ class LineItemAdjustmentService extends TransactionBaseService {
|
||||
return []
|
||||
}
|
||||
|
||||
const adjustments = [
|
||||
return [
|
||||
{
|
||||
amount,
|
||||
discount_id: discount.id,
|
||||
description: "discount",
|
||||
},
|
||||
]
|
||||
|
||||
return adjustments
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,12 @@ import { DeepPartial } from "typeorm/common/DeepPartial"
|
||||
import { CartRepository } from "../repositories/cart"
|
||||
import { LineItemRepository } from "../repositories/line-item"
|
||||
import { LineItemTaxLineRepository } from "../repositories/line-item-tax-line"
|
||||
import { Cart, LineItem, LineItemAdjustment, LineItemTaxLine } from "../models"
|
||||
import {
|
||||
LineItem,
|
||||
LineItemAdjustment,
|
||||
LineItemTaxLine,
|
||||
ProductVariant,
|
||||
} from "../models"
|
||||
import { FindConfig, Selector } from "../types/common"
|
||||
import { FlagRouter } from "../utils/flag-router"
|
||||
import LineItemAdjustmentService from "./line-item-adjustment"
|
||||
@@ -18,8 +23,10 @@ import {
|
||||
RegionService,
|
||||
TaxProviderService,
|
||||
} from "./index"
|
||||
import { buildQuery, setMetadata } from "../utils"
|
||||
import { buildQuery, isString, setMetadata } from "../utils"
|
||||
import { TransactionBaseService } from "../interfaces"
|
||||
import { GenerateInputData, GenerateLineItemContext } from "../types/line-item"
|
||||
import { ProductVariantPricing } from "../types/pricing"
|
||||
|
||||
type InjectedDependencies = {
|
||||
manager: EntityManager
|
||||
@@ -178,115 +185,205 @@ class LineItemService extends TransactionBaseService {
|
||||
)
|
||||
}
|
||||
|
||||
async generate(
|
||||
variantId: string,
|
||||
regionId: string,
|
||||
quantity: number,
|
||||
context: {
|
||||
unit_price?: number
|
||||
includes_tax?: boolean
|
||||
metadata?: Record<string, unknown>
|
||||
customer_id?: string
|
||||
order_edit_id?: string
|
||||
cart?: Cart
|
||||
} = {}
|
||||
): Promise<LineItem> {
|
||||
/**
|
||||
* Generate a single or multiple line item without persisting the data into the db
|
||||
* @param variantIdOrData
|
||||
* @param regionIdOrContext
|
||||
* @param quantity
|
||||
* @param context
|
||||
*/
|
||||
async generate<
|
||||
T = string | GenerateInputData | GenerateInputData[],
|
||||
TResult = T extends string
|
||||
? LineItem
|
||||
: T extends LineItem
|
||||
? LineItem
|
||||
: LineItem[]
|
||||
>(
|
||||
variantIdOrData: string | T,
|
||||
regionIdOrContext: T extends string ? string : GenerateLineItemContext,
|
||||
quantity?: number,
|
||||
context: GenerateLineItemContext = {}
|
||||
): Promise<TResult> {
|
||||
return await this.atomicPhase_(
|
||||
async (transactionManager: EntityManager) => {
|
||||
const [variant, region] = await Promise.all([
|
||||
this.productVariantService_
|
||||
.withTransaction(transactionManager)
|
||||
.retrieve(variantId, {
|
||||
relations: ["product"],
|
||||
}),
|
||||
this.regionService_
|
||||
.withTransaction(transactionManager)
|
||||
.retrieve(regionId),
|
||||
])
|
||||
|
||||
let unit_price = Number(context.unit_price) < 0 ? 0 : context.unit_price
|
||||
|
||||
let unitPriceIncludesTax = false
|
||||
|
||||
let shouldMerge = false
|
||||
|
||||
if (context.unit_price === undefined || context.unit_price === null) {
|
||||
shouldMerge = true
|
||||
const variantPricing = await this.pricingService_
|
||||
.withTransaction(transactionManager)
|
||||
.getProductVariantPricingById(variant.id, {
|
||||
region_id: region.id,
|
||||
quantity: quantity,
|
||||
customer_id: context?.customer_id,
|
||||
include_discount_prices: true,
|
||||
})
|
||||
|
||||
unitPriceIncludesTax = !!variantPricing.calculated_price_includes_tax
|
||||
|
||||
unit_price = variantPricing.calculated_price ?? undefined
|
||||
}
|
||||
|
||||
const rawLineItem: Partial<LineItem> = {
|
||||
unit_price: unit_price,
|
||||
title: variant.product.title,
|
||||
description: variant.title,
|
||||
thumbnail: variant.product.thumbnail,
|
||||
variant_id: variant.id,
|
||||
quantity: quantity || 1,
|
||||
allow_discounts: variant.product.discountable,
|
||||
is_giftcard: variant.product.is_giftcard,
|
||||
metadata: context?.metadata || {},
|
||||
should_merge: shouldMerge,
|
||||
}
|
||||
|
||||
if (
|
||||
this.featureFlagRouter_.isFeatureEnabled(
|
||||
TaxInclusivePricingFeatureFlag.key
|
||||
)
|
||||
) {
|
||||
rawLineItem.includes_tax = unitPriceIncludesTax
|
||||
}
|
||||
|
||||
if (
|
||||
this.featureFlagRouter_.isFeatureEnabled(OrderEditingFeatureFlag.key)
|
||||
) {
|
||||
rawLineItem.order_edit_id = context.order_edit_id || null
|
||||
}
|
||||
|
||||
const lineItemRepo = transactionManager.getCustomRepository(
|
||||
this.lineItemRepository_
|
||||
this.validateGenerateArguments(
|
||||
variantIdOrData,
|
||||
regionIdOrContext,
|
||||
quantity
|
||||
)
|
||||
const lineItem = lineItemRepo.create({
|
||||
...rawLineItem,
|
||||
variant,
|
||||
})
|
||||
|
||||
if (context.cart) {
|
||||
const adjustments = await this.lineItemAdjustmentService_
|
||||
.withTransaction(transactionManager)
|
||||
.generateAdjustments(context.cart, lineItem, { variant })
|
||||
lineItem.adjustments = adjustments as unknown as LineItemAdjustment[]
|
||||
const data = isString(variantIdOrData)
|
||||
? {
|
||||
variantId: variantIdOrData,
|
||||
quantity: quantity as number,
|
||||
}
|
||||
: variantIdOrData
|
||||
const resolvedContext = isString(variantIdOrData)
|
||||
? context
|
||||
: (regionIdOrContext as GenerateLineItemContext)
|
||||
const regionId = (
|
||||
isString(variantIdOrData)
|
||||
? regionIdOrContext
|
||||
: resolvedContext.region_id
|
||||
) as string
|
||||
|
||||
const resolvedData = (
|
||||
Array.isArray(data) ? data : [data]
|
||||
) as GenerateInputData[]
|
||||
|
||||
const variants = await this.productVariantService_.list(
|
||||
{
|
||||
id: resolvedData.map((d) => d.variantId),
|
||||
},
|
||||
{
|
||||
relations: ["product"],
|
||||
}
|
||||
)
|
||||
|
||||
const variantsMap = new Map<string, ProductVariant>()
|
||||
const variantIdsToCalculatePricingFor: string[] = []
|
||||
|
||||
for (const variant of variants) {
|
||||
variantsMap.set(variant.id, variant)
|
||||
if (resolvedContext.unit_price == null) {
|
||||
variantIdsToCalculatePricingFor.push(variant.id)
|
||||
}
|
||||
}
|
||||
|
||||
return lineItem
|
||||
const variantsPricing = await this.pricingService_
|
||||
.withTransaction(transactionManager)
|
||||
.getProductVariantsPricing(variantIdsToCalculatePricingFor, {
|
||||
region_id: regionId,
|
||||
quantity: quantity,
|
||||
customer_id: context?.customer_id,
|
||||
include_discount_prices: true,
|
||||
})
|
||||
|
||||
const generatedItems: LineItem[] = []
|
||||
|
||||
for (const variantData of resolvedData) {
|
||||
const variant = variantsMap.get(
|
||||
variantData.variantId
|
||||
) as ProductVariant
|
||||
const variantPricing = variantsPricing[variantData.variantId]
|
||||
|
||||
const lineItem = await this.generateLineItem(
|
||||
variant,
|
||||
variantData.quantity,
|
||||
{
|
||||
...resolvedContext,
|
||||
variantPricing,
|
||||
}
|
||||
)
|
||||
|
||||
if (resolvedContext.cart) {
|
||||
const adjustments = await this.lineItemAdjustmentService_
|
||||
.withTransaction(transactionManager)
|
||||
.generateAdjustments(resolvedContext.cart, lineItem, { variant })
|
||||
lineItem.adjustments =
|
||||
adjustments as unknown as LineItemAdjustment[]
|
||||
}
|
||||
|
||||
generatedItems.push(lineItem)
|
||||
}
|
||||
|
||||
return (Array.isArray(data)
|
||||
? generatedItems
|
||||
: generatedItems[0]) as unknown as TResult
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
protected async generateLineItem(
|
||||
variant: {
|
||||
id: string
|
||||
title: string
|
||||
product_id: string
|
||||
product: {
|
||||
title: string
|
||||
thumbnail: string | null
|
||||
discountable: boolean
|
||||
is_giftcard: boolean
|
||||
}
|
||||
},
|
||||
quantity: number,
|
||||
context: GenerateLineItemContext & {
|
||||
variantPricing: ProductVariantPricing
|
||||
}
|
||||
): Promise<LineItem> {
|
||||
const transactionManager = this.transactionManager_ ?? this.manager_
|
||||
|
||||
let unit_price = Number(context.unit_price) < 0 ? 0 : context.unit_price
|
||||
let unitPriceIncludesTax = false
|
||||
let shouldMerge = false
|
||||
|
||||
if (context.unit_price == null) {
|
||||
shouldMerge = true
|
||||
|
||||
unitPriceIncludesTax =
|
||||
!!context.variantPricing?.calculated_price_includes_tax
|
||||
unit_price = context.variantPricing?.calculated_price ?? undefined
|
||||
}
|
||||
|
||||
const rawLineItem: Partial<LineItem> = {
|
||||
unit_price: unit_price,
|
||||
title: variant.product.title,
|
||||
description: variant.title,
|
||||
thumbnail: variant.product.thumbnail,
|
||||
variant_id: variant.id,
|
||||
quantity: quantity || 1,
|
||||
allow_discounts: variant.product.discountable,
|
||||
is_giftcard: variant.product.is_giftcard,
|
||||
metadata: context?.metadata || {},
|
||||
should_merge: shouldMerge,
|
||||
}
|
||||
|
||||
if (
|
||||
this.featureFlagRouter_.isFeatureEnabled(
|
||||
TaxInclusivePricingFeatureFlag.key
|
||||
)
|
||||
) {
|
||||
rawLineItem.includes_tax = unitPriceIncludesTax
|
||||
}
|
||||
|
||||
if (this.featureFlagRouter_.isFeatureEnabled(OrderEditingFeatureFlag.key)) {
|
||||
rawLineItem.order_edit_id = context.order_edit_id || null
|
||||
}
|
||||
|
||||
const lineItemRepo = transactionManager.getCustomRepository(
|
||||
this.lineItemRepository_
|
||||
)
|
||||
|
||||
const lineItem = lineItemRepo.create(rawLineItem)
|
||||
lineItem.variant = variant as ProductVariant
|
||||
|
||||
return lineItem
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a line item
|
||||
* @param data - the line item object to create
|
||||
* @return the created line item
|
||||
*/
|
||||
async create(data: Partial<LineItem>): Promise<LineItem> {
|
||||
async create<
|
||||
T = LineItem | LineItem[],
|
||||
TResult = T extends LineItem ? LineItem : LineItem[]
|
||||
>(data: T): Promise<TResult> {
|
||||
return await this.atomicPhase_(
|
||||
async (transactionManager: EntityManager) => {
|
||||
const lineItemRepository = transactionManager.getCustomRepository(
|
||||
this.lineItemRepository_
|
||||
)
|
||||
|
||||
const item = lineItemRepository.create(data)
|
||||
return await lineItemRepository.save(item)
|
||||
const data_ = Array.isArray(data) ? data : [data]
|
||||
|
||||
const items = lineItemRepository.create(data_)
|
||||
const lineItems = await lineItemRepository.save(items)
|
||||
|
||||
return (Array.isArray(data)
|
||||
? lineItems
|
||||
: lineItems[0]) as unknown as TResult
|
||||
}
|
||||
)
|
||||
}
|
||||
@@ -326,14 +423,8 @@ class LineItemService extends TransactionBaseService {
|
||||
}
|
||||
|
||||
lineItems = lineItems.map((item) => {
|
||||
const lineItemMetadata = metadata
|
||||
? setMetadata(item, metadata)
|
||||
: item.metadata
|
||||
|
||||
return Object.assign(item, {
|
||||
...rest,
|
||||
metadata: lineItemMetadata,
|
||||
})
|
||||
item.metadata = metadata ? setMetadata(item, metadata) : item.metadata
|
||||
return Object.assign(item, rest)
|
||||
})
|
||||
|
||||
return await lineItemRepository.save(lineItems)
|
||||
@@ -464,6 +555,37 @@ class LineItemService extends TransactionBaseService {
|
||||
return await lineItemRepository.save(clonedLineItemEntities)
|
||||
})
|
||||
}
|
||||
|
||||
protected validateGenerateArguments<
|
||||
T = string | GenerateInputData | GenerateInputData[],
|
||||
TResult = T extends string
|
||||
? LineItem
|
||||
: T extends LineItem
|
||||
? LineItem
|
||||
: LineItem[]
|
||||
>(
|
||||
variantIdOrData: string | T,
|
||||
regionIdOrContext: T extends string ? string : GenerateLineItemContext,
|
||||
quantity?: number
|
||||
): void | never {
|
||||
if (isString(variantIdOrData)) {
|
||||
if (!quantity || !regionIdOrContext || !isString(regionIdOrContext)) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.UNEXPECTED_STATE,
|
||||
"The generate method has been called with a variant id but one of the argument quantity or regionId is missing. Please, provide the variantId, quantity and regionId."
|
||||
)
|
||||
}
|
||||
} else {
|
||||
const resolvedContext = regionIdOrContext as GenerateLineItemContext
|
||||
|
||||
if (!resolvedContext.region_id) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.UNEXPECTED_STATE,
|
||||
"The generate method has been called with the data but the context is missing either region_id or region. Please provide at least one of region or region_id."
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default LineItemService
|
||||
|
||||
@@ -7,7 +7,7 @@ import {
|
||||
PriceSelectionContext,
|
||||
} from "../interfaces/price-selection-strategy"
|
||||
import TaxInclusivePricingFeatureFlag from "../loaders/feature-flags/tax-inclusive-pricing"
|
||||
import { Product, ProductVariant, ShippingOption } from "../models"
|
||||
import { Product, ProductVariant, Region, ShippingOption } from "../models"
|
||||
import {
|
||||
PricedProduct,
|
||||
PricedShippingOption,
|
||||
@@ -73,8 +73,9 @@ class PricingService extends TransactionBaseService {
|
||||
let taxRate: number | null = null
|
||||
let currencyCode = context.currency_code
|
||||
|
||||
let region: Region
|
||||
if (context.region_id) {
|
||||
const region = await this.regionService
|
||||
region = await this.regionService
|
||||
.withTransaction(this.manager_)
|
||||
.retrieve(context.region_id, {
|
||||
select: ["id", "currency_code", "automatic_taxes", "tax_rate"],
|
||||
@@ -247,6 +248,7 @@ class PricingService extends TransactionBaseService {
|
||||
* @param variantId - the id of the variant to get prices for
|
||||
* @param context - the price selection context to use
|
||||
* @return The product variant prices
|
||||
* @deprecated Use {@link getProductVariantsPricing} instead.
|
||||
*/
|
||||
async getProductVariantPricingById(
|
||||
variantId: string,
|
||||
@@ -267,6 +269,7 @@ class PricingService extends TransactionBaseService {
|
||||
const { product_id } = await this.productVariantService
|
||||
.withTransaction(this.manager_)
|
||||
.retrieve(variantId, { select: ["id", "product_id"] })
|
||||
|
||||
productRates = await this.taxProviderService
|
||||
.withTransaction(this.manager_)
|
||||
.getRegionRatesForProduct(product_id, {
|
||||
@@ -282,6 +285,69 @@ class PricingService extends TransactionBaseService {
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the prices for a collection of variants.
|
||||
* @param variantIds - the id of the variants to get the prices for
|
||||
* @param context - the price selection context to use
|
||||
* @return The product variant prices
|
||||
*/
|
||||
async getProductVariantsPricing<
|
||||
T = string | string[],
|
||||
TOutput = T extends string
|
||||
? ProductVariantPricing
|
||||
: { [variant_id: string]: ProductVariantPricing }
|
||||
>(
|
||||
variantIds: T,
|
||||
context: PriceSelectionContext | PricingContext
|
||||
): Promise<TOutput> {
|
||||
let pricingContext: PricingContext
|
||||
if ("automatic_taxes" in context) {
|
||||
pricingContext = context
|
||||
} else {
|
||||
pricingContext = await this.collectPricingContext(context)
|
||||
}
|
||||
|
||||
const ids = (
|
||||
Array.isArray(variantIds) ? variantIds : [variantIds]
|
||||
) as string[]
|
||||
|
||||
const variants = await this.productVariantService
|
||||
.withTransaction(this.manager_)
|
||||
.list({ id: ids }, { select: ["id", "product_id"] })
|
||||
|
||||
const variantsMap = new Map(
|
||||
variants.map((variant) => {
|
||||
return [variant.id, variant]
|
||||
})
|
||||
)
|
||||
|
||||
const pricingResult: { [variant_id: string]: ProductVariantPricing } = {}
|
||||
for (const variantId of ids) {
|
||||
const { id, product_id } = variantsMap.get(variantId)!
|
||||
|
||||
let productRates: TaxServiceRate[] = []
|
||||
|
||||
if (pricingContext.price_selection.region_id) {
|
||||
productRates = await this.taxProviderService
|
||||
.withTransaction(this.manager_)
|
||||
.getRegionRatesForProduct(product_id, {
|
||||
id: pricingContext.price_selection.region_id,
|
||||
tax_rate: pricingContext.tax_rate,
|
||||
})
|
||||
}
|
||||
|
||||
pricingResult[id] = await this.getProductVariantPricing_(
|
||||
id,
|
||||
productRates,
|
||||
pricingContext
|
||||
)
|
||||
}
|
||||
|
||||
return (!Array.isArray(variantIds)
|
||||
? Object.values(pricingResult)[0]
|
||||
: pricingResult) as unknown as TOutput
|
||||
}
|
||||
|
||||
private async getProductPricing_(
|
||||
productId: string,
|
||||
variants: ProductVariant[],
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
} from "../interfaces"
|
||||
import {
|
||||
Cart,
|
||||
ClaimOrder,
|
||||
Discount,
|
||||
DiscountRuleType,
|
||||
LineItem,
|
||||
@@ -13,10 +14,12 @@ import {
|
||||
Order,
|
||||
ShippingMethod,
|
||||
ShippingMethodTaxLine,
|
||||
Swap,
|
||||
} from "../models"
|
||||
import { isCart } from "../types/cart"
|
||||
import { isOrder } from "../types/orders"
|
||||
import {
|
||||
CalculationContextData,
|
||||
LineAllocationsMap,
|
||||
LineDiscount,
|
||||
LineDiscountAmount,
|
||||
@@ -429,7 +432,12 @@ class TotalsService extends TransactionBaseService {
|
||||
* @return the allocation map for the line items in the cart or order.
|
||||
*/
|
||||
async getAllocationMap(
|
||||
orderOrCart: Cart | Order,
|
||||
orderOrCart: {
|
||||
discounts?: Discount[]
|
||||
items: LineItem[]
|
||||
swaps?: Swap[]
|
||||
claims?: ClaimOrder[]
|
||||
},
|
||||
options: AllocationMapOptions = {}
|
||||
): Promise<LineAllocationsMap> {
|
||||
const allocationMap: LineAllocationsMap = {}
|
||||
@@ -700,19 +708,23 @@ class TotalsService extends TransactionBaseService {
|
||||
* order
|
||||
*/
|
||||
getLineDiscounts(
|
||||
cartOrOrder: Cart | Order,
|
||||
cartOrOrder: {
|
||||
items: LineItem[]
|
||||
swaps?: Swap[]
|
||||
claims?: ClaimOrder[]
|
||||
},
|
||||
discount: Discount
|
||||
): LineDiscountAmount[] {
|
||||
let merged: LineItem[] = [...(cartOrOrder.items ?? [])]
|
||||
|
||||
// merge items from order with items from order swaps
|
||||
if ("swaps" in cartOrOrder && cartOrOrder.swaps.length) {
|
||||
if ("swaps" in cartOrOrder && cartOrOrder.swaps?.length) {
|
||||
for (const s of cartOrOrder.swaps) {
|
||||
merged = [...merged, ...s.additional_items]
|
||||
}
|
||||
}
|
||||
|
||||
if ("claims" in cartOrOrder && cartOrOrder.claims.length) {
|
||||
if ("claims" in cartOrOrder && cartOrOrder.claims?.length) {
|
||||
for (const c of cartOrOrder.claims) {
|
||||
merged = [...merged, ...c.additional_items]
|
||||
}
|
||||
@@ -1051,15 +1063,15 @@ class TotalsService extends TransactionBaseService {
|
||||
|
||||
/**
|
||||
* Prepares the calculation context for a tax total calculation.
|
||||
* @param cartOrOrder - the cart or order to get the calculation context for
|
||||
* @param calculationContextData - the calculationContextData to get the calculation context for
|
||||
* @param options - options to gather context by
|
||||
* @return the tax calculation context
|
||||
*/
|
||||
async getCalculationContext(
|
||||
cartOrOrder: Cart | Order,
|
||||
calculationContextData: CalculationContextData,
|
||||
options: CalculationContextOptions = {}
|
||||
): Promise<TaxCalculationContext> {
|
||||
const allocationMap = await this.getAllocationMap(cartOrOrder, {
|
||||
const allocationMap = await this.getAllocationMap(calculationContextData, {
|
||||
exclude_gift_cards: options.exclude_gift_cards,
|
||||
exclude_discounts: options.exclude_discounts,
|
||||
})
|
||||
@@ -1067,14 +1079,14 @@ class TotalsService extends TransactionBaseService {
|
||||
let shippingMethods: ShippingMethod[] = []
|
||||
// Default to include shipping methods
|
||||
if (!options.exclude_shipping) {
|
||||
shippingMethods = cartOrOrder.shipping_methods || []
|
||||
shippingMethods = calculationContextData.shipping_methods || []
|
||||
}
|
||||
|
||||
return {
|
||||
shipping_address: cartOrOrder.shipping_address,
|
||||
shipping_address: calculationContextData.shipping_address,
|
||||
shipping_methods: shippingMethods,
|
||||
customer: cartOrOrder.customer,
|
||||
region: cartOrOrder.region,
|
||||
customer: calculationContextData.customer,
|
||||
region: calculationContextData.region,
|
||||
is_return: options.is_return ?? false,
|
||||
allocation_map: allocationMap,
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import TaxInclusivePricingFeatureFlag from "../../loaders/feature-flags/tax-inclusive-pricing"
|
||||
import { FlagRouter } from "../../utils/flag-router"
|
||||
import PriceSelectionStrategy from "../price-selection"
|
||||
import { cacheServiceMock } from "../../services/__mocks__/cache"
|
||||
|
||||
const executeTest =
|
||||
(flagValue) =>
|
||||
@@ -226,6 +227,7 @@ const executeTest =
|
||||
manager: mockEntityManager,
|
||||
moneyAmountRepository: mockMoneyAmountRepository,
|
||||
featureFlagRouter,
|
||||
cacheService: cacheServiceMock,
|
||||
})
|
||||
|
||||
try {
|
||||
|
||||
@@ -211,13 +211,19 @@ export default class ProductExportStrategy extends AbstractBatchJobStrategy {
|
||||
|
||||
return await this.atomicPhase_(
|
||||
async (transactionManager) => {
|
||||
let batchJob = (await this.batchJobService_
|
||||
.withTransaction(transactionManager)
|
||||
.retrieve(batchJobId)) as ProductExportBatchJob
|
||||
const productServiceTx =
|
||||
this.productService_.withTransaction(transactionManager)
|
||||
const batchJobServiceTx =
|
||||
this.batchJobService_.withTransaction(transactionManager)
|
||||
const fileServiceTx =
|
||||
this.fileService_.withTransaction(transactionManager)
|
||||
|
||||
const { writeStream, fileKey, promise } = await this.fileService_
|
||||
.withTransaction(transactionManager)
|
||||
.getUploadStreamDescriptor({
|
||||
let batchJob = (await batchJobServiceTx.retrieve(
|
||||
batchJobId
|
||||
)) as ProductExportBatchJob
|
||||
|
||||
const { writeStream, fileKey, promise } =
|
||||
await fileServiceTx.getUploadStreamDescriptor({
|
||||
name: `exports/products/product-export-${Date.now()}`,
|
||||
ext: "csv",
|
||||
})
|
||||
@@ -226,14 +232,12 @@ export default class ProductExportStrategy extends AbstractBatchJobStrategy {
|
||||
writeStream.write(header)
|
||||
approximateFileSize += Buffer.from(header).byteLength
|
||||
|
||||
await this.batchJobService_
|
||||
.withTransaction(transactionManager)
|
||||
.update(batchJobId, {
|
||||
result: {
|
||||
file_key: fileKey,
|
||||
file_size: approximateFileSize,
|
||||
},
|
||||
})
|
||||
await batchJobServiceTx.update(batchJobId, {
|
||||
result: {
|
||||
file_key: fileKey,
|
||||
file_size: approximateFileSize,
|
||||
},
|
||||
})
|
||||
|
||||
advancementCount =
|
||||
batchJob.result?.advancement_count ?? advancementCount
|
||||
@@ -241,26 +245,25 @@ export default class ProductExportStrategy extends AbstractBatchJobStrategy {
|
||||
limit = batchJob.context?.list_config?.take ?? limit
|
||||
|
||||
const { list_config = {}, filterable_fields = {} } = batchJob.context
|
||||
const [productList, count] = await this.productService_
|
||||
.withTransaction(transactionManager)
|
||||
.listAndCount(filterable_fields, {
|
||||
const [productList, count] = await productServiceTx.listAndCount(
|
||||
filterable_fields,
|
||||
{
|
||||
...list_config,
|
||||
skip: offset,
|
||||
take: Math.min(batchJob.context.batch_size ?? Infinity, limit),
|
||||
} as FindProductConfig)
|
||||
} as FindProductConfig
|
||||
)
|
||||
|
||||
productCount = batchJob.context?.batch_size ?? count
|
||||
let products: Product[] = productList
|
||||
|
||||
while (offset < productCount) {
|
||||
if (!products?.length) {
|
||||
products = await this.productService_
|
||||
.withTransaction(transactionManager)
|
||||
.list(filterable_fields, {
|
||||
...list_config,
|
||||
skip: offset,
|
||||
take: Math.min(productCount - offset, limit),
|
||||
} as FindProductConfig)
|
||||
products = await productServiceTx.list(filterable_fields, {
|
||||
...list_config,
|
||||
skip: offset,
|
||||
take: Math.min(productCount - offset, limit),
|
||||
} as FindProductConfig)
|
||||
}
|
||||
|
||||
products.forEach((product: Product) => {
|
||||
@@ -275,16 +278,14 @@ export default class ProductExportStrategy extends AbstractBatchJobStrategy {
|
||||
offset += products.length
|
||||
products = []
|
||||
|
||||
batchJob = (await this.batchJobService_
|
||||
.withTransaction(transactionManager)
|
||||
.update(batchJobId, {
|
||||
result: {
|
||||
file_size: approximateFileSize,
|
||||
count: productCount,
|
||||
advancement_count: advancementCount,
|
||||
progress: advancementCount / productCount,
|
||||
},
|
||||
})) as ProductExportBatchJob
|
||||
batchJob = (await batchJobServiceTx.update(batchJobId, {
|
||||
result: {
|
||||
file_size: approximateFileSize,
|
||||
count: productCount,
|
||||
advancement_count: advancementCount,
|
||||
progress: advancementCount / productCount,
|
||||
},
|
||||
})) as ProductExportBatchJob
|
||||
|
||||
if (batchJob.status === BatchJobStatus.CANCELED) {
|
||||
writeStream.end()
|
||||
|
||||
@@ -1,27 +1,36 @@
|
||||
import { EntityManager } from "typeorm"
|
||||
import {
|
||||
AbstractPriceSelectionStrategy,
|
||||
ICacheService,
|
||||
IPriceSelectionStrategy,
|
||||
PriceSelectionContext,
|
||||
PriceSelectionResult,
|
||||
PriceType,
|
||||
} from "../interfaces/price-selection-strategy"
|
||||
} from "../interfaces"
|
||||
import TaxInclusivePricingFeatureFlag from "../loaders/feature-flags/tax-inclusive-pricing"
|
||||
import { MoneyAmountRepository } from "../repositories/money-amount"
|
||||
import { TaxServiceRate } from "../types/tax-service"
|
||||
import { FlagRouter } from "../utils/flag-router"
|
||||
import { isDefined } from "../utils/is-defined"
|
||||
import { isDefined } from "../utils"
|
||||
|
||||
class PriceSelectionStrategy extends AbstractPriceSelectionStrategy {
|
||||
private moneyAmountRepository_: typeof MoneyAmountRepository
|
||||
private featureFlagRouter_: FlagRouter
|
||||
private manager_: EntityManager
|
||||
protected manager_: EntityManager
|
||||
|
||||
constructor({ manager, featureFlagRouter, moneyAmountRepository }) {
|
||||
protected readonly featureFlagRouter_: FlagRouter
|
||||
protected moneyAmountRepository_: typeof MoneyAmountRepository
|
||||
protected cacheService_: ICacheService
|
||||
|
||||
constructor({
|
||||
manager,
|
||||
featureFlagRouter,
|
||||
moneyAmountRepository,
|
||||
cacheService,
|
||||
}) {
|
||||
super()
|
||||
this.manager_ = manager
|
||||
this.moneyAmountRepository_ = moneyAmountRepository
|
||||
this.featureFlagRouter_ = featureFlagRouter
|
||||
this.cacheService_ = cacheService
|
||||
}
|
||||
|
||||
withTransaction(manager: EntityManager): IPriceSelectionStrategy {
|
||||
@@ -33,6 +42,7 @@ class PriceSelectionStrategy extends AbstractPriceSelectionStrategy {
|
||||
manager: manager,
|
||||
moneyAmountRepository: this.moneyAmountRepository_,
|
||||
featureFlagRouter: this.featureFlagRouter_,
|
||||
cacheService: this.cacheService_,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -40,14 +50,30 @@ class PriceSelectionStrategy extends AbstractPriceSelectionStrategy {
|
||||
variant_id: string,
|
||||
context: PriceSelectionContext
|
||||
): Promise<PriceSelectionResult> {
|
||||
// TODO: Refactor using the cache decorators when it will be finished
|
||||
const cacheKey = this.getCacheKey(variant_id, context)
|
||||
const cached = await this.cacheService_
|
||||
.get<PriceSelectionResult>(cacheKey)
|
||||
.catch(() => void 0)
|
||||
if (cached) {
|
||||
return cached
|
||||
}
|
||||
|
||||
let result
|
||||
|
||||
if (
|
||||
this.featureFlagRouter_.isFeatureEnabled(
|
||||
TaxInclusivePricingFeatureFlag.key
|
||||
)
|
||||
) {
|
||||
return this.calculateVariantPrice_new(variant_id, context)
|
||||
result = await this.calculateVariantPrice_new(variant_id, context)
|
||||
} else {
|
||||
result = await this.calculateVariantPrice_old(variant_id, context)
|
||||
}
|
||||
return this.calculateVariantPrice_old(variant_id, context)
|
||||
|
||||
await this.cacheService_.set(cacheKey, result)
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
private async calculateVariantPrice_new(
|
||||
@@ -213,6 +239,21 @@ class PriceSelectionStrategy extends AbstractPriceSelectionStrategy {
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
private getCacheKey(
|
||||
variantId: string,
|
||||
context: PriceSelectionContext
|
||||
): string {
|
||||
const taxRate =
|
||||
context.tax_rates?.reduce(
|
||||
(accRate: number, nextTaxRate: TaxServiceRate) => {
|
||||
return accRate + (nextTaxRate.rate || 0) / 100
|
||||
},
|
||||
0
|
||||
) || 0
|
||||
|
||||
return `ps:${variantId}:${context.region_id}:${context.currency_code}:${context.customer_id}:${context.quantity}:${context.include_discount_prices}:${taxRate}`
|
||||
}
|
||||
}
|
||||
|
||||
const isValidAmount = (
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
import {
|
||||
CacheService,
|
||||
EventBusService,
|
||||
ProductVariantService,
|
||||
} from "../services"
|
||||
|
||||
type ProductVariantUpdatedEventData = {
|
||||
id: string
|
||||
product_id: string
|
||||
fields: string[]
|
||||
}
|
||||
|
||||
class PricingSubscriber {
|
||||
protected readonly eventBus_: EventBusService
|
||||
protected readonly cacheService_: CacheService
|
||||
|
||||
constructor({ eventBusService, cacheService }) {
|
||||
this.eventBus_ = eventBusService
|
||||
this.cacheService_ = cacheService
|
||||
|
||||
this.eventBus_.subscribe(
|
||||
ProductVariantService.Events.UPDATED,
|
||||
async (data) => {
|
||||
const { id, fields } = data as ProductVariantUpdatedEventData
|
||||
if (fields.includes("prices")) {
|
||||
await this.cacheService_.invalidate(`ps:${id}*`)
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export default PricingSubscriber
|
||||
@@ -1,11 +1,8 @@
|
||||
import { ValidateNested } from "class-validator"
|
||||
import { IsType } from "../utils/validators/is-type"
|
||||
import { Cart, CartType } from "../models/cart"
|
||||
import {
|
||||
AddressPayload,
|
||||
DateComparisonOperator,
|
||||
StringComparisonOperator,
|
||||
} from "./common"
|
||||
import { AddressPayload, DateComparisonOperator, StringComparisonOperator } from "./common"
|
||||
import { Region } from "../models"
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
export function isCart(object: any): object is Cart {
|
||||
@@ -34,6 +31,11 @@ export type LineItemUpdate = {
|
||||
variant_id?: string
|
||||
}
|
||||
|
||||
export type LineItemValidateData = {
|
||||
variant?: { product_id: string };
|
||||
variant_id: string
|
||||
}
|
||||
|
||||
class GiftCard {
|
||||
code: string
|
||||
}
|
||||
@@ -44,6 +46,7 @@ class Discount {
|
||||
|
||||
export type CartCreateProps = {
|
||||
region_id?: string
|
||||
region?: Region
|
||||
email?: string
|
||||
billing_address_id?: string
|
||||
billing_address?: Partial<AddressPayload>
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
import { CalculationContextData } from "./totals"
|
||||
|
||||
export type GenerateInputData = {
|
||||
variantId: string
|
||||
quantity: number
|
||||
}
|
||||
|
||||
export type GenerateLineItemContext = {
|
||||
region_id?: string
|
||||
unit_price?: number
|
||||
includes_tax?: boolean
|
||||
metadata?: Record<string, unknown>
|
||||
customer_id?: string
|
||||
order_edit_id?: string
|
||||
cart?: CalculationContextData
|
||||
}
|
||||
@@ -1,4 +1,24 @@
|
||||
import { LineItem } from "../models"
|
||||
import {
|
||||
Address,
|
||||
ClaimOrder,
|
||||
Customer,
|
||||
Discount,
|
||||
LineItem,
|
||||
Region,
|
||||
ShippingMethod,
|
||||
Swap,
|
||||
} from "../models"
|
||||
|
||||
export type CalculationContextData = {
|
||||
discounts: Discount[]
|
||||
items: LineItem[]
|
||||
customer: Customer
|
||||
region: Region
|
||||
shipping_address: Address | null
|
||||
swaps?: Swap[]
|
||||
claims?: ClaimOrder[]
|
||||
shipping_methods?: ShippingMethod[]
|
||||
}
|
||||
|
||||
/** The amount of a gift card allocated to a line item */
|
||||
export type GiftCardAllocation = {
|
||||
|
||||
Reference in New Issue
Block a user