@@ -9,6 +9,29 @@ import jwt from 'jsonwebtoken';
99import R from '../utils/responseBuilder.js' ;
1010import { requirePlace } from '../utils/controllerValidators.js' ;
1111
12+ // --- Helper Functions (Private) ---
13+
14+ /** Allowed fields for place updates */
15+ const ALLOWED_PLACE_FIELDS = [ 'name' , 'description' , 'category' , 'website' ] ;
16+
17+ /** Prepare update DTO from request body, picking only allowed fields */
18+ const preparePlaceUpdateDTO = ( body , existingLocation ) => {
19+ const updateData = { } ;
20+ ALLOWED_PLACE_FIELDS . forEach ( k => { if ( body [ k ] ) updateData [ k ] = body [ k ] ; } ) ;
21+ if ( body . location ) updateData . location = { ...existingLocation , ...body . location } ;
22+ return updateData ;
23+ } ;
24+
25+ /** Add HATEOAS links to each report */
26+ const enrichReportsWithLinks = ( reports , placeId , adminId ) => {
27+ return reports . map ( report => {
28+ const reportObj = report . toObject ? report . toObject ( ) : report ;
29+ return { ...reportObj , links : buildHateoasLinks . adminReport ( placeId , adminId ) } ;
30+ } ) ;
31+ } ;
32+
33+ // --- Controllers ---
34+
1235const getPlaceReports = async ( req , res , next ) => {
1336 try {
1437 const adminId = parseInt ( req . params . adminId ) ;
@@ -18,10 +41,7 @@ const getPlaceReports = async (req, res, next) => {
1841 if ( ! place ) return ;
1942
2043 const placeReports = await db . getReportsForPlace ( placeId ) ;
21- const reportsWithLinks = placeReports . map ( report => {
22- const reportObj = report . toObject ? report . toObject ( ) : report ;
23- return { ...reportObj , links : buildHateoasLinks . adminReport ( placeId , adminId ) } ;
24- } ) ;
44+ const reportsWithLinks = enrichReportsWithLinks ( placeReports , placeId , adminId ) ;
2545
2646 return R . success ( res , { reports : reportsWithLinks , totalReports : reportsWithLinks . length , links : buildHateoasLinks . adminReportsCollection ( adminId , placeId ) } , 'Place reports retrieved successfully' ) ;
2747 } catch ( error ) { next ( error ) ; }
@@ -36,9 +56,7 @@ const updatePlace = async (req, res, next) => {
3656 if ( ! place ) return ;
3757
3858 const placeObj = place . toObject ? place . toObject ( ) : place ;
39- const updateData = { } ;
40- [ 'name' , 'description' , 'category' , 'website' ] . forEach ( k => { if ( req . body [ k ] ) updateData [ k ] = req . body [ k ] ; } ) ;
41- if ( req . body . location ) updateData . location = { ...placeObj . location , ...req . body . location } ;
59+ const updateData = preparePlaceUpdateDTO ( req . body , placeObj . location ) ;
4260
4361 const updatedPlace = await db . updatePlace ( placeId , updateData ) ;
4462 const updatedPlaceObj = updatedPlace . toObject ? updatedPlace . toObject ( ) : updatedPlace ;
@@ -68,3 +86,4 @@ const generateAdminToken = (req, res, next) => {
6886} ;
6987
7088export default { getPlaceReports, updatePlace, generateAdminToken } ;
89+
0 commit comments