mirror of
				https://gitlab.com/comunic/comunicapiv2
				synced 2025-11-04 03:24:04 +00:00 
			
		
		
		
	Export all responses of user to surveys
This commit is contained in:
		@@ -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)
 | 
			
		||||
		};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
@@ -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) {
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										28
									
								
								src/entities/SurveyResponse.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								src/entities/SurveyResponse.ts
									
									
									
									
									
										Normal file
									
								
							@@ -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];
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
@@ -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)
 | 
			
		||||
		})
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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<SurveyResponse[]> {
 | 
			
		||||
		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
 | 
			
		||||
		});
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user