diff --git a/src/controllers/AccountController.ts b/src/controllers/AccountController.ts index 077f40b..8672e27 100644 --- a/src/controllers/AccountController.ts +++ b/src/controllers/AccountController.ts @@ -9,6 +9,7 @@ import { UserController } from "./UserController"; import { PostsController } from "./PostsController"; import { CommentsController } from "./CommentsController"; import { LikesController } from "./LikesController"; +import { SurveyController } from "./SurveyController"; /** * Account controller @@ -240,7 +241,10 @@ export class AccountController { comments: await CommentsController.CommentsToAPI(h, data.comments), // User likes - likes: data.likes.map(LikesController.UserLikeToAPI) + likes: data.likes.map(LikesController.UserLikeToAPI), + + // Responses to surveys + survey_responses: data.surveyResponses.map(SurveyController.SurveyResponseToAPI) }; diff --git a/src/controllers/SurveyController.ts b/src/controllers/SurveyController.ts index 3fcbaa8..d4e05ba 100644 --- a/src/controllers/SurveyController.ts +++ b/src/controllers/SurveyController.ts @@ -1,6 +1,7 @@ import { Survey, SurveyChoice } from "../entities/Survey"; import { RequestHandler } from "../entities/RequestHandler"; import { SurveyHelper } from "../helpers/SurveyHelper"; +import { SurveyResponse } from "../entities/SurveyResponse"; /** * Survey controller @@ -92,4 +93,19 @@ export class SurveyController { responses: c.count } } + + /** + * Turn a {SurveyResponse} object into an API entry + * + * @param r The survey response + */ + public static SurveyResponseToAPI(r: SurveyResponse) : any { + return { + id: r.id, + time_sent: r.timeSent, + userID: r.userID, + surveyID: r.surveyID, + choiceID: r.choiceID + } + } } \ No newline at end of file diff --git a/src/entities/AccountExport.ts b/src/entities/AccountExport.ts index 1a20ecd..45c0a50 100644 --- a/src/entities/AccountExport.ts +++ b/src/entities/AccountExport.ts @@ -8,6 +8,7 @@ import { User } from "./User"; import { Post } from "./Post"; import { Comment } from "./Comment"; import { UserLike } from "./UserLike"; +import { SurveyResponse } from "./SurveyResponse"; export interface AccountExportBuilder { userID: number; @@ -15,6 +16,7 @@ export interface AccountExportBuilder { postsList: Post[]; comments: Comment[]; likes: UserLike[]; + surveyResponses: SurveyResponse[]; } export class AccountExport implements AccountExportBuilder { @@ -23,6 +25,7 @@ export class AccountExport implements AccountExportBuilder { postsList: Post[]; comments: Comment[]; likes: UserLike[]; + surveyResponses: SurveyResponse[]; public constructor(info: AccountExportBuilder) { for (const key in info) { diff --git a/src/entities/SurveyResponse.ts b/src/entities/SurveyResponse.ts new file mode 100644 index 0000000..e5b1b4c --- /dev/null +++ b/src/entities/SurveyResponse.ts @@ -0,0 +1,28 @@ +/** + * Response to a survey + * + * @author Pierre HUBERT + */ + +export interface SurveyResponseBuilder { + id: number, + timeSent: number, + userID: number, + surveyID: number, + choiceID: number +} + +export class SurveyResponse implements SurveyResponseBuilder { + id: number; + timeSent: number; + userID: number; + surveyID: number; + choiceID: number; + + public constructor(info: SurveyResponseBuilder) { + for (const key in info) { + if (info.hasOwnProperty(key)) + this[key] = info[key]; + } + } +} \ No newline at end of file diff --git a/src/helpers/AccountHelper.ts b/src/helpers/AccountHelper.ts index 597de09..e96a0d4 100644 --- a/src/helpers/AccountHelper.ts +++ b/src/helpers/AccountHelper.ts @@ -10,6 +10,7 @@ import { AccountExport } from "../entities/AccountExport"; import { PostsHelper } from "./PostsHelper"; import { CommentsHelper } from "./CommentsHelper"; import { LikesHelper } from "./LikesHelper"; +import { SurveyHelper } from "./SurveyHelper"; /** * Account helper @@ -398,6 +399,9 @@ export class AccountHelper { // Export user likes likes: await LikesHelper.ExportAllUser(userID), + + // Export all responses of user to surveys + surveyResponses: await SurveyHelper.ExportAllUserResponses(userID) }) diff --git a/src/helpers/SurveyHelper.ts b/src/helpers/SurveyHelper.ts index f109002..46b7d37 100644 --- a/src/helpers/SurveyHelper.ts +++ b/src/helpers/SurveyHelper.ts @@ -2,6 +2,7 @@ import { Survey, SurveyChoice } from "../entities/Survey"; import { DatabaseHelper, JoinType } from "./DatabaseHelper"; import { NewSurvey } from "../entities/NewSurvey"; import { mysql_date } from "../utils/DateUtils"; +import { SurveyResponse } from "../entities/SurveyResponse"; /** * Survey helper @@ -217,6 +218,20 @@ export class SurveyHelper { return result == null ? 0 /* no response yet */ : result.ID_sondage_choix; } + /** + * Export all the responses to surveys of a user + * + * @param userID The ID of the target user + */ + public static async ExportAllUserResponses(userID: number) : Promise { + return (await DatabaseHelper.Query({ + table: SURVEY_RESPONSE_TABLE, + where: { + ID_utilisateurs: userID + } + })).map(this.DBTosurveyResponse) + } + /** * Turn a database entry into a survey object @@ -246,4 +261,19 @@ export class SurveyHelper { count: row.count_choice }; } + + /** + * Turn a database entry into a survey response object + * + * @param row The row + */ + private static DBTosurveyResponse(row: any) : SurveyResponse { + return new SurveyResponse({ + id: row.ID, + timeSent: new Date(row.date_envoi).getTime()/1000, + userID: row.ID_utilisateurs, + surveyID: row.ID_sondage, + choiceID: row.ID_sondage_choix + }); + } } \ No newline at end of file