You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

48 lines
1.3 KiB

import * as AnalyticsController from '../controllers/analytics_controller';
import { AnalyticUnitId } from '../models/analytic_units';
import * as Segment from '../models/segment_model';
import * as Router from 'koa-router';
export async function getSegments(ctx: Router.IRouterContext) {
const id = ctx.request.query.id as AnalyticUnitId;
if(id === undefined || id === '') {
throw new Error('analyticUnitId (id) is missing');
}
let from = +ctx.request.query.from;
if(isNaN(from)) {
from = undefined;
}
let to = +ctx.request.query.to;
if(isNaN(to)) {
to = undefined;
}
const segments = await Segment.findIntersectedSegments(id, from, to);
ctx.response.body = { segments };
}
async function updateSegments(ctx: Router.IRouterContext) {
const {
addedSegments, id, removedSegments: removedIds
} = ctx.request.body as {
addedSegments: any[], id: AnalyticUnitId, removedSegments: Segment.SegmentId[]
};
const segmentsToInsert: Segment.Segment[] = addedSegments.map(
s => Segment.Segment.fromObject({ analyticUnitId: id, ...s })
);
const { addedIds } = await AnalyticsController.updateSegments(
id, segmentsToInsert, removedIds
);
ctx.response.body = { addedIds };
}
export const router = new Router();
router.get('/', getSegments);
router.patch('/', updateSegments);