diff --git a/src/controllers/Routes.ts b/src/controllers/Routes.ts index a419f75..62e256b 100644 --- a/src/controllers/Routes.ts +++ b/src/controllers/Routes.ts @@ -280,6 +280,8 @@ export const Routes : Route[] = [ {path: "/surveys/cancel_response", cb: (h) => SurveyController.CancelResponse(h)}, + {path: "/survey/block_new_choices_creation", cb: (h) => SurveyController.BlockNewChoicesCreation(h)}, + // Notifications controller {path: "/notifications/count_unread", cb: (h) => NotificationsController.CountUnread(h)}, diff --git a/src/controllers/SurveyController.ts b/src/controllers/SurveyController.ts index e70aae3..08f8c89 100644 --- a/src/controllers/SurveyController.ts +++ b/src/controllers/SurveyController.ts @@ -2,6 +2,7 @@ import { Survey, SurveyChoice } from "../entities/Survey"; import { RequestHandler } from "../entities/RequestHandler"; import { SurveyHelper } from "../helpers/SurveyHelper"; import { SurveyResponse } from "../entities/SurveyResponse"; +import { PostAccessLevel } from "../entities/Post"; /** * Survey controller @@ -43,6 +44,19 @@ export class SurveyController { h.success(); } + /** + * Block the creation of new survey choices + * + * @param h Request handler + */ + public static async BlockNewChoicesCreation(h: RequestHandler) { + const surveyID = await this.PostSurveyIDFromPostID(h, "postID", PostAccessLevel.FULL_ACCESS); + + await SurveyHelper.BlockNewChoicesCreation(surveyID); + + h.success(); + } + /** * Turn a survey into an API entry @@ -76,9 +90,11 @@ export class SurveyController { * @param h Request handler * @param field The name of the POST field containing the * POST id + * @param minLevel Minimal access level to the post */ - private static async PostSurveyIDFromPostID(h: RequestHandler, field: string) : Promise { - const postID = await h.postPostIDWithAccess(field); + private static async PostSurveyIDFromPostID(h: RequestHandler, field: string, + minLevel?: PostAccessLevel) : Promise { + const postID = await h.postPostIDWithAccess(field, minLevel); return await SurveyHelper.GetID(postID); } diff --git a/src/helpers/SurveyHelper.ts b/src/helpers/SurveyHelper.ts index 257e391..e0950ee 100644 --- a/src/helpers/SurveyHelper.ts +++ b/src/helpers/SurveyHelper.ts @@ -135,6 +135,23 @@ export class SurveyHelper { }); } + /** + * Block new survey choices from being created + * + * @param surveyID Target survey ID + */ + public static async BlockNewChoicesCreation(surveyID: number) { + await DatabaseHelper.UpdateRows({ + table: SURVEY_INFO_TABLE, + where: { + "ID": surveyID + }, + set: { + allow_new_choices: 0 + } + }) + } + /** * Delete the survey associated to a post *