insert and modify reports fixed for multiple report types
This commit is contained in:
parent
0588ea770e
commit
7c06ada685
|
|
@ -52,19 +52,14 @@ const countUserHasPlacesListQuery = rest =>
|
|||
_userHasPlacesSelectQuery()({ count: 'COUNT(DISTINCT(up.uuid)) AS count' })(rest);
|
||||
|
||||
const insertUserHasPlacesQuery = ({ user_uuid, place_uuid, description, createdBy, report_type_uuid, images }) => {
|
||||
const report_type_uuidStatements = report_type_uuid && report_type_uuid.length > 0
|
||||
? report_type_uuid.map(rt =>
|
||||
`INSERT INTO dbmaster.report_report_type (report_uuid, report_type_uuid) VALUES (:uuid, '${rt}');`
|
||||
).join('\n ')
|
||||
: '';
|
||||
|
||||
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 createdByCondition = createdBy ? ':createdBy' : 'NULL';
|
||||
const descriptionCondition = description ? ':description' : 'NULL';
|
||||
const imagesCondition = images ? ':images' : 'NULL';
|
||||
|
||||
return `
|
||||
// Main insert statement
|
||||
const mainInsert = `
|
||||
INSERT INTO dbmaster.users_has_places (
|
||||
uuid,
|
||||
fk_user,
|
||||
|
|
@ -84,59 +79,15 @@ const insertUserHasPlacesQuery = ({ user_uuid, place_uuid, description, createdB
|
|||
${descriptionCondition},
|
||||
${createdByCondition},
|
||||
:now
|
||||
);
|
||||
|
||||
${report_type_uuidStatements}
|
||||
|
||||
SELECT
|
||||
up.*,
|
||||
u.username AS user_username,
|
||||
u.uuid AS user_uuid,
|
||||
p.name AS place_name,
|
||||
p.uuid AS place_uuid,
|
||||
GROUP_CONCAT(rt.uuid) AS report_type_uuids,
|
||||
GROUP_CONCAT(rt.name) AS report_type_names
|
||||
FROM dbmaster.users_has_places AS up
|
||||
LEFT JOIN dbmaster.users AS u ON up.fk_user = u.id
|
||||
LEFT JOIN dbmaster.places AS p ON up.fk_place = p.id
|
||||
LEFT JOIN dbmaster.report_report_type AS rrt ON rrt.report_uuid = up.uuid
|
||||
LEFT JOIN dbmaster.report_types AS rt ON rt.uuid = rrt.report_type_uuid
|
||||
WHERE up.created <= :now
|
||||
AND (up.deleted > :now OR up.deleted IS NULL)
|
||||
AND u.deleted IS NULL
|
||||
AND p.deleted IS NULL
|
||||
GROUP BY up.uuid;
|
||||
`;
|
||||
}
|
||||
|
||||
const modifyUserHasPlacesQuery = ({user_uuid, place_uuid, report_type_uuid, rating, images, description}) => {
|
||||
const user_uuidCondition = user_uuid ? 'fk_user = (SELECT id FROM dbmaster.users WHERE uuid = :user_uuid),' : '';
|
||||
const place_uuidCondition = place_uuid ? 'fk_place = (SELECT id FROM dbmaster.places WHERE uuid = :place_uuid),' : '';
|
||||
const report_type_uuidCondition = report_type_uuid !== undefined
|
||||
? `DELETE FROM dbmaster.report_report_type WHERE report_uuid = :uuid;${
|
||||
Array.isArray(report_type_uuid) && report_type_uuid.length > 0
|
||||
? ' ' + report_type_uuid.map(rt =>
|
||||
`INSERT INTO dbmaster.report_report_type (report_uuid, report_type_uuid) VALUES (:uuid, '${rt}');`
|
||||
).join(' ')
|
||||
: ''
|
||||
}`
|
||||
: '';
|
||||
const ratingCondition = rating ? 'rating = :rating,' : '';
|
||||
const imagesCondition = images ? 'images = :images,' : '';
|
||||
const descriptionCondition = description ? 'description = :description,' : '';
|
||||
);`;
|
||||
|
||||
return `
|
||||
${report_type_uuidCondition}
|
||||
UPDATE dbmaster.users_has_places
|
||||
SET
|
||||
${user_uuidCondition}
|
||||
${place_uuidCondition}
|
||||
${ratingCondition}
|
||||
${imagesCondition}
|
||||
${descriptionCondition}
|
||||
uuid = :uuid
|
||||
WHERE uuid = :uuid AND deleted IS NULL;
|
||||
|
||||
// Report types insert (if any)
|
||||
const reportTypesInsert = (report_type_uuid && report_type_uuid.length > 0)
|
||||
? `INSERT INTO dbmaster.report_report_type (report_uuid, report_type_uuid)
|
||||
VALUES ${report_type_uuid.map(rt => `(:uuid, '${rt}')`).join(', ')};`
|
||||
: '';
|
||||
|
||||
const finalSelect = `
|
||||
SELECT
|
||||
up.*,
|
||||
u.username AS user_username,
|
||||
|
|
@ -154,8 +105,61 @@ const modifyUserHasPlacesQuery = ({user_uuid, place_uuid, report_type_uuid, rati
|
|||
AND (up.deleted > :now OR up.deleted IS NULL)
|
||||
AND u.deleted IS NULL
|
||||
AND p.deleted IS NULL
|
||||
GROUP BY up.uuid;
|
||||
`;
|
||||
GROUP BY up.uuid;`;
|
||||
|
||||
return mainInsert + reportTypesInsert + finalSelect;
|
||||
}
|
||||
|
||||
const modifyUserHasPlacesQuery = ({user_uuid, place_uuid, report_type_uuid, rating, images, description}) => {
|
||||
const user_uuidCondition = user_uuid ? 'fk_user = (SELECT id FROM dbmaster.users WHERE uuid = :user_uuid),' : '';
|
||||
const place_uuidCondition = place_uuid ? 'fk_place = (SELECT id FROM dbmaster.places WHERE uuid = :place_uuid),' : '';
|
||||
const ratingCondition = rating !== undefined ? 'rating = :rating,' : '';
|
||||
const imagesCondition = images !== undefined ? 'images = :images,' : '';
|
||||
const descriptionCondition = description !== undefined ? 'description = :description,' : '';
|
||||
|
||||
// Report types handling
|
||||
const deleteReportTypes = (report_type_uuid !== undefined)
|
||||
? `DELETE FROM dbmaster.report_report_type WHERE report_uuid = :uuid;`
|
||||
: '';
|
||||
|
||||
const insertReportTypes = (report_type_uuid !== undefined && Array.isArray(report_type_uuid) && report_type_uuid.length > 0)
|
||||
? `INSERT INTO dbmaster.report_report_type (report_uuid, report_type_uuid)
|
||||
VALUES ${report_type_uuid.map(rt => `(:uuid, '${rt}')`).join(', ')};`
|
||||
: '';
|
||||
|
||||
// Main update statement
|
||||
const mainUpdate = `
|
||||
UPDATE dbmaster.users_has_places
|
||||
SET
|
||||
${user_uuidCondition}
|
||||
${place_uuidCondition}
|
||||
${ratingCondition}
|
||||
${imagesCondition}
|
||||
${descriptionCondition}
|
||||
uuid = :uuid
|
||||
WHERE uuid = :uuid AND deleted IS NULL;`;
|
||||
|
||||
const finalSelect = `
|
||||
SELECT
|
||||
up.*,
|
||||
u.username AS user_username,
|
||||
u.uuid AS user_uuid,
|
||||
p.name AS place_name,
|
||||
p.uuid AS place_uuid,
|
||||
GROUP_CONCAT(rt.uuid) AS report_type_uuids,
|
||||
GROUP_CONCAT(rt.name) AS report_type_names
|
||||
FROM dbmaster.users_has_places AS up
|
||||
LEFT JOIN dbmaster.users AS u ON up.fk_user = u.id
|
||||
LEFT JOIN dbmaster.places AS p ON up.fk_place = p.id
|
||||
LEFT JOIN dbmaster.report_report_type AS rrt ON rrt.report_uuid = up.uuid
|
||||
LEFT JOIN dbmaster.report_types AS rt ON rt.uuid = rrt.report_type_uuid
|
||||
WHERE up.uuid = :uuid
|
||||
AND (up.deleted > :now OR up.deleted IS NULL)
|
||||
AND u.deleted IS NULL
|
||||
AND p.deleted IS NULL
|
||||
GROUP BY up.uuid;`;
|
||||
|
||||
return deleteReportTypes + insertReportTypes + mainUpdate + finalSelect;
|
||||
}
|
||||
|
||||
const softDeleteUserHasPlacesQuery = () => {
|
||||
|
|
|
|||
Loading…
Reference in New Issue