Adds Oauth support to plugins
This commit is contained in:
@@ -25,7 +25,8 @@
|
||||
"cross-env": "^5.2.1",
|
||||
"eslint": "^6.8.0",
|
||||
"jest": "^25.5.2",
|
||||
"medusa-test-utils": "^0.3.0"
|
||||
"medusa-test-utils": "^0.3.0",
|
||||
"prettier": "^2.0.5"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "babel src -d dist",
|
||||
@@ -37,6 +38,7 @@
|
||||
"axios": "^0.19.2",
|
||||
"express": "^4.17.1",
|
||||
"medusa-core-utils": "^0.3.0",
|
||||
"medusa-interfaces": "^0.3.0"
|
||||
"medusa-interfaces": "^0.3.0",
|
||||
"randomatic": "^3.1.1"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,17 @@ import { BaseService } from "medusa-interfaces"
|
||||
import Brightpearl from "../utils/brightpearl"
|
||||
|
||||
class BrightpearlService extends BaseService {
|
||||
constructor({ totalsService, productVariantService, regionService, orderService, discountService }, options) {
|
||||
constructor(
|
||||
{
|
||||
oauthService,
|
||||
totalsService,
|
||||
productVariantService,
|
||||
regionService,
|
||||
orderService,
|
||||
discountService,
|
||||
},
|
||||
options
|
||||
) {
|
||||
super()
|
||||
|
||||
this.options = options
|
||||
@@ -11,92 +21,122 @@ class BrightpearlService extends BaseService {
|
||||
this.orderService_ = orderService
|
||||
this.totalsService_ = totalsService
|
||||
this.discountService_ = discountService
|
||||
this.oauthService_ = oauthService
|
||||
}
|
||||
|
||||
this.brightpearl_ = new Brightpearl({
|
||||
account: options.account,
|
||||
datacenter: options.datacenter,
|
||||
app_ref: options.app_ref,
|
||||
token: options.token
|
||||
async getClient() {
|
||||
if (this.brightpearlClient_) {
|
||||
return this.brightpearlClient_
|
||||
}
|
||||
|
||||
const authData = await this.oauthService_.retrieveByName("brightpearl")
|
||||
const { data } = authData
|
||||
|
||||
if (!data || !data.access_token) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_ALLOWED,
|
||||
"You must authenticate the Brightpearl app in settings before continuing"
|
||||
)
|
||||
}
|
||||
|
||||
const client = new Brightpearl({
|
||||
url: data.api_domain,
|
||||
auth_type: data.token_type,
|
||||
access_token: data.access_token,
|
||||
})
|
||||
|
||||
this.brightpearlClient_ = client
|
||||
return client
|
||||
}
|
||||
|
||||
async verifyWebhooks() {
|
||||
const brightpearl = await this.getClient()
|
||||
const hooks = [
|
||||
{
|
||||
subscribeTo: "product.modified.on-hand-modified",
|
||||
httpMethod: "POST",
|
||||
uriTemplate: `${this.options.backend_url}/brightpearl/inventory-update`,
|
||||
bodyTemplate: '{"account": "${account-code}", "lifecycleEvent": "${lifecycle-event}", "resourceType": "${resource-type}", "id": "${resource-id}" }',
|
||||
bodyTemplate:
|
||||
'{"account": "${account-code}", "lifecycleEvent": "${lifecycle-event}", "resourceType": "${resource-type}", "id": "${resource-id}" }',
|
||||
contentType: "application/json",
|
||||
idSetAccepted: false,
|
||||
}
|
||||
},
|
||||
]
|
||||
|
||||
const installedHooks = await this.brightpearl_.webhooks.list().catch(() => [])
|
||||
const installedHooks = await brightpearl.webhooks.list().catch(() => [])
|
||||
for (const hook of hooks) {
|
||||
const isInstalled = installedHooks.find(i =>
|
||||
i.subscribeTo === hook.subscribeTo &&
|
||||
i.httpMethod === hook.httpMethod &&
|
||||
i.uriTemplate === hook.uriTemplate &&
|
||||
i.bodyTemplate === hook.bodyTemplate &&
|
||||
i.contentType === hook.contentType &&
|
||||
i.idSetAccepted === hook.idSetAccepted
|
||||
const isInstalled = installedHooks.find(
|
||||
(i) =>
|
||||
i.subscribeTo === hook.subscribeTo &&
|
||||
i.httpMethod === hook.httpMethod &&
|
||||
i.uriTemplate === hook.uriTemplate &&
|
||||
i.bodyTemplate === hook.bodyTemplate &&
|
||||
i.contentType === hook.contentType &&
|
||||
i.idSetAccepted === hook.idSetAccepted
|
||||
)
|
||||
|
||||
if (!isInstalled) {
|
||||
await this.brightpearl_.webhooks.create(hook)
|
||||
await brightpearl.webhooks.create(hook)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async syncInventory() {
|
||||
const client = await this.getClient()
|
||||
const variants = await this.productVariantService_.list()
|
||||
return Promise.all(variants.map(async v => {
|
||||
const brightpearlProduct = await this.retrieveProductBySKU(v.sku)
|
||||
if (!brightpearlProduct) {
|
||||
return
|
||||
}
|
||||
return Promise.all(
|
||||
variants.map(async (v) => {
|
||||
const brightpearlProduct = await this.retrieveProductBySKU(v.sku)
|
||||
if (!brightpearlProduct) {
|
||||
return
|
||||
}
|
||||
|
||||
const { productId } = brightpearlProduct
|
||||
const availability = await this.brightpearl_.products.retrieveAvailability(productId)
|
||||
const onHand = availability[productId].total.onHand
|
||||
|
||||
return this.productVariantService_.update(v._id, {
|
||||
inventory_quantity: onHand
|
||||
const { productId } = brightpearlProduct
|
||||
const availability = await client.products.retrieveAvailability(
|
||||
productId
|
||||
)
|
||||
const onHand = availability[productId].total.onHand
|
||||
|
||||
return this.productVariantService_.update(v._id, {
|
||||
inventory_quantity: onHand,
|
||||
})
|
||||
})
|
||||
}))
|
||||
)
|
||||
}
|
||||
|
||||
async updateInventory(productId) {
|
||||
const brightpearlProduct = await this.brightpearl_.products.retrieve(productId)
|
||||
const availability = await this.brightpearl_.products.retrieveAvailability(productId)
|
||||
const client = await this.getClient()
|
||||
const brightpearlProduct = await client.products.retrieve(productId)
|
||||
const availability = await client.products.retrieveAvailability(productId)
|
||||
|
||||
const onHand = availability[productId].total.onHand
|
||||
|
||||
const sku = brightpearlProduct.identity.sku
|
||||
const [ variant ] = await this.productVariantService_.list({ sku })
|
||||
const [variant] = await this.productVariantService_.list({ sku })
|
||||
|
||||
if (variant && variant.manage_inventory) {
|
||||
await this.productVariantService_.update(variant._id, {
|
||||
inventory_quantity: onHand
|
||||
inventory_quantity: onHand,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
async createGoodsOutNote(fromOrder, shipment) {
|
||||
const id = fromOrder.metadata && fromOrder.metadata.brightpearl_sales_order_id
|
||||
const client = await this.getClient()
|
||||
const id =
|
||||
fromOrder.metadata && fromOrder.metadata.brightpearl_sales_order_id
|
||||
|
||||
if (!id) {
|
||||
return
|
||||
}
|
||||
|
||||
const order = await this.brightpearl_.orders.retrieve(id)
|
||||
const productRows = shipment.item_ids.map(id => {
|
||||
const order = await client.orders.retrieve(id)
|
||||
const productRows = shipment.item_ids.map((id) => {
|
||||
const row = order.rows.find(({ externalRef }) => externalRef === id)
|
||||
return {
|
||||
productId: row.productId,
|
||||
salesOrderRowId: row.id,
|
||||
quantity: row.quantity
|
||||
quantity: row.quantity,
|
||||
}
|
||||
})
|
||||
|
||||
@@ -107,36 +147,39 @@ class BrightpearlService extends BaseService {
|
||||
warehouseId: this.options.warehouse,
|
||||
transfer: false,
|
||||
products: productRows,
|
||||
}
|
||||
},
|
||||
],
|
||||
priority: false,
|
||||
}
|
||||
|
||||
return this.brightpearl_.warehouses.createGoodsOutNote(id, goodsOut)
|
||||
return client.warehouses.createGoodsOutNote(id, goodsOut)
|
||||
}
|
||||
|
||||
registerGoodsOutShipped(noteId, shipment) {
|
||||
return this.brightpearl_.warehouses.registerGoodsOutEvent(noteId, {
|
||||
async registerGoodsOutShipped(noteId, shipment) {
|
||||
const client = await this.getClient()
|
||||
return client.warehouses.registerGoodsOutEvent(noteId, {
|
||||
events: [
|
||||
{
|
||||
eventCode: "SHW",
|
||||
occured: new Date(),
|
||||
eventOwnerId: this.options.event_owner,
|
||||
}
|
||||
]
|
||||
},
|
||||
],
|
||||
})
|
||||
}
|
||||
|
||||
registerGoodsOutTrackingNumber(noteId, shipment) {
|
||||
return this.brightpearl_.warehouses.updateGoodsOutNote(noteId, {
|
||||
async registerGoodsOutTrackingNumber(noteId, shipment) {
|
||||
const client = await this.getClient()
|
||||
return client.warehouses.updateGoodsOutNote(noteId, {
|
||||
priority: false,
|
||||
shipping: {
|
||||
reference: shipment.tracking_number,
|
||||
}
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
async createSalesOrder(fromOrder) {
|
||||
const client = await this.getClient()
|
||||
let customer = await this.retrieveCustomerByEmail(fromOrder.email)
|
||||
|
||||
// All sales orders must have a customer
|
||||
@@ -147,7 +190,7 @@ class BrightpearlService extends BaseService {
|
||||
const { shipping_address } = fromOrder
|
||||
const order = {
|
||||
currency: {
|
||||
code: fromOrder.currency_code
|
||||
code: fromOrder.currency_code,
|
||||
},
|
||||
externalRef: fromOrder._id,
|
||||
customer: {
|
||||
@@ -160,7 +203,7 @@ class BrightpearlService extends BaseService {
|
||||
countryIsoCode: shipping_address.country_code,
|
||||
telephone: shipping_address.phone,
|
||||
email: fromOrder.email,
|
||||
}
|
||||
},
|
||||
},
|
||||
delivery: {
|
||||
shippingMethodId: 0,
|
||||
@@ -172,18 +215,22 @@ class BrightpearlService extends BaseService {
|
||||
countryIsoCode: shipping_address.country_code,
|
||||
telephone: shipping_address.phone,
|
||||
email: fromOrder.email,
|
||||
}
|
||||
},
|
||||
},
|
||||
rows: await this.getBrightpearlRows(fromOrder)
|
||||
rows: await this.getBrightpearlRows(fromOrder),
|
||||
}
|
||||
|
||||
return this.brightpearl_.orders.create(order)
|
||||
.then(async salesOrderId => {
|
||||
const order = await this.brightpearl_.orders.retrieve(salesOrderId)
|
||||
const resResult = await this.brightpearl_.warehouses.createReservation(order, this.options.warehouse)
|
||||
return client.orders
|
||||
.create(order)
|
||||
.then(async (salesOrderId) => {
|
||||
const order = await client.orders.retrieve(salesOrderId)
|
||||
const resResult = await client.warehouses.createReservation(
|
||||
order,
|
||||
this.options.warehouse
|
||||
)
|
||||
return salesOrderId
|
||||
})
|
||||
.then(async salesOrderId => {
|
||||
.then(async (salesOrderId) => {
|
||||
const paymentMethod = fromOrder.payment_method
|
||||
const paymentType = "AUTH"
|
||||
const payment = {
|
||||
@@ -200,23 +247,31 @@ class BrightpearlService extends BaseService {
|
||||
if (paymentType === "AUTH") {
|
||||
const today = new Date()
|
||||
const authExpire = today.setDate(today.getDate() + 7)
|
||||
payment.amountAuthorized = await this.totalsService_.getTotal(fromOrder)
|
||||
payment.amountAuthorized = await this.totalsService_.getTotal(
|
||||
fromOrder
|
||||
)
|
||||
payment.authorizationExpiry = new Date(authExpire)
|
||||
} else {
|
||||
// For captured
|
||||
}
|
||||
|
||||
await this.brightpearl_.payments.create(payment)
|
||||
await client.payments.create(payment)
|
||||
|
||||
return salesOrderId
|
||||
})
|
||||
.then((salesOrderId) => {
|
||||
return this.orderService_.setMetadata(fromOrder._id, "brightpearl_sales_order_id", salesOrderId)
|
||||
return this.orderService_.setMetadata(
|
||||
fromOrder._id,
|
||||
"brightpearl_sales_order_id",
|
||||
salesOrderId
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
async createCapturedPayment(fromOrder) {
|
||||
const soId = fromOrder.metadata && fromOrder.metadata.brightpearl_sales_order_id
|
||||
const client = await this.getClient()
|
||||
const soId =
|
||||
fromOrder.metadata && fromOrder.metadata.brightpearl_sales_order_id
|
||||
if (!soId) {
|
||||
return
|
||||
}
|
||||
@@ -234,43 +289,54 @@ class BrightpearlService extends BaseService {
|
||||
paymentType,
|
||||
}
|
||||
|
||||
await this.brightpearl_.payments.create(payment)
|
||||
await client.payments.create(payment)
|
||||
}
|
||||
|
||||
async getBrightpearlRows(fromOrder) {
|
||||
async getBrightpearlRows(fromOrder) {
|
||||
const region = await this.regionService_.retrieve(fromOrder.region_id)
|
||||
const discount = fromOrder.discounts.find(({ discount_rule }) => discount_rule.type !== "free_shipping")
|
||||
const discount = fromOrder.discounts.find(
|
||||
({ discount_rule }) => discount_rule.type !== "free_shipping"
|
||||
)
|
||||
let lineDiscounts = []
|
||||
if (discount) {
|
||||
lineDiscounts = this.discountService_.getLineDiscounts(fromOrder, discount)
|
||||
lineDiscounts = this.discountService_.getLineDiscounts(
|
||||
fromOrder,
|
||||
discount
|
||||
)
|
||||
}
|
||||
|
||||
const lines = await Promise.all(fromOrder.items.map(async item => {
|
||||
const bpProduct = await this.retrieveProductBySKU(item.content.variant.sku)
|
||||
const lines = await Promise.all(
|
||||
fromOrder.items.map(async (item) => {
|
||||
const bpProduct = await this.retrieveProductBySKU(
|
||||
item.content.variant.sku
|
||||
)
|
||||
|
||||
const discount = lineDiscounts.find(l => l.item._id.equals(item._id)) || { amount: 0 }
|
||||
const discount = lineDiscounts.find((l) =>
|
||||
l.item._id.equals(item._id)
|
||||
) || { amount: 0 }
|
||||
|
||||
const row = {}
|
||||
if (bpProduct) {
|
||||
row.productId = bpProduct.productId
|
||||
} else {
|
||||
row.name = item.title
|
||||
}
|
||||
row.net = item.content.unit_price * item.quantity - discount.amount
|
||||
row.tax = row.net * fromOrder.tax_rate
|
||||
row.quantity = item.quantity
|
||||
row.taxCode = region.tax_code
|
||||
row.externalRef = item._id
|
||||
row.nominalCode = this.options.sales_account_code || "4000"
|
||||
const row = {}
|
||||
if (bpProduct) {
|
||||
row.productId = bpProduct.productId
|
||||
} else {
|
||||
row.name = item.title
|
||||
}
|
||||
row.net = item.content.unit_price * item.quantity - discount.amount
|
||||
row.tax = row.net * fromOrder.tax_rate
|
||||
row.quantity = item.quantity
|
||||
row.taxCode = region.tax_code
|
||||
row.externalRef = item._id
|
||||
row.nominalCode = this.options.sales_account_code || "4000"
|
||||
|
||||
return row
|
||||
}))
|
||||
return row
|
||||
})
|
||||
)
|
||||
|
||||
const shippingTotal = this.totalsService_.getShippingTotal(fromOrder)
|
||||
const shippingMethods = fromOrder.shipping_methods
|
||||
if (shippingMethods.length > 0) {
|
||||
lines.push({
|
||||
name: `Shipping: ${shippingMethods.map(m => m.name).join(" + ")}`,
|
||||
name: `Shipping: ${shippingMethods.map((m) => m.name).join(" + ")}`,
|
||||
quantity: 1,
|
||||
net: shippingTotal,
|
||||
tax: shippingTotal * fromOrder.tax_rate,
|
||||
@@ -281,17 +347,19 @@ class BrightpearlService extends BaseService {
|
||||
return lines
|
||||
}
|
||||
|
||||
retrieveCustomerByEmail(email) {
|
||||
return this.brightpearl_.customers.retrieveByEmail(email).then(customers => {
|
||||
async retrieveCustomerByEmail(email) {
|
||||
const client = await this.getClient()
|
||||
return client.customers.retrieveByEmail(email).then((customers) => {
|
||||
if (!customers.length) {
|
||||
return null
|
||||
}
|
||||
return customers.find(c => c.primaryEmail === email)
|
||||
return customers.find((c) => c.primaryEmail === email)
|
||||
})
|
||||
}
|
||||
|
||||
retrieveProductBySKU(sku) {
|
||||
return this.brightpearl_.products.retrieveBySKU(sku).then(products => {
|
||||
async retrieveProductBySKU(sku) {
|
||||
const client = await this.getClient()
|
||||
return client.products.retrieveBySKU(sku).then((products) => {
|
||||
if (!products.length) {
|
||||
return null
|
||||
}
|
||||
@@ -300,21 +368,22 @@ class BrightpearlService extends BaseService {
|
||||
}
|
||||
|
||||
async createCustomer(fromOrder) {
|
||||
const address = await this.brightpearl_.addresses.create({
|
||||
const client = await this.getClient()
|
||||
const address = await client.addresses.create({
|
||||
addressLine1: fromOrder.shipping_address.address_1,
|
||||
addressLine2: fromOrder.shipping_address.address_2,
|
||||
postalCode: fromOrder.shipping_address.postal_code,
|
||||
countryIsoCode: fromOrder.shipping_address.country_code,
|
||||
})
|
||||
|
||||
const customer = await this.brightpearl_.customers.create({
|
||||
const customer = await client.customers.create({
|
||||
firstName: fromOrder.shipping_address.first_name,
|
||||
lastName: fromOrder.shipping_address.last_name,
|
||||
postAddressIds: {
|
||||
DEF: address,
|
||||
BIL: address,
|
||||
DEL: address,
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
return { contactId: customer }
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
import randomize from "randomatic"
|
||||
import { OauthService } from "medusa-interfaces"
|
||||
import Brightpearl from "../utils/brightpearl"
|
||||
|
||||
const CLIENT_SECRET = process.env.BP_CLIENT_SECRET || ""
|
||||
|
||||
class BrightpearlOauth extends OauthService {
|
||||
constructor({}, options) {
|
||||
super()
|
||||
|
||||
this.account_ = options.account
|
||||
}
|
||||
|
||||
static getAppDetails(options) {
|
||||
const client_id = "medusa-dev"
|
||||
const client_secret = CLIENT_SECRET
|
||||
const state = randomize("A0", 16)
|
||||
const redirect = "https://localhost:8000/a/oauth/brightpearl"
|
||||
return {
|
||||
application_name: "brightpearl",
|
||||
display_name: "Brightpearl",
|
||||
install_url: `https://oauth.brightpearl.com/authorize/${options.account}?response_type=code&client_id=${client_id}&redirect_uri=${redirect}&state=${state}`,
|
||||
state,
|
||||
}
|
||||
}
|
||||
|
||||
async generateToken(code) {
|
||||
const params = {
|
||||
client_id: "medusa-dev",
|
||||
client_secret: CLIENT_SECRET,
|
||||
redirect: "https://localhost:8000/a/oauth/brightpearl",
|
||||
code,
|
||||
}
|
||||
|
||||
const data = await Brightpearl.createToken(this.account_, params)
|
||||
return data
|
||||
}
|
||||
}
|
||||
|
||||
export default BrightpearlOauth
|
||||
@@ -1,12 +1,33 @@
|
||||
import axios from "axios"
|
||||
import qs from "querystring"
|
||||
|
||||
class BrightpearlClient {
|
||||
static createToken(account, data) {
|
||||
const params = {
|
||||
grant_type: "authorization_code",
|
||||
code: data.code,
|
||||
client_id: data.client_id,
|
||||
client_secret: data.client_secret,
|
||||
redirect_uri: data.redirect,
|
||||
}
|
||||
|
||||
return axios({
|
||||
url: `https://ws-eu1.brightpearl.com/${account}/oauth/token`,
|
||||
method: "POST",
|
||||
headers: {
|
||||
"content-type": "application/x-www-form-urlencoded",
|
||||
},
|
||||
data: qs.stringify(params),
|
||||
}).then(({ data }) => data)
|
||||
}
|
||||
|
||||
constructor(options) {
|
||||
this.client_ = axios.create({
|
||||
baseURL: `https://${options.datacenter}.brightpearl.com/public-api/${options.account}`,
|
||||
baseURL: `${options.url}/public-api/${options.account}`,
|
||||
headers: {
|
||||
'brightpearl-app-ref': options.app_ref,
|
||||
'brightpearl-account-token': options.token
|
||||
"brightpearl-app-ref": "medusa-dev",
|
||||
"brightpearl-dev-ref": "sebrindom",
|
||||
Authorization: `${options.auth_type} ${options.access_token}`,
|
||||
},
|
||||
})
|
||||
|
||||
@@ -22,7 +43,7 @@ class BrightpearlClient {
|
||||
buildSearchResults_(response) {
|
||||
const { results, metaData } = response
|
||||
// Map the column names to the columns
|
||||
return results.map(resColumns => {
|
||||
return results.map((resColumns) => {
|
||||
const object = {}
|
||||
for (let i = 0; i < resColumns.length; i++) {
|
||||
const fieldName = metaData.columns[i].name
|
||||
@@ -35,99 +56,108 @@ class BrightpearlClient {
|
||||
buildWebhookEndpoints = () => {
|
||||
return {
|
||||
list: () => {
|
||||
return this.client_.request({
|
||||
url: `/integration-service/webhook`,
|
||||
method: "GET",
|
||||
})
|
||||
.then(({ data }) => data.response)
|
||||
return this.client_
|
||||
.request({
|
||||
url: `/integration-service/webhook`,
|
||||
method: "GET",
|
||||
})
|
||||
.then(({ data }) => data.response)
|
||||
},
|
||||
create: (data) => {
|
||||
return this.client_.request({
|
||||
url: `/integration-service/webhook`,
|
||||
method: "POST",
|
||||
data
|
||||
data,
|
||||
})
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
buildPaymentEndpoints = () => {
|
||||
return {
|
||||
create: (payment) => {
|
||||
return this.client_.request({
|
||||
url: `/accounting-service/customer-payment`,
|
||||
method: "POST",
|
||||
data: payment
|
||||
})
|
||||
.then(({ data }) => data.response)
|
||||
}
|
||||
return this.client_
|
||||
.request({
|
||||
url: `/accounting-service/customer-payment`,
|
||||
method: "POST",
|
||||
data: payment,
|
||||
})
|
||||
.then(({ data }) => data.response)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
buildWarehouseEndpoints = () => {
|
||||
return {
|
||||
retrieveReservation: (orderId) => {
|
||||
return this.client_.request({
|
||||
url: `/warehouse-service/order/${orderId}/reservation`,
|
||||
method: "GET",
|
||||
})
|
||||
.then(({ data }) => data.response)
|
||||
return this.client_
|
||||
.request({
|
||||
url: `/warehouse-service/order/${orderId}/reservation`,
|
||||
method: "GET",
|
||||
})
|
||||
.then(({ data }) => data.response)
|
||||
},
|
||||
createGoodsOutNote: (orderId, data) => {
|
||||
return this.client_.request({
|
||||
url: `/warehouse-service/order/${orderId}/goods-note/goods-out`,
|
||||
method: "POST",
|
||||
data,
|
||||
}).then(({ data }) => data.response)
|
||||
return this.client_
|
||||
.request({
|
||||
url: `/warehouse-service/order/${orderId}/goods-note/goods-out`,
|
||||
method: "POST",
|
||||
data,
|
||||
})
|
||||
.then(({ data }) => data.response)
|
||||
},
|
||||
updateGoodsOutNote: (noteId, update) => {
|
||||
return this.client_.request({
|
||||
url: `/warehouse-service/goods-note/goods-out/${noteId}`,
|
||||
method: "PUT",
|
||||
data: update
|
||||
data: update,
|
||||
})
|
||||
},
|
||||
registerGoodsOutEvent: (noteId, data) => {
|
||||
return this.client_.request({
|
||||
url: `/warehouse-service/goods-note/goods-out/${noteId}/event`,
|
||||
method: "POST",
|
||||
data
|
||||
data,
|
||||
})
|
||||
},
|
||||
createReservation: (order, warehouse) => {
|
||||
const id = order.id
|
||||
const data = order.rows.map(r => ({
|
||||
const data = order.rows.map((r) => ({
|
||||
productId: r.productId,
|
||||
salesOrderRowId: r.id,
|
||||
quantity: r.quantity
|
||||
quantity: r.quantity,
|
||||
}))
|
||||
return this.client_.request({
|
||||
url: `/warehouse-service/order/${id}/reservation/warehouse/${warehouse}`,
|
||||
method: "POST",
|
||||
data: {
|
||||
products: data
|
||||
}
|
||||
}).then(({ data }) => data.response)
|
||||
}
|
||||
return this.client_
|
||||
.request({
|
||||
url: `/warehouse-service/order/${id}/reservation/warehouse/${warehouse}`,
|
||||
method: "POST",
|
||||
data: {
|
||||
products: data,
|
||||
},
|
||||
})
|
||||
.then(({ data }) => data.response)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
buildOrderEndpoints = () => {
|
||||
return {
|
||||
retrieve: (orderId) => {
|
||||
return this.client_.request({
|
||||
url: `/order-service/sales-order/${orderId}`,
|
||||
method: "GET",
|
||||
})
|
||||
.then(({ data }) => data.response.length && data.response[0])
|
||||
.catch(err => console.log(err))
|
||||
return this.client_
|
||||
.request({
|
||||
url: `/order-service/sales-order/${orderId}`,
|
||||
method: "GET",
|
||||
})
|
||||
.then(({ data }) => data.response.length && data.response[0])
|
||||
.catch((err) => console.log(err))
|
||||
},
|
||||
create: (order) => {
|
||||
return this.client_.request({
|
||||
url: `/order-service/sales-order`,
|
||||
method: "POST",
|
||||
data: order
|
||||
})
|
||||
return this.client_
|
||||
.request({
|
||||
url: `/order-service/sales-order`,
|
||||
method: "POST",
|
||||
data: order,
|
||||
})
|
||||
.then(({ data }) => data.response)
|
||||
},
|
||||
}
|
||||
@@ -136,61 +166,66 @@ class BrightpearlClient {
|
||||
buildAddressEndpoints = () => {
|
||||
return {
|
||||
create: (address) => {
|
||||
return this.client_.request({
|
||||
url: `/contact-service/postal-address`,
|
||||
method: "POST",
|
||||
data: address
|
||||
})
|
||||
.then(({ data }) => data.response)
|
||||
}
|
||||
return this.client_
|
||||
.request({
|
||||
url: `/contact-service/postal-address`,
|
||||
method: "POST",
|
||||
data: address,
|
||||
})
|
||||
.then(({ data }) => data.response)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
buildProductEndpoints = () => {
|
||||
buildProductEndpoints = () => {
|
||||
return {
|
||||
retrieveAvailability: productId => {
|
||||
return this.client_.request({
|
||||
url: `/warehouse-service/product-availability/${productId}`,
|
||||
})
|
||||
.then(({ data }) => data.response && data.response)
|
||||
retrieveAvailability: (productId) => {
|
||||
return this.client_
|
||||
.request({
|
||||
url: `/warehouse-service/product-availability/${productId}`,
|
||||
})
|
||||
.then(({ data }) => data.response && data.response)
|
||||
},
|
||||
retrieve: (productId) => {
|
||||
return this.client_.request({
|
||||
url: `/product-service/product/${productId}`,
|
||||
})
|
||||
.then(({ data }) => data.response && data.response[0])
|
||||
return this.client_
|
||||
.request({
|
||||
url: `/product-service/product/${productId}`,
|
||||
})
|
||||
.then(({ data }) => data.response && data.response[0])
|
||||
},
|
||||
retrieveBySKU: (sku) => {
|
||||
return this.client_.request({
|
||||
url: `/product-service/product-search?SKU=${sku}`,
|
||||
})
|
||||
.then(({ data }) => {
|
||||
return this.buildSearchResults_(data.response)
|
||||
})
|
||||
}
|
||||
return this.client_
|
||||
.request({
|
||||
url: `/product-service/product-search?SKU=${sku}`,
|
||||
})
|
||||
.then(({ data }) => {
|
||||
return this.buildSearchResults_(data.response)
|
||||
})
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
buildCustomerEndpoints = () => {
|
||||
return {
|
||||
retrieveByEmail: (email) => {
|
||||
return this.client_.request({
|
||||
url: `/contact-service/contact-search?primaryEmail=${email}`,
|
||||
})
|
||||
.then(({data }) => {
|
||||
return this.buildSearchResults_(data.response)
|
||||
})
|
||||
return this.client_
|
||||
.request({
|
||||
url: `/contact-service/contact-search?primaryEmail=${email}`,
|
||||
})
|
||||
.then(({ data }) => {
|
||||
return this.buildSearchResults_(data.response)
|
||||
})
|
||||
},
|
||||
|
||||
create: (customerData) => {
|
||||
return this.client_.request({
|
||||
url: `/contact-service/contact`,
|
||||
method: "POST",
|
||||
data: customerData
|
||||
})
|
||||
return this.client_
|
||||
.request({
|
||||
url: `/contact-service/contact`,
|
||||
method: "POST",
|
||||
data: customerData,
|
||||
})
|
||||
.then(({ data }) => data.response)
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,8 @@ exports["default"] = void 0;
|
||||
|
||||
var _axios = _interopRequireDefault(require("axios"));
|
||||
|
||||
var _querystring = _interopRequireDefault(require("querystring"));
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
||||
|
||||
function _instanceof(left, right) { if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) { return !!right[Symbol.hasInstance](left); } else { return left instanceof right; } }
|
||||
@@ -20,6 +22,30 @@ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _d
|
||||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
||||
|
||||
var BrightpearlClient = /*#__PURE__*/function () {
|
||||
_createClass(BrightpearlClient, null, [{
|
||||
key: "createToken",
|
||||
value: function createToken(account, data) {
|
||||
var params = {
|
||||
grant_type: "authorization_code",
|
||||
code: data.code,
|
||||
client_id: data.client_id,
|
||||
client_secret: data.client_secret,
|
||||
redirect_uri: data.redirect
|
||||
};
|
||||
return (0, _axios["default"])({
|
||||
url: "https://ws-eu1.brightpearl.com/".concat(account, "/oauth/token"),
|
||||
method: "POST",
|
||||
headers: {
|
||||
"content-type": "application/x-www-form-urlencoded"
|
||||
},
|
||||
data: _querystring["default"].stringify(params)
|
||||
}).then(function (_ref) {
|
||||
var data = _ref.data;
|
||||
return data;
|
||||
});
|
||||
}
|
||||
}]);
|
||||
|
||||
function BrightpearlClient(options) {
|
||||
var _this = this;
|
||||
|
||||
@@ -31,8 +57,8 @@ var BrightpearlClient = /*#__PURE__*/function () {
|
||||
return _this.client_.request({
|
||||
url: "/integration-service/webhook",
|
||||
method: "GET"
|
||||
}).then(function (_ref) {
|
||||
var data = _ref.data;
|
||||
}).then(function (_ref2) {
|
||||
var data = _ref2.data;
|
||||
return data.response;
|
||||
});
|
||||
},
|
||||
@@ -53,8 +79,8 @@ var BrightpearlClient = /*#__PURE__*/function () {
|
||||
url: "/accounting-service/customer-payment",
|
||||
method: "POST",
|
||||
data: payment
|
||||
}).then(function (_ref2) {
|
||||
var data = _ref2.data;
|
||||
}).then(function (_ref3) {
|
||||
var data = _ref3.data;
|
||||
return data.response;
|
||||
});
|
||||
}
|
||||
@@ -67,8 +93,8 @@ var BrightpearlClient = /*#__PURE__*/function () {
|
||||
return _this.client_.request({
|
||||
url: "/warehouse-service/order/".concat(orderId, "/reservation"),
|
||||
method: "GET"
|
||||
}).then(function (_ref3) {
|
||||
var data = _ref3.data;
|
||||
}).then(function (_ref4) {
|
||||
var data = _ref4.data;
|
||||
return data.response;
|
||||
});
|
||||
},
|
||||
@@ -77,8 +103,8 @@ var BrightpearlClient = /*#__PURE__*/function () {
|
||||
url: "/warehouse-service/order/".concat(orderId, "/goods-note/goods-out"),
|
||||
method: "POST",
|
||||
data: data
|
||||
}).then(function (_ref4) {
|
||||
var data = _ref4.data;
|
||||
}).then(function (_ref5) {
|
||||
var data = _ref5.data;
|
||||
return data.response;
|
||||
});
|
||||
},
|
||||
@@ -111,8 +137,8 @@ var BrightpearlClient = /*#__PURE__*/function () {
|
||||
data: {
|
||||
products: data
|
||||
}
|
||||
}).then(function (_ref5) {
|
||||
var data = _ref5.data;
|
||||
}).then(function (_ref6) {
|
||||
var data = _ref6.data;
|
||||
return data.response;
|
||||
});
|
||||
}
|
||||
@@ -125,8 +151,8 @@ var BrightpearlClient = /*#__PURE__*/function () {
|
||||
return _this.client_.request({
|
||||
url: "/order-service/sales-order/".concat(orderId),
|
||||
method: "GET"
|
||||
}).then(function (_ref6) {
|
||||
var data = _ref6.data;
|
||||
}).then(function (_ref7) {
|
||||
var data = _ref7.data;
|
||||
return data.response.length && data.response[0];
|
||||
})["catch"](function (err) {
|
||||
return console.log(err);
|
||||
@@ -137,8 +163,8 @@ var BrightpearlClient = /*#__PURE__*/function () {
|
||||
url: "/order-service/sales-order",
|
||||
method: "POST",
|
||||
data: order
|
||||
}).then(function (_ref7) {
|
||||
var data = _ref7.data;
|
||||
}).then(function (_ref8) {
|
||||
var data = _ref8.data;
|
||||
return data.response;
|
||||
});
|
||||
}
|
||||
@@ -152,8 +178,8 @@ var BrightpearlClient = /*#__PURE__*/function () {
|
||||
url: "/contact-service/postal-address",
|
||||
method: "POST",
|
||||
data: address
|
||||
}).then(function (_ref8) {
|
||||
var data = _ref8.data;
|
||||
}).then(function (_ref9) {
|
||||
var data = _ref9.data;
|
||||
return data.response;
|
||||
});
|
||||
}
|
||||
@@ -165,24 +191,24 @@ var BrightpearlClient = /*#__PURE__*/function () {
|
||||
retrieveAvailability: function retrieveAvailability(productId) {
|
||||
return _this.client_.request({
|
||||
url: "/warehouse-service/product-availability/".concat(productId)
|
||||
}).then(function (_ref9) {
|
||||
var data = _ref9.data;
|
||||
}).then(function (_ref10) {
|
||||
var data = _ref10.data;
|
||||
return data.response && data.response;
|
||||
});
|
||||
},
|
||||
retrieve: function retrieve(productId) {
|
||||
return _this.client_.request({
|
||||
url: "/product-service/product/".concat(productId)
|
||||
}).then(function (_ref10) {
|
||||
var data = _ref10.data;
|
||||
}).then(function (_ref11) {
|
||||
var data = _ref11.data;
|
||||
return data.response && data.response[0];
|
||||
});
|
||||
},
|
||||
retrieveBySKU: function retrieveBySKU(sku) {
|
||||
return _this.client_.request({
|
||||
url: "/product-service/product-search?SKU=".concat(sku)
|
||||
}).then(function (_ref11) {
|
||||
var data = _ref11.data;
|
||||
}).then(function (_ref12) {
|
||||
var data = _ref12.data;
|
||||
return _this.buildSearchResults_(data.response);
|
||||
});
|
||||
}
|
||||
@@ -194,8 +220,8 @@ var BrightpearlClient = /*#__PURE__*/function () {
|
||||
retrieveByEmail: function retrieveByEmail(email) {
|
||||
return _this.client_.request({
|
||||
url: "/contact-service/contact-search?primaryEmail=".concat(email)
|
||||
}).then(function (_ref12) {
|
||||
var data = _ref12.data;
|
||||
}).then(function (_ref13) {
|
||||
var data = _ref13.data;
|
||||
return _this.buildSearchResults_(data.response);
|
||||
});
|
||||
},
|
||||
@@ -204,8 +230,8 @@ var BrightpearlClient = /*#__PURE__*/function () {
|
||||
url: "/contact-service/contact",
|
||||
method: "POST",
|
||||
data: customerData
|
||||
}).then(function (_ref13) {
|
||||
var data = _ref13.data;
|
||||
}).then(function (_ref14) {
|
||||
var data = _ref14.data;
|
||||
return data.response;
|
||||
});
|
||||
}
|
||||
@@ -213,10 +239,11 @@ var BrightpearlClient = /*#__PURE__*/function () {
|
||||
});
|
||||
|
||||
this.client_ = _axios["default"].create({
|
||||
baseURL: "https://".concat(options.datacenter, ".brightpearl.com/public-api/").concat(options.account),
|
||||
baseURL: "".concat(options.url, "/public-api/").concat(options.account),
|
||||
headers: {
|
||||
'brightpearl-app-ref': options.app_ref,
|
||||
'brightpearl-account-token': options.token
|
||||
"brightpearl-app-ref": "medusa-dev",
|
||||
"brightpearl-dev-ref": "sebrindom",
|
||||
Authorization: "".concat(options.auth_type, " ").concat(options.access_token)
|
||||
}
|
||||
});
|
||||
this.webhooks = this.buildWebhookEndpoints();
|
||||
|
||||
Reference in New Issue
Block a user