From f72dd027b03a7ae72d7290bd764d15a84ade8403 Mon Sep 17 00:00:00 2001 From: Pau Date: Tue, 8 Apr 2025 00:38:43 +0200 Subject: [PATCH] added and fixed resource types repositories --- .../resource_types/coordinatesRepository.js | 84 +++++++++++++++++++ .../resource_types/placesRepository.js | 63 ++++++-------- .../resource_types/reportTypesRepository.js | 78 +++++++++++++++++ .../usersHasPlacesRepository.js | 48 ++++++----- 4 files changed, 215 insertions(+), 58 deletions(-) create mode 100644 src/repositories/resource_types/reportTypesRepository.js diff --git a/src/repositories/resource_types/coordinatesRepository.js b/src/repositories/resource_types/coordinatesRepository.js index e69de29..209e6eb 100644 --- a/src/repositories/resource_types/coordinatesRepository.js +++ b/src/repositories/resource_types/coordinatesRepository.js @@ -0,0 +1,84 @@ +import { pagination } from "../../utils/pagination"; + +const _coordinatesSelectQuery = (_pagination = '') => ({ count }) => ({ uuid, latitude, longitude }) => { + const uuidCondition = uuid ? 'AND c.uuid = :uuid ' : ''; + const latitudeCondition = latitude ? 'AND c.latitude = :latitude ' : ''; + const longitudeCondition = longitude ? 'AND c.longitude = :longitude ' : ''; + + return ` + SELECT + c.uuid, + c.created, + c.createdby, + c.latitude, + c.longitude + FROM acloc.coordinates AS c + WHERE c.created <= :now + AND (c.deleted > :now OR c.deleted IS NULL) + AND true + ${uuidCondition} + ${latitudeCondition} + ${longitudeCondition} + ${_pagination} + `; +} + +const getCoordinatesListQuery = ({ limit, page, ...rest }) => + _coordinatesSelectQuery(pagination({ limit, page }))({ count: false })(rest); + +const countCoordinatesListQuery = rest => + _coordinatesSelectQuery()({ count: 'COUNT(DISTINCT(c.uuid)) AS count' })(rest); + +const insertCoordinatesQuery = () => { + return ` + INSERT INTO acloc.coordinates ( + uuid, + latitude, + longitude, + created, + createdBy + ) + VALUES ( + :uuid, + :latitude, + :longitude, + :now, + :createdBy + ); + SELECT * FROM acloc.coordinates WHERE uuid = :uuid; + ` +} + +const updateCoordinatesQuery = (latitude, longitude) => { + const latitudeCondition = latitude ? 'latitude = :latitude,' : ''; + const longitudeCondition = longitude ? 'longitude = :longitude,' : ''; + return ` + UPDATE acloc.coordinates + SET + ${latitudeCondition} + ${longitudeCondition} + uuid = :uuid + WHERE uuid = :uuid + AND deleted IS NULL; + SELECT * FROM acloc.coordinates WHERE uuid = :uuid; + ` +} + +const softDeleteCoordinatesQuery = () => { + return ` + UPDATE + acloc.coordinates + SET + deleted = :deleted, deletedby = :deletedBy + WHERE uuid = :uuid + AND deleted IS NULL; + ` +} + +export { + getCoordinatesListQuery, + countCoordinatesListQuery, + insertCoordinatesQuery, + updateCoordinatesQuery, + softDeleteCoordinatesQuery +} \ No newline at end of file diff --git a/src/repositories/resource_types/placesRepository.js b/src/repositories/resource_types/placesRepository.js index 4c5dd38..67230f4 100644 --- a/src/repositories/resource_types/placesRepository.js +++ b/src/repositories/resource_types/placesRepository.js @@ -5,8 +5,7 @@ const _placeSelectQuery = (_pagination = '') => ({ count }) => ({ uuid, name, de const nameCondition = name ? `AND p.name LIKE CONCAT('%',:name,'%')` : ''; const descriptionCondition = description ? `AND p.description LIKE CONCAT('%',:description,'%')` : ''; const addressCondition = address ? `AND p.address LIKE CONCAT('%',:address,'%')` : ''; - const latitudeCondition = latitude ? 'AND p.latitude = :latitude ' : ''; - const longitudeCondition = longitude ? 'AND p.longitude = :longitude ' : ''; + const fk_coordinateCondition = latitude && longitude ? 'AND fk_coordinate = (SELECT id FROM acloc.coordinates WHERE latitude = :latitude AND longitude = :longitude)' : ''; return ` SELECT p.uuid, @@ -18,7 +17,7 @@ const _placeSelectQuery = (_pagination = '') => ({ count }) => ({ uuid, name, de p.created, p.createdby FROM acloc.places AS p - JOIN acloc.coordinates AS c ON p.fk_coordinates = c.id + JOIN acloc.coordinates AS c ON p.fk_coordinate = c.id WHERE p.deleted IS NULL AND p.created <= :now AND (p.deleted > :now OR p.deleted IS NULL) @@ -27,8 +26,7 @@ const _placeSelectQuery = (_pagination = '') => ({ count }) => ({ uuid, name, de ${nameCondition} ${descriptionCondition} ${addressCondition} - ${latitudeCondition} - ${longitudeCondition} + ${fk_coordinateCondition} ${_pagination} `; } @@ -39,12 +37,9 @@ const getPlaceListQuery = ({ limit, page, ...rest }) => const countPlaceListQuery = rest => _placeSelectQuery()({ count: 'COUNT(DISTINCT(p.uuid)) AS count' })(rest); -const insertPlaceQuery = ({ name, description, address, latitude, longitude, createdBy }) => { - const nameCondition = name ? ':name' : null; +const insertPlaceQuery = ({ description, address, createdBy }) => { const descriptionCondition = description ? ':description' : null; const addressCondition = address ? ':address' : null; - const latitudeCondition = latitude ? ':latitude' : null; - const longitudeCondition = longitude ? ':longitude' : null; const createdByCondition = createdBy ? 'createdBy = :createdBy' : null; return ` INSERT INTO acloc.places ( @@ -52,63 +47,53 @@ const insertPlaceQuery = ({ name, description, address, latitude, longitude, cre name, description, address, - latitude, - longitude, + fk_coordinates, created, createdBy ) VALUES ( - UUID(), - ${nameCondition}, + :uuid, + :name, ${descriptionCondition}, ${addressCondition}, - ${latitudeCondition}, - ${longitudeCondition}, - NOW(), + (SELECT id FROM acloc.coordinates WHERE latitude = :latitude AND longitude = :longitude), + :now, ${createdByCondition} ) `; } -const updatePlaceQuery = ({ uuid, name, description, address, latitude, longitude, updatedBy }) => { - const uuidCondition = uuid ? 'AND p.uuid = :uuid ' : ''; - const nameCondition = name ? ':name' : null; - const descriptionCondition = description ? ':description' : null; - const addressCondition = address ? ':address' : null; - const latitudeCondition = latitude ? ':latitude' : null; - const longitudeCondition = longitude ? ':longitude' : null; - const updatedByCondition = updatedBy ? 'updatedBy = :updatedBy' : null; +const updatePlaceQuery = ({ name, description, address, latitude, longitude }) => { + const nameCondition = name ? 'name = :name,' : ''; + const descriptionCondition = description ? 'description = :description,' : ''; + const addressCondition = address ? 'address = :address' : ''; + const fk_coordinateCondition = latitude && longitude ? 'fk_coordinate = (SELECT id FROM acloc.coordinates WHERE latitude = :latitude AND longitude = :longitude),' : ''; + return ` UPDATE acloc.places AS p SET - name = ${nameCondition}, - description = ${descriptionCondition}, - address = ${addressCondition}, - latitude = ${latitudeCondition}, - longitude = ${longitudeCondition}, - updated = NOW(), - ${updatedByCondition} - WHERE p.deleted IS NULL + ${nameCondition} + ${descriptionCondition} + ${addressCondition} + ${fk_coordinateCondition} + WHERE + p.uuid = :uuid + AND p.deleted IS NULL AND p.created <= NOW() AND (p.deleted > NOW() OR p.deleted IS NULL) - ${uuidCondition} `; } -const deletePlaceQuery = ({ uuid, deletedBy }) => { - const uuidCondition = uuid ? 'AND p.uuid = :uuid ' : ''; - const deletedByCondition = deletedBy ? 'deletedBy = :deletedBy' : null; +const deletePlaceQuery = () => { return ` UPDATE acloc.places AS p SET - deleted = NOW(), - ${deletedByCondition} + deleted = :deleted, deletedby = :deletedBy WHERE p.uuid = :uuid AND p.deleted IS NULL AND p.created <= NOW() AND (p.deleted > NOW() OR p.deleted IS NULL) - ${uuidCondition} `; } diff --git a/src/repositories/resource_types/reportTypesRepository.js b/src/repositories/resource_types/reportTypesRepository.js new file mode 100644 index 0000000..379f616 --- /dev/null +++ b/src/repositories/resource_types/reportTypesRepository.js @@ -0,0 +1,78 @@ +import { pagination } from "../../utils/pagination"; + +const _coordinatesSelectQuery = (_pagination = '') => ({ count }) => ({ uuid, name }) => { + 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 + FROM acloc.report_types AS rt + WHERE rt.created <= :now + AND (rt.deleted > :now OR rt.deleted IS NULL) + AND true + ${uuidCondition} + ${nameCondition} + ${_pagination} + `; +} + +const getReportTypesListQuery = ({ limit, page, ...rest }) => + _coordinatesSelectQuery(pagination({ limit, page }))({ count: false })(rest); + +const countReportTypesListQuery = rest => + _coordinatesSelectQuery()({ count: 'COUNT(DISTINCT(rt.uuid)) AS count' })(rest); + +const insertReportTypesQuery = () => { + return ` + INSERT INTO acloc.report_types ( + uuid, + name, + created, + createdBy + ) + VALUES ( + :uuid, + :name, + :now, + :createdBy + ); + SELECT * FROM acloc.report_types WHERE uuid = :uuid; + ` +} + +const updateReportTypesQuery = (name) => { + const nameCondition = name ? 'name = :name,' : ''; + return ` + UPDATE acloc.report_types + SET + ${nameCondition} + uuid = :uuid + WHERE uuid = :uuid + AND deleted IS NULL; + SELECT * FROM acloc.report_types WHERE uuid = :uuid; + ` +} + +const softDeleteReportTypesQuery = () => { + return ` + UPDATE + acloc.report_types + SET + deleted = :deleted, + deletedBy = :deletedBy + WHERE uuid = :uuid + AND deleted IS NULL; + SELECT * FROM acloc.report_types WHERE uuid = :uuid; + ` +} + +export { + getReportTypesListQuery, + countReportTypesListQuery, + insertReportTypesQuery, + updateReportTypesQuery, + softDeleteReportTypesQuery +} \ No newline at end of file diff --git a/src/repositories/resource_types/usersHasPlacesRepository.js b/src/repositories/resource_types/usersHasPlacesRepository.js index c26e6a9..21bdc40 100644 --- a/src/repositories/resource_types/usersHasPlacesRepository.js +++ b/src/repositories/resource_types/usersHasPlacesRepository.js @@ -1,9 +1,10 @@ import { pagination } from "../../utils/pagination"; - -const _userHasPlacesSelectQuery = (_pagination = '') => ({ count }) => ({ uuid, uuidUser, uuidPlace }) => { +//reports repository +const _userHasPlacesSelectQuery = (_pagination = '') => ({ count }) => ({ uuid, uuidUser, uuidPlace, uuidReportType }) => { const uuidCondition = uuid ? 'AND up.uuid = :uuid ' : ''; const uuidUserCondition = uuidUser ? 'AND up.fk_user = (SELECT id FROM mydb.users WHERE uuid = :uuidUser)' : ''; const uuidPlaceCondition = uuidPlace ? 'AND up.fk_place = (SELECT id FROM mydb.places WHERE uuid = :uuidPlace)' : ''; + const uuidReportTypeCondition = uuidReportType ? 'AND up.fk_report_type = (SELECT id FROM acloc.report_types WHERE uuid = :uuidReportType)' : ''; return ` SELECT @@ -15,19 +16,23 @@ const _userHasPlacesSelectQuery = (_pagination = '') => ({ count }) => ({ uuid, u.username AS user_username, u.uuid AS user_uuid, p.name AS place_name, - p.uuid AS place_uuid - FROM mydb.users_has_places AS up - JOIN mydb.users AS u ON up.fk_user = u.id - JOIN mydb.places AS p ON up.fk_place = p.id + p.uuid AS place_uuid, + rt.name AS report_type_name, + 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 + JOIN acloc.report_types AS rt ON up.fk_report_type = rt.id WHERE up.created <= :now AND (up.deleted > :now OR up.deleted IS NULL) AND u.deleted IS NULL - AND up.created <= :now - AND (up.deleted > :now OR up.deleted IS NULL) + AND p.deleted IS NULL + AND rt.deleted IS NULL AND true ${uuidCondition} ${uuidUserCondition} ${uuidPlaceCondition} + ${uuidReportTypeCondition} ${_pagination} `; } @@ -38,15 +43,17 @@ const getUserHasPlacesListQuery = ({ limit, page, ...rest }) => const countUserHasPlacesListQuery = rest => _userHasPlacesSelectQuery()({ count: 'COUNT(DISTINCT(up.uuid)) AS count' })(rest); -const insertUserHasPlacesQuery = ({ uuidUser, uuidPlace, createdBy }) => { - const uuidUserCondition = uuidUser ? '(SELECT id FROM mydb.users WHERE uuid = :uuidUser),' : null; - const uuidPlaceCondition = uuidPlace ? '(SELECT id FROM mydb.places WHERE uuid = :uuidPlace),' : null; +const insertUserHasPlacesQuery = ({ uuidUser, uuidPlace, createdBy, uuidReportType }) => { + const uuidUserCondition = uuidUser ? '(SELECT id FROM acloc.users WHERE uuid = :uuidUser),' : null; + 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 mydb.users_has_places ( + INSERT INTO acloc.users_has_places ( uuid, fk_user, fk_place, + fk_report_type, created, createdBy ) @@ -54,34 +61,37 @@ const insertUserHasPlacesQuery = ({ uuidUser, uuidPlace, createdBy }) => { :uuid ${uuidUserCondition} ${uuidPlaceCondition} + ${reportTypeCondition} :now, ${createdByCondition} ); - SELECT * FROM mydb.users_has_places WHERE uuid = UUID(); + SELECT * FROM acloc.users_has_places WHERE uuid = :uuid; ` } -const modifyUserHasPlacesQuery = () => { - const uuidUserCondition = uuidUser ? 'fk_user = (SELECT id FROM mydb.users WHERE uuid = :uuidUser)' : ``; - const uuidPlaceCondition = uuidPlace ? 'fk_place = (SELECT id FROM mydb.places WHERE uuid = :uuidPlace)' : ``; +const modifyUserHasPlacesQuery = (uuidUser, uuidPlace, uuidReportType) => { + 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)' : ``; return ` - UPDATE mydb.users_has_places + UPDATE acloc.users_has_places SET ${uuidUserCondition} ${uuidPlaceCondition} + ${uuidReportTypeCondition} uuid = :uuid WHERE users_has_places.uuid = :uuid AND users_has_places.deleted IS NULL; - SELECT * FROM mydb.users_has_places WHERE uuid = :uuid; + SELECT * FROM acloc.users_has_places WHERE uuid = :uuid; ` } const softDeleteUserHasPlacesQuery = () => { return ` UPDATE - mydb.users_has_places + acloc.users_has_places SET deleted = :deleted, deletedBy = :deletedBy WHERE