Переглянути джерело

fix(db): update cintas summary procedure with camelCase column names and improved null handling

vtugulan 6 місяців тому
батько
коміт
74757ba6f1

+ 38 - 40
prisma/migrations/20250722041246_add_cintas_calculate_summary_procedure/migration.sql

@@ -1,7 +1,7 @@
 -- Create the cintas_calculate_summary stored procedure
-CREATE OR REPLACE PROCEDURE cintas_calculate_summary(IN provided_import_id bigint)
-    LANGUAGE plpgsql
-AS
+create or replace procedure cintas_calculate_summary(IN provided_import_id bigint)
+    language plpgsql
+as
 $$
 BEGIN
     CREATE TEMP TABLE weekSums
@@ -18,11 +18,11 @@ BEGIN
            'trr4wk'
     from cintas_install_calendar i
              inner join lateral (
-        select case when i.install_date <> '' then i.install_date::date else null end,
+        select case when i.install_date <> '' then i.install_date::date end,
                case
                    when i.install_date <> '' then date_part('week', i.install_date::date)
                    when i.opportunity_status = 'Pending - Install Not Scheduled' then 99 + 22
-                   else null end,
+                   end,
                case when i.trr <> '' then i.trr::decimal else 0 end,
                case when i.paper_chem_wk1 <> '' then i.paper_chem_wk1::decimal else 0 end,
                case when i.paper_chem_wk2 <> '' then i.paper_chem_wk2::decimal else 0 end,
@@ -35,11 +35,11 @@ BEGIN
                w.trr + w.paper_chem_wk1
         ) aw (week, week_sum) on true
              inner join lateral (
-        select case when aw.week > 1 then aw.week + 1 else null end,
-               case when aw.week > 2 then aw.week + 2 else null end,
-               case when aw.week > 3 then aw.week + 3 else null end
+        select case when aw.week > 1 then aw.week + 1 end,
+               case when aw.week > 2 then aw.week + 2 end,
+               case when aw.week > 3 then aw.week + 3 end
         ) ow (week_minus_1, week_minus_2, week_minus_3) on true
-    where i.import_id = provided_import_id
+    where i."importId" = provided_import_id
       and rtrim(opportunity_status) <> ''
       and w.week is not null
     group by aw.week
@@ -52,11 +52,10 @@ BEGIN
            'week-1'
     from cintas_install_calendar i
              inner join lateral (
-        select case when i.install_date <> '' then i.install_date::date else null end,
+        select case when i.install_date <> '' then i.install_date::date end,
                case
                    when i.install_date <> '' then date_part('week', i.install_date::date)
-                   when i.opportunity_status = 'Pending - Install Not Scheduled' then 99 + 22
-                   else null end,
+                   when i.opportunity_status = 'Pending - Install Not Scheduled' then 99 + 22 end,
                case when i.trr <> '' then i.trr::decimal else 0 end,
                case when i.paper_chem_wk1 <> '' then i.paper_chem_wk1::decimal else 0 end,
                case when i.paper_chem_wk2 <> '' then i.paper_chem_wk2::decimal else 0 end,
@@ -69,11 +68,11 @@ BEGIN
                w.trr + w.paper_chem_wk1
         ) aw (week, week_sum) on true
              inner join lateral (
-        select case when aw.week > 1 then aw.week + 1 else null end,
-               case when aw.week > 2 then aw.week + 2 else null end,
-               case when aw.week > 3 then aw.week + 3 else null end
+        select case when aw.week > 1 then aw.week + 1 end,
+               case when aw.week > 2 then aw.week + 2 end,
+               case when aw.week > 3 then aw.week + 3 end
         ) ow (week_minus_1, week_minus_2, week_minus_3) on true
-    where i.import_id = provided_import_id
+    where i."importId" = provided_import_id
       and rtrim(opportunity_status) <> ''
       and ow.week_minus_1 is not null
       and aw.week <> 99
@@ -87,11 +86,10 @@ BEGIN
            'week-2'
     from cintas_install_calendar i
              inner join lateral (
-        select case when i.install_date <> '' then i.install_date::date else null end,
+        select case when i.install_date <> '' then i.install_date::date end,
                case
                    when i.install_date <> '' then date_part('week', i.install_date::date)
-                   when i.opportunity_status = 'Pending - Install Not Scheduled' then 99 + 22
-                   else null end,
+                   when i.opportunity_status = 'Pending - Install Not Scheduled' then 99 + 22 end,
                case when i.trr <> '' then i.trr::decimal else 0 end,
                case when i.paper_chem_wk1 <> '' then i.paper_chem_wk1::decimal else 0 end,
                case when i.paper_chem_wk2 <> '' then i.paper_chem_wk2::decimal else 0 end,
@@ -104,11 +102,11 @@ BEGIN
                w.trr + w.paper_chem_wk1
         ) aw (week, week_sum) on true
              inner join lateral (
-        select case when aw.week > 1 then aw.week + 1 else null end,
-               case when aw.week > 2 then aw.week + 2 else null end,
-               case when aw.week > 3 then aw.week + 3 else null end
+        select case when aw.week > 1 then aw.week + 1 end,
+               case when aw.week > 2 then aw.week + 2 end,
+               case when aw.week > 3 then aw.week + 3 end
         ) ow (week_minus_1, week_minus_2, week_minus_3) on true
