mirror of
https://gitlab.com/comunic/comunicapiv2
synced 2025-06-20 00:25:17 +00:00
Can respond to survey
This commit is contained in:
@ -14,6 +14,7 @@ import { MoviesController } from "./MoviesController";
|
||||
import { PostsController } from "./PostsController";
|
||||
import { CommentsController } from "./CommentsController";
|
||||
import { LikesController } from "./LikesController";
|
||||
import { SurveyController } from "./SurveyController";
|
||||
|
||||
/**
|
||||
* Controllers routes
|
||||
@ -221,6 +222,10 @@ export const Routes : Route[] = [
|
||||
{path: "/likes/update", cb: (h) => LikesController.Update(h)},
|
||||
|
||||
|
||||
// Surveys controller
|
||||
{path: "/surveys/send_response", cb: (h) => SurveyController.SendResponse(h)},
|
||||
|
||||
|
||||
// Notifications controller
|
||||
{path: "/notifications/count_unread", cb: (h) => NotificationsController.CountUnread(h)},
|
||||
|
||||
|
@ -10,6 +10,25 @@ import { SurveyHelper } from "../helpers/SurveyHelper";
|
||||
|
||||
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
|
||||
@ -36,6 +55,18 @@ export class SurveyController {
|
||||
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
|
||||
*
|
||||
|
Reference in New Issue
Block a user