* 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>
121 lines
3.2 KiB
TypeScript
121 lines
3.2 KiB
TypeScript
import { Connection } from "typeorm"
|
|
import faker from "faker"
|
|
import {
|
|
Customer,
|
|
Order,
|
|
PaymentStatus,
|
|
FulfillmentStatus,
|
|
} from "@medusajs/medusa"
|
|
|
|
import {
|
|
DiscountFactoryData,
|
|
simpleDiscountFactory,
|
|
} from "./simple-discount-factory"
|
|
import { RegionFactoryData, simpleRegionFactory } from "./simple-region-factory"
|
|
import {
|
|
LineItemFactoryData,
|
|
simpleLineItemFactory,
|
|
} from "./simple-line-item-factory"
|
|
import {
|
|
AddressFactoryData,
|
|
simpleAddressFactory,
|
|
} from "./simple-address-factory"
|
|
import {
|
|
ShippingMethodFactoryData,
|
|
simpleShippingMethodFactory,
|
|
} from "./simple-shipping-method-factory"
|
|
|
|
export type OrderFactoryData = {
|
|
id?: string
|
|
payment_status?: PaymentStatus
|
|
fulfillment_status?: FulfillmentStatus
|
|
region?: RegionFactoryData | string
|
|
email?: string | null
|
|
currency_code?: string
|
|
tax_rate?: number | null
|
|
line_items?: LineItemFactoryData[]
|
|
discounts?: DiscountFactoryData[]
|
|
shipping_address?: AddressFactoryData
|
|
shipping_methods?: ShippingMethodFactoryData[]
|
|
}
|
|
|
|
export const simpleOrderFactory = async (
|
|
connection: Connection,
|
|
data: OrderFactoryData = {},
|
|
seed: number
|
|
): Promise<Order> => {
|
|
if (typeof seed !== "undefined") {
|
|
faker.seed(seed)
|
|
}
|
|
|
|
const manager = connection.manager
|
|
|
|
let currencyCode: string
|
|
let regionId: string
|
|
let taxRate: number
|
|
if (typeof data.region === "string") {
|
|
currencyCode = data.currency_code
|
|
regionId = data.region
|
|
taxRate = data.tax_rate
|
|
} else {
|
|
const region = await simpleRegionFactory(connection, data.region)
|
|
taxRate =
|
|
typeof data.tax_rate !== "undefined" ? data.tax_rate : region.tax_rate
|
|
currencyCode = region.currency_code
|
|
regionId = region.id
|
|
}
|
|
const address = await simpleAddressFactory(connection, data.shipping_address)
|
|
|
|
const customerToSave = manager.create(Customer, {
|
|
email:
|
|
typeof data.email !== "undefined" ? data.email : faker.internet.email(),
|
|
})
|
|
const customer = await manager.save(customerToSave)
|
|
|
|
let discounts = []
|
|
if (typeof data.discounts !== "undefined") {
|
|
discounts = await Promise.all(
|
|
data.discounts.map((d) => simpleDiscountFactory(connection, d, seed))
|
|
)
|
|
}
|
|
|
|
const id = data.id || `simple-order-${Math.random() * 1000}`
|
|
const toSave = manager.create(Order, {
|
|
id,
|
|
discounts,
|
|
payment_status: data.payment_status ?? PaymentStatus.AWAITING,
|
|
fulfillment_status:
|
|
data.fulfillment_status ?? FulfillmentStatus.NOT_FULFILLED,
|
|
customer_id: customer.id,
|
|
email: customer.email,
|
|
region_id: regionId,
|
|
currency_code: currencyCode,
|
|
tax_rate: taxRate,
|
|
shipping_address_id: address.id,
|
|
})
|
|
|
|
const order = await manager.save(toSave)
|
|
|
|
const shippingMethods = data.shipping_methods || []
|
|
for (const sm of shippingMethods) {
|
|
await simpleShippingMethodFactory(connection, { ...sm, order_id: order.id })
|
|
}
|
|
|
|
const items = data.line_items.map((item) => {
|
|
let adjustments = item?.adjustments || []
|
|
return {
|
|
...item,
|
|
adjustments: adjustments.map((adj) => ({
|
|
...adj,
|
|
discount_id: discounts.find((d) => d.code === adj?.discount_code),
|
|
})),
|
|
}
|
|
})
|
|
|
|
for (const item of items) {
|
|
await simpleLineItemFactory(connection, { ...item, order_id: id })
|
|
}
|
|
|
|
return order
|
|
}
|