diff --git a/packages/server/src/api/controllers/public/tables.ts b/packages/server/src/api/controllers/public/tables.ts index 92c5489784..931cfeecf4 100644 --- a/packages/server/src/api/controllers/public/tables.ts +++ b/packages/server/src/api/controllers/public/tables.ts @@ -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 } } diff --git a/packages/server/src/api/controllers/public/users.ts b/packages/server/src/api/controllers/public/users.ts index dd65a81a22..afb17b50e2 100644 --- a/packages/server/src/api/controllers/public/users.ts +++ b/packages/server/src/api/controllers/public/users.ts @@ -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, diff --git a/packages/server/src/api/routes/tests/utilities/index.js b/packages/server/src/api/routes/tests/utilities/index.js index f28bd5a7af..b6dde0cb79 100644 --- a/packages/server/src/api/routes/tests/utilities/index.js +++ b/packages/server/src/api/routes/tests/utilities/index.js @@ -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(), })) diff --git a/packages/server/src/utilities/workerRequests.js b/packages/server/src/utilities/workerRequests.js index 8c81ad161f..d98e8eb8d1 100644 --- a/packages/server/src/utilities/workerRequests.js +++ b/packages/server/src/utilities/workerRequests.js @@ -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 }) }