Skip to content

Commit

Permalink
fix(handle-callback): fix promises with firestore and clean (dry) code
Browse files Browse the repository at this point in the history
  • Loading branch information
leomp12 committed Feb 13, 2020
1 parent 857c603 commit c6ff0c0
Showing 1 changed file with 52 additions and 59 deletions.
111 changes: 52 additions & 59 deletions lib/methods/handle-callback.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict'

const admin = require('firebase-admin')

const handleCallback = client => {
Expand All @@ -8,7 +9,7 @@ const handleCallback = client => {

return async (storeId, reqBody) => {
return new Promise((resolve, reject) => {
let sql, values
let isNew, authenticationId, sql, values, firestoreDoc

// first validation of function params
if (typeof storeId !== 'number' || isNaN(storeId) || storeId <= 0) {
Expand All @@ -21,28 +22,30 @@ const handleCallback = client => {

// application and authentication objects from body if any
const { application, authentication } = reqBody
// whether new application was installed
let isNew

if (application && reqBody.store_id === storeId) {
// new app installed
isNew = true
authenticationId = authentication._id

// insert application with respective authentication data
if (!dbFilename) {
db.collection(table).doc(authentication._id).set({
firestoreDoc = {
application_id: application._id,
application_app_id: application.app_id,
application_title: application.title,
authentication_id: authentication._id,
authentication_id: authenticationId,
authentication_permissions: JSON.stringify(authentication.permissions),
store_id: storeId,
created_at: admin.firestore.Timestamp.fromDate(new Date()),
updated_at: null
}, { merge: true }).catch(err => reject(err))
}
} else {
values = [
application._id,
application.app_id,
application.title,
authentication._id,
authenticationId,
JSON.stringify(authentication.permissions),
storeId
]
Expand All @@ -55,77 +58,67 @@ const handleCallback = client => {
store_id
) VALUES (?, ?, ?, ?, ?, ?)`
}
// new app installed
isNew = true
} else if (reqBody.my_id && reqBody.access_token) {
// authenticating an already installed app
isNew = false
authenticationId = reqBody.my_id

// authentication flux callback
// should update access token for current authentication
values = [
reqBody.access_token,
reqBody.my_id,
storeId
]

if (!dbFilename) {
const ref = db.collection(table).doc(authentication._id)
const token = ref.get()

if (token && token.data() && token.data().store_id === storeId) {
ref.set({
access_token: reqBody.access_token,
updated_at: admin.firestore.Timestamp.fromDate(new Date())
}, { merge: true }).catch(err => reject(err))
firestoreDoc = {
access_token: reqBody.access_token,
updated_at: admin.firestore.Timestamp.fromDate(new Date())
}
} else {
values = [
reqBody.access_token,
reqBody.my_id,
storeId
]
sql = 'UPDATE ' + table + ` SET
access_token = ?,
updated_at = CURRENT_TIMESTAMP
WHERE authentication_id = ? AND store_id = ?`
}

// authenticating an already installed app
isNew = false
} else {
reject(new Error('Unexpected request body, properties not found'))
return
}

// run query if exists sqlite file
if (dbFilename) {
db.run(sql, values, err => {
if (!err) {
let authenticationId
if (isNew) {
authenticationId = authentication._id
// generate access token by the first time
// start app authentication flux
refreshToken(storeId, authenticationId)
if (authenticationId) {
const handleResolve = () => {
if (isNew) {
// generate access token by the first time
// start app authentication flux
refreshToken(storeId, authenticationId)
}
// success callback with handled authentication ID
resolve({
isNew,
authenticationId
})
}

if (sql) {
// run SQLite query
db.run(sql, values, err => {
if (!err) {
handleResolve()
} else {
authenticationId = reqBody.my_id
reject(err)
}
// success callback with handled authentication ID
resolve({
isNew,
authenticationId
})
} else {
reject(err)
}
})
} else {
let authenticationId
if (isNew) {
authenticationId = authentication._id
// generate access token by the first time
// start app authentication flux
refreshToken(storeId, authenticationId)
} else {
authenticationId = reqBody.my_id
})
} else if (firestoreDoc) {
// run Firestore collection set
db.collection(table)
.doc(authenticationId)
.set(firestoreDoc, { merge: true })
.then(handleResolve)
.catch(reject)
}
resolve({
isNew,
authenticationId
})
} else {
reject(new Error('Can\'t set Authentication ID from request body'))
}
})
}
Expand Down

0 comments on commit c6ff0c0

Please sign in to comment.