11'use strict'
22
33const handleCallback = client => {
4- const { collRef, db , table } = client
4+ const { collRef } = client
55 // handle access token refresh
66 const refreshToken = require ( './refresh-token' ) ( client )
77
88 return async ( storeId , reqBody ) => {
99 return new Promise ( ( resolve , reject ) => {
10- let isNew , authenticationId , sql , values
11-
1210 // first validation of function params
1311 if ( typeof storeId !== 'number' || isNaN ( storeId ) || storeId <= 0 ) {
1412 reject ( new Error ( 'Undefined or invalid Store ID, must be a positive number' ) )
@@ -22,70 +20,34 @@ const handleCallback = client => {
2220 const { application, authentication } = reqBody
2321
2422 // preset Firestore document common values
25- const firestoreDoc = collRef ? { store_id : storeId } : null
26- if ( firestoreDoc && application ) {
27- Object . assign ( firestoreDoc , {
28- application_id : application . _id ,
29- application_app_id : application . app_id ,
30- application_title : application . title
31- } )
32- }
23+ const firestoreDoc = { store_id : storeId }
24+ let isNew , authenticationId
3325
3426 if ( application && reqBody . store_id === storeId ) {
3527 // new app installed
3628 isNew = true
3729 authenticationId = authentication . _id
3830
39- // insert application with respective authentication data
40- if ( collRef ) {
41- const { Timestamp } = require ( 'firebase-admin' ) . firestore
42- Object . assign ( firestoreDoc , {
43- authentication_permissions : JSON . stringify ( authentication . permissions ) ,
44- created_at : Timestamp . now ( ) ,
45- updated_at : new Timestamp ( 1500000000 , 0 ) // Jul 13 2017
46- } )
47- } else {
48- values = [
49- application . _id ,
50- application . app_id ,
51- application . title ,
52- authenticationId ,
53- JSON . stringify ( authentication . permissions ) ,
54- storeId
55- ]
56- sql = 'INSERT INTO ' + table + ` (
57- application_id,
58- application_app_id,
59- application_title,
60- authentication_id,
61- authentication_permissions,
62- store_id
63- ) VALUES (?, ?, ?, ?, ?, ?)`
64- }
31+ const { Timestamp } = require ( 'firebase-admin' ) . firestore
32+ Object . assign ( firestoreDoc , {
33+ application_id : application . _id ,
34+ application_app_id : application . app_id ,
35+ application_title : application . title ,
36+ authentication_permissions : JSON . stringify ( authentication . permissions ) ,
37+ setted_up : false ,
38+ created_at : Timestamp . now ( ) ,
39+ updated_at : new Timestamp ( 1500000000 , 0 ) // Jul 13 2017
40+ } )
6541 } else if ( reqBody . my_id && reqBody . access_token ) {
6642 // authenticating an already installed app
6743 isNew = false
6844 authenticationId = reqBody . my_id
6945
70- // authentication flux callback
71- // should update access token for current authentication
72- if ( collRef ) {
73- Object . assign ( firestoreDoc , {
74- access_token : reqBody . access_token ,
75- expires : reqBody . expires ,
76- updated_at : require ( 'firebase-admin' ) . firestore . Timestamp . now ( )
77- } )
78- } else {
79- values = [
80- reqBody . access_token ,
81- reqBody . my_id ,
82- storeId
83- ]
84- sql = 'UPDATE ' + table + ` SET
85- access_token = ?,
86- updated_at = CURRENT_TIMESTAMP
87- WHERE authentication_id = ? AND store_id = ?`
88- }
46+ Object . assign ( firestoreDoc , {
47+ access_token : reqBody . access_token ,
48+ expires : reqBody . expires ,
49+ updated_at : require ( 'firebase-admin' ) . firestore . Timestamp . now ( )
50+ } )
8951 } else {
9052 reject ( new Error ( 'Unexpected request body, properties not found' ) )
9153 return
@@ -105,24 +67,13 @@ const handleCallback = client => {
10567 } )
10668 }
10769
108- if ( sql ) {
109- // run SQLite query
110- db . run ( sql , values , err => {
111- if ( ! err ) {
112- handleResolve ( )
113- } else {
114- reject ( err )
115- }
116- } )
117- } else if ( firestoreDoc ) {
118- // run Firestore collection set
119- firestoreDoc . authentication_id = authenticationId
120- collRef
121- . doc ( authenticationId )
122- . set ( firestoreDoc , { merge : true } )
123- . then ( handleResolve )
124- . catch ( reject )
125- }
70+ // run Firestore collection set
71+ firestoreDoc . authentication_id = authenticationId
72+ collRef
73+ . doc ( authenticationId )
74+ . set ( firestoreDoc , { merge : true } )
75+ . then ( handleResolve )
76+ . catch ( reject )
12677 } else {
12778 reject ( new Error ( 'Can\'t set Authentication ID from request body' ) )
12879 }
0 commit comments