2020-05-07 11:53:34 +02:00
|
|
|
const CouchDB = require("../../db")
|
|
|
|
const bcrypt = require("../../utilities/bcrypt")
|
2020-11-24 15:04:14 +01:00
|
|
|
const { generateUserID, getUserParams, ViewNames } = require("../../db/utils")
|
2020-12-02 18:08:25 +01:00
|
|
|
const { getRole } = require("../../utilities/security/roles")
|
2020-04-07 18:25:09 +02:00
|
|
|
|
2020-06-29 15:56:41 +02:00
|
|
|
exports.fetch = async function(ctx) {
|
2020-10-29 11:28:27 +01:00
|
|
|
const database = new CouchDB(ctx.user.appId)
|
2020-10-02 13:37:46 +02:00
|
|
|
const data = await database.allDocs(
|
2020-12-07 16:21:06 +01:00
|
|
|
getUserParams(null, {
|
2020-10-02 13:37:46 +02:00
|
|
|
include_docs: true,
|
|
|
|
})
|
|
|
|
)
|
2020-05-07 11:53:34 +02:00
|
|
|
ctx.body = data.rows.map(row => row.doc)
|
|
|
|
}
|
2020-04-07 18:25:09 +02:00
|
|
|
|
2020-06-29 15:56:41 +02:00
|
|
|
exports.create = async function(ctx) {
|
2020-10-29 11:28:27 +01:00
|
|
|
const db = new CouchDB(ctx.user.appId)
|
2020-12-08 12:42:29 +01:00
|
|
|
const { email, password, roleId } = ctx.request.body
|
2020-05-21 15:31:23 +02:00
|
|
|
|
2020-12-04 13:22:45 +01:00
|
|
|
if (!email || !password) {
|
|
|
|
ctx.throw(400, "email and Password Required.")
|
2020-05-21 15:31:23 +02:00
|
|
|
}
|
2020-04-23 15:37:08 +02:00
|
|
|
|
2020-12-02 18:08:25 +01:00
|
|
|
const role = await getRole(ctx.user.appId, roleId)
|
2020-04-23 15:37:08 +02:00
|
|
|
|
2020-12-02 14:20:56 +01:00
|
|
|
if (!role) ctx.throw(400, "Invalid Role")
|
2020-05-21 15:31:23 +02:00
|
|
|
|
|
|
|
const user = {
|
2020-12-04 13:22:45 +01:00
|
|
|
_id: generateUserID(email),
|
|
|
|
email,
|
2020-04-23 15:37:08 +02:00
|
|
|
password: await bcrypt.hash(password),
|
2020-05-07 11:53:34 +02:00
|
|
|
type: "user",
|
2020-12-02 14:20:56 +01:00
|
|
|
roleId,
|
2020-11-24 15:04:14 +01:00
|
|
|
tableId: ViewNames.USERS,
|
2020-05-21 15:31:23 +02:00
|
|
|
}
|
|
|
|
|
2020-11-06 13:30:30 +01:00
|
|
|
try {
|
|
|
|
const response = await db.post(user)
|
|
|
|
ctx.status = 200
|
|
|
|
ctx.message = "User created successfully."
|
|
|
|
ctx.userId = response._id
|
|
|
|
ctx.body = {
|
|
|
|
_rev: response.rev,
|
2020-12-04 13:22:45 +01:00
|
|
|
email,
|
2020-11-06 13:30:30 +01:00
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
if (err.status === 409) {
|
|
|
|
ctx.throw(400, "User exists already")
|
|
|
|
} else {
|
|
|
|
ctx.throw(err.status, err)
|
|
|
|
}
|
2020-04-10 17:37:59 +02:00
|
|
|
}
|
2020-05-07 11:53:34 +02:00
|
|
|
}
|
2020-04-09 17:53:48 +02:00
|
|
|
|
2020-06-29 15:56:41 +02:00
|
|
|
exports.update = async function(ctx) {
|
2020-10-29 11:28:27 +01:00
|
|
|
const db = new CouchDB(ctx.user.appId)
|
2020-06-26 11:05:09 +02:00
|
|
|
const user = ctx.request.body
|
2020-06-29 15:55:12 +02:00
|
|
|
const dbUser = db.get(ctx.request.body._id)
|
|
|
|
const newData = { ...dbUser, ...user }
|
2020-05-21 15:31:23 +02:00
|
|
|
|
2020-06-29 15:55:12 +02:00
|
|
|
const response = await db.put(newData)
|
2020-06-26 11:05:09 +02:00
|
|
|
user._rev = response.rev
|
|
|
|
|
|
|
|
ctx.status = 200
|
2020-12-04 13:22:45 +01:00
|
|
|
ctx.message = `User ${ctx.request.body.email} updated successfully.`
|
2020-06-29 13:14:15 +02:00
|
|
|
ctx.body = response
|
2020-06-26 11:05:09 +02:00
|
|
|
}
|
|
|
|
|
2020-06-29 15:56:41 +02:00
|
|
|
exports.destroy = async function(ctx) {
|
2020-10-29 11:28:27 +01:00
|
|
|
const database = new CouchDB(ctx.user.appId)
|
2020-12-04 13:22:45 +01:00
|
|
|
await database.destroy(generateUserID(ctx.params.email))
|
|
|
|
ctx.message = `User ${ctx.params.email} deleted.`
|
2020-05-14 16:12:30 +02:00
|
|
|
ctx.status = 200
|
|
|
|
}
|
|
|
|
|
2020-06-29 15:56:41 +02:00
|
|
|
exports.find = async function(ctx) {
|
2020-10-29 11:28:27 +01:00
|
|
|
const database = new CouchDB(ctx.user.appId)
|
2020-12-04 13:22:45 +01:00
|
|
|
const user = await database.get(generateUserID(ctx.params.email))
|
2020-04-10 17:37:59 +02:00
|
|
|
ctx.body = {
|
2020-12-04 13:22:45 +01:00
|
|
|
email: user.email,
|
2020-05-14 16:12:30 +02:00
|
|
|
_rev: user._rev,
|
2020-04-10 17:37:59 +02:00
|
|
|
}
|
2020-05-07 11:53:34 +02:00
|
|
|
}
|