mirror of
				https://gitlab.com/comunic/comunicapiv2
				synced 2025-11-03 19:14:03 +00:00 
			
		
		
		
	Can respond to survey
This commit is contained in:
		@@ -14,6 +14,7 @@ import { MoviesController } from "./MoviesController";
 | 
				
			|||||||
import { PostsController } from "./PostsController";
 | 
					import { PostsController } from "./PostsController";
 | 
				
			||||||
import { CommentsController } from "./CommentsController";
 | 
					import { CommentsController } from "./CommentsController";
 | 
				
			||||||
import { LikesController } from "./LikesController";
 | 
					import { LikesController } from "./LikesController";
 | 
				
			||||||
 | 
					import { SurveyController } from "./SurveyController";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * Controllers routes
 | 
					 * Controllers routes
 | 
				
			||||||
@@ -221,6 +222,10 @@ export const Routes : Route[] = [
 | 
				
			|||||||
	{path: "/likes/update", cb: (h) => LikesController.Update(h)},
 | 
						{path: "/likes/update", cb: (h) => LikesController.Update(h)},
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Surveys controller
 | 
				
			||||||
 | 
						{path: "/surveys/send_response", cb: (h) => SurveyController.SendResponse(h)},
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Notifications controller
 | 
						// Notifications controller
 | 
				
			||||||
	{path: "/notifications/count_unread", cb: (h) => NotificationsController.CountUnread(h)},
 | 
						{path: "/notifications/count_unread", cb: (h) => NotificationsController.CountUnread(h)},
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -10,6 +10,25 @@ import { SurveyHelper } from "../helpers/SurveyHelper";
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
export class SurveyController {
 | 
					export class SurveyController {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * Send the response to a survey to the server
 | 
				
			||||||
 | 
						 * 
 | 
				
			||||||
 | 
						 * @param h Request handler
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						public static async SendResponse(h: RequestHandler) {
 | 
				
			||||||
 | 
							const surveyID = await this.PostSurveyIDFromPostID(h, "postID");
 | 
				
			||||||
 | 
							const choiceID = h.postInt("choiceID");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							await SurveyHelper.CancelResponse(h.getUserId(), surveyID);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if(!await SurveyHelper.ChoiceExists(surveyID, choiceID))
 | 
				
			||||||
 | 
								h.error(404, "Choice not found for this survey!");
 | 
				
			||||||
 | 
							
 | 
				
			||||||
 | 
							await SurveyHelper.SendResponse(h.getUserId(), surveyID, choiceID);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							h.success();
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * Turn a survey into an API entry
 | 
						 * Turn a survey into an API entry
 | 
				
			||||||
@@ -36,6 +55,18 @@ export class SurveyController {
 | 
				
			|||||||
		return data;
 | 
							return data;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * Get the ID of a survey from a POST id
 | 
				
			||||||
 | 
						 * 
 | 
				
			||||||
 | 
						 * @param h Request handler
 | 
				
			||||||
 | 
						 * @param field The name of the POST field containing the
 | 
				
			||||||
 | 
						 * POST id
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						private static async PostSurveyIDFromPostID(h: RequestHandler, field: string) : Promise<number> {
 | 
				
			||||||
 | 
							const postID = await h.postPostIDWithAccess(field);
 | 
				
			||||||
 | 
							return await SurveyHelper.GetID(postID);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * Turn a survey choice into an API entry
 | 
						 * Turn a survey choice into an API entry
 | 
				
			||||||
	 * 
 | 
						 * 
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -73,7 +73,7 @@ export class SurveyHelper {
 | 
				
			|||||||
	 * 
 | 
						 * 
 | 
				
			||||||
	 * @param postID Target post ID
 | 
						 * @param postID Target post ID
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	private static async GetID(postID: number) : Promise<number> {
 | 
						public static async GetID(postID: number) : Promise<number> {
 | 
				
			||||||
		const info = await DatabaseHelper.QueryRow({
 | 
							const info = await DatabaseHelper.QueryRow({
 | 
				
			||||||
			table: SURVEY_INFO_TABLE,
 | 
								table: SURVEY_INFO_TABLE,
 | 
				
			||||||
			where: {
 | 
								where: {
 | 
				
			||||||
@@ -88,6 +88,51 @@ export class SurveyHelper {
 | 
				
			|||||||
		return info.ID;
 | 
							return info.ID;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * Cancel the response of a user to a survey
 | 
				
			||||||
 | 
						 * 
 | 
				
			||||||
 | 
						 * @param userID Target user ID
 | 
				
			||||||
 | 
						 * @param surveyID Target survey
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						public static async CancelResponse(userID: number, surveyID: number) {
 | 
				
			||||||
 | 
							await DatabaseHelper.DeleteRows(SURVEY_RESPONSE_TABLE, {
 | 
				
			||||||
 | 
								ID_sondage: surveyID,
 | 
				
			||||||
 | 
								ID_utilisateurs: userID
 | 
				
			||||||
 | 
							});
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * Check out whether a choice for the survey exists or not
 | 
				
			||||||
 | 
						 * 
 | 
				
			||||||
 | 
						 * @param surveyID Target survey ID
 | 
				
			||||||
 | 
						 * @param choiceID Target choice ID
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						public static async ChoiceExists(surveyID: number, choiceID: number) : Promise<boolean> {
 | 
				
			||||||
 | 
							return await DatabaseHelper.Count({
 | 
				
			||||||
 | 
								table: SURVEY_CHOICES_TABLE,
 | 
				
			||||||
 | 
								where: {
 | 
				
			||||||
 | 
									ID_sondage: surveyID,
 | 
				
			||||||
 | 
									ID: choiceID
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}) > 0;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * Save user response to a survey
 | 
				
			||||||
 | 
						 * 
 | 
				
			||||||
 | 
						 * @param userID Target user
 | 
				
			||||||
 | 
						 * @param surveyID Target survey
 | 
				
			||||||
 | 
						 * @param choiceID Selected choice
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						public static async SendResponse(userID: number, surveyID: number, choiceID: number) {
 | 
				
			||||||
 | 
							await DatabaseHelper.InsertRow(SURVEY_RESPONSE_TABLE, {
 | 
				
			||||||
 | 
								ID_utilisateurs: userID,
 | 
				
			||||||
 | 
								ID_sondage: surveyID,
 | 
				
			||||||
 | 
								ID_sondage_choix: choiceID,
 | 
				
			||||||
 | 
								date_envoi: mysql_date()
 | 
				
			||||||
 | 
							});
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * Delete the survey associated to a post
 | 
						 * Delete the survey associated to a post
 | 
				
			||||||
	 * 
 | 
						 * 
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user