2020-04-23 15:37:08 +02:00
|
|
|
const CouchDB = require("../../db");
|
|
|
|
const bcrypt = require("../../utilities/bcrypt");
|
2020-04-07 18:25:09 +02:00
|
|
|
|
2020-04-09 17:53:48 +02:00
|
|
|
exports.fetch = async function(ctx) {
|
2020-04-23 15:37:08 +02:00
|
|
|
const database = new CouchDB(ctx.params.instanceId);
|
|
|
|
const data = await database.query("database/by_type", {
|
2020-04-09 17:53:48 +02:00
|
|
|
include_docs: true,
|
|
|
|
key: ["user"]
|
|
|
|
});
|
2020-04-08 17:57:27 +02:00
|
|
|
|
2020-04-20 17:17:11 +02:00
|
|
|
ctx.body = data.rows.map(row => row.doc);
|
2020-04-09 17:53:48 +02:00
|
|
|
};
|
2020-04-07 18:25:09 +02:00
|
|
|
|
2020-04-09 17:53:48 +02:00
|
|
|
exports.create = async function(ctx) {
|
2020-04-23 15:37:08 +02:00
|
|
|
const database = new CouchDB(ctx.params.instanceId);
|
|
|
|
const { username, password, name } = ctx.request.body;
|
|
|
|
|
|
|
|
if (!username || !password) ctx.throw(400, "Username and Password Required.");
|
|
|
|
|
|
|
|
const response = await database.post({
|
|
|
|
username,
|
|
|
|
password: await bcrypt.hash(password),
|
|
|
|
name,
|
2020-04-20 17:17:11 +02:00
|
|
|
type: "user"
|
|
|
|
});
|
2020-04-23 15:37:08 +02:00
|
|
|
|
2020-04-10 17:37:59 +02:00
|
|
|
ctx.body = {
|
2020-04-23 15:37:08 +02:00
|
|
|
user: {
|
|
|
|
id: response.id,
|
|
|
|
rev: response.rev,
|
|
|
|
username,
|
|
|
|
name
|
|
|
|
},
|
2020-04-10 17:37:59 +02:00
|
|
|
message: `User created successfully.`,
|
|
|
|
status: 200
|
|
|
|
}
|
2020-04-09 17:53:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.destroy = async function(ctx) {
|
2020-04-23 15:37:08 +02:00
|
|
|
const database = new CouchDB(ctx.params.instanceId);
|
2020-04-10 17:37:59 +02:00
|
|
|
const response = await database.destroy(ctx.params.userId)
|
|
|
|
ctx.body = {
|
|
|
|
...response,
|
|
|
|
message: `User deleted.`,
|
|
|
|
status: 200
|
|
|
|
}
|
2020-04-09 17:53:48 +02:00
|
|
|
};
|