fix: Disallow creating duplicate prices (#7866)
* fix: Disallow creating duplicate prices * fix: Don't pass id to manager create in upsertWithReplace
This commit is contained in:
1 parent
87375db9ef
commit
b4aa7fb9a7
12 files changed
+560
-350
No files matched your search
@@ -2,6 +2,7 @@ import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import {
|
||||
AddPriceListPricesDTO,
|
||||
CreatePriceListPriceDTO,
|
||||
CreatePriceListPriceWorkflowDTO,
|
||||
CreatePriceListPricesWorkflowStepDTO,
|
||||
IPricingModuleService,
|
||||
} from "@medusajs/types"
|
||||
@@ -22,10 +23,12 @@ export const createPriceListPricesStep = createStep(
|
||||
const pricesToAdd: CreatePriceListPriceDTO[] = []
|
||||
|
||||
for (const price of prices) {
|
||||
pricesToAdd.push({
|
||||
const toPush = {
|
||||
...price,
|
||||
price_set_id: variantPriceSetMap[price.variant_id!],
|
||||
})
|
||||
} as CreatePriceListPriceDTO
|
||||
delete (toPush as Partial<CreatePriceListPriceWorkflowDTO>).variant_id
|
||||
pricesToAdd.push(toPush)
|
||||
}
|
||||
|
||||
if (pricesToAdd.length) {
|
||||
|
||||
@@ -4,6 +4,7 @@ import {
|
||||
PriceDTO,
|
||||
UpdatePriceListPriceDTO,
|
||||
UpdatePriceListPricesDTO,
|
||||
UpdatePriceListPriceWorkflowDTO,
|
||||
UpdatePriceListPriceWorkflowStepDTO,
|
||||
} from "@medusajs/types"
|
||||
import { buildPriceSetPricesForModule } from "@medusajs/utils"
|
||||
@@ -25,10 +26,13 @@ export const updatePriceListPricesStep = createStep(
|
||||
const { prices = [], id } = priceListData
|
||||
|
||||
for (const price of prices) {
|
||||
pricesToUpdate.push({
|
||||
const toPush = {
|
||||
...price,
|
||||
price_set_id: variantPriceSetMap[price.variant_id!],
|
||||
})
|
||||
} as UpdatePriceListPriceDTO
|
||||
delete (toPush as Partial<UpdatePriceListPriceWorkflowDTO>).variant_id
|
||||
|
||||
pricesToUpdate.push(toPush)
|
||||
|
||||
if (price.id) {
|
||||
priceIds.push(price.id)
|
||||
|
||||
@@ -747,16 +747,22 @@ export function mikroOrmBaseRepositoryFactory<T extends object = object>(
|
||||
entityName: string,
|
||||
data: any
|
||||
): Record<string, any> & { id: string } {
|
||||
const created = manager.create(entityName, data, {
|
||||
managed: false,
|
||||
persist: false,
|
||||
})
|
||||
// We set the id to undefined to make sure the entity isn't fetched from the entity map if it is an update,
|
||||
// giving us incorrect data for the bignumberdata field (I though managed: false and persist: false would already do this)
|
||||
const created = manager.create(
|
||||
entityName,
|
||||
{ ...data, id: undefined },
|
||||
{
|
||||
managed: false,
|
||||
persist: false,
|
||||
}
|
||||
)
|
||||
|
||||
const resp = {
|
||||
// `create` will omit non-existent fields, but we want to pass the data the user provided through so the correct errors get thrown
|
||||
...data,
|
||||
...(created as any).__helper.__bignumberdata,
|
||||
id: (created as any).id,
|
||||
id: data.id ?? (created as any).id,
|
||||
}
|
||||
|
||||
// Non-persist relation columns should be removed before we do the upsert.
|
||||
|
||||
Reference in new issue
Block a user