Files
medusa-store/integration-tests/api/factories/simple-discount-factory.ts
Oliver Windall Juhl a610805917 feat: Add DiscountConditions (#1230)
* feat: Add DiscountCondition entity + Join table per relation (#1146)

* feat: Convert DiscountService to TypeScript (#1149)

* feat: Add DiscountRepository + bulk insert and remove (#1156)

* feat: Add `conditions` to payload in `POST /discounts` and `POST /discounts/:id` (#1170)

* feat: Add DiscountRuleCondition entity

* fix relation

* fix join key

* Add discount rule condition repo

* add join table per relation

* Convert DiscountService to TypeScript

* feat: Add DiscountConditionRepository

* Add migration + remove use of valid_for

* revert changes to files, not done yet

* init work on create discount endpoint

* Add conditions to create discount endpoint

* Add conditions to update discount endpoint

* Add unique constraint to discount condition

* integration tests passing

* fix imports of models

* fix tests (excluding totals calculations)

* Fix commented code

* add unique constraint on discount condition

* Add generic way of generating retrieve configs

* Requested changes + ExactlyOne validator

* Remove isLocal flag from error handler

* Use postgres error constant

* remove commented code

* feat: Add `isValidForProduct` to check if Discount is valid for a given Product (#1172)

* feat: Add `canApplyForCustomer` to check if Discount is valid for customer groups (#1212)

* feat: Add `calculateDiscountForLineItem` (#1224)

* feat: Adds discount condition test factory (#1228)

* Remove use of valid_for

* Tests passing

* Remove valid_for form relations

* Add integration tests for applying discounts to cart
2022-03-24 16:47:50 +01:00

71 lines
1.6 KiB
TypeScript

import {
AllocationType,
Discount,
DiscountRule,
DiscountRuleType,
} from "@medusajs/medusa"
import faker from "faker"
import { Connection } from "typeorm"
import {
DiscuntConditionFactoryData,
simpleDiscountConditionFactory,
} from "./simple-discount-condition-factory"
export type DiscountRuleFactoryData = {
type?: DiscountRuleType
value?: number
allocation?: AllocationType
conditions: DiscuntConditionFactoryData[]
}
export type DiscountFactoryData = {
id?: string
code?: string
is_dynamic?: boolean
rule?: DiscountRuleFactoryData
regions?: string[]
}
export const simpleDiscountFactory = async (
connection: Connection,
data: DiscountFactoryData = {},
seed?: number
): Promise<Discount> => {
if (typeof seed !== "undefined") {
faker.seed(seed)
}
const manager = connection.manager
const ruleData = data.rule ?? {}
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(
connection,
{ ...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 })) || [],
})
const discount = await manager.save(toSave)
return discount
}