Initial commit
This commit is contained in:
45
api/hooks/orderRevokeRequest/post_create.js
Normal file
45
api/hooks/orderRevokeRequest/post_create.js
Normal file
@@ -0,0 +1,45 @@
|
||||
const { withAccount } = require("../lib/utils")
|
||||
const { getPrintfulOrder, cancelPrintfulOrder } = require("../lib/printfulRestAPI")
|
||||
;(function () {
|
||||
withAccount((login) => {
|
||||
const order = context.db.find("bigCommerceOrder", {
|
||||
filter: {
|
||||
bigCommerceId: Number(context.data.bigCommerceId),
|
||||
},
|
||||
})[0]
|
||||
if (!order)
|
||||
throw {
|
||||
message: "Order not found",
|
||||
code: 404,
|
||||
}
|
||||
|
||||
if (order.customerBigCommerceId !== login.bigCommerceId)
|
||||
throw {
|
||||
message: "You don't have permission to access this order",
|
||||
code: 403,
|
||||
}
|
||||
const existingRevokeRequests = context.db.find("orderRevokeRequest", {
|
||||
filter: {
|
||||
bigCommerceId: context.data.bigCommerceId,
|
||||
},
|
||||
})
|
||||
if (existingRevokeRequests.length > 0)
|
||||
throw {
|
||||
message: "Revoke request already exists",
|
||||
code: 400,
|
||||
}
|
||||
if (!!order.status && order.status !== "draft") {
|
||||
throw {
|
||||
message: "Order is already in process",
|
||||
code: 400,
|
||||
}
|
||||
}
|
||||
const printfulOrder = getPrintfulOrder(order.bigCommerceId)
|
||||
|
||||
cancelPrintfulOrder(context.data.bigCommerceId)
|
||||
context.data.status = "pending"
|
||||
context.data.email = login.email
|
||||
context.data.printfulId = printfulOrder.id
|
||||
})
|
||||
return context
|
||||
})()
|
||||
Reference in New Issue
Block a user