* initial code push * update metadata and only merge if the existing line item allows merging * update should_merge check * undo changes to taxrate service * update results with unit pricing corresponding to the db values after update * add should_merge property to line_item creation * add should_merge property to line_item creation * fix unit tests * undo adding "should_merge" to create-line-item * undo change to "addOrUpdateLineItem" * :wqh_merge from generate method * undo changes to unit tests * revert to adding pricing in updateLineItem method * update cart service test * Create funny-radios-juggle.md --------- Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
95 lines
2.3 KiB
TypeScript
95 lines
2.3 KiB
TypeScript
import {
|
|
CustomerGroup,
|
|
MoneyAmount,
|
|
PriceList,
|
|
PriceListStatus,
|
|
PriceListType,
|
|
} from "@medusajs/medusa"
|
|
|
|
import { DataSource } from "typeorm"
|
|
import faker from "faker"
|
|
import { simpleCustomerGroupFactory } from "./simple-customer-group-factory"
|
|
import { ProductVariantMoneyAmount } from "@medusajs/medusa"
|
|
|
|
type ProductListPrice = {
|
|
variant_id: string
|
|
currency_code: string
|
|
region_id: string
|
|
amount: number
|
|
min_quantity?: number
|
|
max_quantity?: number
|
|
}
|
|
|
|
export type PriceListFactoryData = {
|
|
id?: string
|
|
name?: string
|
|
description?: string
|
|
type?: PriceListType
|
|
status?: PriceListStatus
|
|
starts_at?: Date
|
|
ends_at?: Date
|
|
customer_groups?: string[]
|
|
prices?: ProductListPrice[]
|
|
includes_tax?: boolean
|
|
}
|
|
|
|
export const simplePriceListFactory = async (
|
|
dataSource: DataSource,
|
|
data: PriceListFactoryData = {},
|
|
seed?: number
|
|
): Promise<PriceList> => {
|
|
if (typeof seed !== "undefined") {
|
|
faker.seed(seed)
|
|
}
|
|
|
|
const manager = dataSource.manager
|
|
|
|
const listId = data.id || `simple-price-list-${Math.random() * 1000}`
|
|
|
|
let customerGroups: CustomerGroup[] = []
|
|
if (typeof data.customer_groups !== "undefined") {
|
|
customerGroups = await Promise.all(
|
|
data.customer_groups.map((group) =>
|
|
simpleCustomerGroupFactory(dataSource, { id: group })
|
|
)
|
|
)
|
|
}
|
|
|
|
const toCreate = {
|
|
id: listId,
|
|
name: data.name || faker.commerce.productName(),
|
|
description: data.description || "Some text",
|
|
status: data.status || PriceListStatus.ACTIVE,
|
|
type: data.type || PriceListType.OVERRIDE,
|
|
starts_at: data.starts_at || null,
|
|
ends_at: data.ends_at || null,
|
|
customer_groups: customerGroups,
|
|
includes_tax: data.includes_tax,
|
|
}
|
|
|
|
const toSave = manager.create(PriceList, toCreate)
|
|
const toReturn = await manager.save(toSave) as PriceList
|
|
|
|
if (typeof data.prices !== "undefined") {
|
|
toReturn.prices = await Promise.all(data.prices.map(async (ma) => {
|
|
const factoryData = {
|
|
...ma,
|
|
price_list_id: listId,
|
|
}
|
|
const toSave: MoneyAmount = manager.create(MoneyAmount, factoryData)
|
|
await manager.save(toSave)
|
|
|
|
await manager.insert(ProductVariantMoneyAmount, {
|
|
id: `${ma.variant_id}-${toSave.id}`,
|
|
variant_id: ma.variant_id,
|
|
money_amount_id: toSave.id,
|
|
})
|
|
|
|
return toSave
|
|
}))
|
|
}
|
|
|
|
|
|
return toReturn
|
|
}
|