2021-07-06 19:10:04 +02:00
|
|
|
const redis = require("../redis/authRedis")
|
2021-08-02 19:34:43 +02:00
|
|
|
const {
|
2021-08-03 16:32:25 +02:00
|
|
|
getTenantId,
|
2021-08-02 19:34:43 +02:00
|
|
|
lookupTenantId,
|
|
|
|
getGlobalDB,
|
|
|
|
} = require("../tenancy")
|
2021-07-06 19:10:04 +02:00
|
|
|
|
|
|
|
const EXPIRY_SECONDS = 3600
|
|
|
|
|
2021-08-03 16:32:25 +02:00
|
|
|
exports.getUser = async (userId, tenantId = null) => {
|
|
|
|
if (!tenantId) {
|
|
|
|
try {
|
|
|
|
tenantId = getTenantId()
|
|
|
|
} catch (err) {
|
|
|
|
tenantId = await lookupTenantId(userId)
|
|
|
|
}
|
2021-07-15 18:57:02 +02:00
|
|
|
}
|
2021-07-06 19:10:04 +02:00
|
|
|
const client = await redis.getUserClient()
|
|
|
|
// try cache
|
|
|
|
let user = await client.get(userId)
|
|
|
|
if (!user) {
|
2021-08-03 16:32:25 +02:00
|
|
|
user = await getGlobalDB(tenantId).get(userId)
|
2021-07-06 19:10:04 +02:00
|
|
|
client.store(userId, user, EXPIRY_SECONDS)
|
|
|
|
}
|
|
|
|
return user
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.invalidateUser = async userId => {
|
|
|
|
const client = await redis.getUserClient()
|
|
|
|
await client.delete(userId)
|
|
|
|
}
|