@@ -7,7 +7,7 @@ const handleCallback = client => {
7
7
8
8
return async ( storeId , reqBody ) => {
9
9
return new Promise ( ( resolve , reject ) => {
10
- let isNew , authenticationId , sql , values , firestoreDoc
10
+ let isNew , authenticationId , sql , values
11
11
12
12
// first validation of function params
13
13
if ( typeof storeId !== 'number' || isNaN ( storeId ) || storeId <= 0 ) {
@@ -21,6 +21,21 @@ const handleCallback = client => {
21
21
// application and authentication objects from body if any
22
22
const { application, authentication } = reqBody
23
23
24
+ // preset Firestore document common values
25
+ const firestoreDoc = collRef
26
+ ? {
27
+ store_id : storeId ,
28
+ authentication_id : authenticationId
29
+ }
30
+ : null
31
+ if ( collRef && application ) {
32
+ Object . assign ( application , {
33
+ application_id : application . _id ,
34
+ application_app_id : application . app_id ,
35
+ application_title : application . title
36
+ } )
37
+ }
38
+
24
39
if ( application && reqBody . store_id === storeId ) {
25
40
// new app installed
26
41
isNew = true
@@ -29,16 +44,11 @@ const handleCallback = client => {
29
44
// insert application with respective authentication data
30
45
if ( collRef ) {
31
46
const { Timestamp } = require ( 'firebase-admin' ) . firestore
32
- firestoreDoc = {
33
- application_id : application . _id ,
34
- application_app_id : application . app_id ,
35
- application_title : application . title ,
36
- authentication_id : authenticationId ,
47
+ Object . assign ( application , {
37
48
authentication_permissions : JSON . stringify ( authentication . permissions ) ,
38
- store_id : storeId ,
39
49
created_at : Timestamp . now ( ) ,
40
50
updated_at : new Timestamp ( 1500000000 , 0 ) // Jul 13 2017
41
- }
51
+ } )
42
52
} else {
43
53
values = [
44
54
application . _id ,
@@ -65,13 +75,11 @@ const handleCallback = client => {
65
75
// authentication flux callback
66
76
// should update access token for current authentication
67
77
if ( collRef ) {
68
- firestoreDoc = {
78
+ Object . assign ( application , {
69
79
access_token : reqBody . access_token ,
70
80
expires : reqBody . expires ,
71
- updated_at : require ( 'firebase-admin' ) . firestore . Timestamp . now ( ) ,
72
- authentication_id : authenticationId ,
73
- store_id : storeId
74
- }
81
+ updated_at : require ( 'firebase-admin' ) . firestore . Timestamp . now ( )
82
+ } )
75
83
} else {
76
84
values = [
77
85
reqBody . access_token ,
0 commit comments