* chore: Move factories and helpers to a better place * align factory product variant * fix factory cart * add simple store fac * fix tests * fix tests * fix * fix cart seeder
74 lines
1.7 KiB
TypeScript
74 lines
1.7 KiB
TypeScript
import {
|
|
AllocationType,
|
|
Discount,
|
|
DiscountRule,
|
|
DiscountRuleType,
|
|
} from "@medusajs/medusa"
|
|
import faker from "faker"
|
|
import { DataSource } from "typeorm"
|
|
import {
|
|
DiscountConditionFactoryData,
|
|
simpleDiscountConditionFactory,
|
|
} from "./simple-discount-condition-factory"
|
|
|
|
export type DiscountRuleFactoryData = {
|
|
type?: DiscountRuleType
|
|
value?: number
|
|
allocation?: AllocationType
|
|
conditions: DiscountConditionFactoryData[]
|
|
}
|
|
|
|
export type DiscountFactoryData = {
|
|
id?: string
|
|
code?: string
|
|
is_dynamic?: boolean
|
|
rule?: DiscountRuleFactoryData
|
|
regions?: string[]
|
|
starts_at?: Date
|
|
ends_at?: Date
|
|
}
|
|
|
|
export const simpleDiscountFactory = async (
|
|
dataSource: DataSource,
|
|
data: DiscountFactoryData = {},
|
|
seed?: number
|
|
): Promise<Discount> => {
|
|
if (typeof seed !== "undefined") {
|
|
faker.seed(seed)
|
|
}
|
|
|
|
const manager = dataSource.manager
|
|
|
|
const ruleData = data.rule ?? ({} as DiscountRuleFactoryData)
|
|
const ruleToSave = manager.create(DiscountRule, {
|
|
type: ruleData.type ?? DiscountRuleType.PERCENTAGE,
|
|
value: ruleData.value ?? 10,
|
|
allocation: ruleData.allocation ?? AllocationType.TOTAL,
|
|
})
|
|
|
|
const dRule = await manager.save(ruleToSave)
|
|
|
|
if (data?.rule?.conditions) {
|
|
for (const condition of data.rule.conditions) {
|
|
await simpleDiscountConditionFactory(
|
|
dataSource,
|
|
{ ...condition, rule_id: dRule.id },
|
|
1
|
|
)
|
|
}
|
|
}
|
|
|
|
const toSave = manager.create(Discount, {
|
|
id: data.id,
|
|
is_dynamic: data.is_dynamic ?? false,
|
|
is_disabled: false,
|
|
rule_id: dRule.id,
|
|
code: data.code ?? "TESTCODE",
|
|
regions: data.regions?.map((r) => ({ id: r })) || [],
|
|
starts_at: data.starts_at,
|
|
ends_at: data.ends_at,
|
|
})
|
|
|
|
return await manager.save(toSave)
|
|
}
|