chore: Move factories and helpers to a better place (#4551)
* 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
This commit is contained in:
1 parent
26e2f81c24
commit
4d326fbbdf
197 files changed
+1073
-4332
No files matched your search
@@ -0,0 +1,73 @@
|
||||
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)
|
||||
}
|
||||
Reference in new issue
Block a user