Skip to content

Commit

Permalink
Reformat to make more readable
Browse files Browse the repository at this point in the history
  • Loading branch information
Cruikshanks committed Aug 30, 2024
1 parent 91bf4d7 commit 485ad8e
Showing 1 changed file with 66 additions and 21 deletions.
87 changes: 66 additions & 21 deletions src/modules/charging-import/lib/queries/return-versions.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,27 +208,72 @@ AND return_version_id NOT IN (
);
`

const importReturnVersionsAddMissingReturnVersionEndDates = `UPDATE water.return_versions rv
SET end_date = bq.new_end_date
FROM (SELECT rv.return_version_id,
(SELECT rv3.start_date - 1 FROM water.return_versions rv3 WHERE rv3.licence_id = madness.licence_id AND rv3.version_number = madness.min_version AND rv3.status != 'draft' AND rv3.external_id IS NOT NULL) AS new_end_date
FROM water.return_versions rv
INNER JOIN (SELECT no_end.return_version_id, rv1.licence_id, min(rv1.version_number) AS min_version
FROM water.return_versions rv1
INNER JOIN (SELECT rv2.return_version_id, rv2.licence_id, rv2.version_number
FROM water.return_versions rv2
INNER JOIN (SELECT licence_id, max(version_number) AS max_version
FROM water.return_versions
WHERE status != 'draft' AND external_id IS NOT NULL
GROUP BY licence_id) AS lv
ON rv2.licence_id = lv.licence_id
AND rv2.version_number != lv.max_version
WHERE rv2.end_date IS NULL AND rv2.status <> 'draft' AND rv2.external_id IS NOT NULL) AS no_end
ON rv1.licence_id = no_end.licence_id
AND rv1.version_number > no_end.version_number
GROUP BY rv1.licence_id, no_end.return_version_id) AS madness
ON rv.return_version_id = madness.return_version_id) AS bq
WHERE rv.return_version_id = bq.return_version_id;
const importReturnVersionsAddMissingReturnVersionEndDates = `
UPDATE
water.return_versions rv
SET
end_date = bq.new_end_date
FROM
(
SELECT
rv.return_version_id,
(
SELECT
rv3.start_date - 1
FROM
water.return_versions rv3
WHERE
rv3.licence_id = madness.licence_id
AND rv3.version_number = madness.min_version
) AS new_end_date
FROM
water.return_versions rv
INNER JOIN (
SELECT
no_end.return_version_id,
rv1.licence_id,
min(rv1.version_number) AS min_version
FROM
water.return_versions rv1
INNER JOIN (
SELECT
rv2.return_version_id,
rv2.licence_id,
rv2.version_number
FROM
water.return_versions rv2
INNER JOIN (
SELECT
licence_id,
max(version_number) AS max_version
FROM
water.return_versions
WHERE
status != 'draft'
AND external_id IS NOT NULL
GROUP BY
licence_id
) AS lv
ON
rv2.licence_id = lv.licence_id
AND rv2.version_number != lv.max_version
WHERE
rv2.end_date IS NULL
AND rv2.status <> 'draft'
AND rv2.external_id IS NOT NULL
) AS no_end
ON
rv1.licence_id = no_end.licence_id
AND rv1.version_number > no_end.version_number
GROUP BY
rv1.licence_id,
no_end.return_version_id
) AS madness
ON
rv.return_version_id = madness.return_version_id
) AS bq
WHERE
rv.return_version_id = bq.return_version_idd;
`

module.exports = {
Expand Down

0 comments on commit 485ad8e

Please sign in to comment.