Feat: Price selection implementation (#1158)

* init

* added buld id validation to repo

* admin done

* updated price reqs

* initial price selection strategy

* update customer seeder

* format models

* price selection strategy

* price selection testing

* update price selection tests

* update price selection strategy

* remove console.warn

* update price selection strat

* remove console.log

* fix unit tests

* update product snapshot integration tests

* fix failing unit tests

* update variant test snapshots

* intial implementation of PriceList

* integration tests for price lists

* updated admin/product integration tests

* update updateVariantPrices method

* remove comment from error handler

* add integration test for batch deleting prices associated with price list

* make update to prices through variant service limited to default prices

* update store/products.js snapshot

* add api unit tests and update product integration tests to validate that prices from Price List are ignored

* fix product test

* requested changes

* cascade

* ensure delete variant cascades to MoneyAmount

* addresses PR feedback

* removed unused endpoint

* update mock

* fix failing store integration tests

* remove medusajs ressource

* re add env.template

* price selection strategy methods

* fix integration tests

* update unit tests

* update jsdoc

* update price selection strategy parameter

* fix unit tests

* pr feedback

Co-authored-by: Kasper <kasper@medusajs.com>
Co-authored-by: Kasper Fabricius Kristensen <45367945+kasperkristensen@users.noreply.github.com>
This commit is contained in:
Philip Korsholm
2022-03-21 19:03:42 +01:00
committed by GitHub
co-authored by Kasper Kasper Fabricius Kristensen
parent 5300926db8
commit dfa3502e41
10 changed files with 701 additions and 58 deletions
@@ -0,0 +1,74 @@
import { EntityManager } from "typeorm"
import { MoneyAmount } from ".."
import { MoneyAmountRepository } from "../repositories/money-amount"
import { PriceListType } from "../types/price-list"
export interface IPriceSelectionStrategy {
/**
* Instantiate a new price selection strategy with the active transaction in
* order to ensure reads are accurate.
* @param manager EntityManager with the queryrunner of the active transaction
* @returns a new price selection strategy
*/
withTransaction(manager: EntityManager): IPriceSelectionStrategy
/**
* Calculate the original and discount price for a given variant in a set of
* circumstances described in the context.
* @param variant The variant id of the variant for which to retrieve prices
* @param context Details relevant to determine the correct pricing of the variant
* @return pricing details in an object containing the calculated lowest price,
* the default price an all valid prices for the given variant
*/
calculateVariantPrice(
variant_id: string,
context: PriceSelectionContext
): Promise<PriceSelectionResult>
}
export abstract class AbstractPriceSelectionStrategy
implements IPriceSelectionStrategy
{
public abstract withTransaction(
manager: EntityManager
): IPriceSelectionStrategy
public abstract calculateVariantPrice(
variant_id: string,
context: PriceSelectionContext
): Promise<PriceSelectionResult>
}
export function isPriceSelectionStrategy(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
object: any
): object is IPriceSelectionStrategy {
return (
typeof object.calculateVariantPrice === "function" &&
typeof object.withTransaction === "function"
)
}
export type PriceSelectionContext = {
cart_id?: string
customer_id?: string
quantity?: number
region_id?: string
currency_code?: string
include_discount_prices?: boolean
}
enum DefaultPriceType {
DEFAULT = "default",
}
// both exports are needed in order to get proper typing of the calculatedPriceType field.
export type PriceType = DefaultPriceType | PriceListType
export const PriceType = { ...DefaultPriceType, ...PriceListType }
export type PriceSelectionResult = {
originalPrice: number | null
calculatedPrice: number | null
calculatedPriceType?: PriceType
prices: MoneyAmount[] // prices is an array of all possible price for the input customer and region prices
}