Add redirect url
This commit is contained in:
1 parent
9699d6d1d0
commit
c3898bcea4
5 files changed
+267
-46
No files matched your search
@@ -4,6 +4,20 @@ import bodyParser from "body-parser"
|
||||
export default (container) => {
|
||||
const app = Router()
|
||||
|
||||
app.post("/brightpearl/product", bodyParser.json(), async (req, res) => {
|
||||
const { id, lifecycle_event } = req.body
|
||||
|
||||
if (lifecycle_event === "created") {
|
||||
const eventBus = req.scope.resolve("eventBusService")
|
||||
const brightpearlService = req.scope.resolve("brightpearlService")
|
||||
const bpProduct = await brightpearlService.retrieveProduct(id)
|
||||
|
||||
eventBus.emit("brightpearl-product.created", bpProduct)
|
||||
}
|
||||
|
||||
res.sendStatus(200)
|
||||
})
|
||||
|
||||
app.post("/brightpearl/goods-out", bodyParser.json(), async (req, res) => {
|
||||
const { id, lifecycle_event } = req.body
|
||||
const brightpearlService = req.scope.resolve("brightpearlService")
|
||||
|
||||
@@ -40,12 +40,25 @@ class BrightpearlService extends BaseService {
|
||||
)
|
||||
}
|
||||
|
||||
const client = new Brightpearl({
|
||||
account: this.options.account,
|
||||
url: data.api_domain,
|
||||
auth_type: data.token_type,
|
||||
access_token: data.access_token,
|
||||
})
|
||||
const client = new Brightpearl(
|
||||
{
|
||||
account: this.options.account,
|
||||
url: data.api_domain,
|
||||
auth_type: data.token_type,
|
||||
access_token: data.access_token,
|
||||
},
|
||||
async (client) => {
|
||||
const newAuth = await this.oauthService_.refreshToken(
|
||||
"brightpearl",
|
||||
data.refresh_token
|
||||
)
|
||||
console.log(newAuth)
|
||||
client.updateAuth({
|
||||
auth_type: newAuth.token_type,
|
||||
access_token: newAuth.access_token,
|
||||
})
|
||||
}
|
||||
)
|
||||
|
||||
this.authData_ = data
|
||||
this.brightpearlClient_ = client
|
||||
@@ -542,6 +555,11 @@ class BrightpearlService extends BaseService {
|
||||
})
|
||||
}
|
||||
|
||||
async retrieveProduct(byId) {
|
||||
const client = await this.getClient()
|
||||
return client.products.retrieve(byId)
|
||||
}
|
||||
|
||||
async retrieveProductBySKU(sku) {
|
||||
const client = await this.getClient()
|
||||
return client.products.retrieveBySKU(sku).then((products) => {
|
||||
@@ -559,8 +577,6 @@ class BrightpearlService extends BaseService {
|
||||
const goodsOut = await client.warehouses.retrieveGoodsOutNote(id)
|
||||
const order = await client.orders.retrieve(goodsOut.orderId)
|
||||
|
||||
console.log(order)
|
||||
|
||||
// Combine the line items that we are going to create a fulfillment for
|
||||
const lines = Object.keys(goodsOut.orderRows)
|
||||
.map((key) => {
|
||||
@@ -576,7 +592,7 @@ class BrightpearlService extends BaseService {
|
||||
})
|
||||
.filter((i) => !!i)
|
||||
|
||||
return this.orderService_.createFulfillment(order.ref, lines, {
|
||||
return this.orderService_.createFulfillment(order.externalRef, lines, {
|
||||
goods_out_note: id,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -15,7 +15,8 @@ class BrightpearlOauth extends OauthService {
|
||||
const client_id = "medusa-dev"
|
||||
const client_secret = CLIENT_SECRET
|
||||
const state = randomize("A0", 16)
|
||||
const redirect = "https://localhost:8000/a/oauth/brightpearl"
|
||||
const redirect =
|
||||
"https://staging-tekla-admin.medusa-commerce.com/a/oauth/brightpearl"
|
||||
return {
|
||||
application_name: "brightpearl",
|
||||
display_name: "Brightpearl",
|
||||
@@ -24,11 +25,23 @@ class BrightpearlOauth extends OauthService {
|
||||
}
|
||||
}
|
||||
|
||||
async refreshToken(refreshToken) {
|
||||
const params = {
|
||||
refresh_token: refreshToken,
|
||||
client_id: "medusa-dev",
|
||||
client_secret: CLIENT_SECRET,
|
||||
}
|
||||
|
||||
const data = await Brightpearl.refreshToken(this.account_, params)
|
||||
return data
|
||||
}
|
||||
|
||||
async generateToken(code) {
|
||||
const params = {
|
||||
client_id: "medusa-dev",
|
||||
client_secret: CLIENT_SECRET,
|
||||
redirect: "https://localhost:8000/a/oauth/brightpearl",
|
||||
redirect:
|
||||
"https://staging-tekla-admin.medusa-commerce.com/a/oauth/brightpearl",
|
||||
code,
|
||||
}
|
||||
|
||||
|
||||
@@ -21,16 +21,35 @@ class BrightpearlClient {
|
||||
}).then(({ data }) => data)
|
||||
}
|
||||
|
||||
constructor(options) {
|
||||
static refreshToken(account, data) {
|
||||
const params = {
|
||||
grant_type: "refresh_token",
|
||||
refresh_token: data.refresh_token,
|
||||
client_id: data.client_id,
|
||||
client_secret: data.client_secret,
|
||||
}
|
||||
|
||||
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, onRefreshToken) {
|
||||
this.client_ = axios.create({
|
||||
baseURL: `https://${options.url}/public-api/${options.account}`,
|
||||
headers: {
|
||||
"brightpearl-app-ref": "medusa-dev",
|
||||
"brightpearl-dev-ref": "sebrindom",
|
||||
Authorization: `${options.auth_type} ${options.access_token}`,
|
||||
},
|
||||
})
|
||||
|
||||
this.authType_ = options.auth_type
|
||||
this.token_ = options.access_token
|
||||
this.webhooks = this.buildWebhookEndpoints()
|
||||
this.payments = this.buildPaymentEndpoints()
|
||||
this.warehouses = this.buildWarehouseEndpoints()
|
||||
@@ -38,6 +57,57 @@ class BrightpearlClient {
|
||||
this.addresses = this.buildAddressEndpoints()
|
||||
this.customers = this.buildCustomerEndpoints()
|
||||
this.products = this.buildProductEndpoints()
|
||||
|
||||
this.buildRefreshTokenInterceptor_(onRefreshToken)
|
||||
}
|
||||
|
||||
updateAuth(data) {
|
||||
if (data.auth_type) {
|
||||
this.authType_ = data.auth_type
|
||||
}
|
||||
|
||||
if (data.access_token) {
|
||||
this.token_ = data.access_token
|
||||
}
|
||||
}
|
||||
|
||||
buildRefreshTokenInterceptor_(onRefresh) {
|
||||
this.client_.interceptors.request.use((request) => {
|
||||
const authType = this.authType_
|
||||
const token = this.token_
|
||||
|
||||
if (token) {
|
||||
request.headers["Authorization"] = `${authType} ${token}`
|
||||
}
|
||||
|
||||
return request
|
||||
})
|
||||
|
||||
this.client_.interceptors.response.use(undefined, async (error) => {
|
||||
const response = error.response
|
||||
|
||||
if (response) {
|
||||
if (
|
||||
response.status === 401 &&
|
||||
error.config &&
|
||||
!error.config.__isRetryRequest
|
||||
) {
|
||||
try {
|
||||
await onRefresh(this)
|
||||
} catch (authError) {
|
||||
console.log(authError)
|
||||
// refreshing has failed, but report the original error, i.e. 401
|
||||
return Promise.reject(error)
|
||||
}
|
||||
|
||||
// retry the original request
|
||||
error.config.__isRetryRequest = true
|
||||
return this.client_(error.config)
|
||||
}
|
||||
}
|
||||
|
||||
return Promise.reject(error)
|
||||
})
|
||||
}
|
||||
|
||||
buildSearchResults_(response) {
|
||||
|
||||
Reference in new issue
Block a user