Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missing fields in bill runs view #599

Merged
merged 1 commit into from
Dec 15, 2023
Merged

Conversation

Cruikshanks
Copy link
Member

https://eaflood.atlassian.net/browse/WATER-4057

As part of the work we have been doing on two-part tariff, we will create all our new tables in the default public schema.

We have also decided that when there is a legacy table that we are still going to need we will create a View of it in the public schema. This allows us to correct any issues with naming conventions, strip out unused fields, and join entities currently sat in different schemas. The first example of this approach was done in PR #531 .

In Create water schema views we created the view for water.billing_batches and dropped a column we thought wasn't needed and renamed one we thought was wrong.

We use errorCode: in our codebase during supplementary billing if a bill run fails so it is needed. And invoiceCount refers to the number of debit (positive value) bills in a bill run, not the total number. 'Invoice' rather than 'Debit' is used throughout the UI. So, we need to revert the name change.

Note - when it comes to views you have to drop and recreate them. Unlike tables, you cannot alter columns individually

https://eaflood.atlassian.net/browse/WATER-4057

As part of the work we have been doing on two-part tariff, we will be creating all our new tables in the default `public` schema.

We have also decided that when there is a legacy table that we are still going to need we will create a [View](https://www.postgresql.org/docs/current/sql-createview.html) of it in the `public` schema. This allows us to correct any issues with naming conventions, strip out unused fields, and join entities currently sat in different schemas. The first example of this approach was done in PR #531 .

In [Create water schema views](#551) we created the view for `water.billing_batches` and dropped a column we thought wasn't needed and renamed one we thought was wrong.

We actually use `errorCode:` in our own codebase during supplementary billing if a bill run fails so it is needed. And `invoiceCount` actually refers to the number of debit (positive value) bills in a bill run, not the total number of bills. 'Invoice' rather than 'Debit' is the term used throughout the UI. So, we need to revert the name change.

> Note - when it comes to views you have to drop and recreate them. Unlike tables you cannot alter columns individually

diff --git a/db/migrations/public/20231215174532_alter-bill-runs-view.js b/db/migrations/public/20231215174532_alter-bill-runs-view.js
new file mode 100644
index 0000000..af07d8e
--- /dev/null
+++ b/db/migrations/public/20231215174532_alter-bill-runs-view.js
@@ -0,0 +1,68 @@
+'use strict'
+
+const viewName = 'bill_runs'
+
+exports.up = function (knex) {
+  return knex
+    .schema
+    .dropView(viewName)
+    .createView(viewName, (view) => {
+      view.as(knex('billing_batches').withSchema('water').select([
+        'billing_batch_id AS id',
+        'region_id',
+        'batch_type',
+        'from_financial_year_ending',
+        'to_financial_year_ending',
+        'status',
+        'invoice_count',
+        'credit_note_count',
+        'net_total',
+        'bill_run_number',
+        'error_code',
+        'external_id',
+        'is_summer AS summer',
+        'source',
+        'legacy_id',
+        'metadata',
+        'invoice_value',
+        'credit_note_value',
+        'transaction_file_reference',
+        'scheme',
+        'date_created AS created_at',
+        'date_updated AS updated_at'
+      ]))
+    })
+}
+
+exports.down = function (knex) {
+  return knex
+    .schema
+    .dropView(viewName)
+    .createView(viewName, (view) => {
+      // NOTE: We have commented out unused columns from the source table
+      view.as(knex('billing_batches').withSchema('water').select([
+        'billing_batch_id AS id',
+        'region_id',
+        'batch_type',
+        'from_financial_year_ending',
+        'to_financial_year_ending',
+        'status',
+        'invoice_count AS bill_count',
+        'credit_note_count',
+        'net_total',
+        'bill_run_number',
+        // 'error_code',
+        'external_id',
+        'is_summer AS summer',
+        'source',
+        'legacy_id',
+        'metadata',
+        'invoice_value',
+        'credit_note_value',
+        'transaction_file_reference',
+        'scheme',
+        'date_created AS created_at',
+        'date_updated AS updated_at'
+      ]))
+    })
+}
@Cruikshanks Cruikshanks added the bug Something isn't working label Dec 15, 2023
@Cruikshanks Cruikshanks self-assigned this Dec 15, 2023
@Cruikshanks Cruikshanks marked this pull request as ready for review December 15, 2023 18:04
@Cruikshanks Cruikshanks merged commit fb7f5ad into main Dec 15, 2023
6 checks passed
@Cruikshanks Cruikshanks deleted the fix-bill-runs-view branch December 15, 2023 18:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant