Initial commit
This commit is contained in:
47
api/hooks/orderReturnRequest/delete_delete.js
Normal file
47
api/hooks/orderReturnRequest/delete_delete.js
Normal file
@@ -0,0 +1,47 @@
|
||||
const { withAccount } = require("../lib/utils")
|
||||
;(function () {
|
||||
withAccount((login) => {
|
||||
const id = context.request().param("id")
|
||||
if (!id) {
|
||||
throw {
|
||||
message: "id is required",
|
||||
code: 400,
|
||||
}
|
||||
}
|
||||
const existingReturnRequest = context.db.find("orderReturnRequest", {
|
||||
filter: {
|
||||
_id: id,
|
||||
},
|
||||
})[0]
|
||||
if (!existingReturnRequest)
|
||||
throw {
|
||||
message: "Return request not found",
|
||||
code: 404,
|
||||
}
|
||||
const order = context.db.find("bigCommerceOrder", {
|
||||
filter: {
|
||||
bigCommerceId: Number(existingReturnRequest.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,
|
||||
}
|
||||
if (
|
||||
existingReturnRequest.status !== "pending" &&
|
||||
existingReturnRequest.status !== "approved" &&
|
||||
!!existingReturnRequest.status
|
||||
)
|
||||
throw {
|
||||
message: "Return request is not pending or approved",
|
||||
code: 400,
|
||||
}
|
||||
})
|
||||
})()
|
||||
33
api/hooks/orderReturnRequest/delete_return.js
Normal file
33
api/hooks/orderReturnRequest/delete_return.js
Normal file
@@ -0,0 +1,33 @@
|
||||
const { serverBaseURL, logoPath, operatorEmail, noReplyEmail, contactEmail } = require("../config")
|
||||
const { frontendBase } = require("../config-client")
|
||||
const { withAccount } = require("../lib/utils")
|
||||
;(function () {
|
||||
if (context.user.auth()) return
|
||||
withAccount((login) => {
|
||||
const store = {
|
||||
logo: `${serverBaseURL}${logoPath}`,
|
||||
frontendBase,
|
||||
}
|
||||
const html = context.tpl.execute(context.fs.readFile("templates/orderReturnRequestRevoked.html"), {
|
||||
store,
|
||||
})
|
||||
|
||||
context.smtp.sendMail({
|
||||
to: contactEmail,
|
||||
from: noReplyEmail,
|
||||
fromName: "BinKrassDuFass",
|
||||
replyTo: noReplyEmail,
|
||||
subject: "New Order Return Request Cancellation",
|
||||
plain: `Die Bestellung von ${login.email} soll doch nicht zurückgegeben werden.`,
|
||||
})
|
||||
|
||||
context.smtp.sendMail({
|
||||
to: login.email,
|
||||
from: operatorEmail,
|
||||
fromName: "BinKrassDuFass",
|
||||
replyTo: noReplyEmail,
|
||||
subject: "Widerruf deiner Bestellung abgebrochen",
|
||||
html,
|
||||
})
|
||||
})
|
||||
})()
|
||||
41
api/hooks/orderReturnRequest/get_read.js
Normal file
41
api/hooks/orderReturnRequest/get_read.js
Normal file
@@ -0,0 +1,41 @@
|
||||
const { withAccount } = require("../lib/utils")
|
||||
;(function () {
|
||||
const queryParam = context.request().query("orderId")
|
||||
if (!queryParam) {
|
||||
if (context.user.auth()) return
|
||||
throw {
|
||||
message: "orderId is required",
|
||||
code: 400,
|
||||
}
|
||||
}
|
||||
|
||||
withAccount((login) => {
|
||||
const order = context.db.find("bigCommerceOrder", {
|
||||
filter: {
|
||||
bigCommerceId: Number(queryParam),
|
||||
},
|
||||
})[0]
|
||||
if (!order) {
|
||||
throw {
|
||||
message: "Order not found",
|
||||
code: 404,
|
||||
}
|
||||
}
|
||||
if (order.customerBigCommerceId !== login.bigCommerceId) {
|
||||
console.log(
|
||||
"order.customerBigCommerceId",
|
||||
order.customerBigCommerceId,
|
||||
"login.bigCommerceId",
|
||||
login.bigCommerceId
|
||||
)
|
||||
throw {
|
||||
message: "You don't have permission to access this order",
|
||||
code: 403,
|
||||
}
|
||||
}
|
||||
context.filter = {
|
||||
bigCommerceId: order.bigCommerceId,
|
||||
}
|
||||
})
|
||||
return context
|
||||
})()
|
||||
81
api/hooks/orderReturnRequest/post_create.js
Normal file
81
api/hooks/orderReturnRequest/post_create.js
Normal file
@@ -0,0 +1,81 @@
|
||||
const { withAccount } = require("../lib/utils")
|
||||
const { getOrderProductsById } = require("../lib/bigcommerceRestAPI")
|
||||
;(function () {
|
||||
if (context.user.auth()) return
|
||||
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 bigcommerceProducts = getOrderProductsById(context.data.bigCommerceId)
|
||||
const returnRequestProducts = context.data.products
|
||||
// make sure all ids are also in the orderproducts
|
||||
returnRequestProducts.forEach((/** @type {OrderReturnRequestProduct} */ returnRequestProduct) => {
|
||||
const bigcommerceProduct = bigcommerceProducts.find(
|
||||
(bigcommerceProduct) => Number(bigcommerceProduct.id) === returnRequestProduct.productId
|
||||
)
|
||||
if (!bigcommerceProduct)
|
||||
throw {
|
||||
message: "Product not found",
|
||||
code: 404,
|
||||
}
|
||||
if (bigcommerceProduct.quantity < returnRequestProduct.quantity)
|
||||
throw {
|
||||
message: "Quantity exceeds the available quantity",
|
||||
code: 400,
|
||||
}
|
||||
const existingReturnRequests = context.db.find("orderReturnRequest", {
|
||||
filter: {
|
||||
bigCommerceId: context.data.bigCommerceId,
|
||||
$or: [
|
||||
{
|
||||
status: "pending",
|
||||
},
|
||||
{
|
||||
status: "approved",
|
||||
},
|
||||
{
|
||||
status: "refunded",
|
||||
},
|
||||
],
|
||||
},
|
||||
})
|
||||
returnRequestProduct.baseProductId = bigcommerceProduct.product_id
|
||||
let totalExistingQuantity = 0
|
||||
existingReturnRequests.forEach((existingReturnRequest) => {
|
||||
existingReturnRequest.products.forEach(
|
||||
(/** @type {OrderReturnRequestProduct} */ existingReturnRequestProduct) => {
|
||||
if (existingReturnRequestProduct.productId === returnRequestProduct.productId) {
|
||||
totalExistingQuantity += existingReturnRequestProduct.quantity
|
||||
}
|
||||
}
|
||||
)
|
||||
})
|
||||
if (totalExistingQuantity + returnRequestProduct.quantity > bigcommerceProduct.quantity) {
|
||||
throw {
|
||||
message: "Quantity exceeds the available quantity",
|
||||
code: 400,
|
||||
}
|
||||
}
|
||||
})
|
||||
context.data.products = context.data.products.filter(
|
||||
(/** @type {OrderReturnRequestProduct} */ p) => p.quantity > 0
|
||||
)
|
||||
context.data.status = "pending"
|
||||
context.data.email = login.email
|
||||
})
|
||||
return context
|
||||
})()
|
||||
30
api/hooks/orderReturnRequest/post_return.js
Normal file
30
api/hooks/orderReturnRequest/post_return.js
Normal file
@@ -0,0 +1,30 @@
|
||||
const { serverBaseURL, logoPath, operatorEmail, noReplyEmail, contactEmail } = require("../config")
|
||||
const { frontendBase } = require("../config-client")
|
||||
|
||||
;(function () {
|
||||
const store = {
|
||||
logo: `${serverBaseURL}${logoPath}`,
|
||||
frontendBase,
|
||||
}
|
||||
const html = context.tpl.execute(context.fs.readFile("templates/orderReturnRequestApproval.html"), {
|
||||
store,
|
||||
})
|
||||
|
||||
context.smtp.sendMail({
|
||||
to: contactEmail,
|
||||
from: noReplyEmail,
|
||||
fromName: "BinKrassDuFass",
|
||||
replyTo: noReplyEmail,
|
||||
subject: "New Order Return Request",
|
||||
plain: `Die Bestellung von ${context.data.email} soll zurückgegeben werden.`,
|
||||
})
|
||||
|
||||
context.smtp.sendMail({
|
||||
to: context.data.email,
|
||||
from: operatorEmail,
|
||||
fromName: "BinKrassDuFass",
|
||||
replyTo: noReplyEmail,
|
||||
subject: "Widerruf deiner Bestellung",
|
||||
html,
|
||||
})
|
||||
})()
|
||||
51
api/hooks/orderReturnRequest/put_update.js
Normal file
51
api/hooks/orderReturnRequest/put_update.js
Normal file
@@ -0,0 +1,51 @@
|
||||
const { logoPath, noReplyEmail, operatorEmail, serverBaseURL } = require("../config")
|
||||
const { frontendBase } = require("../config-client")
|
||||
const { getOrderById, updateOrderById } = require("../lib/bigcommerceRestAPI")
|
||||
;(function () {
|
||||
const oldOrderReturnRequest = context.db.find("orderReturnRequest", {
|
||||
filter: {
|
||||
_id: context.data.id,
|
||||
},
|
||||
})[0]
|
||||
if (!oldOrderReturnRequest)
|
||||
throw {
|
||||
message: "Order return request not found",
|
||||
code: 404,
|
||||
}
|
||||
if (context.data.status == "approved") {
|
||||
const bigCommerceOrder = getOrderById(context.data.bigCommerceId)
|
||||
delete bigCommerceOrder.productObjs
|
||||
delete bigCommerceOrder.shipping_addressObjs
|
||||
bigCommerceOrder.status_id = 5
|
||||
updateOrderById(bigCommerceOrder.id, { status_id: 5 })
|
||||
}
|
||||
if (oldOrderReturnRequest.status !== context.data.status) {
|
||||
const oRR = context.data
|
||||
const customer = context.db.find("bigCommerceCustomer", {
|
||||
filter: { email: oRR.email },
|
||||
})[0]
|
||||
|
||||
if (!customer) {
|
||||
throw {
|
||||
status: 404,
|
||||
message: "Customer not found",
|
||||
}
|
||||
}
|
||||
const store = {
|
||||
logo: `${serverBaseURL}${logoPath}`,
|
||||
frontendBase,
|
||||
}
|
||||
const html = context.tpl.execute(context.fs.readFile("templates/statusUpdateReturnRequest.html"), {
|
||||
store,
|
||||
})
|
||||
|
||||
context.smtp.sendMail({
|
||||
to: oRR.email,
|
||||
from: operatorEmail,
|
||||
fromName: "BinKrassDuFass",
|
||||
replyTo: noReplyEmail,
|
||||
subject: "Stornierung deiner Bestellung",
|
||||
html,
|
||||
})
|
||||
}
|
||||
})()
|
||||
Reference in New Issue
Block a user