diff --git a/src/controllers/SurveyController.ts b/src/controllers/SurveyController.ts index 23c2f38..b499927 100644 --- a/src/controllers/SurveyController.ts +++ b/src/controllers/SurveyController.ts @@ -1,5 +1,6 @@ import { Survey, SurveyChoice } from "../entities/Survey"; import { RequestHandler } from "../entities/RequestHandler"; +import { SurveyHelper } from "../helpers/SurveyHelper"; /** * Survey controller @@ -29,6 +30,9 @@ export class SurveyController { survey.choices.forEach((c) => data.choices[c.id.toString()] = this.SurveyChoiceToAPI(c)) + if(h.signedIn) + data.user_choice = await SurveyHelper.GetUserChoice(survey.id, h.getUserId()); + return data; } diff --git a/src/helpers/SurveyHelper.ts b/src/helpers/SurveyHelper.ts index 740f856..d4a22af 100644 --- a/src/helpers/SurveyHelper.ts +++ b/src/helpers/SurveyHelper.ts @@ -71,6 +71,24 @@ export class SurveyHelper { return survey; } + /** + * Get the choice of a user for a survey + * + * @param surveyID Target survey + * @param userID Target user + */ + public static async GetUserChoice(surveyID: number, userID: number) { + const result = await DatabaseHelper.QueryRow({ + table: SURVEY_RESPONSE_TABLE, + where: { + ID_utilisateurs: userID, + ID_sondage: surveyID + } + }); + + return result == null ? 0 /* no response yet */ : result.ID_sondage_choix; + } + /** * Turn a database entry into a survey object