1
0
Fork 0
mirror of synced 2024-09-08 21:51:58 +12:00

Users implementation added.

This commit is contained in:
mike12345567 2022-02-25 19:00:12 +00:00
parent 295b65678c
commit cf27667580
4 changed files with 164 additions and 20 deletions

View file

@ -1,5 +1,16 @@
import { search as stringSearch, addRev } from "./utils"
import { default as controller } from "../table"
import { Table } from "../../../definitions/common"
function fixTable(table: Table, params: any) {
if (!params || !table) {
return table
}
if (params.tableId) {
table._id = params.tableId
}
return table
}
export async function search(ctx: any) {
const { name } = ctx.request.body
@ -20,7 +31,7 @@ export async function read(ctx: any) {
}
export async function update(ctx: any) {
ctx.request.body = await addRev(ctx.request.body, ctx.params.tableId)
ctx.request.body = await addRev(fixTable(ctx.request.body, ctx.params), ctx.params.tableId)
await controller.save(ctx)
ctx.body = { table: ctx.body }
}

View file

@ -1,12 +1,78 @@
export async function search() {}
import {
allGlobalUsers,
readGlobalUser,
saveGlobalUser,
deleteGlobalUser,
} from "../../../utilities/workerRequests"
import { search as stringSearch } from "./utils"
export async function create() {}
function fixUser(ctx: any) {
if (!ctx.request.body) {
return ctx
}
if (!ctx.request.body._id && ctx.params.userId) {
ctx.request.body._id = ctx.params.userId
}
if (!ctx.request.body.roles) {
ctx.request.body.roles = {}
}
return ctx
}
export async function read() {}
function getUser(ctx: any, userId?: string) {
if (userId) {
ctx.params = {userId}
} else if (!ctx.params?.userId) {
throw "No user ID provided for getting"
}
return readGlobalUser(ctx)
}
export async function update() {}
export async function search(ctx: any) {
try {
const { name } = ctx.request.body
const users = await allGlobalUsers(ctx)
ctx.body = {
users: stringSearch(users, name, "email"),
}
} catch (err) {
console.log(err)
}
}
export async function destroy() {}
export async function create(ctx: any) {
const response = await saveGlobalUser(fixUser(ctx))
ctx.body = {
user: await getUser(ctx, response._id),
}
}
export async function read(ctx: any) {
const response = await readGlobalUser(ctx)
ctx.body = {
user: response,
}
}
export async function update(ctx: any) {
const user = await readGlobalUser(ctx)
ctx.request.body = {
...ctx.request.body,
_rev: user._rev,
}
const response = await saveGlobalUser(fixUser(ctx))
ctx.body = {
user: await getUser(ctx, response._id),
}
}
export async function destroy(ctx: any) {
const user = await getUser(ctx)
await deleteGlobalUser(ctx)
ctx.body = {
user,
}
}
export default {
create,

View file

@ -2,6 +2,18 @@ const TestConfig = require("../../../../tests/utilities/TestConfiguration")
const structures = require("../../../../tests/utilities/structures")
const env = require("../../../../environment")
function user() {
return {
_id: "user",
_rev: "rev",
createdAt: Date.now(),
email: "test@test.com",
roles: {},
tenantId: "default",
status: "active",
}
}
jest.mock("../../../../utilities/workerRequests", () => ({
getGlobalUsers: jest.fn(() => {
return {
@ -13,6 +25,18 @@ jest.mock("../../../../utilities/workerRequests", () => ({
_id: "us_uuid1",
}
}),
allGlobalUsers: jest.fn(() => {
return [user()]
}),
readGlobalUser: jest.fn(() => {
return user()
}),
saveGlobalUser: jest.fn(() => {
return { _id: "user", _rev: "rev" }
}),
deleteGlobalUser: jest.fn(() => {
return { message: "deleted user" }
}),
removeAppFromUserRoles: jest.fn(),
}))

View file

@ -31,6 +31,24 @@ function request(ctx, request) {
return request
}
async function checkResponse(response, errorMsg, { ctx } = {}) {
if (response.status !== 200) {
let error
try {
error = await response.json()
} catch (err) {
error = await response.text()
}
const msg = `Unable to ${errorMsg} - ${error.message ? error.message : error}`
if (ctx) {
ctx.throw(400, msg)
} else {
throw msg
}
}
return response.json()
}
exports.request = request
// have to pass in the tenant ID as this could be coming from an automation
@ -50,12 +68,7 @@ exports.sendSmtpEmail = async (to, from, subject, contents, automation) => {
},
})
)
if (response.status !== 200) {
const error = await response.text()
throw `Unable to send email - ${error}`
}
return response.json()
return checkResponse(response, "send email")
}
exports.getGlobalSelf = async (ctx, appId = null) => {
@ -65,10 +78,7 @@ exports.getGlobalSelf = async (ctx, appId = null) => {
// we don't want to use API key when getting self
request(ctx, { method: "GET" })
)
if (response.status !== 200) {
ctx.throw(400, "Unable to get self globally.")
}
let json = await response.json()
let json = await checkResponse(response, "get self globally", { ctx })
if (appId) {
json = updateAppRole(json)
}
@ -83,8 +93,41 @@ exports.removeAppFromUserRoles = async (ctx, appId) => {
method: "DELETE",
})
)
if (response.status !== 200) {
throw "Unable to remove app role"
}
return response.json()
return checkResponse(response, "remove app role")
}
exports.allGlobalUsers = async ctx => {
const response = await fetch(
checkSlashesInUrl(env.WORKER_URL + "/api/global/users"),
// we don't want to use API key when getting self
request(ctx, { method: "GET" })
)
return checkResponse(response, "get users", { ctx })
}
exports.saveGlobalUser = async ctx => {
const response = await fetch(
checkSlashesInUrl(env.WORKER_URL + "/api/global/users"),
// we don't want to use API key when getting self
request(ctx, { method: "POST", body: ctx.request.body })
)
return checkResponse(response, "save user", { ctx })
}
exports.deleteGlobalUser = async ctx => {
const response = await fetch(
checkSlashesInUrl(env.WORKER_URL + `/api/global/users/${ctx.params.userId}`),
// we don't want to use API key when getting self
request(ctx, { method: "DELETE" })
)
return checkResponse(response, "delete user", { ctx, body: ctx.request.body })
}
exports.readGlobalUser = async ctx => {
const response = await fetch(
checkSlashesInUrl(env.WORKER_URL + `/api/global/users/${ctx.params.userId}`),
// we don't want to use API key when getting self
request(ctx, { method: "GET" })
)
return checkResponse(response, "get user", { ctx })
}