fixed report management

This commit is contained in:
Pau 2025-04-21 23:57:56 +02:00
parent 372278e669
commit 8d883a9cdb
2 changed files with 23 additions and 10 deletions

View File

@ -30,7 +30,7 @@ const getUserHasPlacesListController = (req, res, next, config) => {
}) })
.catch((err) => { .catch((err) => {
const error = errorHandler(err, config.environment) const error = errorHandler(err, config.environment)
return res.status(error.code).json(error) res.status(error.code).json(error)
}) })
.finally(() => { .finally(() => {
mysql.end(conn) mysql.end(conn)
@ -86,7 +86,7 @@ const getUserHasPlacesByUserUuidController = (req, res, next, config) => {
}) })
.catch((err) => { .catch((err) => {
const error = errorHandler(err, config.environment) const error = errorHandler(err, config.environment)
return res.status(error.code).json(error) res.status(error.code).json(error)
}) })
.finally(() => { .finally(() => {
mysql.end(conn) mysql.end(conn)

View File

@ -6,25 +6,38 @@ const _userHasPlacesSelectQuery = (_pagination = '') => ({ count }) => ({ uuid,
const place_uuidCondition = place_uuid ? 'AND up.fk_place = (SELECT id FROM dbmaster.places WHERE uuid = :place_uuid)' : ''; const place_uuidCondition = place_uuid ? 'AND up.fk_place = (SELECT id FROM dbmaster.places WHERE uuid = :place_uuid)' : '';
const report_type_uuidCondition = report_type_uuid ? 'AND up.fk_report_type = (SELECT id FROM dbmaster.report_types WHERE uuid = :report_type_uuid)' : ''; const report_type_uuidCondition = report_type_uuid ? 'AND up.fk_report_type = (SELECT id FROM dbmaster.report_types WHERE uuid = :report_type_uuid)' : '';
const ratingCondition = rating ? 'AND up.rating = :rating' : ''; const ratingCondition = rating ? 'AND up.rating = :rating' : '';
// Conditional fields and joins for the report type
const reportTypeFields = `
${count ? '' : `,
rt.name AS report_type_name,
rt.uuid AS report_type_uuid`}
`;
const reportTypeJoin = `
${up.fk_report_type ? 'JOIN dbmaster.report_types AS rt ON up.fk_report_type = rt.id' : ''}
`;
const reportTypeDeletedCondition = `
${up.fk_report_type ? 'AND rt.deleted IS NULL' : ''}
`;
return ` return `
SELECT ${count || SELECT ${count ||
`up.*, `up.*,
u.username AS user_username, u.username AS user_username,
u.uuid AS user_uuid, u.uuid AS user_uuid,
p.name AS place_name, p.name AS place_name,
p.uuid AS place_uuid, p.uuid AS place_uuid${reportTypeFields}`}
rt.name AS report_type_name,
rt.uuid AS report_type_uuid`}
FROM dbmaster.users_has_places AS up FROM dbmaster.users_has_places AS up
JOIN dbmaster.users AS u ON up.fk_user = u.id JOIN dbmaster.users AS u ON up.fk_user = u.id
JOIN dbmaster.places AS p ON up.fk_place = p.id JOIN dbmaster.places AS p ON up.fk_place = p.id
JOIN dbmaster.report_types AS rt ON up.fk_report_type = rt.id ${reportTypeJoin}
WHERE up.created <= :now WHERE up.created <= :now
AND (up.deleted > :now OR up.deleted IS NULL) AND (up.deleted > :now OR up.deleted IS NULL)
AND u.deleted IS NULL AND u.deleted IS NULL
AND p.deleted IS NULL AND p.deleted IS NULL
AND rt.deleted IS NULL ${reportTypeDeletedCondition}
AND true
${uuidCondition} ${uuidCondition}
${user_uuidCondition} ${user_uuidCondition}
${place_uuidCondition} ${place_uuidCondition}
@ -43,8 +56,8 @@ const countUserHasPlacesListQuery = rest =>
const insertUserHasPlacesQuery = ({ user_uuid, place_uuid, description, createdBy, report_type_uuid }) => { const insertUserHasPlacesQuery = ({ user_uuid, place_uuid, description, createdBy, report_type_uuid }) => {
const user_uuidCondition = user_uuid ? '(SELECT id FROM dbmaster.users WHERE uuid = :user_uuid)' : null; const user_uuidCondition = user_uuid ? '(SELECT id FROM dbmaster.users WHERE uuid = :user_uuid)' : null;
const place_uuidCondition = place_uuid ? '(SELECT id FROM dbmaster.places WHERE uuid = :place_uuid)' : null; const place_uuidCondition = place_uuid ? '(SELECT id FROM dbmaster.places WHERE uuid = :place_uuid)' : null;
const createdByCondition = createdBy ? 'createdBy = :createdBy' : null; const createdByCondition = createdBy ? ':createdBy' : null;
const descriptionCondition = description ? 'description = :description' : null; const descriptionCondition = description ? ':description' : null;
const reportTypeCondition = report_type_uuid ? `(SELECT id FROM dbmaster.report_types WHERE uuid = :report_type_uuid)` : null; const reportTypeCondition = report_type_uuid ? `(SELECT id FROM dbmaster.report_types WHERE uuid = :report_type_uuid)` : null;