hotfix(medusa): shipping address updates

This commit is contained in:
Sebastian Rindom committed 2021-10-04 17:44:25 +02:00
1 parent 27ebe2aaf9
commit 09247e00ef
1 file changed
+34 -32
+34 -32
View File
@@ -160,7 +160,7 @@ class CartService extends BaseService {
"total", "total",
] ]
const totalsToSelect = select.filter(v => totalFields.includes(v)) const totalsToSelect = select.filter((v) => totalFields.includes(v))
if (totalsToSelect.length > 0) { if (totalsToSelect.length > 0) {
const relationSet = new Set(relations) const relationSet = new Set(relations)
relationSet.add("items") relationSet.add("items")
@@ -173,7 +173,7 @@ class CartService extends BaseService {
relationSet.add("region") relationSet.add("region")
relations = [...relationSet] relations = [...relationSet]
select = select.filter(v => !totalFields.includes(v)) select = select.filter((v) => !totalFields.includes(v))
} }
return { return {
@@ -273,7 +273,7 @@ class CartService extends BaseService {
* @return {Promise} the result of the create operation * @return {Promise} the result of the create operation
*/ */
async create(data) { async create(data) {
return this.atomicPhase_(async manager => { return this.atomicPhase_(async (manager) => {
const cartRepo = manager.getCustomRepository(this.cartRepository_) const cartRepo = manager.getCustomRepository(this.cartRepository_)
const addressRepo = manager.getCustomRepository(this.addressRepository_) const addressRepo = manager.getCustomRepository(this.addressRepository_)
const { region_id } = data const { region_id } = data
@@ -342,7 +342,7 @@ class CartService extends BaseService {
* @retur {Promise} the result of the update operation * @retur {Promise} the result of the update operation
*/ */
async removeLineItem(cartId, lineItemId) { async removeLineItem(cartId, lineItemId) {
return this.atomicPhase_(async manager => { return this.atomicPhase_(async (manager) => {
const cart = await this.retrieve(cartId, { const cart = await this.retrieve(cartId, {
relations: [ relations: [
"items", "items",
@@ -352,7 +352,7 @@ class CartService extends BaseService {
], ],
}) })
const lineItem = cart.items.find(li => li.id === lineItemId) const lineItem = cart.items.find((li) => li.id === lineItemId)
if (!lineItem) { if (!lineItem) {
return cart return cart
} }
@@ -421,7 +421,7 @@ class CartService extends BaseService {
* @return {Promise} the result of the update operation * @return {Promise} the result of the update operation
*/ */
async addLineItem(cartId, lineItem) { async addLineItem(cartId, lineItem) {
return this.atomicPhase_(async manager => { return this.atomicPhase_(async (manager) => {
const cart = await this.retrieve(cartId, { const cart = await this.retrieve(cartId, {
relations: [ relations: [
"shipping_methods", "shipping_methods",
@@ -434,7 +434,7 @@ class CartService extends BaseService {
let currentItem let currentItem
if (lineItem.should_merge) { if (lineItem.should_merge) {
currentItem = cart.items.find(line => { currentItem = cart.items.find((line) => {
if (line.should_merge && line.variant_id === lineItem.variant_id) { if (line.should_merge && line.variant_id === lineItem.variant_id) {
return _.isEqual(line.metadata, lineItem.metadata) return _.isEqual(line.metadata, lineItem.metadata)
} }
@@ -500,13 +500,13 @@ class CartService extends BaseService {
* @return {Promise} the result of the update operation * @return {Promise} the result of the update operation
*/ */
async updateLineItem(cartId, lineItemId, lineItemUpdate) { async updateLineItem(cartId, lineItemId, lineItemUpdate) {
return this.atomicPhase_(async manager => { return this.atomicPhase_(async (manager) => {
const cart = await this.retrieve(cartId, { const cart = await this.retrieve(cartId, {
relations: ["items", "payment_sessions"], relations: ["items", "payment_sessions"],
}) })
// Ensure that the line item exists in the cart // Ensure that the line item exists in the cart
const lineItemExists = cart.items.find(i => i.id === lineItemId) const lineItemExists = cart.items.find((i) => i.id === lineItemId)
if (!lineItemExists) { if (!lineItemExists) {
throw new MedusaError( throw new MedusaError(
MedusaError.Types.INVALID_DATA, MedusaError.Types.INVALID_DATA,
@@ -553,7 +553,7 @@ class CartService extends BaseService {
// if any free shipping discounts, we ensure to update shipping method amount // if any free shipping discounts, we ensure to update shipping method amount
if (shouldAdd) { if (shouldAdd) {
await Promise.all( await Promise.all(
cart.shipping_methods.map(async sm => { cart.shipping_methods.map(async (sm) => {
const smRepo = this.manager_.getCustomRepository( const smRepo = this.manager_.getCustomRepository(
this.shippingMethodRepository_ this.shippingMethodRepository_
) )
@@ -568,7 +568,7 @@ class CartService extends BaseService {
) )
} else { } else {
await Promise.all( await Promise.all(
cart.shipping_methods.map(async sm => { cart.shipping_methods.map(async (sm) => {
const smRepo = this.manager_.getCustomRepository( const smRepo = this.manager_.getCustomRepository(
this.shippingMethodRepository_ this.shippingMethodRepository_
) )
@@ -584,7 +584,7 @@ class CartService extends BaseService {
} }
async update(cartId, update) { async update(cartId, update) {
return this.atomicPhase_(async manager => { return this.atomicPhase_(async (manager) => {
const cartRepo = manager.getCustomRepository(this.cartRepository_) const cartRepo = manager.getCustomRepository(this.cartRepository_)
const cart = await this.retrieve(cartId, { const cart = await this.retrieve(cartId, {
select: [ select: [
@@ -611,7 +611,9 @@ class CartService extends BaseService {
}) })
if ("region_id" in update) { if ("region_id" in update) {
await this.setRegion_(cart, update.region_id, update.country_code) const countryCode =
update.country_code || update.shipping_address?.country_code
await this.setRegion_(cart, update.region_id, countryCode)
} }
if ("customer_id" in update) { if ("customer_id" in update) {
@@ -936,7 +938,7 @@ class CartService extends BaseService {
const toParse = [...cart.discounts, discount] const toParse = [...cart.discounts, discount]
let sawNotShipping = false let sawNotShipping = false
const newDiscounts = toParse.map(d => { const newDiscounts = toParse.map((d) => {
const drule = d.rule const drule = d.rule
switch (drule.type) { switch (drule.type) {
case "free_shipping": case "free_shipping":
@@ -966,7 +968,7 @@ class CartService extends BaseService {
* @return {Promise<Cart>} the resulting cart * @return {Promise<Cart>} the resulting cart
*/ */
async removeDiscount(cartId, discountCode) { async removeDiscount(cartId, discountCode) {
return this.atomicPhase_(async manager => { return this.atomicPhase_(async (manager) => {
const cart = await this.retrieve(cartId, { const cart = await this.retrieve(cartId, {
relations: ["discounts", "payment_sessions", "shipping_methods"], relations: ["discounts", "payment_sessions", "shipping_methods"],
}) })
@@ -975,7 +977,7 @@ class CartService extends BaseService {
await this.adjustFreeShipping_(cart, false) await this.adjustFreeShipping_(cart, false)
} }
cart.discounts = cart.discounts.filter(d => d.code !== discountCode) cart.discounts = cart.discounts.filter((d) => d.code !== discountCode)
const cartRepo = manager.getCustomRepository(this.cartRepository_) const cartRepo = manager.getCustomRepository(this.cartRepository_)
@@ -1006,7 +1008,7 @@ class CartService extends BaseService {
* Updates the currently selected payment session. * Updates the currently selected payment session.
*/ */
async updatePaymentSession(cartId, update) { async updatePaymentSession(cartId, update) {
return this.atomicPhase_(async manager => { return this.atomicPhase_(async (manager) => {
const cart = await this.retrieve(cartId, { const cart = await this.retrieve(cartId, {
relations: ["payment_sessions"], relations: ["payment_sessions"],
}) })
@@ -1040,7 +1042,7 @@ class CartService extends BaseService {
* @return {Promise<Cart>} the resulting cart * @return {Promise<Cart>} the resulting cart
*/ */
async authorizePayment(cartId, context = {}) { async authorizePayment(cartId, context = {}) {
return this.atomicPhase_(async manager => { return this.atomicPhase_(async (manager) => {
const cartRepository = manager.getCustomRepository(this.cartRepository_) const cartRepository = manager.getCustomRepository(this.cartRepository_)
const cart = await this.retrieve(cartId, { const cart = await this.retrieve(cartId, {
@@ -1087,7 +1089,7 @@ class CartService extends BaseService {
* @returns {Promise} result of update operation * @returns {Promise} result of update operation
*/ */
async setPaymentSession(cartId, providerId) { async setPaymentSession(cartId, providerId) {
return this.atomicPhase_(async manager => { return this.atomicPhase_(async (manager) => {
const psRepo = manager.getCustomRepository(this.paymentSessionRepository_) const psRepo = manager.getCustomRepository(this.paymentSessionRepository_)
const cart = await this.retrieve(cartId, { const cart = await this.retrieve(cartId, {
@@ -1116,13 +1118,13 @@ class CartService extends BaseService {
} }
await Promise.all( await Promise.all(
cart.payment_sessions.map(ps => { cart.payment_sessions.map((ps) => {
return psRepo.save({ ...ps, is_selected: null }) return psRepo.save({ ...ps, is_selected: null })
}) })
) )
const sess = cart.payment_sessions.find( const sess = cart.payment_sessions.find(
ps => ps.provider_id === providerId (ps) => ps.provider_id === providerId
) )
sess.is_selected = true sess.is_selected = true
@@ -1148,7 +1150,7 @@ class CartService extends BaseService {
* @returns {Promise} the result of the update operation. * @returns {Promise} the result of the update operation.
*/ */
async setPaymentSessions(cartOrCartId) { async setPaymentSessions(cartOrCartId) {
return this.atomicPhase_(async manager => { return this.atomicPhase_(async (manager) => {
const psRepo = manager.getCustomRepository(this.paymentSessionRepository_) const psRepo = manager.getCustomRepository(this.paymentSessionRepository_)
let cartId = let cartId =
@@ -1231,7 +1233,7 @@ class CartService extends BaseService {
* @returns {Promise<Cart>} the resulting cart. * @returns {Promise<Cart>} the resulting cart.
*/ */
async deletePaymentSession(cartId, providerId) { async deletePaymentSession(cartId, providerId) {
return this.atomicPhase_(async manager => { return this.atomicPhase_(async (manager) => {
const cart = await this.retrieve(cartId, { const cart = await this.retrieve(cartId, {
relations: ["payment_sessions"], relations: ["payment_sessions"],
}) })
@@ -1272,7 +1274,7 @@ class CartService extends BaseService {
* @returns {Promise<Cart>} the resulting cart. * @returns {Promise<Cart>} the resulting cart.
*/ */
async refreshPaymentSession(cartId, providerId) { async refreshPaymentSession(cartId, providerId) {
return this.atomicPhase_(async manager => { return this.atomicPhase_(async (manager) => {
const cart = await this.retrieve(cartId, { const cart = await this.retrieve(cartId, {
relations: ["payment_sessions"], relations: ["payment_sessions"],
}) })
@@ -1311,7 +1313,7 @@ class CartService extends BaseService {
* @return {Promise} the result of the update operation * @return {Promise} the result of the update operation
*/ */
async addShippingMethod(cartId, optionId, data) { async addShippingMethod(cartId, optionId, data) {
return this.atomicPhase_(async manager => { return this.atomicPhase_(async (manager) => {
const cart = await this.retrieve(cartId, { const cart = await this.retrieve(cartId, {
select: ["subtotal"], select: ["subtotal"],
relations: [ relations: [
@@ -1395,7 +1397,7 @@ class CartService extends BaseService {
if (cart.items.length) { if (cart.items.length) {
cart.items = await Promise.all( cart.items = await Promise.all(
cart.items cart.items
.map(async item => { .map(async (item) => {
const availablePrice = await this.productVariantService_ const availablePrice = await this.productVariantService_
.getRegionPrice(item.variant_id, regionId) .getRegionPrice(item.variant_id, regionId)
.catch(() => undefined) .catch(() => undefined)
@@ -1474,20 +1476,20 @@ class CartService extends BaseService {
} }
if (cart.discounts && cart.discounts.length) { if (cart.discounts && cart.discounts.length) {
const newDiscounts = cart.discounts.map(d => { const newDiscounts = cart.discounts.map((d) => {
if (d.regions.find(({ id }) => id === regionId)) { if (d.regions.find(({ id }) => id === regionId)) {
return d return d
} }
}) })
cart.discounts = newDiscounts.filter(d => !!d) cart.discounts = newDiscounts.filter((d) => !!d)
} }
cart.gift_cards = [] cart.gift_cards = []
if (cart.payment_sessions && cart.payment_sessions.length) { if (cart.payment_sessions && cart.payment_sessions.length) {
await Promise.all( await Promise.all(
cart.payment_sessions.map(ps => cart.payment_sessions.map((ps) =>
this.paymentProviderService_ this.paymentProviderService_
.withTransaction(this.manager_) .withTransaction(this.manager_)
.deleteSession(ps) .deleteSession(ps)
@@ -1505,7 +1507,7 @@ class CartService extends BaseService {
* not found. * not found.
*/ */
async delete(cartId) { async delete(cartId) {
return this.atomicPhase_(async manager => { return this.atomicPhase_(async (manager) => {
const cart = await this.retrieve(cartId, { const cart = await this.retrieve(cartId, {
relations: ["items", "discounts", "payment_sessions"], relations: ["items", "discounts", "payment_sessions"],
}) })
@@ -1539,7 +1541,7 @@ class CartService extends BaseService {
* @return {Promise} resolves to the updated result. * @return {Promise} resolves to the updated result.
*/ */
async setMetadata(cartId, key, value) { async setMetadata(cartId, key, value) {
return this.atomicPhase_(async manager => { return this.atomicPhase_(async (manager) => {
const cartRepo = manager.getCustomRepository(this.cartRepository_) const cartRepo = manager.getCustomRepository(this.cartRepository_)
const validatedId = this.validateId_(cartId) const validatedId = this.validateId_(cartId)
@@ -1573,7 +1575,7 @@ class CartService extends BaseService {
* @return {Promise} resolves to the updated result. * @return {Promise} resolves to the updated result.
*/ */
async deleteMetadata(cartId, key) { async deleteMetadata(cartId, key) {
return this.atomicPhase_(async manager => { return this.atomicPhase_(async (manager) => {
const cartRepo = manager.getCustomRepository(this.cartRepository_) const cartRepo = manager.getCustomRepository(this.cartRepository_)
const validatedId = this.validateId_(cartId) const validatedId = this.validateId_(cartId)