Browse Source

Fix GET /analyticUnits/status (#112)

* redo code in getStatus method
pull/1/head
rozetko 6 years ago committed by GitHub
parent
commit
1299a7eb6e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 36
      server/src/routes/analytic_units_router.ts

36
server/src/routes/analytic_units_router.ts

@ -1,35 +1,47 @@
import * as AnalyticUnit from '../models/analytic_unit_model'; import * as AnalyticUnit from '../models/analytic_unit_model';
import { createAnalyticUnitFromObject } from '../controllers/analytics_controller' import { createAnalyticUnitFromObject } from '../controllers/analytics_controller';
import * as Router from 'koa-router'; import * as Router from 'koa-router';
async function getStatus(ctx: Router.IRouterContext) { async function getStatus(ctx: Router.IRouterContext) {
try { try {
ctx.response.body = { status: 'READY', errorMessage: undefined }; let analyticUnitId = ctx.request.query.id;
if(analyticUnitId === undefined) {
throw new Error('Cannot get status of undefined id');
}
let analyticUnit = await AnalyticUnit.findById(analyticUnitId);
ctx.response.body = {
status: analyticUnit.status
};
if(analyticUnit.status === AnalyticUnit.AnalyticUnitStatus.FAILED) {
ctx.response.body.errorMessage = analyticUnit.error;
}
} catch(e) { } catch(e) {
console.error(e); console.error(e);
// TODO: better send 404 when we know than isn`t found ctx.response.status = 404;
ctx.response.status = 500; ctx.response.body = 'Can`t find anything';
ctx.response.body = { error: 'Can`t return anything' };
} }
} }
async function getUnit(ctx: Router.IRouterContext) { async function getUnit(ctx: Router.IRouterContext) {
try { try {
let id = ctx.request.query.id; let analyticUnitId = ctx.request.query.id;
if(id === undefined) { if(analyticUnitId === undefined) {
throw new Error('No id param in query'); throw new Error('No id param in query');
} }
let unit: AnalyticUnit.AnalyticUnit = await AnalyticUnit.findById(id); let analyticUnit = await AnalyticUnit.findById(analyticUnitId);
ctx.response.body = { ctx.response.body = {
name: unit.name, name: analyticUnit.name,
metric: unit.metric, metric: analyticUnit.metric,
status: unit.status status: analyticUnit.status
}; };
} catch(e) { } catch(e) {
@ -51,7 +63,7 @@ async function createUnit(ctx: Router.IRouterContext) {
message: `Creation error: ${e.message}` message: `Creation error: ${e.message}`
}; };
} }
} }
async function deleteUnit(ctx: Router.IRouterContext) { async function deleteUnit(ctx: Router.IRouterContext) {

Loading…
Cancel
Save