57 lines
2.3 KiB
JavaScript
57 lines
2.3 KiB
JavaScript
const { getOrderById, getOrdersForCustomer, createInternalOrderObject } = require("../lib/bigcommerceRestAPI")
|
|
const { withAccount } = require("../lib/utils")
|
|
|
|
;(function () {
|
|
if (context.user.auth()) return {}
|
|
withAccount((login) => {
|
|
const orderId = context.request().param("id")
|
|
if (orderId) {
|
|
/** @type {Order} */
|
|
// @ts-ignore
|
|
const internalOrder = context.db.find("bigCommerceOrder", {
|
|
filter: { _id: orderId },
|
|
})[0]
|
|
if (internalOrder.customerTibiId !== login.tibiId) {
|
|
if (String(internalOrder?.customerBigCommerceId) !== String(login?.bigCommerceId)) {
|
|
throw {
|
|
message: "You don't have permission to access this order",
|
|
code: 403,
|
|
}
|
|
} else {
|
|
context.db.update("bigCommerceOrder", internalOrder.id, {
|
|
customerTibiId: login.tibiId,
|
|
})
|
|
}
|
|
}
|
|
const order = getOrderById(internalOrder.bigCommerceId)
|
|
order.tibiId = internalOrder.id
|
|
order.status = internalOrder.status
|
|
order.shipments = internalOrder.shipments
|
|
order.statusSetAt = internalOrder.statusSetAt
|
|
throw {
|
|
data: order,
|
|
status: 200,
|
|
}
|
|
} else {
|
|
const orders = getOrdersForCustomer(login.bigCommerceId)
|
|
orders.forEach((order) => {
|
|
let internalOrder = context.db.find("bigCommerceOrder", {
|
|
filter: { bigCommerceId: order.id },
|
|
})[0]
|
|
if (!internalOrder && order.id) {
|
|
const internalOrderReference = createInternalOrderObject(order.id)
|
|
internalOrder = context.db.create("bigCommerceOrder", internalOrderReference)
|
|
} else if (!internalOrder) return
|
|
order.tibiId = internalOrder?.id
|
|
order.status = internalOrder?.status
|
|
order.shipments = internalOrder?.shipments
|
|
order.statusSetAt = internalOrder?.statusSetAt
|
|
})
|
|
throw {
|
|
data: orders,
|
|
status: 200,
|
|
}
|
|
}
|
|
})
|
|
})()
|