fix: adds transformer to map field names to field_id names

This commit is contained in:
Sebastian Rindom
2021-06-15 17:55:22 +02:00
parent dbf9ba7b63
commit 88d96a29fd
4 changed files with 29 additions and 12 deletions
+1
View File
@@ -1,5 +1,6 @@
export { countries } from "./countries"
export { isoCountryLookup } from "./countries"
export { transformIdableFields } from "./transform-idable-fields"
export { default as Validator } from "./validator"
export { default as MedusaError } from "./errors"
export { default as getConfigFile } from "./get-config-file"
@@ -0,0 +1,12 @@
export const transformIdableFields = (obj, fields) => {
const ret = { ...obj }
for (const key of fields) {
if (key in obj && typeof key === "string") {
ret[`${key}_id`] = ret[key]
delete ret[key]
}
}
return ret
}
@@ -1,4 +1,8 @@
import { MedusaError, Validator } from "medusa-core-utils"
import {
MedusaError,
Validator,
transformIdableFields,
} from "medusa-core-utils"
import { defaultFields, defaultRelations } from "."
/**
@@ -133,11 +137,13 @@ export default async (req, res) => {
metadata: Validator.object().optional(),
})
const { value, error } = schema.validate(req.body)
let { value, error } = schema.validate(req.body)
if (error) {
throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details)
}
value = transformIdableFields(value, ["shipping_address", "billing_address"])
try {
const draftOrderService = req.scope.resolve("draftOrderService")
let draftOrder = await draftOrderService.create(value)
+8 -10
View File
@@ -306,8 +306,8 @@ class CartService extends BaseService {
const regCountries = region.countries.map(({ iso_2 }) => iso_2)
if (data.shipping_address && typeof data.shipping_address === `string`) {
const addr = await addressRepo.findOne(data.shipping_address)
if (data.shipping_address_id) {
const addr = await addressRepo.findOne(data.shipping_address_id)
data.shipping_address = addr
}
@@ -650,16 +650,14 @@ class CartService extends BaseService {
}
const addrRepo = manager.getCustomRepository(this.addressRepository_)
if ("shipping_address" in update) {
await this.updateShippingAddress_(
cart,
update.shipping_address,
addrRepo
)
if ("shipping_address_id" in update || "shipping_address" in update) {
const address = update.shipping_address_id || update.shipping_address
await this.updateShippingAddress_(cart, address, addrRepo)
}
if ("billing_address" in update) {
await this.updateBillingAddress_(cart, update.billing_address, addrRepo)
if ("billing_address_id" in update || "billing_address" in update) {
const address = update.billing_address_id || update.billing_address
await this.updateBillingAddress_(cart, address, addrRepo)
}
if ("discounts" in update) {