-    where i.import_id = provided_import_id
+    where i."importId" = provided_import_id
       and rtrim(opportunity_status) <> ''
       and ow.week_minus_2 is not null
       and aw.week <> 99
@@ -122,11 +120,10 @@ BEGIN
            'week-3'
     from cintas_install_calendar i
              inner join lateral (
-        select case when i.install_date <> '' then i.install_date::date else null end,
+        select case when i.install_date <> '' then i.install_date::date end,
                case
                    when i.install_date <> '' then date_part('week', i.install_date::date)
-                   when i.opportunity_status = 'Pending - Install Not Scheduled' then 99 + 22
-                   else null end,
+                   when i.opportunity_status = 'Pending - Install Not Scheduled' then 99 + 22 end,
                case when i.trr <> '' then i.trr::decimal else 0 end,
                case when i.paper_chem_wk1 <> '' then i.paper_chem_wk1::decimal else 0 end,
                case when i.paper_chem_wk2 <> '' then i.paper_chem_wk2::decimal else 0 end,
@@ -139,11 +136,11 @@ BEGIN
                w.trr + w.paper_chem_wk1
         ) aw (week, week_sum) on true
              inner join lateral (
-        select case when aw.week > 1 then aw.week + 1 else null end,
-               case when aw.week > 2 then aw.week + 2 else null end,
-               case when aw.week > 3 then aw.week + 3 else null end
+        select case when aw.week > 1 then aw.week + 1 end,
+               case when aw.week > 2 then aw.week + 2 end,
+               case when aw.week > 3 then aw.week + 3 end
         ) ow (week_minus_1, week_minus_2, week_minus_3) on true
-    where i.import_id = provided_import_id
+    where i."importId" = provided_import_id
       and rtrim(opportunity_status) <> ''
       and ow.week_minus_3 is not null
       and aw.week <> 99
@@ -157,11 +154,10 @@ BEGIN
            'poweradd'
     from cintas_install_calendar i
              inner join lateral (
-        select case when i.install_date <> '' then i.install_date::date else null end,
+        select case when i.install_date <> '' then i.install_date::date end,
                case
                    when i.install_date <> '' then date_part('week', i.install_date::date)
-                   when i.opportunity_status = 'Pending - Install Not Scheduled' then 99 + 22
-                   else null end,
+                   when i.opportunity_status = 'Pending - Install Not Scheduled' then 99 + 22 end,
                case when i.trr <> '' then i.trr::decimal else 0 end,
                case when i.paper_chem_wk1 <> '' then i.paper_chem_wk1::decimal else 0 end,
                case when i.paper_chem_wk2 <> '' then i.paper_chem_wk2::decimal else 0 end,
@@ -174,18 +170,18 @@ BEGIN
                w.trr + w.paper_chem_wk1
         ) aw (week, week_sum) on true
              inner join lateral (
-        select case when aw.week > 1 then aw.week + 1 else null end,
-               case when aw.week > 2 then aw.week + 2 else null end,
-               case when aw.week > 3 then aw.week + 3 else null end
+        select case when aw.week > 1 then aw.week + 1 end,
+               case when aw.week > 2 then aw.week + 2 end,
+               case when aw.week > 3 then aw.week + 3 end
         ) ow (week_minus_1, week_minus_2, week_minus_3) on true
-    where i.import_id = provided_import_id
+    where i."importId" = provided_import_id
       and rtrim(opportunity_status) <> ''
       and aw.week is not null
       and aw.week <> 99
     group by aw.week
     ;
 
-    insert into cintas_intall_calendar_summary (import_id, week, trr_total, fourwk_averages, trr_plus_4wk, power_adds, week_id)
+    insert into cintas_intall_calendar_summary ("importId", week, "trrTotal", "fourWkAverages", "trrPlus4Wk", "powerAdds", "weekId", "createdAt", "updatedAt")
     select distinct
         provided_import_id,
         case when t.week = 99 then 'Pending - Install Not Scheduled' else concat('Week ', t.week::varchar(100)) end "Week",
@@ -193,11 +189,13 @@ BEGIN
         cast(sum(case when t.amountType <> 'poweradd' and t.amountType <> 'trr4wk' then t.amount else 0 end) over (partition by t.week) as decimal(9,0)) "4WK Averages",
         cast(sum(case when t.amountType <> 'poweradd' then t.amount else 0 end) over (partition by t.week) as decimal(9,0)) "TRR + 4WK",
         cast(sum(case when t.amountType = 'poweradd' then t.amount else 0 end) over (partition by t.week) as decimal(9,0)) "Power Adds",
-        t.week "WeekId"
+        t.week "WeekId",
+        now(),
+        now()
     from weekSums t
     order by t.week desc
     ;
 
     drop table weekSums;
-END;
+END
 $$;