Skip to content
Snippets Groups Projects
Commit 58482412 authored by Stephanie Briere Americo's avatar Stephanie Briere Americo
Browse files

Merge branch 'issue/75' into 'develop'

Issue #75: Fix route to list forms

See merge request !75
parents a6cc08e9 20fd9532
Branches
No related tags found
1 merge request!75Issue #75: Fix route to list forms
Pipeline #23266 passed
......@@ -5,6 +5,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## 1.2.5 - 02/06/2020
## Changed
- Route to list forms now returns all the dates and answers of the forms #75 (Richard Heise)
- Added two extras steps on route waterfall using eachSeries.
- Tests weren't changed since the steps where tested by themselfs in other routes.
## 1.2.4 - 01/06/2020
## Added
- Created route to get modified dates of a form #76 (Richard Heise)
......
{
"name": "form-creator-api",
"version": "1.2.4",
"version": "1.2.5",
"description": "RESTful API used to manage and answer forms.",
"main": "index.js",
"scripts": {
......
......@@ -23,10 +23,11 @@ import { User } from "../../core/user";
import { Response, NextFunction } from "express";
import { Request } from "../apiTypes";
import { OptHandler } from "../../utils/optHandler";
import { waterfall } from "async";
import { eachSeries, map, waterfall } from "async";
import * as bcrypt from "bcrypt";
import * as jwt from "jsonwebtoken";
import { Form } from "../../core/form";
import { FormAnswer, FormAnswerOptions } from "../../core/formAnswer";
export class UserCtrl {
......@@ -231,6 +232,8 @@ export class UserCtrl {
public static listForms(req: Request, res: Response, next: NextFunction) {
waterfall([
(callback: (err: Error, forms?: any[]) => void) => {
req.db.form.list(req.params.id, (err: Error, forms?: Form[]) => {
if (err) {
res.status(500).json({
......@@ -244,11 +247,62 @@ export class UserCtrl {
id: form.id
, title: form.title
, description: form.description
, answersNumber: 0
, date: ""
}));
res.json(mappedForms);
callback(null, mappedForms);
});
},
(forms: any[], callback: (err: Error, result?: Object[]) => void) => {
eachSeries(forms, (form: any, innerCallback) => {
req.db.answer.readAll(form.id, (err: Error, resultAnswer?: FormAnswer[]) => {
if (err) {
innerCallback(err);
return;
}
let sum: number = 0;
for (const forms of resultAnswer) {
sum += Object.keys(forms.inputAnswers).length
}
form.answersNumber = sum;
innerCallback(null);
});
}, (e) => {
callback(e, forms);
});
},
(forms: any[], callback: (err: Error, result?: Object[]) => void) => {
eachSeries(forms, (form: any, innerCallback) => {
req.db.form.readDate(form.id, (err: Error, dates?: any[]) => {
if (err) {
innerCallback(err);
return;
}
if (dates.length) {
form.date = dates.sort().slice(-1)[0].update_date;
}
innerCallback(null);
});
}, (e) => {
callback(e, forms);
});
},
], (error: Error, forms) => {
if (error) {
res.status(500).json({
message: "Some error has ocurred. Check error property for details.",
error: error.message
});
return;
}
res.json(forms);
});
}
public static update(req: Request, res: Response, next: NextFunction) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment