Removing the lookup of _id in usage quota when in dev/self host for performance reasons as part of usage quota, re-writing some bits of fetch self for cleaner implementation, fixing some issues with updating/saving users from within app.
This commit is contained in:
parent
7e6855262b
commit
d0bdd113e1
|
@ -79,20 +79,24 @@ exports.fetchSelf = async ctx => {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!appId) {
|
|
||||||
const db = new CouchDB(StaticDatabases.USER.name)
|
|
||||||
const user = await db.get(userId)
|
|
||||||
delete user.password
|
|
||||||
ctx.body = { user }
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
const db = new CouchDB(appId)
|
|
||||||
const user = await getFullUser({ ctx, userId: userId })
|
const user = await getFullUser({ ctx, userId: userId })
|
||||||
const userTable = await db.get(InternalTables.USER_METADATA)
|
|
||||||
if (user) {
|
if (appId) {
|
||||||
delete user.password
|
const db = new CouchDB(appId)
|
||||||
|
// remove the full roles structure
|
||||||
|
delete user.roles
|
||||||
|
try {
|
||||||
|
const userTable = await db.get(InternalTables.USER_METADATA)
|
||||||
|
const metadata = await db.get(userId)
|
||||||
|
// specifically needs to make sure is enriched
|
||||||
|
ctx.body = await outputProcessing(appId, userTable, {
|
||||||
|
...user,
|
||||||
|
...metadata,
|
||||||
|
})
|
||||||
|
} catch (err) {
|
||||||
|
ctx.body = user
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
ctx.body = user
|
||||||
}
|
}
|
||||||
// specifically needs to make sure is enriched
|
|
||||||
ctx.body = await outputProcessing(appId, userTable, user)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -130,7 +130,7 @@ exports.save = async function(ctx) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// if the row obj had an _id then it will have been retrieved
|
// if the row obj had an _id then it will have been retrieved
|
||||||
const existingRow = ctx.preExisting
|
const existingRow = await db.get(inputs._id)
|
||||||
if (existingRow) {
|
if (existingRow) {
|
||||||
ctx.params.rowId = inputs._id
|
ctx.params.rowId = inputs._id
|
||||||
await exports.patch(ctx)
|
await exports.patch(ctx)
|
||||||
|
|
|
@ -26,6 +26,9 @@ exports.fetchMetadata = async function(ctx) {
|
||||||
const users = []
|
const users = []
|
||||||
for (let user of global) {
|
for (let user of global) {
|
||||||
const info = metadata.find(meta => meta._id.includes(user.email))
|
const info = metadata.find(meta => meta._id.includes(user.email))
|
||||||
|
// remove these props, not for the correct DB
|
||||||
|
delete user._id
|
||||||
|
delete user._rev
|
||||||
users.push({
|
users.push({
|
||||||
...user,
|
...user,
|
||||||
...info,
|
...info,
|
||||||
|
|
|
@ -76,8 +76,10 @@ describe("usageQuota middleware", () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
it("passes through to next middleware if document already exists", async () => {
|
it("passes through to next middleware if document already exists", async () => {
|
||||||
|
config.setProd(true)
|
||||||
config.setBody({
|
config.setBody({
|
||||||
_id: "test"
|
_id: "test",
|
||||||
|
_rev: "test",
|
||||||
})
|
})
|
||||||
|
|
||||||
CouchDB.mockImplementationOnce(() => ({
|
CouchDB.mockImplementationOnce(() => ({
|
||||||
|
@ -87,13 +89,14 @@ describe("usageQuota middleware", () => {
|
||||||
await config.executeMiddleware()
|
await config.executeMiddleware()
|
||||||
|
|
||||||
expect(config.next).toHaveBeenCalled()
|
expect(config.next).toHaveBeenCalled()
|
||||||
expect(config.ctx.preExisting).toBe(true)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it("throws if request has _id, but the document no longer exists", async () => {
|
it("throws if request has _id, but the document no longer exists", async () => {
|
||||||
config.setBody({
|
config.setBody({
|
||||||
_id: "123"
|
_id: "123",
|
||||||
|
_rev: "test",
|
||||||
})
|
})
|
||||||
|
config.setProd(true)
|
||||||
|
|
||||||
CouchDB.mockImplementationOnce(() => ({
|
CouchDB.mockImplementationOnce(() => ({
|
||||||
get: async () => {
|
get: async () => {
|
||||||
|
|
|
@ -27,6 +27,11 @@ function getProperty(url) {
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = async (ctx, next) => {
|
module.exports = async (ctx, next) => {
|
||||||
|
// if in development or a self hosted cloud usage quotas should not be executed
|
||||||
|
if (env.isDev() || env.SELF_HOSTED) {
|
||||||
|
return next()
|
||||||
|
}
|
||||||
|
|
||||||
const db = new CouchDB(ctx.appId)
|
const db = new CouchDB(ctx.appId)
|
||||||
let usage = METHOD_MAP[ctx.req.method]
|
let usage = METHOD_MAP[ctx.req.method]
|
||||||
const property = getProperty(ctx.req.url)
|
const property = getProperty(ctx.req.url)
|
||||||
|
@ -34,20 +39,15 @@ module.exports = async (ctx, next) => {
|
||||||
return next()
|
return next()
|
||||||
}
|
}
|
||||||
// post request could be a save of a pre-existing entry
|
// post request could be a save of a pre-existing entry
|
||||||
if (ctx.request.body && ctx.request.body._id) {
|
if (ctx.request.body && ctx.request.body._id && ctx.request.body._rev) {
|
||||||
try {
|
try {
|
||||||
ctx.preExisting = await db.get(ctx.request.body._id)
|
await db.get(ctx.request.body._id)
|
||||||
return next()
|
return next()
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
ctx.throw(404, `${ctx.request.body._id} does not exist`)
|
ctx.throw(404, `${ctx.request.body._id} does not exist`)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// if in development or a self hosted cloud usage quotas should not be executed
|
|
||||||
if (env.isDev() || env.SELF_HOSTED) {
|
|
||||||
return next()
|
|
||||||
}
|
|
||||||
// update usage for uploads to be the total size
|
// update usage for uploads to be the total size
|
||||||
if (property === usageQuota.Properties.UPLOAD) {
|
if (property === usageQuota.Properties.UPLOAD) {
|
||||||
const files =
|
const files =
|
||||||
|
|
|
@ -96,7 +96,7 @@ exports.saveGlobalUser = async (ctx, appId, email, body) => {
|
||||||
body: {
|
body: {
|
||||||
...globalUser,
|
...globalUser,
|
||||||
email,
|
email,
|
||||||
password: body.password,
|
password: body.password || undefined,
|
||||||
status: body.status,
|
status: body.status,
|
||||||
roles,
|
roles,
|
||||||
},
|
},
|
||||||
|
@ -114,5 +114,7 @@ exports.saveGlobalUser = async (ctx, appId, email, body) => {
|
||||||
delete body.password
|
delete body.password
|
||||||
delete body.roleId
|
delete body.roleId
|
||||||
delete body.status
|
delete body.status
|
||||||
|
delete body.roles
|
||||||
|
delete body.builder
|
||||||
return body
|
return body
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,7 +12,7 @@ function buildUserSaveValidation() {
|
||||||
_id: Joi.string(),
|
_id: Joi.string(),
|
||||||
_rev: Joi.string(),
|
_rev: Joi.string(),
|
||||||
email: Joi.string(),
|
email: Joi.string(),
|
||||||
password: Joi.string(),
|
password: Joi.string().allow(null, ""),
|
||||||
// maps appId -> roleId for the user
|
// maps appId -> roleId for the user
|
||||||
roles: Joi.object()
|
roles: Joi.object()
|
||||||
.pattern(/.*/, Joi.string())
|
.pattern(/.*/, Joi.string())
|
||||||
|
|
Loading…
Reference in New Issue