56 lines
2.0 KiB
JavaScript
56 lines
2.0 KiB
JavaScript
const { getCustomerAddressById, getCustomerAddresses, getCustomerById } = require("../lib/bigcommerceRestAPI")
|
|
const { withAccount } = require("../lib/utils")
|
|
;(function () {
|
|
console.log("wtf")
|
|
if (context.user.auth()) return
|
|
const request = context.request()
|
|
const queryUsername = request.query("username")
|
|
if (queryUsername) {
|
|
const user = context.db.find("bigCommerceCustomer", {
|
|
filter: { username: queryUsername },
|
|
})[0]
|
|
if (!user) {
|
|
throw { status: 404, error: "customer not found" }
|
|
}
|
|
throw {
|
|
data: {
|
|
id: user.id,
|
|
username: user.username,
|
|
socialMediaAccounts: user.socialMediaAccounts,
|
|
personalRecords: user.personalRecords,
|
|
},
|
|
status: 200,
|
|
}
|
|
}
|
|
const foreign = request.query("foreign")
|
|
if (foreign) {
|
|
const id = request.param("id")
|
|
const user = context.db.find("bigCommerceCustomer", {
|
|
filter: { _id: id },
|
|
})[0]
|
|
if (!user) {
|
|
throw { status: 404, error: "customer not found" }
|
|
}
|
|
throw {
|
|
data: {
|
|
id: user.id,
|
|
username: user.username,
|
|
socialMediaAccounts: user.socialMediaAccounts,
|
|
personalRecords: user.personalRecords,
|
|
},
|
|
status: 200,
|
|
}
|
|
}
|
|
withAccount((loginClaims) => {
|
|
const queryAddressId = request.query("address")
|
|
const queryAddresses = request.query("addresses")
|
|
|
|
if (queryAddressId) {
|
|
throw { data: getCustomerAddressById(loginClaims.bigCommerceId, Number(queryAddressId)), status: 200 }
|
|
} else if (queryAddresses) {
|
|
throw { data: getCustomerAddresses(loginClaims.bigCommerceId), status: 200 }
|
|
} else throw { data: getCustomerById(loginClaims.bigCommerceId), status: 200 }
|
|
})
|
|
throw { status: 401, error: "unauthorized", log: false }
|
|
})()
|