Skip to content

Commit

Permalink
fix(refresh-tokens): add scheduled cloud function to run update
Browse files Browse the repository at this point in the history
github disables workflow on repo inactivity
  • Loading branch information
leomp12 committed Nov 5, 2020
1 parent 7290f9b commit f2fecc6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 32 deletions.
25 changes: 0 additions & 25 deletions .github/workflows/call-refresh-tokens.yml

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# App Manda Bem

[![Deploy](https://github.com/ecomplus/app-mandabem/workflows/Deploy/badge.svg)](https://github.com/ecomplus/app-mandabem/actions?workflow=Deploy) [![Call refresh tokens](https://github.com/ecomplus/app-mandabem/workflows/Call%20refresh%20tokens/badge.svg)](https://github.com/ecomplus/app-mandabem/actions?workflow=Call+refresh+tokens) [![Publish](https://github.com/ecomplus/app-mandabem/workflows/Publish/badge.svg)](https://github.com/ecomplus/app-mandabem/actions?workflow=Publish) [![License MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Deploy](https://github.com/ecomplus/app-mandabem/workflows/Deploy/badge.svg)](https://github.com/ecomplus/app-mandabem/actions?workflow=Deploy) [![Publish](https://github.com/ecomplus/app-mandabem/workflows/Publish/badge.svg)](https://github.com/ecomplus/app-mandabem/actions?workflow=Publish) [![License MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

E-Com Plus app to integrate Manda Bem shipping intermediator

Expand Down
24 changes: 18 additions & 6 deletions functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ router.get('/', (req, res) => {
require(`${routes}/`)(req, res)
})

const prepareAppSdk = () => {
// debug ecomAuth processes and ensure enable token updates by default
process.env.ECOM_AUTH_DEBUG = 'true'
process.env.ECOM_AUTH_UPDATE = 'enabled'
// setup ecomAuth client with Firestore instance
return setup(null, true, admin.firestore())
}

// base routes for E-Com Plus Store API
const routesDir = path.join(__dirname, routes)
recursiveReadDir(routesDir).filter(filepath => filepath.endsWith('.js')).forEach(filepath => {
Expand Down Expand Up @@ -103,12 +111,7 @@ recursiveReadDir(routesDir).filter(filepath => filepath.endsWith('.js')).forEach
if (middleware) {
router[method](filename, (req, res) => {
console.log(`${method} ${filename}`)

// debug ecomAuth processes and ensure enable token updates by default
process.env.ECOM_AUTH_DEBUG = 'true'
process.env.ECOM_AUTH_UPDATE = 'enabled'
// setup ecomAuth client with Firestore instance
setup(null, true, admin.firestore()).then(appSdk => {
prepareAppSdk().then(appSdk => {
middleware({ appSdk, admin }, req, res)
}).catch(err => {
console.error(err)
Expand All @@ -128,3 +131,12 @@ server.use(express.static('public'))

exports[functionName] = functions.https.onRequest(server)
console.log(`-- Starting '${app.title}' E-Com Plus app with Function '${functionName}'`)

// schedule update tokens job
const cron = '25 2,10,18 * * *'
exports.updateTokens = functions.pubsub.schedule(cron).onRun(() => {
return prepareAppSdk().then(appSdk => {
return appSdk.updateTokens()
})
})
console.log(`-- Sheduled update E-Com Plus tokens '${cron}'`)

0 comments on commit f2fecc6

Please sign in to comment.