1cfeb5dbd8
* add: crud services + model + totals * fix: enforce unique constraint on line item adjustment model and update service (#1241) * add: unique constraint on model + fix service * fix: unique constraint * fix: add cascade on delete + fix discount relation * fix: remove optional unique prop * add: tests for ensuring line item adjustment db constraints (#1279) * add: tests for ensuring db constraints * fix: use given when then * feat: adjust cart to include line item adjustments (#1242) * fix: cart service + cart tests * fix: remaining tests * fix: swap tests * fix: add relationship + fix oas * refactor: applyDiscount * fix: refactor applyDiscount and fix + add unit tests * fix: plugins tests * feat: line item adjustments draft orders (#1243) * fix: draft order tests * fix: constraint * fix: wrong variable name * fix: unique constraint * progress: add tests * fix: add cascade on delete + fix discount relation * fix: remove optional unique prop * fix: cart removeLineItem + tests * fix: cart unit tests * fix: update snapshot * remove: verbose option * rename arg Co-authored-by: Sebastian Rindom <skrindom@gmail.com> * add: create adjustments for swap additional_items * add: create adjustments for return lines * fix: unit test for creating adjustment for additional_items * fix: create adjustments only for non return items + no deletion when item is a return item * add: integration tests * refactor: use refreshAdjustments method * refactor test Co-authored-by: Sebastian Rindom <skrindom@gmail.com> Co-authored-by: Sebastian Rindom <skrindom@gmail.com> Co-authored-by: Sebastian Rindom <skrindom@gmail.com>
92 lines
2.3 KiB
TypeScript
92 lines
2.3 KiB
TypeScript
import { Connection } from "typeorm"
|
|
import faker from "faker"
|
|
import { LineItem, LineItemAdjustment, LineItemTaxLine } from "@medusajs/medusa"
|
|
|
|
type TaxLineFactoryData = {
|
|
rate: number
|
|
code: string
|
|
name: string
|
|
}
|
|
|
|
type LineItemAdjustmentFactoryData = Omit<LineItemAdjustment, "discount_id"> & {
|
|
discount_id: string
|
|
discount_code: string
|
|
}
|
|
|
|
export type LineItemFactoryData = {
|
|
id?: string
|
|
cart_id?: string
|
|
order_id?: string
|
|
variant_id: string | null
|
|
title?: string
|
|
description?: string
|
|
thumbnail?: string
|
|
should_merge?: boolean
|
|
allow_discounts?: boolean
|
|
unit_price?: number
|
|
quantity?: number
|
|
fulfilled_quantity?: boolean
|
|
shipped_quantity?: boolean
|
|
returned_quantity?: boolean
|
|
tax_lines?: TaxLineFactoryData[]
|
|
adjustments: LineItemAdjustmentFactoryData[]
|
|
}
|
|
|
|
export const simpleLineItemFactory = async (
|
|
connection: Connection,
|
|
data: LineItemFactoryData,
|
|
seed?: number
|
|
): Promise<LineItem> => {
|
|
if (
|
|
typeof data.cart_id === "undefined" &&
|
|
typeof data.order_id === "undefined"
|
|
) {
|
|
throw Error()
|
|
}
|
|
|
|
if (typeof seed !== "undefined") {
|
|
faker.seed(seed)
|
|
Math
|
|
}
|
|
|
|
const manager = connection.manager
|
|
|
|
const id = data.id || `simple-line-${Math.random() * 1000}`
|
|
const toSave = manager.create(LineItem, {
|
|
id,
|
|
cart_id: data.cart_id,
|
|
order_id: data.order_id,
|
|
title: data.title || faker.commerce.productName(),
|
|
description: data.description || "",
|
|
thumbnail: data.thumbnail || "",
|
|
should_merge:
|
|
typeof data.should_merge !== "undefined" ? data.should_merge : true,
|
|
allow_discounts:
|
|
typeof data.allow_discounts !== "undefined" ? data.allow_discounts : true,
|
|
unit_price: typeof data.unit_price !== "undefined" ? data.unit_price : 100,
|
|
variant_id: data.variant_id,
|
|
quantity: data.quantity || 1,
|
|
fulfilled_quantity: data.fulfilled_quantity || null,
|
|
shipped_quantity: data.shipped_quantity || null,
|
|
returned_quantity: data.returned_quantity || null,
|
|
adjustments: data.adjustments,
|
|
})
|
|
|
|
const line = await manager.save(toSave)
|
|
|
|
if (typeof data.tax_lines !== "undefined") {
|
|
const taxLinesToSave = data.tax_lines.map((tl) =>
|
|
manager.create(LineItemTaxLine, {
|
|
item_id: id,
|
|
rate: tl.rate,
|
|
code: tl.code,
|
|
name: tl.name,
|
|
})
|
|
)
|
|
|
|
await manager.save(taxLinesToSave)
|
|
}
|
|
|
|
return line
|
|
}
|