Skip to content
2 changes: 2 additions & 0 deletions core/database/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ if( ENABLE_FOXX_TESTS )
add_test(NAME foxx_version COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/tests/test_foxx.sh" -t "unit_version:")
add_test(NAME foxx_support COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/tests/test_foxx.sh" -t "unit_support:")
add_test(NAME foxx_user_router COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/tests/test_foxx.sh" -t "unit_user_router:")
add_test(NAME foxx_proj_router COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/tests/test_foxx.sh" -t "unit_proj_router:")
add_test(NAME foxx_acl_router COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/tests/test_foxx.sh" -t "unit_acl_router:")
add_test(NAME foxx_admin_router COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/tests/test_foxx.sh" -t "unit_admin_router:")
add_test(NAME foxx_config_router COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/tests/test_foxx.sh" -t "unit_config_router:")
Expand Down Expand Up @@ -62,6 +63,7 @@ if( ENABLE_FOXX_TESTS )
set_tests_properties(foxx_validation_repo PROPERTIES FIXTURES_REQUIRED Foxx)
set_tests_properties(foxx_path PROPERTIES FIXTURES_REQUIRED Foxx)
set_tests_properties(foxx_user_router PROPERTIES FIXTURES_REQUIRED "Foxx;FoxxDBFixtures")
set_tests_properties(foxx_proj_router PROPERTIES FIXTURES_REQUIRED Foxx)
set_tests_properties(foxx_acl_router PROPERTIES FIXTURES_REQUIRED Foxx)
set_tests_properties(foxx_config_router PROPERTIES FIXTURES_REQUIRED Foxx)
set_tests_properties(foxx_topic_router PROPERTIES FIXTURES_REQUIRED Foxx)
Expand Down
251 changes: 246 additions & 5 deletions core/database/foxx/api/proj_router.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,25 @@ const g_lib = require("./support");
const error = require("./lib/error_codes");
const permissions = require("./lib/permissions");
const g_tasks = require("./tasks");
const logger = require("./lib/logger");
const basePath = "prj";

module.exports = router;

//==================== PROJECT API FUNCTIONS

router
.get("/create", function (req, res) {
let result = null;
try {
var result;
logger.logRequestStarted({
client: req.queryParams.client,
correlationId: req.headers["x-correlation-id"],
httpVerb: "GET",
routePath: basePath + "/create",
status: "Started",
description: `Create new projects. Project ID: ${req.queryParams.id}`,
});

g_db._executeTransaction({
collections: {
Expand Down Expand Up @@ -199,7 +209,44 @@ router
});

res.send(result);
logger.logRequestSuccess({
client: req.queryParams.client,
correlationId: req.headers["x-correlation-id"],
httpVerb: "GET",
routePath: basePath + "/create",
status: "Success",
description: `Create new projects. Project ID: ${req.queryParams.id}`,
});
if (req.queryParams.admins?.length) {
logger.logRequestSuccess({
client: req.queryParams.client,
correlationId: req.headers["x-correlation-id"],
httpVerb: "GET",
routePath: basePath + "/create",
status: "Success",
description: `Admins added: ${req.queryParams.admins}`,
});
}
if (req.queryParams.members?.length) {
logger.logRequestSuccess({
client: req.queryParams.client,
correlationId: req.headers["x-correlation-id"],
httpVerb: "GET",
routePath: basePath + "/create",
status: "Success",
description: `Members added: ${req.queryParams.members}`,
});
}
} catch (e) {
logger.logRequestFailure({
client: req.queryParams.client,
correlationId: req.headers["x-correlation-id"],
httpVerb: "GET",
routePath: basePath + "/create",
status: "Failure",
description: `Create new projects. Project ID: ${req.queryParams.id}`,
error: e,
});
g_lib.handleException(e, res);
}
})
Expand All @@ -218,8 +265,17 @@ router

router
.get("/update", function (req, res) {
let proj = null;
let result = null;
try {
var result;
logger.logRequestStarted({
client: req.queryParams.client,
correlationId: req.headers["x-correlation-id"],
httpVerb: "GET",
routePath: basePath + "/update",
status: "Started",
description: `Update project information. Project ID: ${req.queryParams.id}`,
});

g_db._executeTransaction({
collections: {
Expand Down Expand Up @@ -264,7 +320,7 @@ router
}
}

var proj = g_db._update(proj_id, obj, {
proj = g_db._update(proj_id, obj, {
keepNull: false,
returnNew: true,
});
Expand Down Expand Up @@ -367,7 +423,61 @@ router
});

res.send(result);
logger.logRequestSuccess({
client: req.queryParams.client,
correlationId: req.headers["x-correlation-id"],
httpVerb: "GET",
routePath: basePath + "/update",
status: "Success",
description: `Update project information. Project ID: ${req.queryParams.id}`,
extra: {
owner: proj.new?.owner,
title: proj.new?.title
? proj.new?.title.length > 15
? proj.new?.title.slice(0, 15) + "…"
: proj.new?.title
: undefined,
},
});
if (req.queryParams.admins?.length) {
logger.logRequestSuccess({
client: req.queryParams.client,
correlationId: req.headers["x-correlation-id"],
httpVerb: "GET",
routePath: basePath + "/update",
status: "Success",
description: `Admins added: ${req.queryParams.admins}`,
});
}
if (req.queryParams.members?.length) {
logger.logRequestSuccess({
client: req.queryParams.client,
correlationId: req.headers["x-correlation-id"],
httpVerb: "GET",
routePath: basePath + "/update",
status: "Success",
description: `Members added: ${req.queryParams.members}`,
});
}
} catch (e) {
logger.logRequestFailure({
client: req.queryParams.client,
correlationId: req.headers["x-correlation-id"],
httpVerb: "GET",
routePath: basePath + "/update",
status: "Failure",
description: `Update project information. Project ID: ${req.queryParams.id}`,
extra: {
owner: proj.new?.owner,
title: proj.new?.title
? proj.new?.title.length > 15
? proj.new?.title.slice(0, 15) + "…"
: proj.new?.title
: undefined,
},

error: e,
});
g_lib.handleException(e, res);
}
})
Expand All @@ -386,15 +496,25 @@ router

router
.get("/view", function (req, res) {
let proj = null;
try {
logger.logRequestStarted({
client: req.queryParams.client,
correlationId: req.headers["x-correlation-id"],
httpVerb: "GET",
routePath: basePath + "/view",
status: "Started",
description: `View project information. ID: ${req.queryParams.id}`,
});

// TODO Enforce view permission

const client = g_lib.getUserFromClientID_noexcept(req.queryParams.client);

if (!g_db.p.exists(req.queryParams.id))
throw [error.ERR_INVALID_PARAM, "No such project '" + req.queryParams.id + "'"];

var proj = g_db.p.document({
proj = g_db.p.document({
_id: req.queryParams.id,
});

Expand Down Expand Up @@ -451,7 +571,26 @@ router
delete proj._rev;

res.send([proj]);
logger.logRequestSuccess({
client: req.queryParams.client,
correlationId: req.headers["x-correlation-id"],
httpVerb: "GET",
routePath: basePath + "/view",
status: "Success",
description: `View project information. ID: ${req.queryParams.id}`,
result: proj,
});
} catch (e) {
logger.logRequestFailure({
client: req.queryParams.client,
correlationId: req.headers["x-correlation-id"],
httpVerb: "GET",
routePath: basePath + "/view",
status: "Failure",
description: `View project information. ID: ${req.queryParams.id}`,
extra: proj,
error: e,
});
g_lib.handleException(e, res);
}
})
Expand All @@ -462,6 +601,15 @@ router

router
.get("/list", function (req, res) {
logger.logRequestStarted({
client: req.queryParams.client,
correlationId: req.headers["x-correlation-id"],
httpVerb: "GET",
routePath: basePath + "/list",
status: "Started",
description: `List projects`,
});

const client = g_lib.getUserFromClientID(req.queryParams.client);
var qry,
result,
Expand Down Expand Up @@ -570,6 +718,15 @@ router

//res.send( g_db._query( qry, { user: client._id }));
res.send(result);
logger.logRequestSuccess({
client: req.queryParams.client,
correlationId: req.headers["x-correlation-id"],
httpVerb: "GET",
routePath: basePath + "/list",
status: "Success",
description: `List projects`,
extra: { NumOfProjs: tot },
});
})
.queryParam("client", joi.string().required(), "Client ID")
.queryParam("subject", joi.string().optional(), "Subject (user) ID")
Expand All @@ -592,10 +749,35 @@ router
router
.get("/search", function (req, res) {
try {
logger.logRequestStarted({
client: req.queryParams.client,
correlationId: req.headers["x-correlation-id"],
httpVerb: "GET",
routePath: basePath + "/search",
status: "Started",
description: `Find all projects that match query: ${req.queryParams.query}`,
});

g_lib.getUserFromClientID(req.queryParams.client);

res.send(g_db._query(req.queryParams.query, {}));
logger.logRequestSuccess({
client: req.queryParams.client,
correlationId: req.headers["x-correlation-id"],
httpVerb: "GET",
routePath: basePath + "/search",
status: "Success",
description: `Find all projects that match query: ${req.queryParams.query}`,
});
} catch (e) {
logger.logRequestFailure({
client: req.queryParams.client,
correlationId: req.headers["x-correlation-id"],
httpVerb: "GET",
routePath: basePath + "/search",
status: "Failure",
description: `Find all projects that match query: ${req.queryParams.query}`,
});
g_lib.handleException(e, res);
}
})
Expand All @@ -606,7 +788,17 @@ router

router
.post("/delete", function (req, res) {
let result = null;
try {
logger.logRequestStarted({
client: req.queryParams.client,
correlationId: req.headers["x-correlation-id"],
httpVerb: "POST",
routePath: basePath + "/delete",
status: "Started",
description: `Delete project(s) and all associated data records and raw data. IDs: ${req.body.ids}`,
});

g_db._executeTransaction({
collections: {
read: ["u", "uuid", "accn", "p", "alloc"],
Expand All @@ -621,7 +813,26 @@ router
res.send(result);
},
});
logger.logRequestSuccess({
client: req.queryParams.client,
correlationId: req.headers["x-correlation-id"],
httpVerb: "POST",
routePath: basePath + "/delete",
status: "Success",
description: `Delete project(s) and all associated data records and raw data. IDs: ${req.body.ids}`,
extra: result,
});
} catch (e) {
logger.logRequestFailure({
client: req.queryParams.client,
correlationId: req.headers["x-correlation-id"],
httpVerb: "POST",
routePath: basePath + "/delete",
status: "Failure",
description: `Delete project(s) and all associated data records and raw data. IDs: ${req.body.ids}`,
extra: result,
error: e,
});
g_lib.handleException(e, res);
}
})
Expand All @@ -639,7 +850,17 @@ router

router
.get("/get_role", function (req, res) {
let role = null;
try {
logger.logRequestStarted({
client: req.queryParams.client,
correlationId: req.headers["x-correlation-id"],
httpVerb: "GET",
routePath: basePath + "/get_role",
status: "Started",
description: `Get client/subject project role. ID: ${req.queryParams.id}`,
});

const client = g_lib.getUserFromClientID(req.queryParams.client);
var subj;

Expand All @@ -653,12 +874,32 @@ router
if (!g_db._exists(req.queryParams.id))
throw [error.ERR_NOT_FOUND, "Project, " + req.queryParams.id + ", not found"];

var role = g_lib.getProjectRole(subj, req.queryParams.id);
role = g_lib.getProjectRole(subj, req.queryParams.id);

res.send({
role: role,
});

logger.logRequestSuccess({
client: req.queryParams.client,
correlationId: req.headers["x-correlation-id"],
httpVerb: "GET",
routePath: basePath + "/get_role",
status: "Success",
description: `Get client/subject project role. ID: ${req.queryParams.id}`,
extra: { role: role },
});
} catch (e) {
logger.logRequestSuccess({
client: req.queryParams.client,
correlationId: req.headers["x-correlation-id"],
httpVerb: "GET",
routePath: basePath + "/get_role",
status: "Success",
description: `Get client/subject project role. ID: ${req.queryParams.id}`,
extra: { role: role },
error: e,
});
g_lib.handleException(e, res);
}
})
Expand Down
Loading