22const mongoose = require ( 'mongoose' ) ;
33const userProfile = require ( '../models/userProfile' ) ;
44const helper = require ( '../utilities/permissions' ) ;
5+ const { setOutdatedWarningsFlag } = require ( '../utilities/warningsCache' ) ;
56const currentWarningsController = function ( currentWarnings ) {
67 const normalizeWarningTitle = ( warningTitle /*: string */ ) => warningTitle . toLowerCase ( ) . trim ( ) ;
78
89 const checkIfSpecialCharacter = ( warning ) => {
910 return ! / ^ [ a - z A - Z ] [ a - z A - Z 0 - 9 , + - ] * (?: [ a - z A - Z 0 - 9 , + - ] + ) * $ / . test ( warning ) ;
1011 } ;
1112
13+ const addMissingOrderValues = async ( warnings ) => {
14+ const updates = [ ] ;
15+ warnings . forEach ( ( warning , index ) => {
16+ if ( warning . order === null || warning . order === undefined || warning . order === - 1 ) {
17+ updates . push ( {
18+ updateOne : {
19+ filter : { _id : warning . _id } ,
20+ update : { $set : { order : index } } ,
21+ } ,
22+ } ) ;
23+ }
24+ } ) ;
25+
26+ if ( updates . length > 0 ) {
27+ await currentWarnings . bulkWrite ( updates ) ;
28+ }
29+ // only updates warnings missing an order value
30+ return warnings . map ( ( warning , index ) => ( {
31+ ...warning ,
32+ order : warning . order ?? index ,
33+ } ) ) ;
34+ } ;
35+
1236 const getCurrentWarnings = async ( req , res ) => {
1337 try {
14- const response = await currentWarnings . find ( { } ) ;
38+ const response = await currentWarnings . find ( { } ) . sort ( { order : 1 } ) ;
1539
1640 if ( response . length === 0 ) {
1741 return res . status ( 400 ) . send ( { message : 'No records' , response : response } ) ;
1842 }
19- return res . status ( 200 ) . send ( { currentWarningDescriptions : response } ) ;
43+
44+ const missingOrder = response . some (
45+ ( warning ) => warning . order === null || warning . order === undefined || warning . order === - 1 ,
46+ ) ;
47+
48+ const updatedWarnings = missingOrder ? await addMissingOrderValues ( response ) : response ;
49+ return res . status ( 200 ) . send ( { currentWarningDescriptions : updatedWarnings } ) ;
2050 } catch ( error ) {
51+ console . log ( 'Entered error of fetch warnings' ) ;
2152 res . status ( 401 ) . send ( { message : error . message || error } ) ;
2253 }
2354 } ;
@@ -55,6 +86,7 @@ const currentWarningsController = function (currentWarnings) {
5586 isPermanent,
5687 } ) . save ( ) ;
5788
89+ setOutdatedWarningsFlag ( ) ;
5890 const updatedWarnings = await currentWarnings . find ( { } ) ;
5991 return res . status ( 201 ) . send ( { newWarnings : updatedWarnings } ) ;
6092 } catch ( error ) {
@@ -63,6 +95,14 @@ const currentWarningsController = function (currentWarnings) {
6395 } ;
6496
6597 const editWarningDescription = async ( req , res ) => {
98+ if (
99+ ! ( await helper . hasPermission ( req . body . requestor , 'addWarningTracker' ) ) &&
100+ ! ( await helper . hasPermission ( req . body . requestor , 'deleteWarningTracker' ) )
101+ ) {
102+ res . status ( 403 ) . send ( 'You are not authorized to edit a WarningTracker.' ) ;
103+ return ;
104+ }
105+
66106 try {
67107 const { editedWarning } = req . body ;
68108 const normalizedWarningTitle = normalizeWarningTitle ( editedWarning . warningTitle ) ;
@@ -91,11 +131,49 @@ const currentWarningsController = function (currentWarnings) {
91131 warning . warningTitle = trimmedWarning ;
92132
93133 await warning . save ( ) ;
134+ setOutdatedWarningsFlag ( ) ;
94135 res . status ( 201 ) . send ( { message : 'warning description was updated' } ) ;
95136 } catch ( error ) {
96137 res . status ( 401 ) . send ( { message : error . message || error } ) ;
97138 }
98139 } ;
140+
141+ const reorderWarningDescriptions = async ( req , res ) => {
142+ if (
143+ ! ( await helper . hasPermission ( req . body . requestor , 'addWarningTracker' ) ) &&
144+ ! ( await helper . hasPermission ( req . body . requestor , 'deleteWarningTracker' ) )
145+ ) {
146+ res . status ( 403 ) . send ( 'You are not authorized to edit the order of the WarningTrackers.' ) ;
147+ return ;
148+ }
149+ try {
150+ const reorderedWarningDescriptions = req . body . warningDescriptions ;
151+ const response = await currentWarnings . find ( { } ) . sort ( { order : 1 } ) ;
152+
153+ reorderedWarningDescriptions . map ( ( warning , index ) => ( warning . order = index ) ) ;
154+ await currentWarnings . bulkWrite (
155+ response . map ( ( warning , index ) => ( {
156+ updateOne : {
157+ filter : { _id : warning . _id } ,
158+ update : {
159+ $set : {
160+ order : reorderedWarningDescriptions . findIndex (
161+ ( warn ) => warn . warningTitle === warning . warningTitle ,
162+ ) ,
163+ } ,
164+ } ,
165+ } ,
166+ } ) ) ,
167+ ) ;
168+
169+ setOutdatedWarningsFlag ( ) ;
170+ res . status ( 201 ) . send ( {
171+ reorderedWarningDescriptions : reorderedWarningDescriptions ,
172+ } ) ;
173+ } catch ( error ) {
174+ res . status ( 401 ) . send ( { message : error . message || error } ) ;
175+ }
176+ } ;
99177 const updateWarningDescription = async ( req , res ) => {
100178 if (
101179 ! ( await helper . hasPermission ( req . body . requestor , 'reactivateWarningTracker' ) ) &&
@@ -116,7 +194,7 @@ const currentWarningsController = function (currentWarnings) {
116194 [ { $set : { activeWarning : { $not : '$activeWarning' } } } ] ,
117195 { new : true } ,
118196 ) ;
119-
197+ setOutdatedWarningsFlag ( ) ;
120198 res . status ( 201 ) . send ( { message : 'warning description was updated' } ) ;
121199 } catch ( error ) {
122200 res . status ( 401 ) . send ( { message : error . message || error } ) ;
@@ -148,7 +226,7 @@ const currentWarningsController = function (currentWarnings) {
148226 } ,
149227 } ,
150228 ) ;
151-
229+ setOutdatedWarningsFlag ( ) ;
152230 return res . status ( 200 ) ;
153231 } catch ( error ) {
154232 res . status ( 401 ) . send ( { message : error . message || error } ) ;
@@ -161,6 +239,7 @@ const currentWarningsController = function (currentWarnings) {
161239 updateWarningDescription,
162240 deleteWarningDescription,
163241 editWarningDescription,
242+ reorderWarningDescriptions,
164243 } ;
165244} ;
166245module . exports = currentWarningsController ;
0 commit comments