fix: admin sync

This commit is contained in:
Ben Allfree 2024-11-02 17:27:13 +00:00
parent 5923ba7a8a
commit f99ab41235

View File

@ -19,53 +19,29 @@ $app.onBeforeServe().add((e) => {
return return
} }
const updateByEmail = (email) => const update = () =>
dao dao
.db() .db()
.newQuery( .newQuery(
'update _admins set tokenKey={:tokenKey}, passwordHash={:passwordHash} where email={:email}', `
) insert into _admins (id, email, tokenKey, passwordHash) values ({:id}, {:email}, {:tokenKey}, {:passwordHash})
.bind({ email, tokenKey, passwordHash }) ON CONFLICT(email) DO UPDATE SET
.execute() id=excluded.id,
tokenKey=excluded.tokenKey,
const updateById = () => passwordHash=excluded.passwordHash
dao ON CONFLICT(id) DO UPDATE SET
.db() email=excluded.email,
.newQuery( tokenKey=excluded.tokenKey,
'update _admins set tokenKey={:tokenKey}, passwordHash={:passwordHash}, email={:email} where id={:id}', passwordHash=excluded.passwordHash
) `,
.bind({ id, tokenKey, passwordHash, email })
.execute()
const insert = () =>
dao
.db()
.newQuery(
'insert into _admins (id, email, tokenKey, passwordHash) VALUES ({:id}, {:email}, {:tokenKey}, {:passwordHash})',
) )
.bind({ id, email, tokenKey, passwordHash }) .bind({ id, email, tokenKey, passwordHash })
.execute() .execute()
try { try {
updateById() update()
log(`Success updating admin credentials by id ${id}`) log(`Success updating admin credentials ${email}`)
} catch (e) { } catch (e) {
log( log(`Failed to update admin credentials ${email}`)
`Failed to update admin credentials by id ${id}. Trying by email ${email}`,
)
try {
updateByEmail()
log(`Success updating admin credentials by email ${email}`)
} catch (e) {
log(
`Failed to update admin credentials by email ${email} or uid ${id}. Attempting insert.`,
)
try {
insert()
log(`Success inserting admin credentials`)
} catch (e) {
log(`Failed to insert admin credentials: ${e}`)
}
}
} }
}) })