|
|
|
@ -3,7 +3,27 @@ const { getTenantId, lookupTenantId, getGlobalDB } = require("../tenancy") |
|
|
|
|
|
|
|
const EXPIRY_SECONDS = 3600 |
|
|
|
|
|
|
|
exports.getUser = async (userId, tenantId = null) => { |
|
|
|
/** |
|
|
|
* The default populate user function |
|
|
|
*/ |
|
|
|
const populateFromDB = (userId, tenantId) => { |
|
|
|
return getGlobalDB(tenantId).get(userId) |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Get the requested user by id. |
|
|
|
* Use redis cache to first read the user. |
|
|
|
* If not present fallback to loading the user directly and re-caching. |
|
|
|
* @param {*} userId the id of the user to get |
|
|
|
* @param {*} tenantId the tenant of the user to get |
|
|
|
* @param {*} loadUser function to provide the user for re-caching. default to couch db |
|
|
|
* @returns |
|
|
|
*/ |
|
|
|
exports.getUser = async ( |
|
|
|
userId, |
|
|
|
tenantId = null, |
|
|
|
populateUser = populateFromDB |
|
|
|
) => { |
|
|
|
if (!tenantId) { |
|
|
|
try { |
|
|
|
tenantId = getTenantId() |
|
|
|
@ -15,7 +35,7 @@ exports.getUser = async (userId, tenantId = null) => { |
|
|
|
// try cache
|
|
|
|
let user = await client.get(userId) |
|
|
|
if (!user) { |
|
|
|
user = await getGlobalDB(tenantId).get(userId) |
|
|
|
user = await populateUser(userId, tenantId) |
|
|
|
client.store(userId, user, EXPIRY_SECONDS) |
|
|
|
} |
|
|
|
if (user && !user.tenantId && tenantId) { |
|
|
|
|