2019-06-14 11:05:46 +02:00
|
|
|
const Router = require("koa-router");
|
|
|
|
const session = require("./session");
|
|
|
|
const StatusCodes = require("../utilities/statusCodes");
|
|
|
|
module.exports = (config, app) => {
|
|
|
|
|
2019-06-14 18:01:01 +02:00
|
|
|
const router = new Router();
|
2019-06-14 11:05:46 +02:00
|
|
|
|
|
|
|
router
|
2019-06-14 18:01:01 +02:00
|
|
|
/*.use(async (ctx) => {
|
|
|
|
if(!await ctx.master.getApplication(ctx.params.appname)) {
|
|
|
|
ctx.throw(StatusCodes.NOT_FOUND, `could not find app named ${ctx.params.appname}`);
|
|
|
|
}
|
|
|
|
})*/
|
|
|
|
.use(session(config, app))
|
|
|
|
.use(async (ctx, next) => {
|
|
|
|
ctx.sessionId = ctx.session._sessCtx.externalKey;
|
|
|
|
ctx.session.accessed = true;
|
|
|
|
await next();
|
|
|
|
})
|
|
|
|
.get("/:appname", async (ctx) => {
|
|
|
|
ctx.response.status = StatusCodes.OK;
|
|
|
|
ctx.response.body = "UI Served Here";
|
|
|
|
})
|
|
|
|
.post("/:appname/api/authenticate", async (ctx, next) => {
|
2019-06-14 11:05:46 +02:00
|
|
|
const user = await ctx.master.authenticate(
|
2019-06-14 18:01:01 +02:00
|
|
|
ctx.sessionId,
|
2019-06-14 11:05:46 +02:00
|
|
|
ctx.params.appname,
|
|
|
|
ctx.request.body.username,
|
|
|
|
ctx.request.body.password
|
|
|
|
);
|
|
|
|
if(!user) {
|
|
|
|
ctx.throw(StatusCodes.UNAUTHORIZED, "invalid username or password");
|
|
|
|
}
|
2019-06-14 18:01:01 +02:00
|
|
|
|
|
|
|
ctx.response.status = StatusCodes.OK;
|
2019-06-14 11:05:46 +02:00
|
|
|
})
|
2019-06-14 18:01:01 +02:00
|
|
|
.post("/:appname/api/setPasswordFromTemporaryCode", async (ctx) => {
|
2019-06-16 00:55:32 +02:00
|
|
|
|
|
|
|
})
|
|
|
|
.post("/:appname/api/createTemporaryAccess", async (ctx) => {
|
|
|
|
|
2019-06-14 11:05:46 +02:00
|
|
|
})
|
2019-06-14 18:01:01 +02:00
|
|
|
.use(async (ctx, next) => {
|
|
|
|
|
|
|
|
const pathParts = ctx.path.split("/");
|
|
|
|
|
|
|
|
if(pathParts.length < 2) {
|
|
|
|
ctx.throw(StatusCodes.NOT_FOUND, "App Name not declared");
|
|
|
|
}
|
2019-06-14 11:05:46 +02:00
|
|
|
|
2019-06-14 18:01:01 +02:00
|
|
|
ctx.instance = await ctx.master.getInstanceApiForSession(
|
|
|
|
pathParts[1],
|
|
|
|
ctx.sessionId);
|
|
|
|
|
|
|
|
await next();
|
2019-06-14 11:05:46 +02:00
|
|
|
})
|
2019-06-16 00:55:32 +02:00
|
|
|
.post("/:appname/api/changeMyPassword", async (ctx) => {
|
|
|
|
await ctx.instance.authApi.changeMyPassword(
|
|
|
|
ctx.request.body.currentPassword,
|
|
|
|
ctx.request.body.newPassword
|
|
|
|
);
|
|
|
|
ctx.response.status = StatusCodes.OK;
|
|
|
|
})
|
|
|
|
.post("/:appname/api/changeMyPassword", async (ctx) => {
|
|
|
|
await ctx.instance.authApi.changeMyPassword(
|
|
|
|
ctx.request.body.currentPassword,
|
|
|
|
ctx.request.body.newPassword
|
|
|
|
);
|
|
|
|
ctx.response.status = StatusCodes.OK;
|
|
|
|
})
|
2019-06-14 18:01:01 +02:00
|
|
|
.post("/:appname/api/executeAction/:actionname", async (ctx) => {
|
2019-06-16 00:55:32 +02:00
|
|
|
ctx.body = await ctx.instance.actionApi.execute(
|
|
|
|
ctx.request.body.actionname,
|
|
|
|
ctx.request.body.parameters);
|
|
|
|
ctx.response.status = StatusCodes.OK;
|
2019-06-14 11:05:46 +02:00
|
|
|
})
|
2019-06-14 18:01:01 +02:00
|
|
|
.post("/:appname/api/createUser", async (ctx) => {
|
|
|
|
await ctx.instance.authApi.createUser(
|
|
|
|
ctx.request.body.user,
|
|
|
|
ctx.request.body.password
|
|
|
|
);
|
2019-06-14 11:05:46 +02:00
|
|
|
|
2019-06-14 18:01:01 +02:00
|
|
|
ctx.response.status = StatusCodes.OK;
|
2019-06-14 11:05:46 +02:00
|
|
|
})
|
2019-06-14 18:01:01 +02:00
|
|
|
.post("/:appname/api/enableUser", async (ctx) => {
|
2019-06-16 00:55:32 +02:00
|
|
|
await ctx.instance.authApi.enableUser(
|
|
|
|
ctx.request.body.username);
|
|
|
|
ctx.response.status = StatusCodes.OK;
|
2019-06-14 11:05:46 +02:00
|
|
|
})
|
2019-06-14 18:01:01 +02:00
|
|
|
.post("/:appname/api/disableUser", async (ctx) => {
|
2019-06-16 00:55:32 +02:00
|
|
|
await ctx.instance.authApi.disableUser(
|
|
|
|
ctx.request.body.username);
|
|
|
|
ctx.response.status = StatusCodes.OK;
|
2019-06-14 11:05:46 +02:00
|
|
|
})
|
2019-06-14 18:01:01 +02:00
|
|
|
.get("/:appname/api/users", async (ctx) => {
|
2019-06-15 00:03:01 +02:00
|
|
|
ctx.body = await ctx.instance.authApi.getUsers();
|
|
|
|
ctx.response.status = StatusCodes.OK;
|
2019-06-14 11:05:46 +02:00
|
|
|
})
|
2019-06-14 18:01:01 +02:00
|
|
|
.get("/:appname/api/accessLevels", async (ctx) => {
|
2019-06-16 00:55:32 +02:00
|
|
|
ctx.body = await ctx.instance.authApi.getAccessLevels();
|
|
|
|
ctx.response.status = StatusCodes.OK;
|
2019-06-14 11:05:46 +02:00
|
|
|
})
|
2019-06-14 18:01:01 +02:00
|
|
|
.post("/:appname/api/listRecords/:indexkey", async (ctx) => {
|
2019-06-16 00:55:32 +02:00
|
|
|
ctx.body = await ctx.instance.indexApi.listItems(
|
|
|
|
ctx.request.body.indexKey,
|
|
|
|
{
|
|
|
|
rangeStartParams:ctx.request.body.rangeStartParams,
|
|
|
|
rangeEndParams:ctx.request.body.rangeEndParams,
|
|
|
|
searchPhrase:ctx.request.body.searchPhrase
|
|
|
|
}
|
|
|
|
);
|
|
|
|
ctx.response.status = StatusCodes.OK;
|
2019-06-14 11:05:46 +02:00
|
|
|
})
|
2019-06-16 00:55:32 +02:00
|
|
|
.post("/:appname/api/aggregates/:indexkey", async (ctx) => {
|
|
|
|
ctx.body = await ctx.instance.indexApi.aggregates(
|
|
|
|
ctx.request.body.indexKey,
|
|
|
|
{
|
|
|
|
rangeStartParams:ctx.request.body.rangeStartParams,
|
|
|
|
rangeEndParams:ctx.request.body.rangeEndParams,
|
|
|
|
searchPhrase:ctx.request.body.searchPhrase
|
|
|
|
}
|
|
|
|
);
|
|
|
|
ctx.response.status = StatusCodes.OK;
|
2019-06-14 11:05:46 +02:00
|
|
|
})
|
2019-06-14 18:01:01 +02:00
|
|
|
.post("/:appname/api/record/:recordkey", async (ctx) => {
|
2019-06-16 00:55:32 +02:00
|
|
|
ctx.body = await ctx.instance.recordApi.save(
|
|
|
|
ctx.request.body
|
|
|
|
);
|
|
|
|
ctx.response.status = StatusCodes.OK;
|
2019-06-14 11:05:46 +02:00
|
|
|
})
|
2019-06-14 18:01:01 +02:00
|
|
|
.get("/:appname/api/record/:recordkey", async (ctx) => {
|
2019-06-16 00:55:32 +02:00
|
|
|
ctx.body = await ctx.instance.recordApi.load(
|
|
|
|
ctx.params.recordKey
|
|
|
|
);
|
|
|
|
ctx.response.status = StatusCodes.OK;
|
2019-06-14 11:05:46 +02:00
|
|
|
})
|
2019-06-14 18:01:01 +02:00
|
|
|
.del("/:appname/api/record/:recordkey", async (ctx) => {
|
2019-06-16 00:55:32 +02:00
|
|
|
await ctx.instance.recordApi.delete(
|
|
|
|
ctx.params.recordKey
|
|
|
|
);
|
|
|
|
ctx.response.status = StatusCodes.OK;
|
2019-06-14 11:05:46 +02:00
|
|
|
})
|
2019-06-14 18:01:01 +02:00
|
|
|
.post("/:appname/api/appHeirarchy", async (ctx) => {
|
2019-06-16 00:55:32 +02:00
|
|
|
ctx.body = await ctx.instance.templateApi.saveApplicationHeirarchy(
|
|
|
|
ctx.body
|
|
|
|
);
|
|
|
|
ctx.response.status = StatusCodes.OK;
|
2019-06-14 11:05:46 +02:00
|
|
|
})
|
2019-06-14 18:01:01 +02:00
|
|
|
.post("/:appname/api/actionsAndTriggers", async (ctx) => {
|
2019-06-16 00:55:32 +02:00
|
|
|
ctx.body = await ctx.instance.templateApi.saveApplicationHeirarchy(
|
|
|
|
ctx.body
|
|
|
|
);
|
|
|
|
ctx.response.status = StatusCodes.OK;
|
2019-06-14 18:01:01 +02:00
|
|
|
})
|
2019-06-16 00:55:32 +02:00
|
|
|
.get("/:appname/api/appDefinition", async (ctx) => {
|
|
|
|
ctx.body = await ctx.instance.templateApi.saveActionsAndTriggers(
|
|
|
|
ctx.body
|
|
|
|
);
|
|
|
|
ctx.response.status = StatusCodes.OK;
|
2019-06-14 18:01:01 +02:00
|
|
|
});
|
2019-06-14 11:05:46 +02:00
|
|
|
|
|
|
|
return router;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
front end get authenticateTemporaryAccess {}
|
|
|
|
*/
|