fix: move admin sync to instance app

This commit is contained in:
Ben Allfree
2024-01-13 04:53:04 -08:00
parent 5e2ce5ef83
commit 98df4a2586
8 changed files with 6501 additions and 4089 deletions

View File

@@ -1,6 +0,0 @@
{
"compilerOptions": {
"baseUrl": ".",
"typeRoots": ["./types"]
}
}

View File

@@ -0,0 +1,55 @@
onAfterBootstrap((e) => {
const dao = $app.dao()
const { mkLog } = /** @type {Lib} */ (require(`${__hooks}/_ph_lib.js`))
const log = mkLog(`admin-sync`)
const { email, tokenKey, passwordHash } = JSON.parse($os.getenv(`ADMIN_SYNC`))
if (!email) {
log(`Not active - skipped`)
}
const result = new DynamicModel({
// describe the shape of the data (used also as initial values)
id: '',
})
try {
dao
.db()
.newQuery('SELECT * from _admins where email = {:email}')
.bind({ email })
.one(result)
log(
`Existing admin record matching PocketHost login found - updating with latest credentials`,
)
try {
dao
.db()
.newQuery(
'update _admins set tokenKey={:tokenKey}, passwordHash={:passwordHash} where email={:email}',
)
.bind({ email, tokenKey, passwordHash })
.execute()
log(`Success`)
} catch (e) {
log(`Failed to update admin credentials: ${e}`)
}
} catch (e) {
log(`No admin record matching PocketHost credentials - creating`)
try {
dao
.db()
.newQuery(
'insert into _admins (email, tokenKey, passwordHash) VALUES ({:email}, {:tokenKey}, {:passwordHash})',
)
.bind({ email, tokenKey, passwordHash })
.execute()
log(`Success`)
} catch (e) {
log(`Failed to insert admin credentials: ${e}`)
}
}
})

View File

@@ -0,0 +1,19 @@
/** @type {Lib['mkLog']} */
const mkLog =
(namespace) =>
/**
* @param {...any} s
* @returns
*/
(...s) =>
console.log(
`[${namespace}]`,
...s.map((p) => {
if (typeof p === 'object') return JSON.stringify(p, null, 2)
return p
}),
)
module.exports = {
mkLog,
}

View File

@@ -0,0 +1,8 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"noImplicitAny": false,
"strictNullChecks": false
},
"include": ["pb_hooks", "types"]
}

5
src/instance-app/types/lib.d.ts vendored Normal file
View File

@@ -0,0 +1,5 @@
type Logger = (...args: any[]) => void
interface Lib {
mkLog: (namespace: string) => Logger
}

File diff suppressed because it is too large Load Diff