Skip to content

Commit

Permalink
chore: teste renovate token
Browse files Browse the repository at this point in the history
  • Loading branch information
matheusgnreis committed Jan 16, 2024
1 parent 63366e9 commit 652df0e
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
4 changes: 2 additions & 2 deletions functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ exports.updateTokens = functions.pubsub.schedule(cron).onRun(() => {
console.log(`-- Sheduled update E-Com Plus tokens '${cron}'`)

// update token job bling
const createAccess = require('./lib/bling-auth/create-access')
const queueCreateAccess = 'every 9 mins'
const createAccess = require('./lib/bling-auth/renovate-token')
const queueCreateAccess = '36 */2 * * *'
exports.scheduledSyncBlingToken = functions.pubsub.schedule(queueCreateAccess).onRun(createAccess)
console.log(`-- Sheduled active access from bling '${queueCreateAccess}'`)

Expand Down
65 changes: 65 additions & 0 deletions functions/lib/bling-auth/renovate-token.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
const auth = require('./create-auth')
const { getFirestore, Timestamp } = require('firebase-admin/firestore')

const firestoreColl = 'bling_tokens'
module.exports = async () => {

let documentRef, storeId, clientId, clientSecret, refreshToken
if (firestoreColl) {
const db = getFirestore()
const d = new Date(new Date().getTime() - 9000)
const documentSnapshot = await db.collection(firestoreColl)
.where('updatedAt', '<=', d)
.orderBy('updatedAt')
.limit(1)
.get()
storeId = documentSnapshot.storeId
clientId = documentSnapshot.clientId
clientSecret = documentSnapshot.clientSecret
refreshToken = documentSnapshot.refreshToken
console.log('doc snap shot', documentSnapshot)
console.log('store id', storeId)
console.log('client id', clientId)
console.log('client secret', clientSecret)
console.log('refresh token', refreshToken)
documentRef = require('firebase-admin')
.firestore()
.doc(`${firestoreColl}/${storeId}`)
}

const handleAuth = (clientId, clientSecret, code = undefined, storeId, refreshToken) => {
console.log('> Bling Auth02 ', storeId)
auth(clientId, clientSecret, code, storeId, refreshToken)
.then((data) => {
console.log('> Bling token => ', JSON.stringify(data))
if (documentRef) {
documentRef.set({
...data,
storeId,
clientId,
clientSecret,
updatedAt: Timestamp.now()
}).catch(console.error)
}
authenticate(data.access_token)
})
.catch(reject)
}

if (documentRef) {
documentRef.get()
.then((documentSnapshot) => {
if (documentSnapshot.exists &&
Date.now() - documentSnapshot.updateTime.toDate().getTime() <= 10000 // token expires in 21600 ms
) {
authenticate(documentSnapshot.get('access_token'))
} else {
handleAuth(clientId, clientSecret, code = undefined, storeId, documentSnapshot.get('refresh_token'))
}
})
.catch(console.error)
} else {
handleAuth(clientId, clientSecret, code, storeId)
}
}

0 comments on commit 652df0e

Please sign in to comment.