From d4ec8922888dd8117c470bbc1a61f6559f338ac4 Mon Sep 17 00:00:00 2001 From: Pau Date: Mon, 14 Apr 2025 01:17:12 +0200 Subject: [PATCH] added ratings, count and a range validator --- .../resource_types/coordinatesRepository.js | 6 ++--- .../resource_types/placesRepository.js | 7 +++--- .../resource_types/reportTypesRepository.js | 7 ++---- .../usersHasPlacesRepository.js | 22 ++++++++++--------- src/routes/index.js | 17 +++++++++----- .../expressValidator/customValidators.js | 2 ++ 6 files changed, 33 insertions(+), 28 deletions(-) diff --git a/src/repositories/resource_types/coordinatesRepository.js b/src/repositories/resource_types/coordinatesRepository.js index d820902..49326f2 100644 --- a/src/repositories/resource_types/coordinatesRepository.js +++ b/src/repositories/resource_types/coordinatesRepository.js @@ -6,12 +6,12 @@ const _coordinatesSelectQuery = (_pagination = '') => ({ count }) => ({ uuid, la const longitudeCondition = longitude ? 'AND c.longitude = :longitude ' : ''; return ` - SELECT - c.uuid, + SELECT ${count || + `c.uuid, c.created, c.createdby, c.latitude, - c.longitude + c.longitude`} FROM acloc.coordinates AS c WHERE c.created <= :now AND (c.deleted > :now OR c.deleted IS NULL) diff --git a/src/repositories/resource_types/placesRepository.js b/src/repositories/resource_types/placesRepository.js index 67230f4..965cff2 100644 --- a/src/repositories/resource_types/placesRepository.js +++ b/src/repositories/resource_types/placesRepository.js @@ -7,15 +7,14 @@ const _placeSelectQuery = (_pagination = '') => ({ count }) => ({ uuid, name, de const addressCondition = address ? `AND p.address LIKE CONCAT('%',:address,'%')` : ''; const fk_coordinateCondition = latitude && longitude ? 'AND fk_coordinate = (SELECT id FROM acloc.coordinates WHERE latitude = :latitude AND longitude = :longitude)' : ''; return ` - SELECT - p.uuid, + SELECT ${count || + `p.uuid, p.name, p.description, p.address, c.latitude, c.longitude, - p.created, - p.createdby + p.created`} FROM acloc.places AS p JOIN acloc.coordinates AS c ON p.fk_coordinate = c.id WHERE p.deleted IS NULL diff --git a/src/repositories/resource_types/reportTypesRepository.js b/src/repositories/resource_types/reportTypesRepository.js index 8e18a01..319da60 100644 --- a/src/repositories/resource_types/reportTypesRepository.js +++ b/src/repositories/resource_types/reportTypesRepository.js @@ -4,11 +4,8 @@ const _coordinatesSelectQuery = (_pagination = '') => ({ count }) => ({ uuid, na const uuidCondition = uuid ? 'AND rt.uuid = :uuid ' : ''; const nameCondition = name ? `AND rt.name LIKE CONCAT('%',:name,'%')` : ''; return ` - SELECT - rt.uuid, - rt.name, - rt.created, - rt.createdby + SELECT ${count || + `*`} FROM acloc.report_types AS rt WHERE rt.created <= :now AND (rt.deleted > :now OR rt.deleted IS NULL) diff --git a/src/repositories/resource_types/usersHasPlacesRepository.js b/src/repositories/resource_types/usersHasPlacesRepository.js index 57d0c43..c77d20e 100644 --- a/src/repositories/resource_types/usersHasPlacesRepository.js +++ b/src/repositories/resource_types/usersHasPlacesRepository.js @@ -1,24 +1,20 @@ import { pagination } from "../../utils/pagination.js"; //reports repository -const _userHasPlacesSelectQuery = (_pagination = '') => ({ count }) => ({ uuid, uuidUser, uuidPlace, uuidReportType }) => { +const _userHasPlacesSelectQuery = (_pagination = '') => ({ count }) => ({ uuid, uuidUser, uuidPlace, uuidReportType, rating }) => { const uuidCondition = uuid ? 'AND up.uuid = :uuid ' : ''; const uuidUserCondition = uuidUser ? 'AND up.fk_user = (SELECT id FROM acloc.users WHERE uuid = :uuidUser)' : ''; const uuidPlaceCondition = uuidPlace ? 'AND up.fk_place = (SELECT id FROM acloc.places WHERE uuid = :uuidPlace)' : ''; const uuidReportTypeCondition = uuidReportType ? 'AND up.fk_report_type = (SELECT id FROM acloc.report_types WHERE uuid = :uuidReportType)' : ''; - + const ratingCondition = rating ? 'AND up.rating = :rating' : ''; return ` - SELECT - up.uuid, - up.fk_user, - up.fk_place, - up.created, - up.createdby, + SELECT ${count || + `up.*, u.username AS user_username, u.uuid AS user_uuid, p.name AS place_name, p.uuid AS place_uuid, rt.name AS report_type_name, - rt.uuid AS report_type_uuid + rt.uuid AS report_type_uuid`} FROM acloc.users_has_places AS up JOIN acloc.users AS u ON up.fk_user = u.id JOIN acloc.places AS p ON up.fk_place = p.id @@ -33,6 +29,7 @@ const _userHasPlacesSelectQuery = (_pagination = '') => ({ count }) => ({ uuid, ${uuidUserCondition} ${uuidPlaceCondition} ${uuidReportTypeCondition} + ${ratingCondition} ${_pagination} `; } @@ -48,12 +45,14 @@ const insertUserHasPlacesQuery = ({ uuidUser, uuidPlace, createdBy, uuidReportTy const uuidPlaceCondition = uuidPlace ? '(SELECT id FROM acloc.places WHERE uuid = :uuidPlace),' : null; const createdByCondition = createdBy ? 'createdBy = :createdBy' : null; const reportTypeCondition = uuidReportType ? '(SELECT id FROM acloc.report_types WHERE uuid = :reportType),' : null; + return ` INSERT INTO acloc.users_has_places ( uuid, fk_user, fk_place, fk_report_type, + rating, created, createdBy ) @@ -62,6 +61,7 @@ const insertUserHasPlacesQuery = ({ uuidUser, uuidPlace, createdBy, uuidReportTy ${uuidUserCondition} ${uuidPlaceCondition} ${reportTypeCondition} + :rating, :now, ${createdByCondition} ); @@ -69,16 +69,18 @@ const insertUserHasPlacesQuery = ({ uuidUser, uuidPlace, createdBy, uuidReportTy ` } -const modifyUserHasPlacesQuery = (uuidUser, uuidPlace, uuidReportType) => { +const modifyUserHasPlacesQuery = (uuidUser, uuidPlace, uuidReportType, rating) => { const uuidUserCondition = uuidUser ? 'fk_user = (SELECT id FROM acloc.users WHERE uuid = :uuidUser)' : ``; const uuidPlaceCondition = uuidPlace ? 'fk_place = (SELECT id FROM acloc.places WHERE uuid = :uuidPlace)' : ``; const uuidReportTypeCondition = uuidReportType ? 'fk_report_type = (SELECT id FROM acloc.report_types WHERE uuid = :uuidReportType)' : ``; + const ratingCondition = rating ? 'rating = :rating' : ``; return ` UPDATE acloc.users_has_places SET ${uuidUserCondition} ${uuidPlaceCondition} ${uuidReportTypeCondition} + ${ratingCondition} uuid = :uuid WHERE users_has_places.uuid = :uuid diff --git a/src/routes/index.js b/src/routes/index.js index 09c5368..81c75f8 100644 --- a/src/routes/index.js +++ b/src/routes/index.js @@ -15,7 +15,8 @@ import { sendOkResponse, sendResponseNoContent, } from '../utils/responses.js' -import { uuid, varChar} from '../validators/expressValidator/customValidators.js' +import { integer, uuid, varChar } from '../validators/expressValidator/customValidators.js' +import { integerRange } from '../validators/expressValidator/integerRangeValidator.js' // Adjust the path as needed import {payloadExpressValidator} from '../validators/expressValidator/payloadExpressValidator.js' import { authorizePermission, setToken, authenticateToken, refreshAuthenticate} from '../middlewares/auth.js' import { postRegisterController } from '../controllers/authorization/registerController.js' @@ -1181,7 +1182,8 @@ export default(config) => { uuid('uuid'), uuid('uuidPlace').optional({ nullable: false, values: 'falsy' }), uuid('uuidUser').optional({ nullable: false, values: 'falsy' }), - uuid('uuidReportType').optional({ nullable: false, values: 'falsy' }) + uuid('uuidReportType').optional({ nullable: false, values: 'falsy' }), + integerRange('rating', {min: 1, max: 3}).optional({ nullable: false, values: 'falsy' }) ], (req, res, next) => payloadExpressValidator(req, res, next, config), (req, res, next) => getUserHasPlacesListController(req, res, next, config), @@ -1214,7 +1216,8 @@ export default(config) => { uuid('uuid'), uuid('uuidPlace').optional({ nullable: false, values: 'falsy' }), uuid('uuidUser').optional({ nullable: false, values: 'falsy' }), - uuid('uuidReportType').optional({ nullable: false, values: 'falsy' }) + uuid('uuidReportType').optional({ nullable: false, values: 'falsy' }), + integerRange('rating', {min: 1, max: 3}).optional({ nullable: false, values: 'falsy' }) ], (req, res, next) => payloadExpressValidator(req, res, next, config), (req, res, next) => getUserHasPlacesByUuidController(req, res, next, config), @@ -1245,9 +1248,10 @@ export default(config) => { (req, res, next) => authenticateToken(req, res, next, config), (req, res, next) => authorizePermission('/reports')(req, res, next, config), [ - uuid('uuidPlace').optional({ nullable: false, values: 'falsy' }), - uuid('uuidUser').optional({ nullable: false, values: 'falsy' }), - uuid('uuidReportType').optional({ nullable: false, values: 'falsy' }), + uuid('uuidPlace'), + uuid('uuidUser'), + uuid('uuidReportType'), + integerRange('rating', {min: 1, max: 3}), varChar('description').optional({ nullable: true, values: 'falsy' }), ], (req, res, next) => payloadExpressValidator(req, res, next, config), @@ -1284,6 +1288,7 @@ export default(config) => { uuid('uuidPlace').optional({ nullable: false, values: 'falsy' }), uuid('uuidUser').optional({ nullable: false, values: 'falsy' }), uuid('uuidReportType').optional({ nullable: false, values: 'falsy' }), + integerRange('rating', {min: 1, max: 3}).optional({ nullable: false, values: 'falsy' }), varChar('description').optional({ nullable: true, values: 'falsy' }), ], (req, res, next) => payloadExpressValidator(req, res, next, config), diff --git a/src/validators/expressValidator/customValidators.js b/src/validators/expressValidator/customValidators.js index 31d6dc9..91e6991 100644 --- a/src/validators/expressValidator/customValidators.js +++ b/src/validators/expressValidator/customValidators.js @@ -2,11 +2,13 @@ import { check } from 'express-validator' const varChar = (field, { max = 255 } = {}) => check(field).isString().trim().isLength({ min: 1, max }).withMessage(`|${field}| must be a string with a length between 1 and ${max}`) const integer = field => check(field).isInt({min: Number.MIN_SAFE_INTEGER, max: Number.MAX_SAFE_INTEGER}).withMessage(`|${field}| must be an integer`) +const integerRange = (field, { min = Number.MIN_SAFE_INTEGER, max = Number.MAX_SAFE_INTEGER } = {}) => check(field).isInt({min, max}).withMessage(`|${field}| must be an integer between ${min} and ${max}`) const uuid = field => check(field).isUUID('all').withMessage(`|${field}| must be a valid UUID`) const bigInt = field => check(field).isBigInt().withMessage(`|${field}| must be a valid BigInt`) export { integer, + integerRange, uuid, varChar, bigInt