Files
medusa-store/integration-tests/factories/simple-product-tax-rate-factory.ts.txt
Riqwan Thamir 0573bb924a chore: Remove typeORM (#9005)
* chore: rename js files to txt

* chore: rename ts files to txt

* chore: delete environment helpers

* chore: convert global setup & teardown to txt

* chore: rename helper js/ts files to txt

* chore: rename seeder js/ts files to txt

* chore: remove typeorm

* chore: reintroduce used helpers
2024-09-05 15:45:30 +02:00

49 lines
1.1 KiB
Plaintext

import { DataSource } from "typeorm"
import faker from "faker"
import { ProductTaxRate, TaxRate } from "@medusajs/medusa"
type RateFactoryData = {
region_id: string
rate?: number | null
code?: string
name?: string
}
export type ProductTaxRateFactoryData = {
product_id: string
rate: RateFactoryData | string
}
export const simpleProductTaxRateFactory = async (
dataSource: DataSource,
data: ProductTaxRateFactoryData,
seed?: number
): Promise<ProductTaxRate> => {
if (typeof seed !== "undefined") {
faker.seed(seed)
}
const manager = dataSource.manager
let rateId: string
if (typeof data.rate === "string") {
rateId = data.rate
} else {
const newRate = manager.create(TaxRate, {
region_id: data.rate.region_id,
rate: data.rate.rate,
code: data.rate.code || "sales_tax",
name: data.rate.name || "Sales Tax",
})
const rate = await manager.save(newRate)
rateId = rate.id
}
const toSave = manager.create(ProductTaxRate, {
product_id: data.product_id,
rate_id: rateId,
})
return await manager.save(toSave)
}