From c6ff0c0a09fd0b2bc4dcca4632bb101eab94b792 Mon Sep 17 00:00:00 2001 From: Leonardo Matos Date: Thu, 13 Feb 2020 15:24:50 -0300 Subject: [PATCH] fix(handle-callback): fix promises with firestore and clean (dry) code --- lib/methods/handle-callback.js | 111 +++++++++++++++------------------ 1 file changed, 52 insertions(+), 59 deletions(-) diff --git a/lib/methods/handle-callback.js b/lib/methods/handle-callback.js index 93bafe5..a8f468b 100644 --- a/lib/methods/handle-callback.js +++ b/lib/methods/handle-callback.js @@ -1,4 +1,5 @@ 'use strict' + const admin = require('firebase-admin') const handleCallback = client => { @@ -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) { @@ -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 ] @@ -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')) } }) }