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,26 @@
|
||||
export * from "./simple-batch-job-factory"
|
||||
export * from "./simple-cart-factory"
|
||||
export * from "./simple-custom-shipping-option-factory"
|
||||
export * from "./simple-customer-factory"
|
||||
export * from "./simple-discount-factory"
|
||||
export * from "./simple-gift-card-factory"
|
||||
export * from "./simple-line-item-factory"
|
||||
export * from "./simple-order-edit-factory"
|
||||
export * from "./simple-order-factory"
|
||||
export * from "./simple-order-item-change-factory"
|
||||
export * from "./simple-payment-collection-factory"
|
||||
export * from "./simple-payment-factory"
|
||||
export * from "./simple-price-list-factory"
|
||||
export * from "./simple-product-category-factory"
|
||||
export * from "./simple-product-factory"
|
||||
export * from "./simple-product-tax-rate-factory"
|
||||
export * from "./simple-product-type-tax-rate-factory"
|
||||
export * from "./simple-product-variant-factory"
|
||||
export * from "./simple-region-factory"
|
||||
export * from "./simple-sales-channel-factory"
|
||||
export * from "./simple-shipping-method-factory"
|
||||
export * from "./simple-shipping-option-factory"
|
||||
export * from "./simple-shipping-profile-factory"
|
||||
export * from "./simple-shipping-tax-rate-factory"
|
||||
export * from "./simple-tax-rate-factory"
|
||||
export * from "./simple-store-factory"
|
||||
@@ -0,0 +1,34 @@
|
||||
import { DataSource } from "typeorm"
|
||||
import faker from "faker"
|
||||
import { Address } from "@medusajs/medusa"
|
||||
|
||||
export type AddressFactoryData = {
|
||||
first_name?: string
|
||||
last_name?: string
|
||||
country_code?: string
|
||||
address_1?: string
|
||||
postal_code?: string
|
||||
}
|
||||
|
||||
export const simpleAddressFactory = async (
|
||||
dataSource: DataSource,
|
||||
data: AddressFactoryData = {},
|
||||
seed?: number
|
||||
): Promise<Address> => {
|
||||
if (typeof seed !== "undefined") {
|
||||
faker.seed(seed)
|
||||
}
|
||||
|
||||
const manager = dataSource.manager
|
||||
|
||||
const address = manager.create(Address, {
|
||||
id: `simple-id-${Math.random() * 1000}`,
|
||||
first_name: data.first_name || faker.name.firstName(),
|
||||
last_name: data.last_name || faker.name.lastName(),
|
||||
country_code: data.country_code || "us",
|
||||
address_1: data.address_1 || faker.address.streetAddress(),
|
||||
postal_code: data.postal_code || faker.address.zipCode(),
|
||||
})
|
||||
|
||||
return await manager.save(address)
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import { AnalyticsConfig } from "@medusajs/medusa"
|
||||
import { DataSource } from "typeorm"
|
||||
|
||||
export type AnalyticsConfigData = {
|
||||
id?: string
|
||||
user_id?: string
|
||||
opt_out?: boolean
|
||||
anonymize?: boolean
|
||||
}
|
||||
|
||||
export const simpleAnalyticsConfigFactory = async (
|
||||
dataSource: DataSource,
|
||||
data: AnalyticsConfigData = {}
|
||||
): Promise<AnalyticsConfig> => {
|
||||
const manager = dataSource.manager
|
||||
|
||||
const job = manager.create<AnalyticsConfig>(AnalyticsConfig, {
|
||||
id: data.id ?? "test-analytics-config",
|
||||
user_id: data.user_id ?? "admin_user",
|
||||
opt_out: data.opt_out ?? false,
|
||||
anonymize: data.anonymize ?? false,
|
||||
})
|
||||
|
||||
return await manager.save<AnalyticsConfig>(job)
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
import { DataSource } from "typeorm"
|
||||
import { BatchJob, BatchJobStatus } from "@medusajs/medusa"
|
||||
|
||||
export type BatchJobFactoryData = {
|
||||
id?: string
|
||||
type?: string
|
||||
status?: BatchJobStatus
|
||||
created_by?: string
|
||||
context?: Record<string, unknown>
|
||||
awaiting_confirmation_at?: Date | string
|
||||
completed_at?: Date | string
|
||||
}
|
||||
|
||||
export const simpleBatchJobFactory = async (
|
||||
dataSource: DataSource,
|
||||
data: BatchJobFactoryData = {}
|
||||
): Promise<BatchJob> => {
|
||||
const manager = dataSource.manager
|
||||
|
||||
const job = manager.create<BatchJob>(BatchJob, {
|
||||
id: data.id,
|
||||
status: data.status ?? BatchJobStatus.CREATED,
|
||||
completed_at: data.completed_at ?? null,
|
||||
type: data.type ?? "test-job",
|
||||
created_by: data.created_by ?? null,
|
||||
context: data.context ?? {},
|
||||
})
|
||||
|
||||
return await manager.save<BatchJob>(job)
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
import { Cart } from "@medusajs/medusa"
|
||||
import faker from "faker"
|
||||
import { DataSource } from "typeorm"
|
||||
import {
|
||||
AddressFactoryData,
|
||||
simpleAddressFactory,
|
||||
} from "./simple-address-factory"
|
||||
import { simpleCustomerFactory } from "./simple-customer-factory"
|
||||
import {
|
||||
LineItemFactoryData,
|
||||
simpleLineItemFactory,
|
||||
} from "./simple-line-item-factory"
|
||||
import { RegionFactoryData, simpleRegionFactory } from "./simple-region-factory"
|
||||
import {
|
||||
SalesChannelFactoryData,
|
||||
simpleSalesChannelFactory,
|
||||
} from "./simple-sales-channel-factory"
|
||||
import {
|
||||
ShippingMethodFactoryData,
|
||||
simpleShippingMethodFactory,
|
||||
} from "./simple-shipping-method-factory"
|
||||
|
||||
export type CartFactoryData = {
|
||||
id?: string
|
||||
customer?: string | { email: string }
|
||||
region?: RegionFactoryData | string
|
||||
email?: string | null
|
||||
line_items?: LineItemFactoryData[]
|
||||
shipping_address?: AddressFactoryData
|
||||
shipping_methods?: ShippingMethodFactoryData[]
|
||||
sales_channel?: SalesChannelFactoryData
|
||||
sales_channel_id?: string
|
||||
}
|
||||
|
||||
export const simpleCartFactory = async (
|
||||
dataSource: DataSource,
|
||||
data: CartFactoryData = {},
|
||||
seed?: number
|
||||
): Promise<Cart> => {
|
||||
if (typeof seed !== "undefined") {
|
||||
faker.seed(seed)
|
||||
}
|
||||
|
||||
const manager = dataSource.manager
|
||||
|
||||
let regionId: string
|
||||
if (typeof data.region === "string") {
|
||||
regionId = data.region
|
||||
} else {
|
||||
const region = await simpleRegionFactory(dataSource, data.region)
|
||||
regionId = region.id
|
||||
}
|
||||
|
||||
let customerId!: string
|
||||
if (typeof data.customer === "string") {
|
||||
customerId = data.customer
|
||||
} else {
|
||||
if (data?.customer?.email) {
|
||||
const customer = await simpleCustomerFactory(dataSource, data.customer)
|
||||
customerId = customer.id
|
||||
} else if (data.email) {
|
||||
const customer = await simpleCustomerFactory(dataSource, {
|
||||
email: data.email,
|
||||
})
|
||||
customerId = customer.id
|
||||
}
|
||||
}
|
||||
|
||||
const address = await simpleAddressFactory(dataSource, data.shipping_address)
|
||||
|
||||
let sales_channel
|
||||
if (typeof data.sales_channel !== "undefined") {
|
||||
sales_channel = await simpleSalesChannelFactory(
|
||||
dataSource,
|
||||
data.sales_channel
|
||||
)
|
||||
}
|
||||
|
||||
const id = data.id || `simple-cart-${Math.random() * 1000}`
|
||||
const toSave = manager.create(Cart, {
|
||||
id,
|
||||
email:
|
||||
typeof data.email !== "undefined" ? data.email : faker.internet.email(),
|
||||
region_id: regionId,
|
||||
customer_id: customerId,
|
||||
shipping_address_id: address.id,
|
||||
sales_channel_id: sales_channel?.id ?? data.sales_channel_id ?? null,
|
||||
})
|
||||
|
||||
const cart = await manager.save(toSave)
|
||||
|
||||
const shippingMethods = data.shipping_methods || []
|
||||
for (const sm of shippingMethods) {
|
||||
await simpleShippingMethodFactory(dataSource, { ...sm, cart_id: id })
|
||||
}
|
||||
|
||||
const items = data.line_items || []
|
||||
for (const item of items) {
|
||||
await simpleLineItemFactory(dataSource, { ...item, cart_id: id })
|
||||
}
|
||||
|
||||
return cart
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
import { DataSource } from "typeorm"
|
||||
import faker from "faker"
|
||||
import { CustomShippingOption } from "@medusajs/medusa"
|
||||
|
||||
export type CustomShippingOptionFactoryData = {
|
||||
id?: string
|
||||
cart_id: string
|
||||
shipping_option_id: string
|
||||
price?: number
|
||||
metadata?: Record<string, unknown>
|
||||
}
|
||||
|
||||
export const simpleCustomShippingOptionFactory = async (
|
||||
dataSource: DataSource,
|
||||
data: CustomShippingOptionFactoryData,
|
||||
seed?: number
|
||||
): Promise<CustomShippingOption> => {
|
||||
if (typeof seed !== "undefined") {
|
||||
faker.seed(seed)
|
||||
}
|
||||
|
||||
const manager = dataSource.manager
|
||||
|
||||
const customShippingOptionData = {
|
||||
id: data.id ?? `custon-simple-so-${Math.random() * 1000}`,
|
||||
price: typeof data.price !== "undefined" ? data.price : 500,
|
||||
cart_id: data.cart_id,
|
||||
shipping_option_id: data.shipping_option_id,
|
||||
metadata: data.metadata ?? {},
|
||||
}
|
||||
|
||||
const created = manager.create(CustomShippingOption, customShippingOptionData)
|
||||
return await manager.save(created)
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
import faker from "faker"
|
||||
import { DataSource } from "typeorm"
|
||||
import { Customer, CustomerGroup } from "@medusajs/medusa"
|
||||
import { CustomerGroupFactoryData, simpleCustomerGroupFactory, } from "./simple-customer-group-factory"
|
||||
|
||||
export type CustomerFactoryData = {
|
||||
id?: string
|
||||
email?: string
|
||||
phone?: string
|
||||
first_name?: string
|
||||
last_name?: string
|
||||
groups?: CustomerGroupFactoryData[]
|
||||
password_hash?: string
|
||||
has_account?: boolean
|
||||
}
|
||||
|
||||
export const simpleCustomerFactory = async (
|
||||
dataSource: DataSource,
|
||||
data: CustomerFactoryData = {},
|
||||
seed?: number
|
||||
): Promise<Customer> => {
|
||||
if (typeof seed !== "undefined") {
|
||||
faker.seed(seed)
|
||||
}
|
||||
|
||||
const manager = dataSource.manager
|
||||
|
||||
const customerId = data.id || `simple-customer-${Math.random() * 1000}`
|
||||
const c = manager.create(Customer, {
|
||||
id: customerId,
|
||||
email: data.email ?? faker.internet.email(),
|
||||
phone: data.phone ?? faker.phone.phoneNumber(),
|
||||
first_name: data.first_name ?? faker.name.firstName(),
|
||||
last_name: data.last_name ?? faker.name.lastName(),
|
||||
password_hash:
|
||||
data.password_hash ??
|
||||
"c2NyeXB0AAEAAAABAAAAAVMdaddoGjwU1TafDLLlBKnOTQga7P2dbrfgf3fB+rCD/cJOMuGzAvRdKutbYkVpuJWTU39P7OpuWNkUVoEETOVLMJafbI8qs8Qx/7jMQXkN", // password matching "test"
|
||||
has_account: data.has_account ?? true,
|
||||
})
|
||||
|
||||
if (data.password_hash) {
|
||||
c.password_hash = data.password_hash
|
||||
c.has_account = true
|
||||
}
|
||||
|
||||
const customer = await manager.save(c)
|
||||
|
||||
if (data.groups) {
|
||||
const groups: CustomerGroup[] = []
|
||||
for (const g of data.groups) {
|
||||
const created = await simpleCustomerGroupFactory(dataSource, g)
|
||||
groups.push(created)
|
||||
}
|
||||
|
||||
customer.groups = groups
|
||||
await manager.save(customer)
|
||||
}
|
||||
|
||||
return customer
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
import { CustomerGroup } from "@medusajs/medusa"
|
||||
import faker from "faker"
|
||||
import { DataSource } from "typeorm"
|
||||
|
||||
export type CustomerGroupFactoryData = {
|
||||
id?: string
|
||||
name?: string
|
||||
}
|
||||
|
||||
export const simpleCustomerGroupFactory = async (
|
||||
dataSource: DataSource,
|
||||
data: CustomerGroupFactoryData = {},
|
||||
seed?: number
|
||||
): Promise<CustomerGroup> => {
|
||||
if (typeof seed !== "undefined") {
|
||||
faker.seed(seed)
|
||||
}
|
||||
|
||||
const manager = dataSource.manager
|
||||
|
||||
const customerGroupId =
|
||||
data.id || `simple-customer-group-${Math.random() * 1000}`
|
||||
const c = manager.create(CustomerGroup, {
|
||||
id: customerGroupId,
|
||||
name: data.name || faker.company.companyName(),
|
||||
})
|
||||
|
||||
const group = await manager.save(c)
|
||||
|
||||
return group
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
import {
|
||||
DiscountCondition,
|
||||
DiscountConditionOperator,
|
||||
DiscountConditionType,
|
||||
} from "@medusajs/medusa/dist/models/discount-condition"
|
||||
import { DiscountConditionCustomerGroup } from "@medusajs/medusa/dist/models/discount-condition-customer-group"
|
||||
import { DiscountConditionProduct } from "@medusajs/medusa/dist/models/discount-condition-product"
|
||||
import { DiscountConditionProductCollection } from "@medusajs/medusa/dist/models/discount-condition-product-collection"
|
||||
import { DiscountConditionProductTag } from "@medusajs/medusa/dist/models/discount-condition-product-tag"
|
||||
import { DiscountConditionProductType } from "@medusajs/medusa/dist/models/discount-condition-product-type"
|
||||
import { DiscountConditionJoinTableForeignKey } from "@medusajs/medusa/dist/repositories/discount-condition"
|
||||
import faker from "faker"
|
||||
import { DataSource } from "typeorm"
|
||||
|
||||
export type DiscountConditionFactoryData = {
|
||||
id?: string
|
||||
rule_id: string
|
||||
type: DiscountConditionType
|
||||
operator: DiscountConditionOperator
|
||||
products: string[]
|
||||
product_collections: string[]
|
||||
product_types: string[]
|
||||
product_tags: string[]
|
||||
customer_groups: string[]
|
||||
}
|
||||
|
||||
const getJoinTableResourceIdentifiers = (type: string) => {
|
||||
let conditionTable: any
|
||||
let resourceKey
|
||||
|
||||
switch (type) {
|
||||
case DiscountConditionType.PRODUCTS: {
|
||||
resourceKey = DiscountConditionJoinTableForeignKey.PRODUCT_ID
|
||||
conditionTable = DiscountConditionProduct
|
||||
break
|
||||
}
|
||||
case DiscountConditionType.PRODUCT_TYPES: {
|
||||
resourceKey = DiscountConditionJoinTableForeignKey.PRODUCT_TYPE_ID
|
||||
conditionTable = DiscountConditionProductType
|
||||
break
|
||||
}
|
||||
case DiscountConditionType.PRODUCT_COLLECTIONS: {
|
||||
resourceKey = DiscountConditionJoinTableForeignKey.PRODUCT_COLLECTION_ID
|
||||
conditionTable = DiscountConditionProductCollection
|
||||
break
|
||||
}
|
||||
case DiscountConditionType.PRODUCT_TAGS: {
|
||||
resourceKey = DiscountConditionJoinTableForeignKey.PRODUCT_TAG_ID
|
||||
|
||||
conditionTable = DiscountConditionProductTag
|
||||
break
|
||||
}
|
||||
case DiscountConditionType.CUSTOMER_GROUPS: {
|
||||
resourceKey = DiscountConditionJoinTableForeignKey.CUSTOMER_GROUP_ID
|
||||
conditionTable = DiscountConditionCustomerGroup
|
||||
break
|
||||
}
|
||||
default:
|
||||
break
|
||||
}
|
||||
|
||||
return {
|
||||
resourceKey,
|
||||
conditionTable,
|
||||
}
|
||||
}
|
||||
|
||||
export const simpleDiscountConditionFactory = async (
|
||||
dataSource: DataSource,
|
||||
data: DiscountConditionFactoryData,
|
||||
seed?: number
|
||||
): Promise<void> => {
|
||||
if (typeof seed !== "undefined") {
|
||||
faker.seed(seed)
|
||||
}
|
||||
|
||||
const manager = dataSource.manager
|
||||
|
||||
let resources = []
|
||||
|
||||
if (data.products) {
|
||||
resources = data.products
|
||||
}
|
||||
if (data.product_collections) {
|
||||
resources = data.product_collections
|
||||
}
|
||||
if (data.product_types) {
|
||||
resources = data.product_types
|
||||
}
|
||||
if (data.product_tags) {
|
||||
resources = data.product_tags
|
||||
}
|
||||
if (data.customer_groups) {
|
||||
resources = data.customer_groups
|
||||
}
|
||||
|
||||
const toCreate = {
|
||||
type: data.type,
|
||||
operator: data.operator,
|
||||
discount_rule_id: data.rule_id,
|
||||
}
|
||||
|
||||
if (data.id) {
|
||||
toCreate["id"] = data.id
|
||||
}
|
||||
|
||||
const condToSave = manager.create(DiscountCondition, toCreate)
|
||||
|
||||
const { conditionTable, resourceKey } = getJoinTableResourceIdentifiers(
|
||||
data.type
|
||||
)
|
||||
|
||||
const condition = await manager.save(condToSave)
|
||||
|
||||
for (const resourceCond of resources) {
|
||||
const toSave = manager.create(conditionTable, {
|
||||
[resourceKey]: resourceCond,
|
||||
condition_id: condition.id,
|
||||
})
|
||||
|
||||
await manager.save(toSave)
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import { GiftCard } from "@medusajs/medusa"
|
||||
import faker from "faker"
|
||||
import { DataSource } from "typeorm"
|
||||
|
||||
export type GiftCardFactoryData = {
|
||||
id?: string
|
||||
code?: string
|
||||
region_id: string
|
||||
value: number
|
||||
balance: number
|
||||
tax_rate?: number
|
||||
}
|
||||
|
||||
export const simpleGiftCardFactory = async (
|
||||
dataSource: DataSource,
|
||||
data: GiftCardFactoryData,
|
||||
seed?: number
|
||||
): Promise<GiftCard> => {
|
||||
if (typeof seed !== "undefined") {
|
||||
faker.seed(seed)
|
||||
}
|
||||
|
||||
const manager = dataSource.manager
|
||||
|
||||
const toSave = manager.create(GiftCard, {
|
||||
id: data.id,
|
||||
code: data.code ?? "TESTGCCODE",
|
||||
region_id: data.region_id,
|
||||
value: data.value,
|
||||
balance: data.balance,
|
||||
tax_rate: data.tax_rate,
|
||||
})
|
||||
|
||||
return await manager.save(toSave)
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
import { DataSource } from "typeorm"
|
||||
import faker from "faker"
|
||||
import { LineItem, LineItemAdjustment, LineItemTaxLine } from "@medusajs/medusa"
|
||||
|
||||
type TaxLineFactoryData = {
|
||||
rate: number
|
||||
code: string
|
||||
name: string
|
||||
}
|
||||
|
||||
type LineItemAdjustmentFactoryData = Omit<LineItemAdjustment, "discount_id"> & {
|
||||
discount_id: string
|
||||
discount_code: string
|
||||
}
|
||||
|
||||
export type LineItemFactoryData = {
|
||||
id?: string
|
||||
cart_id?: string
|
||||
order_id?: string
|
||||
variant_id: string | null
|
||||
title?: string
|
||||
description?: string
|
||||
thumbnail?: string
|
||||
should_merge?: boolean
|
||||
allow_discounts?: boolean
|
||||
is_giftcard?: boolean
|
||||
unit_price?: number
|
||||
quantity?: number
|
||||
fulfilled_quantity?: boolean
|
||||
shipped_quantity?: boolean
|
||||
returned_quantity?: boolean
|
||||
tax_lines?: TaxLineFactoryData[]
|
||||
adjustments: LineItemAdjustmentFactoryData[]
|
||||
includes_tax?: boolean
|
||||
order_edit_id?: string
|
||||
}
|
||||
|
||||
export const simpleLineItemFactory = async (
|
||||
dataSource: DataSource,
|
||||
data: LineItemFactoryData,
|
||||
seed?: number
|
||||
): Promise<LineItem> => {
|
||||
if (
|
||||
typeof data.cart_id === "undefined" &&
|
||||
typeof data.order_id === "undefined"
|
||||
) {
|
||||
throw Error()
|
||||
}
|
||||
|
||||
if (typeof seed !== "undefined") {
|
||||
faker.seed(seed)
|
||||
Math
|
||||
}
|
||||
|
||||
const manager = dataSource.manager
|
||||
|
||||
const id = data.id || `simple-line-${Math.random() * 1000}`
|
||||
const toSave = manager.create(LineItem, {
|
||||
id,
|
||||
cart_id: data.cart_id,
|
||||
order_id: data.order_id,
|
||||
title: data.title || faker.commerce.productName(),
|
||||
description: data.description || "",
|
||||
thumbnail: data.thumbnail || "",
|
||||
should_merge:
|
||||
typeof data.should_merge !== "undefined" ? data.should_merge : true,
|
||||
allow_discounts:
|
||||
typeof data.allow_discounts !== "undefined" ? data.allow_discounts : true,
|
||||
unit_price: typeof data.unit_price !== "undefined" ? data.unit_price : 100,
|
||||
variant_id: data.variant_id,
|
||||
quantity: data.quantity || 1,
|
||||
fulfilled_quantity: data.fulfilled_quantity || null,
|
||||
shipped_quantity: data.shipped_quantity || null,
|
||||
returned_quantity: data.returned_quantity || null,
|
||||
adjustments: data.adjustments,
|
||||
includes_tax: data.includes_tax,
|
||||
order_edit_id: data.order_edit_id,
|
||||
is_giftcard: data.is_giftcard || false
|
||||
})
|
||||
|
||||
const line = await manager.save(toSave)
|
||||
|
||||
if (typeof data.tax_lines !== "undefined") {
|
||||
const taxLinesToSave = data.tax_lines.map((tl) =>
|
||||
manager.create(LineItemTaxLine, {
|
||||
item_id: id,
|
||||
rate: tl.rate,
|
||||
code: tl.code,
|
||||
name: tl.name,
|
||||
})
|
||||
)
|
||||
|
||||
await manager.save(taxLinesToSave)
|
||||
}
|
||||
|
||||
return line
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
import { DataSource } from "typeorm"
|
||||
import { OrderFactoryData, simpleOrderFactory } from "./simple-order-factory"
|
||||
import { OrderEdit } from "@medusajs/medusa"
|
||||
|
||||
export type OrderEditFactoryData = {
|
||||
id?: string
|
||||
order?: OrderFactoryData
|
||||
order_id?: string
|
||||
internal_note?: string
|
||||
declined_reason?: string
|
||||
payment_collection_id?: string
|
||||
confirmed_at?: Date | string
|
||||
confirmed_by?: string
|
||||
created_at?: Date | string
|
||||
created_by?: string
|
||||
requested_at?: Date | string
|
||||
requested_by?: string
|
||||
canceled_at?: Date | string
|
||||
canceled_by?: string
|
||||
declined_at?: Date | string
|
||||
declined_by?: string
|
||||
}
|
||||
|
||||
export const simpleOrderEditFactory = async (
|
||||
dataSource: DataSource,
|
||||
data: OrderEditFactoryData = {}
|
||||
): Promise<OrderEdit> => {
|
||||
const manager = dataSource.manager
|
||||
|
||||
if (!data.order_id) {
|
||||
const order = await simpleOrderFactory(dataSource, data.order)
|
||||
data.order_id = order.id
|
||||
}
|
||||
|
||||
const orderEdit = manager.create<OrderEdit>(OrderEdit, {
|
||||
id: data.id,
|
||||
order_id: data.order_id,
|
||||
internal_note: data.internal_note,
|
||||
declined_reason: data.declined_reason,
|
||||
declined_at: data.declined_at,
|
||||
declined_by: data.declined_by,
|
||||
canceled_at: data.canceled_at,
|
||||
canceled_by: data.canceled_by,
|
||||
requested_at: data.requested_at,
|
||||
requested_by: data.requested_by,
|
||||
created_at: data.created_at,
|
||||
created_by: data.created_by,
|
||||
confirmed_at: data.confirmed_at,
|
||||
confirmed_by: data.confirmed_by,
|
||||
payment_collection_id: data.payment_collection_id,
|
||||
})
|
||||
|
||||
return await manager.save<OrderEdit>(orderEdit)
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
import {
|
||||
Discount,
|
||||
FulfillmentStatus,
|
||||
Order,
|
||||
PaymentStatus,
|
||||
Refund,
|
||||
} from "@medusajs/medusa"
|
||||
import faker from "faker"
|
||||
import { DataSource } from "typeorm"
|
||||
import {
|
||||
AddressFactoryData,
|
||||
simpleAddressFactory,
|
||||
} from "./simple-address-factory"
|
||||
import {
|
||||
CustomerFactoryData,
|
||||
simpleCustomerFactory,
|
||||
} from "./simple-customer-factory"
|
||||
import {
|
||||
DiscountFactoryData,
|
||||
simpleDiscountFactory,
|
||||
} from "./simple-discount-factory"
|
||||
import {
|
||||
LineItemFactoryData,
|
||||
simpleLineItemFactory,
|
||||
} from "./simple-line-item-factory"
|
||||
import { RegionFactoryData, simpleRegionFactory } from "./simple-region-factory"
|
||||
import {
|
||||
SalesChannelFactoryData,
|
||||
simpleSalesChannelFactory,
|
||||
} from "./simple-sales-channel-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
|
||||
customer?: CustomerFactoryData | null
|
||||
currency_code?: string
|
||||
tax_rate?: number | null
|
||||
line_items?: LineItemFactoryData[]
|
||||
discounts?: DiscountFactoryData[]
|
||||
shipping_address?: AddressFactoryData
|
||||
shipping_methods?: ShippingMethodFactoryData[]
|
||||
sales_channel?: SalesChannelFactoryData
|
||||
refunds: Refund[]
|
||||
}
|
||||
|
||||
export const simpleOrderFactory = async (
|
||||
dataSource: DataSource,
|
||||
data: OrderFactoryData = {} as OrderFactoryData,
|
||||
seed?: number
|
||||
): Promise<Order> => {
|
||||
if (typeof seed !== "undefined") {
|
||||
faker.seed(seed)
|
||||
}
|
||||
|
||||
const manager = dataSource.manager
|
||||
|
||||
let currencyCode: string
|
||||
let regionId: string
|
||||
let taxRate: number
|
||||
|
||||
if (typeof data.region === "string") {
|
||||
currencyCode = data.currency_code as string
|
||||
regionId = data.region
|
||||
taxRate = data.tax_rate as number
|
||||
} else {
|
||||
const region = await simpleRegionFactory(dataSource, data.region)
|
||||
taxRate =
|
||||
(typeof data.tax_rate !== "undefined" ? data.tax_rate : region.tax_rate) as number
|
||||
currencyCode = region.currency_code
|
||||
regionId = region.id
|
||||
}
|
||||
|
||||
const address = await simpleAddressFactory(dataSource, data.shipping_address)
|
||||
|
||||
const customer = await simpleCustomerFactory(dataSource, {
|
||||
...data.customer,
|
||||
email: data.email ?? undefined,
|
||||
})
|
||||
|
||||
let discounts: Discount[] = []
|
||||
if (typeof data.discounts !== "undefined") {
|
||||
discounts = await Promise.all(
|
||||
data.discounts.map((d) => simpleDiscountFactory(dataSource, d, seed))
|
||||
)
|
||||
}
|
||||
|
||||
let sales_channel
|
||||
if (typeof data.sales_channel !== "undefined") {
|
||||
sales_channel = await simpleSalesChannelFactory(
|
||||
dataSource,
|
||||
data.sales_channel
|
||||
)
|
||||
}
|
||||
|
||||
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,
|
||||
sales_channel_id: sales_channel?.id ?? null,
|
||||
refunds: data.refunds ?? []
|
||||
})
|
||||
|
||||
const order = await manager.save(toSave)
|
||||
|
||||
const shippingMethods = data.shipping_methods || []
|
||||
for (const sm of shippingMethods) {
|
||||
await simpleShippingMethodFactory(dataSource, { ...sm, order_id: order.id })
|
||||
}
|
||||
|
||||
const items =
|
||||
data.line_items?.map((item) => {
|
||||
const 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(dataSource, { ...item, order_id: id } as unknown as LineItemFactoryData)
|
||||
}
|
||||
|
||||
return order
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import { OrderEditItemChangeType, OrderItemChange } from "@medusajs/medusa"
|
||||
import { DataSource } from "typeorm"
|
||||
|
||||
type OrderItemChangeData = {
|
||||
id: string
|
||||
type: OrderEditItemChangeType
|
||||
order_edit_id: string
|
||||
original_line_item_id?: string
|
||||
line_item_id?: string
|
||||
}
|
||||
|
||||
export const simpleOrderItemChangeFactory = async (
|
||||
dataSource: DataSource,
|
||||
data: OrderItemChangeData
|
||||
) => {
|
||||
const manager = dataSource.manager
|
||||
const change = manager.create<OrderItemChange>(OrderItemChange, {
|
||||
id: data.id,
|
||||
type: data.type,
|
||||
order_edit_id: data.order_edit_id,
|
||||
line_item_id: data.line_item_id,
|
||||
original_line_item_id: data.original_line_item_id,
|
||||
})
|
||||
|
||||
return await manager.save<OrderItemChange>(change)
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
import { DataSource } from "typeorm"
|
||||
|
||||
import { simpleRegionFactory } from "./simple-region-factory"
|
||||
import { simplePaymentFactory } from "./simple-payment-factory"
|
||||
import { Payment, PaymentCollection } from "@medusajs/medusa"
|
||||
|
||||
export const simplePaymentCollectionFactory = async (
|
||||
dataSource: DataSource,
|
||||
data: Partial<PaymentCollection> = {}
|
||||
): Promise<PaymentCollection> => {
|
||||
const manager = dataSource.manager
|
||||
|
||||
const defaultData = {
|
||||
currency_code: data.currency_code ?? "usd",
|
||||
type: data.type ?? "order_edit",
|
||||
status: data.status ?? "not_paid",
|
||||
amount: data.amount ?? 1000,
|
||||
created_by: data.created_by ?? "admin_user",
|
||||
}
|
||||
|
||||
if (!data.region && !data.region_id) {
|
||||
data.region = await simpleRegionFactory(dataSource, {
|
||||
id: data.region_id || "test-region",
|
||||
currency_code: defaultData.currency_code,
|
||||
})
|
||||
data.region_id = data.region.id
|
||||
}
|
||||
|
||||
if (data.payments?.length) {
|
||||
const payments: Payment[] = []
|
||||
for (const payment of data.payments) {
|
||||
payment.currency_code = payment.currency_code ?? defaultData.currency_code
|
||||
|
||||
const savedPayment = await simplePaymentFactory(
|
||||
dataSource,
|
||||
payment as any
|
||||
)
|
||||
payments.push(savedPayment)
|
||||
}
|
||||
|
||||
data.payments = payments
|
||||
}
|
||||
|
||||
const obj = {
|
||||
...defaultData,
|
||||
...data,
|
||||
}
|
||||
|
||||
const payCol = manager.create<PaymentCollection>(PaymentCollection, obj)
|
||||
|
||||
return await manager.save<PaymentCollection>(payCol)
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
import { DataSource } from "typeorm"
|
||||
import { Payment } from "@medusajs/medusa"
|
||||
|
||||
export type PaymentFactoryData = {
|
||||
provider_id?: string
|
||||
order?: string
|
||||
cart?: string
|
||||
data?: any
|
||||
amount?: number
|
||||
currency_code?: string
|
||||
captured?: Date | boolean
|
||||
}
|
||||
|
||||
export const simplePaymentFactory = async (
|
||||
dataSource: DataSource,
|
||||
data: PaymentFactoryData,
|
||||
_?: number
|
||||
): Promise<Payment> => {
|
||||
const manager = dataSource.manager
|
||||
|
||||
let captured_at = data.captured
|
||||
if (typeof captured_at === "boolean") {
|
||||
if (captured_at) {
|
||||
captured_at = new Date()
|
||||
} else {
|
||||
captured_at = null
|
||||
}
|
||||
} else if (typeof captured_at === "undefined") {
|
||||
captured_at = null
|
||||
}
|
||||
|
||||
const address = manager.create(Payment, {
|
||||
provider_id: data.provider_id ?? "test-pay",
|
||||
order_id: data.order,
|
||||
cart_id: data.cart,
|
||||
data: data.data ?? {},
|
||||
amount: data.amount ?? 1000,
|
||||
currency_code: data.currency_code ?? "usd",
|
||||
captured_at,
|
||||
})
|
||||
|
||||
return await manager.save(address)
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
import {
|
||||
CustomerGroup,
|
||||
MoneyAmount,
|
||||
PriceList,
|
||||
PriceListStatus,
|
||||
PriceListType,
|
||||
} from "@medusajs/medusa"
|
||||
import faker from "faker"
|
||||
import { DataSource } from "typeorm"
|
||||
import { simpleCustomerGroupFactory } from "./simple-customer-group-factory"
|
||||
|
||||
type ProductListPrice = {
|
||||
variant_id: string
|
||||
currency_code: string
|
||||
region_id: string
|
||||
amount: number
|
||||
}
|
||||
|
||||
export type PriceListFactoryData = {
|
||||
id?: string
|
||||
name?: string
|
||||
description?: string
|
||||
type?: PriceListType
|
||||
status?: PriceListStatus
|
||||
starts_at?: Date
|
||||
ends_at?: Date
|
||||
customer_groups?: string[]
|
||||
prices?: ProductListPrice[]
|
||||
includes_tax?: boolean
|
||||
}
|
||||
|
||||
export const simplePriceListFactory = async (
|
||||
dataSource: DataSource,
|
||||
data: PriceListFactoryData = {},
|
||||
seed?: number
|
||||
): Promise<PriceList> => {
|
||||
if (typeof seed !== "undefined") {
|
||||
faker.seed(seed)
|
||||
}
|
||||
|
||||
const manager = dataSource.manager
|
||||
|
||||
const listId = data.id || `simple-price-list-${Math.random() * 1000}`
|
||||
|
||||
let customerGroups: CustomerGroup[] = []
|
||||
if (typeof data.customer_groups !== "undefined") {
|
||||
customerGroups = await Promise.all(
|
||||
data.customer_groups.map((group) =>
|
||||
simpleCustomerGroupFactory(dataSource, { id: group })
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
const toCreate = {
|
||||
id: listId,
|
||||
name: data.name || faker.commerce.productName(),
|
||||
description: data.description || "Some text",
|
||||
status: data.status || PriceListStatus.ACTIVE,
|
||||
type: data.type || PriceListType.OVERRIDE,
|
||||
starts_at: data.starts_at || null,
|
||||
ends_at: data.ends_at || null,
|
||||
customer_groups: customerGroups,
|
||||
includes_tax: data.includes_tax,
|
||||
}
|
||||
|
||||
const toSave = manager.create(PriceList, toCreate)
|
||||
const toReturn = await manager.save(toSave)
|
||||
|
||||
if (typeof data.prices !== "undefined") {
|
||||
for (const ma of data.prices) {
|
||||
const factoryData = {
|
||||
...ma,
|
||||
price_list_id: listId,
|
||||
}
|
||||
const toSave = manager.create(MoneyAmount, factoryData)
|
||||
await manager.save(toSave)
|
||||
}
|
||||
}
|
||||
|
||||
return toReturn
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import { DataSource } from "typeorm"
|
||||
import { ProductCategory } from "@medusajs/medusa"
|
||||
|
||||
export const simpleProductCategoryFactory = async (
|
||||
dataSource: DataSource,
|
||||
data: Partial<ProductCategory> = {}
|
||||
): Promise<ProductCategory> => {
|
||||
const manager = dataSource.manager
|
||||
const productCategory = manager.create(ProductCategory, data)
|
||||
|
||||
return (await manager.save(productCategory)) as ProductCategory
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
import { DataSource } from "typeorm"
|
||||
import faker from "faker"
|
||||
import { ProductCollection } from "@medusajs/medusa"
|
||||
|
||||
export type Data = {
|
||||
title?: string,
|
||||
handle?: string
|
||||
}
|
||||
|
||||
export const simpleProductCollectionFactory = async <
|
||||
TData extends Data | Data[] = Data | Data[],
|
||||
TResult = TData extends Array<Data> ? ProductCollection[] : ProductCollection
|
||||
>(
|
||||
dataSource: DataSource,
|
||||
data?: TData,
|
||||
seed?: number
|
||||
): Promise<TResult> => {
|
||||
if (typeof seed !== "undefined") {
|
||||
faker.seed(seed)
|
||||
}
|
||||
|
||||
const manager = dataSource.manager
|
||||
|
||||
data = data || [{
|
||||
title: faker.datatype.string(10),
|
||||
}] as TData
|
||||
|
||||
const collectionsData = Array.isArray(data) ? data : [data]
|
||||
|
||||
const collections: ProductCollection[] = []
|
||||
|
||||
for (const collectionData of collectionsData) {
|
||||
const collection_ = manager.create(ProductCollection, {
|
||||
id: `simple-id-${Math.random() * 1000}`,
|
||||
title: collectionData.title ?? faker.datatype.string(10),
|
||||
handle: collectionData.handle
|
||||
})
|
||||
collections.push(collection_)
|
||||
}
|
||||
|
||||
const productCollections = await manager.save(collections)
|
||||
return (Array.isArray(data) ? productCollections : productCollections[0]) as unknown as TResult
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
import {
|
||||
Product,
|
||||
ProductOption,
|
||||
ProductTag,
|
||||
ProductType,
|
||||
ShippingProfile,
|
||||
ShippingProfileType,
|
||||
Store,
|
||||
} from "@medusajs/medusa"
|
||||
import faker from "faker"
|
||||
import { DataSource } from "typeorm"
|
||||
import {
|
||||
ProductVariantFactoryData,
|
||||
simpleProductVariantFactory,
|
||||
} from "./simple-product-variant-factory"
|
||||
import {
|
||||
SalesChannelFactoryData,
|
||||
simpleSalesChannelFactory,
|
||||
} from "./simple-sales-channel-factory"
|
||||
|
||||
export type ProductFactoryData = {
|
||||
id?: string
|
||||
is_giftcard?: boolean
|
||||
status?: string
|
||||
title?: string
|
||||
type?: string
|
||||
tags?: string[]
|
||||
options?: { id: string; title: string }[]
|
||||
variants?: ProductVariantFactoryData[]
|
||||
sales_channels?: SalesChannelFactoryData[]
|
||||
metadata?: Record<string, unknown>
|
||||
}
|
||||
|
||||
export const simpleProductFactory = async (
|
||||
dataSource: DataSource,
|
||||
data: ProductFactoryData = {},
|
||||
seed?: number
|
||||
): Promise<Product | undefined | null> => {
|
||||
if (typeof seed !== "undefined") {
|
||||
faker.seed(seed)
|
||||
}
|
||||
|
||||
const manager = dataSource.manager
|
||||
|
||||
const defaultProfile = await manager.findOne(ShippingProfile, {
|
||||
where: {
|
||||
type: ShippingProfileType.DEFAULT,
|
||||
},
|
||||
})
|
||||
|
||||
const gcProfile = await manager.findOne(ShippingProfile, {
|
||||
where: {
|
||||
type: ShippingProfileType.GIFT_CARD,
|
||||
},
|
||||
})
|
||||
|
||||
let sales_channels
|
||||
if (data.sales_channels) {
|
||||
sales_channels = await Promise.all(
|
||||
data.sales_channels.map(async (salesChannel) =>
|
||||
salesChannel
|
||||
? await simpleSalesChannelFactory(dataSource, salesChannel)
|
||||
: null
|
||||
)
|
||||
)
|
||||
} else {
|
||||
const stores = await manager.find(Store, {
|
||||
relations: { default_sales_channel: true },
|
||||
})
|
||||
|
||||
const store = stores[0]
|
||||
|
||||
if (store?.default_sales_channel) {
|
||||
sales_channels = [store.default_sales_channel]
|
||||
} else {
|
||||
const salesChannel = await simpleSalesChannelFactory(dataSource, {
|
||||
id: `default-${Math.random() * 1000}`,
|
||||
is_default: true,
|
||||
})
|
||||
|
||||
sales_channels = [salesChannel]
|
||||
}
|
||||
}
|
||||
|
||||
const prodId = data.id || `simple-product-${Math.random() * 1000}`
|
||||
const productToCreate = {
|
||||
id: prodId,
|
||||
title: data.title || faker.commerce.productName(),
|
||||
status: data.status,
|
||||
is_giftcard: data.is_giftcard || false,
|
||||
discountable: !data.is_giftcard,
|
||||
tags: [] as ProductTag[],
|
||||
profile_id: data.is_giftcard ? gcProfile?.id : defaultProfile?.id,
|
||||
metadata: data.metadata || null,
|
||||
} as Product
|
||||
|
||||
if (typeof data.tags !== "undefined") {
|
||||
for (let i = 0; i < data.tags.length; i++) {
|
||||
const createdTag = manager.create(ProductTag, {
|
||||
id: `tag-${Math.random() * 1000}`,
|
||||
value: data.tags[i],
|
||||
})
|
||||
|
||||
const tagRes = await manager.save(createdTag)
|
||||
|
||||
productToCreate.tags.push(tagRes)
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof data.type !== "undefined") {
|
||||
const toSave = manager.create(ProductType, {
|
||||
value: data.type,
|
||||
})
|
||||
const res = await manager.save(toSave)
|
||||
productToCreate["type_id"] = res.id
|
||||
}
|
||||
|
||||
const toSave = manager.create(Product, productToCreate)
|
||||
|
||||
toSave.sales_channels = sales_channels
|
||||
|
||||
const product = await manager.save(toSave)
|
||||
|
||||
const optionId = `${prodId}-option`
|
||||
const options = data.options || [{ id: optionId, title: "Size" }]
|
||||
for (const o of options) {
|
||||
await manager.insert(ProductOption, {
|
||||
id: o.id,
|
||||
title: o.title,
|
||||
product_id: prodId,
|
||||
})
|
||||
}
|
||||
|
||||
const variants = data.variants || [
|
||||
{
|
||||
id: `simple-test-variant-${Math.random() * 1000}`,
|
||||
title: "Test",
|
||||
product_id: prodId,
|
||||
prices: [{ currency: "usd", amount: 100 }],
|
||||
options: [{ option_id: optionId, value: "Large" }],
|
||||
},
|
||||
]
|
||||
|
||||
product.variants = await Promise.all(
|
||||
variants.map(async (pv) => {
|
||||
const factoryData = {
|
||||
...pv,
|
||||
product_id: prodId,
|
||||
}
|
||||
if (typeof pv.options === "undefined") {
|
||||
factoryData.options = [
|
||||
{ option_id: optionId, value: faker.commerce.productAdjective() },
|
||||
]
|
||||
}
|
||||
return await simpleProductVariantFactory(dataSource, factoryData)
|
||||
})
|
||||
)
|
||||
|
||||
return product
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
import { ProductTag } from "@medusajs/medusa"
|
||||
import faker from "faker"
|
||||
import { Connection } from "typeorm"
|
||||
|
||||
type ProductTagFactoryData = {
|
||||
id?: string
|
||||
value?: string
|
||||
metadata?: Record<string, unknown>
|
||||
}
|
||||
|
||||
export const simpleProductTagFactory = async (
|
||||
connection: Connection,
|
||||
data: ProductTagFactoryData = {},
|
||||
seed?: number
|
||||
): Promise<ProductTag | undefined> => {
|
||||
if (typeof seed !== "undefined") {
|
||||
faker.seed(seed)
|
||||
}
|
||||
|
||||
const manager = connection.manager
|
||||
|
||||
const productTag = manager.create(ProductTag, {
|
||||
id: data.id || faker.datatype.uuid(),
|
||||
value: data.value || faker.commerce.productAdjective(),
|
||||
metadata: data.metadata || {},
|
||||
})
|
||||
|
||||
return await manager.save(productTag)
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
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)
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
import { DataSource } from "typeorm"
|
||||
import faker from "faker"
|
||||
import { ProductTypeTaxRate, TaxRate } from "@medusajs/medusa"
|
||||
|
||||
type RateFactoryData = {
|
||||
region_id: string
|
||||
rate?: number | null
|
||||
code?: string
|
||||
name?: string
|
||||
}
|
||||
|
||||
export type ProductTypeTaxRateFactoryData = {
|
||||
product_type_id: string
|
||||
rate: RateFactoryData | string
|
||||
}
|
||||
|
||||
export const simpleProductTypeTaxRateFactory = async (
|
||||
dataSource: DataSource,
|
||||
data: ProductTypeTaxRateFactoryData,
|
||||
seed?: number
|
||||
): Promise<ProductTypeTaxRate> => {
|
||||
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(ProductTypeTaxRate, {
|
||||
product_type_id: data.product_type_id,
|
||||
rate_id: rateId,
|
||||
})
|
||||
|
||||
return await manager.save(toSave)
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
import { DataSource } from "typeorm"
|
||||
import faker from "faker"
|
||||
import {
|
||||
MoneyAmount,
|
||||
ProductOptionValue,
|
||||
ProductVariant,
|
||||
} from "@medusajs/medusa"
|
||||
|
||||
export type ProductVariantFactoryData = {
|
||||
product_id: string
|
||||
id?: string
|
||||
is_giftcard?: boolean
|
||||
sku?: string
|
||||
manage_inventory?: boolean
|
||||
inventory_quantity?: number
|
||||
title?: string
|
||||
allow_backorder?: boolean
|
||||
options?: { option_id: string; value: string }[]
|
||||
prices?: { currency: string; amount: number; region_id?: string }[]
|
||||
}
|
||||
|
||||
export const simpleProductVariantFactory = async (
|
||||
dataSource: DataSource,
|
||||
data: ProductVariantFactoryData,
|
||||
seed?: number
|
||||
): Promise<ProductVariant> => {
|
||||
if (typeof seed !== "undefined") {
|
||||
faker.seed(seed)
|
||||
}
|
||||
|
||||
const manager = dataSource.manager
|
||||
|
||||
const id = data.id || `simple-variant-${Math.random() * 1000}`
|
||||
const toSave = manager.create(ProductVariant, {
|
||||
id,
|
||||
product_id: data.product_id,
|
||||
sku: data.sku,
|
||||
allow_backorder: data.allow_backorder ?? false,
|
||||
manage_inventory:
|
||||
typeof data.manage_inventory !== "undefined"
|
||||
? data.manage_inventory
|
||||
: true,
|
||||
inventory_quantity:
|
||||
typeof data.inventory_quantity !== "undefined"
|
||||
? data.inventory_quantity
|
||||
: 10,
|
||||
title: data.title || faker.commerce.productName(),
|
||||
})
|
||||
|
||||
const variant = await manager.save(toSave)
|
||||
|
||||
const options = data.options || [{ option_id: "test-option", value: "Large" }]
|
||||
for (const o of options) {
|
||||
await manager.insert(ProductOptionValue, {
|
||||
id: `${o.value}-${o.option_id ?? Math.random()}`,
|
||||
value: o.value,
|
||||
variant_id: id,
|
||||
option_id: o.option_id,
|
||||
})
|
||||
}
|
||||
|
||||
const prices = data.prices || [{ currency: "usd", amount: 100 }]
|
||||
for (const p of prices) {
|
||||
await manager.insert(MoneyAmount, {
|
||||
id: `${p.currency}-${p.amount}-${Math.random()}`,
|
||||
variant_id: id,
|
||||
currency_code: p.currency,
|
||||
amount: p.amount,
|
||||
region_id: p.region_id,
|
||||
})
|
||||
}
|
||||
|
||||
return variant
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import faker from "faker"
|
||||
import { DataSource } from "typeorm"
|
||||
|
||||
import { PublishableApiKey } from "@medusajs/medusa"
|
||||
|
||||
export type PublishableApiKeyData = {
|
||||
id?: string
|
||||
revoked_at?: Date
|
||||
revoked_by?: string
|
||||
created_by?: string
|
||||
title?: string
|
||||
}
|
||||
|
||||
export const simplePublishableApiKeyFactory = async (
|
||||
dataSource: DataSource,
|
||||
data: PublishableApiKeyData = {}
|
||||
): Promise<PublishableApiKey> => {
|
||||
const manager = dataSource.manager
|
||||
|
||||
const pubKey = manager.create(PublishableApiKey, {
|
||||
...data,
|
||||
title: data.title || `${faker.commerce.department()} API key`,
|
||||
})
|
||||
|
||||
return await manager.save(pubKey)
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
import { DataSource } from "typeorm"
|
||||
import faker from "faker"
|
||||
import { Region } from "@medusajs/medusa"
|
||||
|
||||
export type RegionFactoryData = {
|
||||
id?: string
|
||||
name?: string
|
||||
currency_code?: string
|
||||
tax_rate?: number
|
||||
countries?: string[]
|
||||
automatic_taxes?: boolean
|
||||
gift_cards_taxable?: boolean
|
||||
fulfillment_providers?: { id: string }[]
|
||||
includes_tax?: boolean
|
||||
}
|
||||
|
||||
export const simpleRegionFactory = async (
|
||||
dataSource: DataSource,
|
||||
data: RegionFactoryData = {},
|
||||
seed?: number
|
||||
): Promise<Region> => {
|
||||
if (typeof seed !== "undefined") {
|
||||
faker.seed(seed)
|
||||
}
|
||||
|
||||
const manager = dataSource.manager
|
||||
|
||||
const regionId = data.id || `simple-region-${Math.random() * 1000}`
|
||||
const r = manager.create(Region, {
|
||||
id: regionId,
|
||||
name: data.name || "Test Region",
|
||||
currency_code: data.currency_code || "usd",
|
||||
tax_rate: data.tax_rate || 0,
|
||||
payment_providers: [{ id: "test-pay" }],
|
||||
fulfillment_providers: data.fulfillment_providers ?? [{ id: "test-ful" }],
|
||||
gift_cards_taxable: data.gift_cards_taxable ?? true,
|
||||
includes_tax: data.includes_tax,
|
||||
automatic_taxes:
|
||||
typeof data.automatic_taxes !== "undefined" ? data.automatic_taxes : true,
|
||||
})
|
||||
|
||||
const region = await manager.save(r)
|
||||
|
||||
const countries = data.countries || ["us"]
|
||||
|
||||
for (const cc of countries) {
|
||||
await manager.query(
|
||||
`UPDATE "country" SET region_id='${regionId}' WHERE iso_2 = '${cc}'`
|
||||
)
|
||||
}
|
||||
|
||||
return region
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
import { Product, SalesChannel } from "@medusajs/medusa"
|
||||
import faker from "faker"
|
||||
import { DataSource } from "typeorm"
|
||||
|
||||
export type SalesChannelFactoryData = {
|
||||
name?: string
|
||||
description?: string
|
||||
is_disabled?: boolean
|
||||
id?: string
|
||||
products?: Product[]
|
||||
is_default?: boolean
|
||||
}
|
||||
|
||||
export const simpleSalesChannelFactory = async (
|
||||
dataSource: DataSource,
|
||||
data: SalesChannelFactoryData = {},
|
||||
seed?: number
|
||||
): Promise<SalesChannel> => {
|
||||
if (typeof seed !== "undefined") {
|
||||
faker.seed(seed)
|
||||
}
|
||||
|
||||
const manager = dataSource.manager
|
||||
|
||||
let salesChannel = manager.create(SalesChannel, {
|
||||
id: data.id ?? `simple-id-${Math.random() * 1000}`,
|
||||
name: data.name || faker.name.firstName(),
|
||||
description: data.description || faker.name.lastName(),
|
||||
is_disabled:
|
||||
typeof data.is_disabled !== undefined ? data.is_disabled : false,
|
||||
})
|
||||
|
||||
salesChannel = await manager.save(salesChannel)
|
||||
|
||||
if (data.products) {
|
||||
const promises = []
|
||||
for (const product of data.products) {
|
||||
promises.push(
|
||||
manager.query(`
|
||||
INSERT INTO product_sales_channel (product_id, sales_channel_id)
|
||||
VALUES ('${product.id}', '${salesChannel.id}');
|
||||
`)
|
||||
)
|
||||
}
|
||||
await Promise.all(promises)
|
||||
}
|
||||
|
||||
if (data.is_default) {
|
||||
await manager.query(
|
||||
`UPDATE store SET default_sales_channel_id = '${salesChannel.id}'`
|
||||
)
|
||||
}
|
||||
|
||||
return salesChannel
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
import { DataSource } from "typeorm"
|
||||
import faker from "faker"
|
||||
import { ShippingMethod, ShippingMethodTaxLine } from "@medusajs/medusa"
|
||||
|
||||
import {
|
||||
ShippingOptionFactoryData,
|
||||
simpleShippingOptionFactory,
|
||||
} from "./simple-shipping-option-factory"
|
||||
|
||||
export type ShippingMethodFactoryData = {
|
||||
id?: string
|
||||
cart_id?: string
|
||||
order_id?: string
|
||||
data?: object
|
||||
price?: number
|
||||
shipping_option: string | ShippingOptionFactoryData
|
||||
tax_lines?: ShippingMethodTaxLine[]
|
||||
includes_tax?: boolean
|
||||
}
|
||||
|
||||
export const simpleShippingMethodFactory = async (
|
||||
dataSource: DataSource,
|
||||
data: ShippingMethodFactoryData,
|
||||
seed?: number
|
||||
): Promise<ShippingMethod> => {
|
||||
if (typeof seed !== "undefined") {
|
||||
faker.seed(seed)
|
||||
}
|
||||
|
||||
const manager = dataSource.manager
|
||||
|
||||
let shippingOptionId: string
|
||||
if (typeof data.shipping_option === "string") {
|
||||
shippingOptionId = data.shipping_option
|
||||
} else {
|
||||
const option = await simpleShippingOptionFactory(
|
||||
dataSource,
|
||||
data.shipping_option
|
||||
)
|
||||
shippingOptionId = option.id
|
||||
}
|
||||
|
||||
const id = data.id || `simple-sm-${Math.random() * 1000}`
|
||||
const toSave = manager.create(ShippingMethod, {
|
||||
id,
|
||||
cart_id: data.cart_id,
|
||||
order_id: data.order_id,
|
||||
shipping_option_id: shippingOptionId,
|
||||
data: data.data || {},
|
||||
price: typeof data.price !== "undefined" ? data.price : 500,
|
||||
includes_tax: data.includes_tax,
|
||||
})
|
||||
|
||||
const shippingMethod = await manager.save(toSave)
|
||||
|
||||
if (typeof data.tax_lines !== "undefined") {
|
||||
const taxLinesToSave = data.tax_lines.map((tl) =>
|
||||
manager.create(ShippingMethodTaxLine, {
|
||||
shipping_method_id: shippingMethod.id,
|
||||
rate: tl.rate,
|
||||
code: tl.code || "default",
|
||||
name: tl.name || "default",
|
||||
})
|
||||
)
|
||||
|
||||
await manager.save(taxLinesToSave)
|
||||
}
|
||||
|
||||
return shippingMethod
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
import {
|
||||
ShippingOption,
|
||||
ShippingOptionPriceType,
|
||||
ShippingOptionRequirement,
|
||||
ShippingProfile,
|
||||
ShippingProfileType,
|
||||
} from "@medusajs/medusa"
|
||||
import faker from "faker"
|
||||
import { DataSource } from "typeorm"
|
||||
import { simpleRegionFactory } from "./simple-region-factory"
|
||||
|
||||
export type ShippingOptionFactoryData = {
|
||||
id?: string
|
||||
name?: string
|
||||
region_id?: string
|
||||
is_return?: boolean
|
||||
is_giftcard?: boolean
|
||||
price?: number
|
||||
price_type?: ShippingOptionPriceType
|
||||
includes_tax?: boolean
|
||||
data?: object
|
||||
requirements?: ShippingOptionRequirementData[]
|
||||
}
|
||||
|
||||
type ShippingOptionRequirementData = {
|
||||
type: "min_subtotal" | "max_subtotal"
|
||||
amount: number
|
||||
}
|
||||
|
||||
export const simpleShippingOptionFactory = async (
|
||||
dataSource: DataSource,
|
||||
data: ShippingOptionFactoryData = {},
|
||||
seed?: number
|
||||
): Promise<ShippingOption> => {
|
||||
if (typeof seed !== "undefined") {
|
||||
faker.seed(seed)
|
||||
}
|
||||
|
||||
const manager = dataSource.manager
|
||||
const defaultProfile = await manager.findOne(ShippingProfile, {
|
||||
where: {
|
||||
type: ShippingProfileType.DEFAULT,
|
||||
}
|
||||
})
|
||||
|
||||
const gcProfile = await manager.findOne(ShippingProfile, {
|
||||
where: {
|
||||
type: ShippingProfileType.GIFT_CARD,
|
||||
}
|
||||
})
|
||||
|
||||
let region_id = data.region_id
|
||||
|
||||
if (!region_id) {
|
||||
const { id } = await simpleRegionFactory(dataSource)
|
||||
region_id = id
|
||||
}
|
||||
|
||||
const shippingOptionData = {
|
||||
id: data.id ?? `simple-so-${Math.random() * 1000}`,
|
||||
name: data.name || "Test Method",
|
||||
is_return: data.is_return ?? false,
|
||||
region_id: region_id,
|
||||
provider_id: "test-ful",
|
||||
profile_id: data.is_giftcard ? gcProfile.id : defaultProfile.id,
|
||||
price_type: data.price_type ?? ShippingOptionPriceType.FLAT_RATE,
|
||||
data: data.data ?? {},
|
||||
requirements: (data.requirements || []) as ShippingOptionRequirement[],
|
||||
amount: typeof data.price !== "undefined" ? data.price : 500,
|
||||
}
|
||||
|
||||
// This is purposefully managed out of the original object for the purpose of separating the data linked to a feature flag
|
||||
// MEDUSA_FF_TAX_INCLUSIVE_PRICING
|
||||
const { includes_tax } = data
|
||||
if (typeof includes_tax !== "undefined") {
|
||||
shippingOptionData["includes_tax"] = includes_tax
|
||||
}
|
||||
|
||||
const created = manager.create(ShippingOption, shippingOptionData)
|
||||
return await manager.save(created)
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import { ShippingProfile, ShippingProfileType } from "@medusajs/medusa"
|
||||
import faker from "faker"
|
||||
import { Connection } from "typeorm"
|
||||
|
||||
export type ShippingProfileFactoryData = {
|
||||
id?: string
|
||||
name?: string
|
||||
type?: ShippingProfileType
|
||||
metadata?: Record<string, unknown>
|
||||
}
|
||||
|
||||
export const simpleShippingProfileFactory = async (
|
||||
connection: Connection,
|
||||
data: ShippingOptionFactoryData = {},
|
||||
seed?: number
|
||||
): Promise<ShippingProfile> => {
|
||||
if (typeof seed !== "undefined") {
|
||||
faker.seed(seed)
|
||||
}
|
||||
|
||||
const manager = connection.manager
|
||||
|
||||
const shippingProfileData = {
|
||||
id: data.id ?? `simple-sp-${Math.random() * 1000}`,
|
||||
name: data.name || `sp-${Math.random() * 1000}`,
|
||||
type: data.type || ShippingProfileType.DEFAULT,
|
||||
metadata: data.metadata,
|
||||
products: [],
|
||||
shipping_options: [],
|
||||
}
|
||||
|
||||
const created = manager.create(ShippingProfile, shippingProfileData)
|
||||
|
||||
return await manager.save(created)
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
import { DataSource } from "typeorm"
|
||||
import faker from "faker"
|
||||
import { ShippingTaxRate, TaxRate } from "@medusajs/medusa"
|
||||
|
||||
type RateFactoryData = {
|
||||
region_id: string
|
||||
rate?: number | null
|
||||
code?: string
|
||||
name?: string
|
||||
}
|
||||
|
||||
export type ShippingTaxRateFactoryData = {
|
||||
shipping_option_id: string
|
||||
rate: RateFactoryData | string
|
||||
}
|
||||
|
||||
export const simpleShippingTaxRateFactory = async (
|
||||
dataSource: DataSource,
|
||||
data: ShippingTaxRateFactoryData,
|
||||
seed?: number
|
||||
): Promise<ShippingTaxRate> => {
|
||||
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(ShippingTaxRate, {
|
||||
shipping_option_id: data.shipping_option_id,
|
||||
rate_id: rateId,
|
||||
})
|
||||
|
||||
return await manager.save(toSave)
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
import { SalesChannel, Store } from "@medusajs/medusa"
|
||||
import faker from "faker"
|
||||
import { DataSource, IsNull, Not } from "typeorm"
|
||||
|
||||
export type StoreFactoryData = {
|
||||
swap_link_template?: string
|
||||
}
|
||||
|
||||
export const simpleStoreFactory = async (
|
||||
dataSource: DataSource,
|
||||
data: StoreFactoryData = {},
|
||||
seed?: number
|
||||
): Promise<Store | undefined> => {
|
||||
if (typeof seed !== "undefined") {
|
||||
faker.seed(seed)
|
||||
}
|
||||
|
||||
const manager = dataSource.manager
|
||||
const stores = await manager.find(Store, { where: { id: Not(IsNull()) } })
|
||||
const store = stores[0]
|
||||
|
||||
if (!store) {
|
||||
return
|
||||
}
|
||||
|
||||
store.swap_link_template = data.swap_link_template ?? "something/{cart_id}"
|
||||
|
||||
await manager.insert(SalesChannel, {
|
||||
id: "test-channel",
|
||||
name: "Default",
|
||||
})
|
||||
|
||||
const storeToSave = await manager.save(store)
|
||||
|
||||
await manager.query(
|
||||
`update store set default_sales_channel_id = 'test-channel' where id = '${
|
||||
storeToSave!.id
|
||||
}'`
|
||||
)
|
||||
|
||||
return storeToSave!
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
import { DataSource } from "typeorm"
|
||||
import faker from "faker"
|
||||
import { TaxRate } from "@medusajs/medusa"
|
||||
|
||||
export type TaxRateFactoryData = {
|
||||
region_id: string
|
||||
rate?: number | null
|
||||
code?: string
|
||||
name?: string
|
||||
}
|
||||
|
||||
export const simpleTaxRateFactory = async (
|
||||
dataSource: DataSource,
|
||||
data: TaxRateFactoryData,
|
||||
seed?: number
|
||||
): Promise<TaxRate> => {
|
||||
if (typeof seed !== "undefined") {
|
||||
faker.seed(seed)
|
||||
}
|
||||
|
||||
const manager = dataSource.manager
|
||||
|
||||
const toSave = manager.create(TaxRate, {
|
||||
region_id: data.region_id,
|
||||
rate:
|
||||
data.rate ?? faker.datatype.number({ min: 0, max: 100, precision: 2 }),
|
||||
code: data.code || faker.random.word(),
|
||||
name: data.name || faker.random.words(2),
|
||||
})
|
||||
|
||||
return await manager.save(toSave)
|
||||
}
|
||||
Reference in new issue
Block a user