Skip to content

Commit 0ceada7

Browse files
leomp12claude
andcommitted
refactor!: remove SQLite support, Firestore-only
Removes all SQLite branches from main.js, get-auth, delete-auth, handle-callback, update-tokens. Rewrites setup-stores entirely for Firestore (was SQLite-only with no Firestore equivalent). Changes setup() signature from (dbFilename, disableUpdates, firestoreDb) to (firestoreDb, disableUpdates). handle-callback now sets setted_up: false on new installs, enabling the Firestore query in setup-stores. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 1420171 commit 0ceada7

6 files changed

Lines changed: 191 additions & 341 deletions

File tree

lib/methods/delete-auth.js

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,9 @@
11
'use strict'
22

3-
const deleteAuth = ({ collRef, db, table }) => {
4-
return authenticationId => new Promise((resolve, reject) => {
5-
if (!collRef) {
6-
const sql = 'DELETE FROM ' + table + ' WHERE authentication_id = ?'
7-
// run SQLite delete query
8-
db.run(sql, [authenticationId], err => {
9-
if (err) {
10-
// SQL error ?
11-
reject(err)
12-
}
13-
resolve()
14-
})
15-
} else {
16-
// delete Firestore document
17-
collRef.doc(authenticationId).delete()
18-
.then(resolve).catch(reject)
19-
}
20-
})
3+
const deleteAuth = ({ collRef }) => {
4+
return authenticationId => {
5+
return collRef.doc(authenticationId).delete()
6+
}
217
}
228

239
module.exports = deleteAuth

lib/methods/get-auth.js

Lines changed: 30 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
const getAuth = ({ collRef, db, table }) => {
3+
const getAuth = ({ collRef }) => {
44
return (storeId, authenticationId) => {
55
return new Promise((resolve, reject) => {
66
const handleResolve = (row, docRef) => {
@@ -19,66 +19,40 @@ const getAuth = ({ collRef, db, table }) => {
1919
reject(err)
2020
}
2121

22-
// select authentication for specified store from database
23-
if (!collRef) {
24-
let query = 'SELECT * FROM ' + table + ' WHERE store_id = ? '
25-
const params = [storeId]
26-
if (authenticationId) {
27-
// also filter by authentication ID
28-
query += 'AND authentication_id = ? '
29-
params.push(authenticationId)
30-
}
31-
// get one row only
32-
query += 'ORDER BY access_token IS NOT NULL DESC, updated_at DESC LIMIT 1'
22+
// working with Firestore collection
23+
let docOrQueryRef
24+
if (authenticationId) {
25+
docOrQueryRef = collRef.doc(authenticationId)
26+
} else {
27+
docOrQueryRef = collRef.where('store_id', '==', storeId)
28+
}
3329

34-
// run query and get row object
35-
db.get(query, params, (err, row) => {
36-
if (err) {
37-
reject(err)
38-
} else if (row) {
39-
handleResolve(row)
30+
// run document get or query
31+
docOrQueryRef.get()
32+
.then(docOrQuerySnapshot => {
33+
let data
34+
if (docOrQuerySnapshot.data) {
35+
// is documentSnapshot
36+
data = docOrQuerySnapshot.data()
37+
} else if (docOrQuerySnapshot.size > 0) {
38+
// is querySnapshot
39+
docOrQuerySnapshot.forEach(documentSnapshot => {
40+
const doc = documentSnapshot.data()
41+
if (
42+
!data || !data.updated_at ||
43+
(doc.updated_at && doc.updated_at.seconds >= data.updated_at.seconds)
44+
) {
45+
data = doc
46+
}
47+
})
48+
}
49+
if (data) {
50+
handleResolve(data, docOrQueryRef)
4051
} else {
4152
noAuthReject()
4253
}
4354
})
44-
} else {
45-
// working with Firestore collection
46-
let docOrQueryRef
47-
if (authenticationId) {
48-
docOrQueryRef = collRef.doc(authenticationId)
49-
} else {
50-
docOrQueryRef = collRef.where('store_id', '==', storeId)
51-
}
52-
53-
// run document get or query
54-
docOrQueryRef.get()
55-
.then(docOrQuerySnapshot => {
56-
let data
57-
if (docOrQuerySnapshot.data) {
58-
// is documentSnapshot
59-
data = docOrQuerySnapshot.data()
60-
} else if (docOrQuerySnapshot.size > 0) {
61-
// is querySnapshot
62-
docOrQuerySnapshot.forEach(documentSnapshot => {
63-
const doc = documentSnapshot.data()
64-
if (
65-
!data || !data.updated_at ||
66-
(doc.updated_at && doc.updated_at.seconds >= data.updated_at.seconds)
67-
) {
68-
data = doc
69-
}
70-
})
71-
}
72-
if (data) {
73-
handleResolve(data, docOrQueryRef)
74-
} else {
75-
noAuthReject()
76-
}
77-
})
78-
.catch(err => {
79-
reject(err)
80-
})
81-
}
55+
.catch(reject)
8256
})
8357
}
8458
}

lib/methods/handle-callback.js

Lines changed: 25 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
'use strict'
22

33
const 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
}

lib/services/setup-stores.js

Lines changed: 50 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,68 @@
11
'use strict'
22

33
const setupStores = (client, options) => {
4-
const { db, table, debug } = client
4+
const { collRef, debug } = client
55
const { procedures, callback } = options
66
// handle saving procedures list to Store API
77
const saveProcedures = require('./../methods/save-procedures')(client)
88

9-
const task = () => {
9+
const task = async () => {
1010
// get one store waiting for setup process
11-
const query = 'SELECT application_id, authentication_id, store_id FROM ' + table +
12-
' WHERE setted_up = 0 AND access_token IS NOT NULL ORDER BY created_at ASC LIMIT 1'
11+
const snapshot = await collRef
12+
.where('setted_up', '==', false)
13+
.orderBy('created_at', 'asc')
14+
.limit(1)
15+
.get()
1316

14-
// run query and get row object
15-
db.get(query, (err, row) => {
16-
if (!err) {
17-
if (!row) {
18-
// no store to setup
19-
// schedule next table reading
20-
setTimeout(task, 1000)
21-
} else {
22-
const storeId = row.store_id
17+
if (snapshot.empty) {
18+
// no store to setup, schedule next check
19+
setTimeout(task, 1000)
20+
return
21+
}
2322

24-
;(async function loop () {
25-
// save procedures
26-
let error
27-
try {
28-
await saveProcedures(storeId, procedures)
29-
} catch (err) {
30-
error = err
31-
}
23+
const doc = snapshot.docs[0]
24+
const row = doc.data()
3225

33-
// after procedures saved
34-
// run callback function if any
35-
if (typeof callback === 'function') {
36-
await callback(error, { storeId })
37-
}
38-
// all async process done
39-
// schedule next store to setup
40-
setTimeout(task, 200)
26+
if (!row.access_token) {
27+
// token not ready yet, wait and retry
28+
setTimeout(task, 500)
29+
return
30+
}
31+
32+
const storeId = row.store_id
33+
34+
;(async function loop () {
35+
// save procedures
36+
let error
37+
try {
38+
await saveProcedures(storeId, procedures)
39+
} catch (err) {
40+
error = err
41+
}
42+
43+
// after procedures saved
44+
// run callback function if any
45+
if (typeof callback === 'function') {
46+
await callback(error, { storeId })
47+
}
48+
// all async process done
49+
// schedule next store to setup
50+
setTimeout(task, 200)
4151

42-
if (!error) {
43-
// all done with success
44-
// remove from queue
45-
const query = 'UPDATE ' + table + ' SET setted_up = 1 WHERE store_id = ?'
46-
db.run(query, [storeId], err => {
47-
if (err) {
48-
throw err
49-
}
50-
if (debug) {
51-
debug(`✓ Store #${storeId} successfully setted up with procedures created`)
52-
}
53-
})
54-
} else {
55-
// TODO: try to save error on app hidden data
52+
if (!error) {
53+
// all done with success
54+
// remove from queue
55+
collRef.doc(row.authentication_id).update({ setted_up: true })
56+
.then(() => {
57+
if (debug) {
58+
debug(`✓ Store #${storeId} successfully setted up with procedures created`)
5659
}
57-
}())
58-
}
60+
})
61+
.catch(err => { throw err })
5962
} else {
60-
// SQL error ?
61-
throw err
63+
// TODO: try to save error on app hidden data
6264
}
63-
})
65+
}())
6466
}
6567

6668
// start task loop

0 commit comments

Comments
 (0)