fix(medusa): cart to be created with a country code (#2197)
Co-authored-by: Oliver Windall Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
1 parent
3767cf26f7
commit
eb3b02baf4
2 files changed
+41
-16
No files matched your search
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/medusa": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Create cart with country code in shipping address
|
||||||
@@ -1,4 +1,7 @@
|
|||||||
import { CartService, LineItemService, RegionService } from "../../../../services"
|
import { EntityManager } from "typeorm"
|
||||||
|
import { MedusaError } from "medusa-core-utils"
|
||||||
|
import reqIp from "request-ip"
|
||||||
|
import { Type } from "class-transformer"
|
||||||
import {
|
import {
|
||||||
IsArray,
|
IsArray,
|
||||||
IsInt,
|
IsInt,
|
||||||
@@ -7,18 +10,20 @@ import {
|
|||||||
IsString,
|
IsString,
|
||||||
ValidateNested,
|
ValidateNested,
|
||||||
} from "class-validator"
|
} from "class-validator"
|
||||||
import { defaultStoreCartFields, defaultStoreCartRelations, } from "."
|
|
||||||
|
|
||||||
import { Cart } from "../../../../models";
|
import {
|
||||||
import { EntityManager } from "typeorm"
|
CartService,
|
||||||
import { FeatureFlagDecorators } from "../../../../utils/feature-flag-decorators";
|
LineItemService,
|
||||||
|
RegionService,
|
||||||
|
} from "../../../../services"
|
||||||
|
import { defaultStoreCartFields, defaultStoreCartRelations } from "."
|
||||||
|
import { Cart } from "../../../../models"
|
||||||
|
import { FeatureFlagDecorators } from "../../../../utils/feature-flag-decorators"
|
||||||
import { FlagRouter } from "../../../../utils/flag-router"
|
import { FlagRouter } from "../../../../utils/flag-router"
|
||||||
import { MedusaError } from "medusa-core-utils"
|
import SalesChannelFeatureFlag from "../../../../loaders/feature-flags/sales-channels"
|
||||||
import SalesChannelFeatureFlag from "../../../../loaders/feature-flags/sales-channels";
|
|
||||||
import { Type } from "class-transformer"
|
|
||||||
import { decorateLineItemsWithTotals } from "./decorate-line-items-with-totals"
|
import { decorateLineItemsWithTotals } from "./decorate-line-items-with-totals"
|
||||||
import reqIp from "request-ip"
|
import { CartCreateProps } from "../../../../types/cart"
|
||||||
import { isDefined } from "../../../../utils";
|
import { isDefined } from "../../../../utils"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @oas [post] /carts
|
* @oas [post] /carts
|
||||||
@@ -131,16 +136,31 @@ export default async (req, res) => {
|
|||||||
regionId = regions[0].id
|
regionId = regions[0].id
|
||||||
}
|
}
|
||||||
|
|
||||||
let cart: Cart
|
const toCreate: Partial<CartCreateProps> = {
|
||||||
await entityManager.transaction(async (manager) => {
|
region_id: regionId,
|
||||||
cart = await cartService.withTransaction(manager).create({
|
sales_channel_id: validated.sales_channel_id,
|
||||||
...validated,
|
|
||||||
context: {
|
context: {
|
||||||
...reqContext,
|
...reqContext,
|
||||||
...validated.context,
|
...validated.context,
|
||||||
},
|
},
|
||||||
region_id: regionId,
|
}
|
||||||
})
|
|
||||||
|
if (req.user && req.user.customer_id) {
|
||||||
|
const customerService = req.scope.resolve("customerService")
|
||||||
|
const customer = await customerService.retrieve(req.user.customer_id)
|
||||||
|
toCreate["customer_id"] = customer.id
|
||||||
|
toCreate["email"] = customer.email
|
||||||
|
}
|
||||||
|
|
||||||
|
if (validated.country_code) {
|
||||||
|
toCreate["shipping_address"] = {
|
||||||
|
country_code: validated.country_code.toLowerCase(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let cart: Cart
|
||||||
|
await entityManager.transaction(async (manager) => {
|
||||||
|
cart = await cartService.withTransaction(manager).create(toCreate)
|
||||||
|
|
||||||
if (validated.items) {
|
if (validated.items) {
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
|
|||||||
Reference in new issue
Block a user