Skip to content

Commit

Permalink
Merge pull request #103 from BudsiesApp/32844-fix-orders-handling
Browse files Browse the repository at this point in the history
#32844: Fix orders handling for logged in customers
  • Loading branch information
gorbunovav authored Aug 22, 2024
2 parents 73d1dd0 + 1f7c4c7 commit 7e1e63d
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 27 deletions.
8 changes: 7 additions & 1 deletion src/api/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,14 @@ export default ({ config, db }) => resource({
apiStatus(res, e, 500);
}
} else {
let customerToken = null

if (req.query && req.query.token) {
customerToken = req.query.token
}

const orderProxy = _getProxy(req, config)
orderProxy.create(req.body).then((result) => {
orderProxy.create(req.body, customerToken).then((result) => {
apiStatus(res, result, 200);
}).catch(err => {
apiError(res, err);
Expand Down
2 changes: 1 addition & 1 deletion src/platform/abstract/order.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class AbstractOrderProxy {
create (orderData) {
create (orderData, customerToken = null) {
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/platform/magento1/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class OrderProxy extends AbstractOrderProxy {
this.api = Magento1Client(multiStoreConfig(config.magento1.api, req));
}

create (orderData) {
create (orderData, customerToken = null) {
return this.api.order.create(orderData);
}
}
Expand Down
37 changes: 19 additions & 18 deletions src/platform/magento2/o2m.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ if (fs.existsSync('../../models/order.schema.extension.json')) {
const validate = ajv.compile(merge(orderSchema, orderSchemaExtension));

function isNumeric (val) {
return Number(parseFloat(val)).toString() === val;
return Number(parseFloat(val)).toString() === val.toString();
}

/**
Expand All @@ -30,7 +30,7 @@ function isNumeric (val) {
* @param {Object} config global CLI configuration
* @param {Function} done callback - @example done(new Error()) - to acknowledge problems
*/
function processSingleOrder (orderData, config, job, done, logger = console) {
function processSingleOrder (orderData, config, job, done, customerToken = null, logger = console) {
const TOTAL_STEPS = 4;
const THREAD_ID = 'ORD:' + (job ? job.id : 1) + ' - '; // job id
let currentStep = 1;
Expand Down Expand Up @@ -69,7 +69,8 @@ function processSingleOrder (orderData, config, job, done, logger = console) {
if (job) job.progress(currentStep++, TOTAL_STEPS);
return;
}
let isThisAuthOrder = parseInt(orderData.user_id) > 0

let isAdminRequest = parseInt(orderData.user_id) > 0 && customerToken === null
const userId = orderData.user_id

let apiConfig = config.magento2.api
Expand All @@ -84,13 +85,13 @@ function processSingleOrder (orderData, config, job, done, logger = console) {
const api = Magento2Client(apiConfig);

logger.info('> Order Id', orderData.order_id)
logger.info('> Is order authorized?', isThisAuthOrder)
logger.info('> Is order authorized?', isAdminRequest)
logger.info('> User Id', userId)

let cartId = orderData.cart_id
const cartIdPrepare = isThisAuthOrder ? api.cart.create(null, userId) : (cartId ? new Promise((resolve, reject) => {
const cartIdPrepare = isAdminRequest ? api.cart.create(null, userId) : (cartId ? new Promise((resolve, reject) => {
resolve(cartId)
}) : api.cart.create(null))
}) : api.cart.create(customerToken))

logger.info(THREAD_ID + '> Cart Id', cartId)

Expand All @@ -100,7 +101,7 @@ function processSingleOrder (orderData, config, job, done, logger = console) {
logger.info(THREAD_ID + '< Cart Id', cartId)

// load current cart from the Magento to synchronize elements
api.cart.pull(null, cartId, null, isThisAuthOrder).then((serverItems) => {
api.cart.pull(customerToken, cartId, null, isAdminRequest).then((serverItems) => {
const clientItems = orderData.products
const syncPromises = []

Expand All @@ -111,21 +112,21 @@ function processSingleOrder (orderData, config, job, done, logger = console) {
const serverItem = serverItems.find(itm => productsEquals(itm, clientItem))
if (!serverItem) {
logger.info(THREAD_ID + '< No server item for ' + clientItem.sku)
syncPromises.push(api.cart.update(null, cartId, { // use magento API
syncPromises.push(api.cart.update(customerToken, cartId, { // use magento API
sku: clientItem.parentSku && config.cart.setConfigurableProductOptions ? clientItem.parentSku : clientItem.sku,
qty: clientItem.qty,
product_option: clientItem.product_option,
quote_id: cartId
}, isThisAuthOrder))
}, isAdminRequest))
} else if (serverItem.qty !== clientItem.qty) {
logger.info(THREAD_ID + '< Wrong qty for ' + clientItem.sku, clientItem.qty, serverItem.qty)
syncPromises.push(api.cart.update(null, cartId, { // use magento API
syncPromises.push(api.cart.update(customerToken, cartId, { // use magento API
sku: clientItem.parentSku && config.cart.setConfigurableProductOptions ? clientItem.parentSku : clientItem.sku,
qty: clientItem.qty,
product_option: clientItem.product_option,
item_id: serverItem.item_id,
quote_id: cartId
}, isThisAuthOrder))
}, isAdminRequest))
} else {
logger.info(THREAD_ID + '< Server and client items synced for ' + clientItem.sku) // here we need just update local item_id
}
Expand All @@ -136,10 +137,10 @@ function processSingleOrder (orderData, config, job, done, logger = console) {
const clientItem = clientItems.find(itm => productsEquals(itm, serverItem))
if (!clientItem) {
logger.info(THREAD_ID + '< No client item for ' + serverItem.sku + ', removing from server cart') // use magento API
syncPromises.push(api.cart.delete(null, cartId, { // delete server side item if not present if client's cart
syncPromises.push(api.cart.delete(customerToken, cartId, { // delete server side item if not present if client's cart
sku: serverItem.sku,
item_id: serverItem.item_id
}, isThisAuthOrder))
}, isAdminRequest))
}
}
}
Expand Down Expand Up @@ -232,20 +233,20 @@ function processSingleOrder (orderData, config, job, done, logger = console) {
}

logger.info(THREAD_ID + '< Billing info', billingAddressInfo)
api.cart.billingAddress(null, cartId, billingAddressInfo, isThisAuthOrder).then((result) => {
api.cart.billingAddress(customerToken, cartId, billingAddressInfo, isAdminRequest).then((result) => {
logger.info(THREAD_ID + '< Billing address assigned', result)
logger.info(THREAD_ID + '< Shipping info', shippingAddressInfo)
api.cart.shippingInformation(null, cartId, shippingAddressInfo, isThisAuthOrder).then((result) => {
api.cart.shippingInformation(customerToken, cartId, shippingAddressInfo, isAdminRequest).then((result) => {
logger.info(THREAD_ID + '< Shipping address assigned', result)

if (job) job.progress(currentStep++, TOTAL_STEPS);

api.cart.order(null, cartId, {
api.cart.order(customerToken, cartId, {
'paymentMethod': {
'method': orderData.addressInformation.payment_method_code,
'additional_data': orderData.addressInformation.payment_method_additional
}
}, isThisAuthOrder).then(result => {
}, isAdminRequest).then(result => {
logger.info(THREAD_ID, result)
if (job) job.progress(currentStep++, TOTAL_STEPS);

Expand Down Expand Up @@ -301,7 +302,7 @@ function processSingleOrder (orderData, config, job, done, logger = console) {
cartIdPrepare.then(processCart).catch((error) => { // cannot create a quote for specific user, so bypass by placing anonymous order
logger.error(THREAD_ID, error)
logger.info('< Bypassing to anonymous order')
isThisAuthOrder = false
isAdminRequest = false

if (isNumeric(cartId)) { // we have numeric id - assigned to the user provided
api.cart.create(null, null).then((result) => {
Expand Down
18 changes: 12 additions & 6 deletions src/platform/magento2/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,21 @@ class OrderProxy extends AbstractOrderProxy {
this.api = Magento2Client(multiStoreConfig(config.magento2.api, req));
}

create (orderData) {
create (orderData, customerToken) {
const inst = this
return new Promise((resolve, reject) => {
try {
processSingleOrder(orderData, inst.config, null, (error, result) => {
console.log(error)
if (error) reject(error)
resolve(result)
})
processSingleOrder(
orderData,
inst.config,
null,
(error, result) => {
console.log(error)
if (error) reject(error)
resolve(result)
},
customerToken
)
} catch (e) {
reject(e)
}
Expand Down

0 comments on commit 7e1e63d

Please sign in to comment.