8a60a73389
* chore: tax_rate is added to giftcards * chore: minor * chore: update gift card tax calculations to look at giftCard.tax_rate * chore: gift card transactions use tax rate from gift card for legacy * fix: gift cart total check for transaction should check the length * chore: use tax exclusive cost + use giftcard tax rate for gctransactions + refactor * chore: fix integration test * chore: address issues brought up in comments * chore: move gift card creation as a part of order service on order placed * chore: add type handling for gift card creation * chore: fix specs * chore: use taxLines to calculate tax of a gift card * chore: specs for line items containing gift cards and without * chore: add integration specs + fix tax rate bug * chore: round totaling + add GC application specs * chore: cleanup trialables * chore: write migration script to backfill gift cards with null tax_rate * chore: update legacy totals service for gift cards * chore: add changeset * chore: address PR review changes * chore: fix tests based on new totals calc * chore: address review comments Co-authored-by: adrien2p <adrien.deperetti@gmail.com> Co-authored-by: Oliver Windall Juhl <59018053+olivermrbl@users.noreply.github.com>
98 lines
2.5 KiB
TypeScript
98 lines
2.5 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
|
|
is_giftcard?: boolean
|
|
unit_price?: number
|
|
quantity?: number
|
|
fulfilled_quantity?: boolean
|
|
shipped_quantity?: boolean
|
|
returned_quantity?: boolean
|
|
tax_lines?: TaxLineFactoryData[]
|
|
adjustments: LineItemAdjustmentFactoryData[]
|
|
includes_tax?: boolean
|
|
order_edit_id?: string
|
|
}
|
|
|
|
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,
|
|
includes_tax: data.includes_tax,
|
|
order_edit_id: data.order_edit_id,
|
|
is_giftcard: data.is_giftcard || false
|
|
})
|
|
|
|
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
|
|
}
|