<commit_before>'use strict';
const Router = require('koa-router');
const logger = require('logger');
const ErrorSerializer = require('serializers/errorSerializer');
const AnswersSerializer = require('serializers/answersSerializer');
const AnswersModel = require('models/answersModel');
const ReportsModel = require('models/reportsModel');
const s3Service = require('services/s3Service');
const passThrough = require('stream').PassThrough;
const json2csv = require('json2csv');
const ObjectId = require('mongoose').Types.ObjectId;

const router = new Router({
    prefix: '/reports/:reportId/answers'
});

class AnswersRouter {

    static * getAll() {
        logger.info(`Obtaining answers for report ${this.params.reportId}`);

        let filter = {};
        if (this.state.loggedUser.role === 'ADMIN') {
            filter = {
                $and: [
                    { report: new ObjectId(this.params.reportId) },
                ]
            };
        } else {
            filter = {
                $and: [
                    { report: new ObjectId(this.params.reportId) },
                    { user: new ObjectId(this.state.loggedUser.id) }
                ]
            };
        }
        if (this.state.query) {
            Object.keys(this.state.query).forEach((key) => {
                filter.$and.push({ [key]: this.state.query[key] });
            });
        }
        const answers = yield AnswersModel.find(filter);
        if (!answers) {
            this.throw(404, 'Answers not found with these permissions');
            return;
        }
        this.body = AnswersSerializer.serialize(answers);
    }

    static * get() {
        logger.info(`Obtaining answer ${this.params.id} for report ${this.params.reportId}`);
        let filter = {};
        if (this.state.loggedUser.role === 'ADMIN') {
            filter = {
                _id: new ObjectId(this.params.id),
                report: new ObjectId(this.params.reportId)
            };
        } else {
            filter = {
                user: new ObjectId(this.state.loggedUser.id),
                _id: new ObjectId(this.params.id),
                report: new ObjectId(this.params.reportId)
            };
        }
        const answer = yield AnswersModel.find(filter);
        if (!answer) {
            this.throw(404, 'Answer not found with these permissions');
            return;
        }
        this.body = AnswersSerializer.serialize(answer);
    }

    static * save() {
        logger.info('Saving answer');
        logger.debug(this.request.body);

        const fields = this.request.body.fields;
        let userPosition = [];
        let clickedPosition = [];

        try {
            userPosition = fields.userPosition.split(',');
            clickedPosition = fields.clickedPosition.split(',');
        } catch(e) {
            this.throw(400, `Position values must be separated by ','`);
        }

        let answer = {
            report: this.params.reportId,
            username: fields.username,
            organization: fields.organization,
            areaOfInterest: fields.areaOfInterest,
            language: fields.language,
            userPosition: userPosition,
            clickedPosition: clickedPosition,<commit_msg>check for userPosition or clickedPosition<commit_after>'use strict';
const Router = require('koa-router');
const logger = require('logger');
const ErrorSerializer = require('serializers/errorSerializer');
const AnswersSerializer = require('serializers/answersSerializer');
const AnswersModel = require('models/answersModel');
const ReportsModel = require('models/reportsModel');
const s3Service = require('services/s3Service');
const passThrough = require('stream').PassThrough;
const json2csv = require('json2csv');
const ObjectId = require('mongoose').Types.ObjectId;

const router = new Router({
    prefix: '/reports/:reportId/answers'
});

class AnswersRouter {

    static * getAll() {
        logger.info(`Obtaining answers for report ${this.params.reportId}`);

        let filter = {};
        if (this.state.loggedUser.role === 'ADMIN') {
            filter = {
                $and: [
                    { report: new ObjectId(this.params.reportId) },
                ]
            };
        } else {
            filter = {
                $and: [
                    { report: new ObjectId(this.params.reportId) },
                    { user: new ObjectId(this.state.loggedUser.id) }
                ]
            };
        }
        if (this.state.query) {
            Object.keys(this.state.query).forEach((key) => {
                filter.$and.push({ [key]: this.state.query[key] });
            });
        }
        const answers = yield AnswersModel.find(filter);
        if (!answers) {
            this.throw(404, 'Answers not found with these permissions');
            return;
        }
        this.body = AnswersSerializer.serialize(answers);
    }

    static * get() {
        logger.info(`Obtaining answer ${this.params.id} for report ${this.params.reportId}`);
        let filter = {};
        if (this.state.loggedUser.role === 'ADMIN') {
            filter = {
                _id: new ObjectId(this.params.id),
                report: new ObjectId(this.params.reportId)
            };
        } else {
            filter = {
                user: new ObjectId(this.state.loggedUser.id),
                _id: new ObjectId(this.params.id),
                report: new ObjectId(this.params.reportId)
            };
        }
        const answer = yield AnswersModel.find(filter);
        if (!answer) {
            this.throw(404, 'Answer not found with these permissions');
            return;
        }
        this.body = AnswersSerializer.serialize(answer);
    }

    static * save() {
        logger.info('Saving answer');
        logger.debug(this.request.body);

        const fields = this.request.body.fields;
        let userPosition = [];
        let clickedPosition = [];

        try {
            userPosition = fields.userPosition ? fields.userPosition.split(',') : [];
            clickedPosition = fields.clickedPosition ? fields.clickedPosition.split(',') : [];
        } catch(e) {
            this.throw(400, `Position values must be separated by ','`);
        }

        let answer = {
            report: this.params.reportId,
            username: fields.username,
            organization: fields.organization,
            areaOfInterest: fields.areaOfInterest,
            language: fields.language,
            userPosition: userPosition,
            clickedPosition: clickedPosition,
{'fraction_non_alphanumeric': 0.12509465394517644, 'fraction_numerical': 0.003937604119339694, 'mean_word_length': 1.9468987059348506, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '915956', 'n_tokens_mistral': 1814, 'n_tokens_neox': 1731, 'n_words': 357}