fix: cron jobs and brightpearl auto refresh (#107)

* fix: adds automatic refresh of tokens

* fix: send options to plugin loaders

* fix: adds README optional inventory sync
This commit is contained in:
Sebastian Rindom authored and GitHub committed 2020-09-10 13:33:38 +02:00
1 parent e3d8eea3cd
commit c7bd7838aa
11 files changed
+109 -580

No files matched your search

@@ -1,21 +1,24 @@
const inventorySync = async (container) => {
const brightpearlService = container.resolve("brightpearlService")
const eventBus = container.resolve("eventBusService")
try {
const client = await brightpearlService.getClient()
const pattern = "43 4,10,14,20 * * *" // nice for tests "*/10 * * * * *"
eventBus.createCronJob(
"inventory-sync",
{},
pattern,
brightpearlService.syncInventory()
)
} catch (err) {
if (err.name === "not_allowed") {
return
const inventorySync = async (container, options) => {
if (!options.inventory_sync_cron) {
return
} else {
const brightpearlService = container.resolve("brightpearlService")
const eventBus = container.resolve("eventBusService")
try {
const client = await brightpearlService.getClient()
const pattern = options.inventory_sync_cron
eventBus.createCronJob(
"inventory-sync",
{},
pattern,
brightpearlService.syncInventory
)
} catch (err) {
if (err.name === "not_allowed") {
return
}
throw err
}
throw err
}
}
@@ -0,0 +1,25 @@
const REFRESH_CRON = process.env.BP_REFRESH_CRON || "5 4 * * */6"
const refreshToken = async (container) => {
const logger = container.resolve("logger")
const oauthService = container.resolve("oauthService")
const eventBus = container.resolve("eventBusService")
try {
logger.info("registering refresh cron job BP")
eventBus.createCronJob("refresh-token-bp", {}, REFRESH_CRON, async () => {
const appData = await oauthService.retrieveByName("brightpearl")
const data = appData.data
if (data && data.refresh_token) {
return oauthService.refreshToken("brightpearl", data.refresh_token)
}
})
} catch (err) {
if (err.name === "not_allowed") {
return
}
throw err
}
}
export default refreshToken
@@ -41,7 +41,11 @@ class BrightpearlClient {
"content-type": "application/x-www-form-urlencoded",
},
data: qs.stringify(params),
}).then(({ data }) => data)
})
.then(({ data }) => data)
.catch((err) => {
throw err
})
}
constructor(options, onRefreshToken) {