Skip to content

Commit

Permalink
fix(update subscription): do not update subscriptions with a specifie…
Browse files Browse the repository at this point in the history
…d quantity
  • Loading branch information
wisley7l committed Nov 24, 2023
1 parent fb87d1c commit 6ccb140
Showing 1 changed file with 103 additions and 101 deletions.
204 changes: 103 additions & 101 deletions functions/routes/ecom/webhook.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,121 +256,123 @@ exports.post = async ({ appSdk, admin }, req, res) => {
let error
for (let i = 0; i < galaxPaySubscriptions.length; i++) {
const subscription = galaxPaySubscriptions[i]
try {
const order = await findOrderById(appSdk, storeId, subscription.myId, auth)
.catch(console.error)

const product = await getProductsById(appSdk, storeId, resourceId, auth)
.catch(console.error)

if (order && product) {
const docSubscription = await getDocSubscription(order._id, collectionSubscription)

order.items.forEach(async (orderItem) => {
let dimensions = product?.dimensions
let weight = product?.weight

if (orderItem.product_id === product._id) {
if (orderItem.variation_id) {
const variation = product.variations.find(itemFind => itemFind.sku === orderItem.sku)
let quantity = orderItem.quantity
if (variation && variation.quantity < orderItem.quantity) {
quantity = variation.quantity
} else if (!variation) {
quantity = 0
if (subscription.quantity === 0) {
try {
const order = await findOrderById(appSdk, storeId, subscription.myId, auth)
.catch(console.error)

const product = await getProductsById(appSdk, storeId, resourceId, auth)
.catch(console.error)

if (order && product) {
const docSubscription = await getDocSubscription(order._id, collectionSubscription)

order.items.forEach(async (orderItem) => {
let dimensions = product?.dimensions
let weight = product?.weight

if (orderItem.product_id === product._id) {
if (orderItem.variation_id) {
const variation = product.variations.find(itemFind => itemFind.sku === orderItem.sku)
let quantity = orderItem.quantity
if (variation && variation.quantity < orderItem.quantity) {
quantity = variation.quantity
} else if (!variation) {
quantity = 0
}
if (variation.dimensions) {
dimensions = variation.dimensions
}
if (variation.weight) {
weight = variation.weight
}
const newItem = {
sku: variation.sku,
price: ecomUtils.price({ ...product, ...variation }),
quantity
}
// update item, price or quantity
checkItemsAndRecalculeteOrder(order.amount, order.items, docSubscription.plan, newItem)
} else {
const newItem = {
sku: product.sku,
price: ecomUtils.price(product),
quantity: product.quantity < orderItem.quantity ? product.quantity : orderItem.quantity
}
// update item, price or quantity
checkItemsAndRecalculeteOrder(order.amount, order.items, docSubscription.plan, newItem)
}
if (variation.dimensions) {
dimensions = variation.dimensions
}
if (variation.weight) {
weight = variation.weight
}
const newItem = {
sku: variation.sku,
price: ecomUtils.price({ ...product, ...variation }),
quantity
}
// update item, price or quantity
checkItemsAndRecalculeteOrder(order.amount, order.items, docSubscription.plan, newItem)
} else {
const newItem = {
sku: product.sku,
price: ecomUtils.price(product),
quantity: product.quantity < orderItem.quantity ? product.quantity : orderItem.quantity
}
// update item, price or quantity
checkItemsAndRecalculeteOrder(order.amount, order.items, docSubscription.plan, newItem)
orderItem.dimensions = dimensions
orderItem.weight = weight
}
orderItem.dimensions = dimensions
orderItem.weight = weight
}
})
})

// Calculates new value
const { value: newSubscriptionValue } = await checkItemsAndRecalculeteOrder(
order.amount,
order.items,
docSubscription.plan,
null,
order.shipping_lines[0],
storeId,
appSdk,
auth
)
if (newSubscriptionValue && newSubscriptionValue !== subscription.value) {
await addItemsAndValueSubscriptionDoc(
collectionSubscription,
// Calculates new value
const { value: newSubscriptionValue } = await checkItemsAndRecalculeteOrder(
order.amount,
order.items,
newSubscriptionValue,
order._id
docSubscription.plan,
null,
order.shipping_lines[0],
storeId,
appSdk,
auth
)
try {
await galaxpayAxios.preparing
await updateValueSubscriptionGalaxpay(
galaxpayAxios,
order._id,
newSubscriptionValue,
subscription.value
)
} catch (err) {
console.error(err)
// back firebase document
updateDocSubscription(
if (newSubscriptionValue && newSubscriptionValue !== subscription.value) {
await addItemsAndValueSubscriptionDoc(
collectionSubscription,
docSubscription,
order.amount,
order.items,
newSubscriptionValue,
order._id
)

throw err
}
//
let queryString = `subscriptionGalaxPayIds=${subscription.galaxPayId}`
queryString += '&status=notSend,pendingBoleto,pendingPix&order=payday.desc'

try {
const { data: { Transactions } } = await galaxpayAxios.axios
.get(`/transactions?startAt=0&limit=100&${queryString}`)
let i = 0
while (i < Transactions?.length) {
const transaction = Transactions[i]
if (transaction.value !== newSubscriptionValue && transaction.galaxPayId !== docSubscription.transactionId) {
await updateTransactionGalaxpay(galaxpayAxios, transaction.galaxPayId, newSubscriptionValue)
.catch(console.error)
try {
await galaxpayAxios.preparing
await updateValueSubscriptionGalaxpay(
galaxpayAxios,
order._id,
newSubscriptionValue,
subscription.value
)
} catch (err) {
console.error(err)
// back firebase document
updateDocSubscription(
collectionSubscription,
docSubscription,
order._id
)

throw err
}
//
let queryString = `subscriptionGalaxPayIds=${subscription.galaxPayId}`
queryString += '&status=notSend,pendingBoleto,pendingPix&order=payday.desc'

try {
const { data: { Transactions } } = await galaxpayAxios.axios
.get(`/transactions?startAt=0&limit=100&${queryString}`)
let i = 0
while (i < Transactions?.length) {
const transaction = Transactions[i]
if (transaction.value !== newSubscriptionValue && transaction.galaxPayId !== docSubscription.transactionId) {
await updateTransactionGalaxpay(galaxpayAxios, transaction.galaxPayId, newSubscriptionValue)
.catch(console.error)
}
i += 1
}
i += 1
} catch (err) {
console.error(err)
}
} catch (err) {
console.error(err)
}
}
} catch (err) {
console.error(`Error trying to update signature #${subscription.myId}`)
if (err?.response) {
console.error('Error: ', err?.response, ' <')
}
error = err
}
} catch (err) {
console.error(`Error trying to update signature #${subscription.myId}`)
if (err?.response) {
console.error('Error: ', err?.response, ' <')
}
error = err
}
} //

Expand Down

0 comments on commit 6ccb140

Please sign in to comment.