allow country code on set region
This commit is contained in:
@@ -6,6 +6,7 @@ export default async (req, res) => {
|
|||||||
|
|
||||||
const schema = Validator.object().keys({
|
const schema = Validator.object().keys({
|
||||||
region_id: Validator.string(),
|
region_id: Validator.string(),
|
||||||
|
country_code: Validator.string().optional(),
|
||||||
email: Validator.string().email(),
|
email: Validator.string().email(),
|
||||||
billing_address: Validator.address(),
|
billing_address: Validator.address(),
|
||||||
shipping_address: Validator.address(),
|
shipping_address: Validator.address(),
|
||||||
@@ -28,7 +29,7 @@ export default async (req, res) => {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
if (value.region_id) {
|
if (value.region_id) {
|
||||||
await cartService.setRegion(id, value.region_id)
|
await cartService.setRegion(id, value.region_id, value.country_code)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (value.email) {
|
if (value.email) {
|
||||||
|
|||||||
@@ -1108,7 +1108,7 @@ class CartService extends BaseService {
|
|||||||
* @param {string} regionId - the id of the region to set the cart to
|
* @param {string} regionId - the id of the region to set the cart to
|
||||||
* @return {Promise} the result of the update operation
|
* @return {Promise} the result of the update operation
|
||||||
*/
|
*/
|
||||||
async setRegion(cartId, regionId) {
|
async setRegion(cartId, regionId, countryCode) {
|
||||||
const cart = await this.retrieve(cartId)
|
const cart = await this.retrieve(cartId)
|
||||||
const region = await this.regionService_.retrieve(regionId)
|
const region = await this.regionService_.retrieve(regionId)
|
||||||
|
|
||||||
@@ -1137,8 +1137,17 @@ class CartService extends BaseService {
|
|||||||
update.items = newItems.filter(i => !!i)
|
update.items = newItems.filter(i => !!i)
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the country code of a shipping address is set we need to clear it
|
|
||||||
let shippingAddress = cart.shipping_address || {}
|
let shippingAddress = cart.shipping_address || {}
|
||||||
|
if (countryCode !== undefined) {
|
||||||
|
if (!region.countries.includes(countryCode)) {
|
||||||
|
throw new MedusaError(
|
||||||
|
MedusaError.Types.NOT_ALLOWED,
|
||||||
|
`Country not available in region`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
shippingAddress.country_code = countryCode
|
||||||
|
} else {
|
||||||
|
// If the country code of a shipping address is set we need to clear it
|
||||||
if (!_.isEmpty(shippingAddress) && shippingAddress.country_code) {
|
if (!_.isEmpty(shippingAddress) && shippingAddress.country_code) {
|
||||||
shippingAddress.country_code = ""
|
shippingAddress.country_code = ""
|
||||||
update.shipping_address = shippingAddress
|
update.shipping_address = shippingAddress
|
||||||
@@ -1149,6 +1158,7 @@ class CartService extends BaseService {
|
|||||||
shippingAddress.country_code = region.countries[0]
|
shippingAddress.country_code = region.countries[0]
|
||||||
update.shipping_address = shippingAddress
|
update.shipping_address = shippingAddress
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Shipping methods are determined by region so the user needs to find a
|
// Shipping methods are determined by region so the user needs to find a
|
||||||
// new shipping method
|
// new shipping method
|
||||||
|
|||||||
Reference in New Issue
Block a user