* feat: sales channel joiner config * feat: product sales channel link config, SC list method * feat: migration * fix: refactor list SC * refactor: SC repo api * chore: changeset * feat: add dedicated FF * wip: cart<>sc link and migration * chore: changeset * fix: update migration with the cart table constraints * feat: populate the pivot table * chore: remove relation from joiner config * fix: constraint name * fix: filter out link relations when calling internal services * feat: product<> sc join entity * fix: update case * fix: add FF on in the repository, fix tests * fix: assign id when FF is on * fix: target table * feat: product service - fetch SC with RQ * feat: admin list products & SC with isolated product domain * feat: get admin product * feat: store endpoints * fix: remove duplicate import * fix: remove "name" prop * feat: typeorm entity changes * feat: pivot table, entity, on cart create changes * feat: update carts' SC * feat: cart - getValidatedSalesChannel with RQ * feat: refactor * wip: changes to create cart workflow * fix: remove join table entity due to migrations failing * fix: product seeder if FF is on * feat: attach SC handler and test * fix: env * feat: workflow compensation, cart service retrieve with RQ * fix: remote joiner implode map * chore: update changesets * fix: remove methods from SC service/repo * feat: use remote link in handlers * fix: remove SC service calls * fix: link params * fix: migration add constraint to make link upsert pass * refactor: workflow product handlers to handle remote links * fix: condition * fix: use correct method * fix: build * wip: update FF * fix: update FF in the handlers * chore: migrate to medusav2 FF * chore: uncomment test * fix: product factory * fix: unlinking SC and product * fix: use module name variable * refactor: cleanup query definitions * fix: add constraint * wip: migrate FF * fix: comments * feat: cart entity callbacks, fix tests * fix: only create SC in test * wip: services updates, changes to models * chore: rename prop * fix: add hook * fix: address comments * fix: temp sc filtering * fix: use RQ to filter by SC * fix: relations on retrieve * feat: migration sync data, remove FF * fix: revert order of queries * fix: alter migration, relations in service * fix: revert id * fix: migrations * fix: make expand work * fix: remote link method call * fix: try making tests work without id in the pivot table * test: use remote link * test: relations changes * fix: preserve channel id column * fix: seeder and factory * fix: remove sales_channels from response * feat: support feature flag arrays * fix: cover everything with correct FF * fix: remove verbose * fix: unit and plugin tests * chore: comments * fix: reenable workflow handler, add comments, split cart create workflow tests * chore: reenable link in the create mehod, update changesets * fix: address feedback * fix: revert migration * fix: change the migration to follow link module * fix: migration syntax * fix: merge conflicts * fix: typo * feat: remove store sales channel foreign key * fix: merge migrations * fix: FF keys * refactor: cart service * refactor: FF missing key * fix: comments * fix: address PR comments * fix: new changesets * fix: revert flag router changes * chore: refactor `isFeatureEnabled` --------- Co-authored-by: Carlos R. L. Rodrigues <rodrigolr@gmail.com> Co-authored-by: Riqwan Thamir <rmthamir@gmail.com>
118 lines
3.2 KiB
TypeScript
118 lines
3.2 KiB
TypeScript
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"
|
|
import { generateEntityId } from "@medusajs/utils"
|
|
|
|
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
|
|
}
|
|
|
|
const isMedusaV2Enabled = process.env.MEDUSA_FF_MEDUSA_V2 == "true"
|
|
|
|
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}`
|
|
let toSave = {
|
|
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,
|
|
}
|
|
|
|
if (isMedusaV2Enabled) {
|
|
await manager.query(
|
|
`INSERT INTO "cart_sales_channel" (id, cart_id, sales_channel_id)
|
|
VALUES ('${generateEntityId(undefined, "cartsc")}', '${toSave.id}', '${
|
|
sales_channel?.id ?? data.sales_channel_id
|
|
}');`
|
|
)
|
|
}
|
|
|
|
toSave = manager.create(Cart, toSave)
|
|
|
|
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
|
|
}
|