feat(medusa): Separate money amount and variant (#4906)
* initial changes * working test * final changes to product tests * update integration tests * update price list integration tests * update integration tests * update unit tests * update plugin integration tests * remove catch from integration test * undo change * add andWhere * update upsertCurrencyMoneyAmount method * undo line item changes * undo changes * update deprecated method * Update packages/medusa/src/migrations/1692953518123-drop_money_amount_constraints_for_pricing_module.ts Co-authored-by: Carlos R. L. Rodrigues <37986729+carlos-r-l-rodrigues@users.noreply.github.com> * rename joinTable * update with joinTable entity * update load methods * remove await create * re-add context test * update price list behavior for prices * update price list snapshots * re-add admin seeder * pr feedback * fix unit tests * fix plugin integration tests * initial review changes * redo changes to variant creation --------- Co-authored-by: Carlos R. L. Rodrigues <37986729+carlos-r-l-rodrigues@users.noreply.github.com> Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
co-authored by
Carlos R. L. Rodrigues
Oli Juhl
parent
3d68be2b6b
commit
5d10c46bb1
@@ -5,9 +5,11 @@ import {
|
||||
PriceListStatus,
|
||||
PriceListType,
|
||||
} from "@medusajs/medusa"
|
||||
import faker from "faker"
|
||||
|
||||
import { DataSource } from "typeorm"
|
||||
import faker from "faker"
|
||||
import { simpleCustomerGroupFactory } from "./simple-customer-group-factory"
|
||||
import { ProductVariantMoneyAmount } from "@medusajs/medusa"
|
||||
|
||||
type ProductListPrice = {
|
||||
variant_id: string
|
||||
@@ -64,18 +66,27 @@ export const simplePriceListFactory = async (
|
||||
}
|
||||
|
||||
const toSave = manager.create(PriceList, toCreate)
|
||||
const toReturn = await manager.save(toSave)
|
||||
const toReturn = await manager.save(toSave) as PriceList
|
||||
|
||||
if (typeof data.prices !== "undefined") {
|
||||
for (const ma of data.prices) {
|
||||
toReturn.prices = await Promise.all(data.prices.map(async (ma) => {
|
||||
const factoryData = {
|
||||
...ma,
|
||||
price_list_id: listId,
|
||||
}
|
||||
const toSave = manager.create(MoneyAmount, factoryData)
|
||||
const toSave: MoneyAmount = manager.create(MoneyAmount, factoryData)
|
||||
await manager.save(toSave)
|
||||
}
|
||||
|
||||
await manager.insert(ProductVariantMoneyAmount, {
|
||||
id: `${ma.variant_id}-${toSave.id}`,
|
||||
variant_id: ma.variant_id,
|
||||
money_amount_id: toSave.id,
|
||||
})
|
||||
|
||||
return toSave
|
||||
}))
|
||||
}
|
||||
|
||||
|
||||
return toReturn
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ export type ProductFactoryData = {
|
||||
type?: string
|
||||
tags?: string[]
|
||||
options?: { id: string; title: string }[]
|
||||
variants?: ProductVariantFactoryData[]
|
||||
variants?: Omit<ProductVariantFactoryData, "product_id">[]
|
||||
sales_channels?: SalesChannelFactoryData[]
|
||||
metadata?: Record<string, unknown>
|
||||
}
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
import { DataSource } from "typeorm"
|
||||
import faker from "faker"
|
||||
import {
|
||||
MoneyAmount,
|
||||
ProductOptionValue,
|
||||
ProductVariant,
|
||||
ProductVariantMoneyAmount,
|
||||
} from "@medusajs/medusa"
|
||||
|
||||
import { DataSource } from "typeorm"
|
||||
import faker from "faker"
|
||||
|
||||
export type ProductVariantFactoryData = {
|
||||
product_id: string
|
||||
id?: string
|
||||
@@ -31,6 +33,7 @@ export const simpleProductVariantFactory = async (
|
||||
const manager = dataSource.manager
|
||||
|
||||
const id = data.id || `simple-variant-${Math.random() * 1000}`
|
||||
|
||||
const toSave = manager.create(ProductVariant, {
|
||||
id,
|
||||
product_id: data.product_id,
|
||||
@@ -61,13 +64,19 @@ export const simpleProductVariantFactory = async (
|
||||
|
||||
const prices = data.prices || [{ currency: "usd", amount: 100 }]
|
||||
for (const p of prices) {
|
||||
const ma_id = `${p.currency}-${p.amount}-${Math.random()}`
|
||||
await manager.insert(MoneyAmount, {
|
||||
id: `${p.currency}-${p.amount}-${Math.random()}`,
|
||||
variant_id: id,
|
||||
id: ma_id,
|
||||
currency_code: p.currency,
|
||||
amount: p.amount,
|
||||
region_id: p.region_id,
|
||||
})
|
||||
|
||||
await manager.insert(ProductVariantMoneyAmount, {
|
||||
id: `${ma_id}-${id}-${Math.random()}`,
|
||||
money_amount_id: ma_id,
|
||||
variant_id: id,
|
||||
})
|
||||
}
|
||||
|
||||
return variant
|
||||
|
||||
Reference in New Issue
Block a user