From 05f1c50fa6581c4f4658393c89f18fef724c5e6c Mon Sep 17 00:00:00 2001
From: Damien <damien.burel@maarch.org>
Date: Wed, 2 Oct 2019 11:12:27 +0200
Subject: [PATCH] FEAT #11955 TIME 1:00 Remove mlb_coll_ext (almost) +
 migration

---
 migration/19.12/1912.sql                      | 104 +++++--
 sql/structure.sql                             | 282 +++++++++---------
 .../indexing-form/indexing-form.component.ts  |   2 +-
 3 files changed, 223 insertions(+), 165 deletions(-)

diff --git a/migration/19.12/1912.sql b/migration/19.12/1912.sql
index d89681b3b87..ffc830b3b97 100644
--- a/migration/19.12/1912.sql
+++ b/migration/19.12/1912.sql
@@ -208,6 +208,74 @@ DO $$ BEGIN
 END$$;
 
 
+/* MLB COLL EXT */
+DO $$ BEGIN
+    IF (SELECT count(attname) FROM pg_attribute WHERE attrelid = (SELECT oid FROM pg_class WHERE relname = 'mlb_coll_ext') AND attname = 'category_id') THEN
+        ALTER TABLE res_letterbox ADD COLUMN category_id character varying(32);
+        UPDATE res_letterbox SET category_id = mlb_coll_ext.category_id FROM mlb_coll_ext WHERE res_letterbox.res_id = mlb_coll_ext.res_id;
+        UPDATE res_letterbox set category_id = 'incoming' WHERE category_id IS NULL;
+        ALTER TABLE res_letterbox ALTER COLUMN category_id set not null;
+        ALTER TABLE mlb_coll_ext DROP COLUMN IF EXISTS category_id;
+
+        ALTER TABLE res_letterbox ADD COLUMN exp_contact_id integer;
+        UPDATE res_letterbox SET exp_contact_id = mlb_coll_ext.exp_contact_id FROM mlb_coll_ext WHERE res_letterbox.res_id = mlb_coll_ext.res_id;
+        ALTER TABLE mlb_coll_ext DROP COLUMN IF EXISTS exp_contact_id;
+
+        ALTER TABLE res_letterbox ADD COLUMN exp_user_id character varying(128);
+        UPDATE res_letterbox SET exp_user_id = mlb_coll_ext.exp_user_id FROM mlb_coll_ext WHERE res_letterbox.res_id = mlb_coll_ext.res_id;
+        ALTER TABLE mlb_coll_ext DROP COLUMN IF EXISTS exp_user_id;
+
+        ALTER TABLE res_letterbox ADD COLUMN dest_contact_id integer;
+        UPDATE res_letterbox SET dest_contact_id = mlb_coll_ext.dest_contact_id FROM mlb_coll_ext WHERE res_letterbox.res_id = mlb_coll_ext.res_id;
+        ALTER TABLE mlb_coll_ext DROP COLUMN IF EXISTS dest_contact_id;
+
+        ALTER TABLE res_letterbox ADD COLUMN dest_user_id character varying(128);
+        UPDATE res_letterbox SET dest_user_id = mlb_coll_ext.dest_user_id FROM mlb_coll_ext WHERE res_letterbox.res_id = mlb_coll_ext.res_id;
+        ALTER TABLE mlb_coll_ext DROP COLUMN IF EXISTS dest_user_id;
+
+        ALTER TABLE res_letterbox ADD COLUMN alt_identifier character varying(256);
+        UPDATE res_letterbox SET alt_identifier = mlb_coll_ext.alt_identifier FROM mlb_coll_ext WHERE res_letterbox.res_id = mlb_coll_ext.res_id;
+        ALTER TABLE mlb_coll_ext DROP COLUMN IF EXISTS alt_identifier;
+
+        ALTER TABLE res_letterbox ADD COLUMN admission_date timestamp without time zone;
+        UPDATE res_letterbox SET admission_date = mlb_coll_ext.admission_date FROM mlb_coll_ext WHERE res_letterbox.res_id = mlb_coll_ext.res_id;
+        ALTER TABLE mlb_coll_ext DROP COLUMN IF EXISTS admission_date;
+
+        ALTER TABLE res_letterbox ADD COLUMN process_limit_date timestamp without time zone;
+        UPDATE res_letterbox SET process_limit_date = mlb_coll_ext.process_limit_date FROM mlb_coll_ext WHERE res_letterbox.res_id = mlb_coll_ext.res_id;
+        ALTER TABLE mlb_coll_ext DROP COLUMN IF EXISTS process_limit_date;
+
+        ALTER TABLE res_letterbox ADD COLUMN closing_date timestamp without time zone;
+        UPDATE res_letterbox SET closing_date = mlb_coll_ext.closing_date FROM mlb_coll_ext WHERE res_letterbox.res_id = mlb_coll_ext.res_id;
+        ALTER TABLE mlb_coll_ext DROP COLUMN IF EXISTS closing_date;
+
+        ALTER TABLE res_letterbox ADD COLUMN flag_alarm1 character(1) DEFAULT 'N'::character varying;
+        UPDATE res_letterbox SET flag_alarm1 = mlb_coll_ext.flag_alarm1 FROM mlb_coll_ext WHERE res_letterbox.res_id = mlb_coll_ext.res_id;
+        ALTER TABLE mlb_coll_ext DROP COLUMN IF EXISTS flag_alarm1;
+
+        ALTER TABLE res_letterbox ADD COLUMN flag_alarm2 character(1) DEFAULT 'N'::character varying;
+        UPDATE res_letterbox SET flag_alarm2 = mlb_coll_ext.flag_alarm2 FROM mlb_coll_ext WHERE res_letterbox.res_id = mlb_coll_ext.res_id;
+        ALTER TABLE mlb_coll_ext DROP COLUMN IF EXISTS flag_alarm2;
+
+        ALTER TABLE res_letterbox ADD COLUMN is_multicontacts character(1);
+        UPDATE res_letterbox SET is_multicontacts = mlb_coll_ext.is_multicontacts FROM mlb_coll_ext WHERE res_letterbox.res_id = mlb_coll_ext.res_id;
+        ALTER TABLE mlb_coll_ext DROP COLUMN IF EXISTS is_multicontacts;
+
+        ALTER TABLE res_letterbox ADD COLUMN address_id INTEGER;
+        UPDATE res_letterbox SET address_id = mlb_coll_ext.address_id FROM mlb_coll_ext WHERE res_letterbox.res_id = mlb_coll_ext.res_id;
+        ALTER TABLE mlb_coll_ext DROP COLUMN IF EXISTS address_id;
+
+        ALTER TABLE res_letterbox ADD COLUMN alarm1_date timestamp without time zone;
+        UPDATE res_letterbox SET alarm1_date = mlb_coll_ext.alarm1_date FROM mlb_coll_ext WHERE res_letterbox.res_id = mlb_coll_ext.res_id;
+        ALTER TABLE mlb_coll_ext DROP COLUMN IF EXISTS alarm1_date;
+
+        ALTER TABLE res_letterbox ADD COLUMN alarm2_date timestamp without time zone;
+        UPDATE res_letterbox SET alarm2_date = mlb_coll_ext.alarm2_date FROM mlb_coll_ext WHERE res_letterbox.res_id = mlb_coll_ext.res_id;
+        ALTER TABLE mlb_coll_ext DROP COLUMN IF EXISTS alarm2_date;
+    END IF;
+END$$;
+
+
 /* REFACTORING DATA */
 DO $$ BEGIN
   IF (SELECT count(attname) FROM pg_attribute WHERE attrelid = (SELECT oid FROM pg_class WHERE relname = 'usergroups') AND attname = 'enabled') THEN
@@ -373,22 +441,21 @@ SELECT r.tablename,
        r.destination,
        r.dest_user,
        r.confidentiality,
-       mlb.category_id,
-       mlb.exp_contact_id,
-       mlb.exp_user_id,
-       mlb.dest_user_id,
-       mlb.dest_contact_id,
-       mlb.address_id,
-       mlb.nature_id,
-       mlb.alt_identifier,
-       mlb.admission_date,
-       mlb.process_limit_date,
-       mlb.closing_date,
-       mlb.alarm1_date,
-       mlb.alarm2_date,
-       mlb.flag_alarm1,
-       mlb.flag_alarm2,
-       mlb.is_multicontacts,
+       r.category_id,
+       r.exp_contact_id,
+       r.exp_user_id,
+       r.dest_user_id,
+       r.dest_contact_id,
+       r.address_id,
+       r.alt_identifier,
+       r.admission_date,
+       r.process_limit_date,
+       r.closing_date,
+       r.alarm1_date,
+       r.alarm2_date,
+       r.flag_alarm1,
+       r.flag_alarm2,
+       r.is_multicontacts,
        r.subject,
        r.identifier,
        r.title,
@@ -408,9 +475,8 @@ FROM doctypes d,
      doctypes_second_level dsl,
      res_letterbox r
          LEFT JOIN entities en ON r.destination::text = en.entity_id::text
-         LEFT JOIN mlb_coll_ext mlb ON mlb.res_id = r.res_id
-         LEFT JOIN contacts_v2 cont ON mlb.exp_contact_id = cont.contact_id OR mlb.dest_contact_id = cont.contact_id
-         LEFT JOIN users u ON mlb.exp_user_id::text = u.user_id::text OR mlb.dest_user_id::text = u.user_id::text
+         LEFT JOIN contacts_v2 cont ON r.exp_contact_id = cont.contact_id OR r.dest_contact_id = cont.contact_id
+         LEFT JOIN users u ON r.exp_user_id::text = u.user_id::text OR r.dest_user_id::text = u.user_id::text
 WHERE r.type_id = d.type_id AND d.doctypes_first_level_id = dfl.doctypes_first_level_id AND d.doctypes_second_level_id = dsl.doctypes_second_level_id;
 
 CREATE VIEW res_view_attachments AS
diff --git a/sql/structure.sql b/sql/structure.sql
index 0429071a2b5..5e9f9d832a9 100755
--- a/sql/structure.sql
+++ b/sql/structure.sql
@@ -1180,6 +1180,21 @@ CREATE TABLE res_letterbox
   department_number_id text,
   barcode text,
   external_signatory_book_id integer,
+  category_id character varying(32)  NOT NULL,
+  exp_contact_id integer,
+  exp_user_id character varying(128),
+  dest_contact_id integer,
+  dest_user_id character varying(128),
+  alt_identifier character varying(255),
+  admission_date timestamp without time zone,
+  process_limit_date timestamp without time zone,
+  closing_date timestamp without time zone,
+  alarm1_date timestamp without time zone,
+  alarm2_date timestamp without time zone,
+  flag_alarm1 char(1) default 'N'::character varying,
+  flag_alarm2 char(1) default 'N'::character varying,
+  is_multicontacts char(1),
+  address_id bigint,
   CONSTRAINT res_letterbox_pkey PRIMARY KEY  (res_id)
 )
 WITH (OIDS=FALSE);
@@ -1217,26 +1232,6 @@ WITH (
   OIDS=FALSE
 );
 
-CREATE TABLE mlb_coll_ext (
-  res_id bigint NOT NULL,
-  category_id character varying(50)  NOT NULL,
-  exp_contact_id integer default NULL,
-  exp_user_id character varying(128) default NULL,
-  dest_contact_id integer default NULL,
-  dest_user_id character varying(128) default NULL,
-  nature_id character varying(50),
-  alt_identifier character varying(255)  default NULL,
-  admission_date timestamp without time zone,
-  process_limit_date timestamp without time zone default NULL,
-  closing_date timestamp without time zone default NULL,
-  alarm1_date timestamp without time zone default NULL,
-  alarm2_date timestamp without time zone default NULL,
-  flag_alarm1 char(1)  default 'N'::character varying ,
-  flag_alarm2 char(1) default 'N'::character varying,
-  is_multicontacts char(1),
-  address_id bigint
-)WITH (OIDS=FALSE);
-
 CREATE TABLE doctypes_indexes
 (
   type_id bigint NOT NULL,
@@ -1362,131 +1357,128 @@ WITH (OIDS=FALSE);
 
 --VIEWS
 -- view for letterbox
-DROP VIEW IF EXISTS res_view_letterbox;
-CREATE OR REPLACE VIEW res_view_letterbox AS 
- SELECT r.tablename,
-    r.is_multi_docservers,
-    r.res_id,
-    r.type_id,
-    r.policy_id,
-    r.cycle_id,
-    d.description AS type_label,
-    d.doctypes_first_level_id,
-    dfl.doctypes_first_level_label,
-    dfl.css_style AS doctype_first_level_style,
-    d.doctypes_second_level_id,
-    dsl.doctypes_second_level_label,
-    dsl.css_style AS doctype_second_level_style,
-    r.format,
-    r.typist,
-    r.creation_date,
-    r.modification_date,
-    r.relation,
-    r.docserver_id,
-    r.path,
-    r.filename,
-    r.fingerprint,
-    r.offset_doc,
-    r.filesize,
-    r.scan_date,
-    r.scan_user,
-    r.scan_location,
-    r.scan_wkstation,
-    r.scan_batch,
-    r.scan_postmark,
-    r.status,
-    r.work_batch,
-    r.doc_date,
-    r.description,
-    r.source,
-    r.author,
-    r.reference_number,
-    r.external_reference,
-    r.external_id,
-    r.external_link,
-    r.departure_date,
-    r.opinion_limit_date,
-    r.department_number_id,
-    r.barcode,
-    r.external_signatory_book_id,
-    r.custom_t1 AS doc_custom_t1,
-    r.custom_t2 AS doc_custom_t2,
-    r.custom_t3 AS doc_custom_t3,
-    r.custom_t4 AS doc_custom_t4,
-    r.custom_t5 AS doc_custom_t5,
-    r.custom_t6 AS doc_custom_t6,
-    r.custom_t7 AS doc_custom_t7,
-    r.custom_t8 AS doc_custom_t8,
-    r.custom_t9 AS doc_custom_t9,
-    r.custom_t10 AS doc_custom_t10,
-    r.custom_t11 AS doc_custom_t11,
-    r.custom_t12 AS doc_custom_t12,
-    r.custom_t13 AS doc_custom_t13,
-    r.custom_t14 AS doc_custom_t14,
-    r.custom_t15 AS doc_custom_t15,
-    r.custom_d1 AS doc_custom_d1,
-    r.custom_d2 AS doc_custom_d2,
-    r.custom_d3 AS doc_custom_d3,
-    r.custom_d4 AS doc_custom_d4,
-    r.custom_d5 AS doc_custom_d5,
-    r.custom_d6 AS doc_custom_d6,
-    r.custom_d7 AS doc_custom_d7,
-    r.custom_d8 AS doc_custom_d8,
-    r.custom_d9 AS doc_custom_d9,
-    r.custom_d10 AS doc_custom_d10,
-    r.custom_n1 AS doc_custom_n1,
-    r.custom_n2 AS doc_custom_n2,
-    r.custom_n3 AS doc_custom_n3,
-    r.custom_n4 AS doc_custom_n4,
-    r.custom_n5 AS doc_custom_n5,
-    r.custom_f1 AS doc_custom_f1,
-    r.custom_f2 AS doc_custom_f2,
-    r.custom_f3 AS doc_custom_f3,
-    r.custom_f4 AS doc_custom_f4,
-    r.custom_f5 AS doc_custom_f5,
-    r.initiator,
-    r.destination,
-    r.dest_user,
-    r.confidentiality,
-    mlb.category_id,
-    mlb.exp_contact_id,
-    mlb.exp_user_id,
-    mlb.dest_user_id,
-    mlb.dest_contact_id,
-    mlb.address_id,
-    mlb.nature_id,
-    mlb.alt_identifier,
-    mlb.admission_date,
-    mlb.process_limit_date,
-    mlb.closing_date,
-    mlb.alarm1_date,
-    mlb.alarm2_date,
-    mlb.flag_alarm1,
-    mlb.flag_alarm2,
-    mlb.is_multicontacts,
-    r.subject,
-    r.identifier,
-    r.title,
-    r.priority,
-    r.locker_user_id,
-    r.locker_time,
-    en.entity_label,
-    en.entity_type AS entitytype,
-    cont.contact_id,
-    cont.firstname AS contact_firstname,
-    cont.lastname AS contact_lastname,
-    cont.society AS contact_society,
-    u.lastname AS user_lastname,
-    u.firstname AS user_firstname
-   FROM doctypes d,
-    doctypes_first_level dfl,
-    doctypes_second_level dsl,
-    res_letterbox r
-     LEFT JOIN entities en ON r.destination::text = en.entity_id::text
-     LEFT JOIN mlb_coll_ext mlb ON mlb.res_id = r.res_id
-     LEFT JOIN contacts_v2 cont ON mlb.exp_contact_id = cont.contact_id OR mlb.dest_contact_id = cont.contact_id
-     LEFT JOIN users u ON mlb.exp_user_id::text = u.user_id::text OR mlb.dest_user_id::text = u.user_id::text
-  WHERE r.type_id = d.type_id AND d.doctypes_first_level_id = dfl.doctypes_first_level_id AND d.doctypes_second_level_id = dsl.doctypes_second_level_id;
+CREATE OR REPLACE VIEW res_view_letterbox AS
+SELECT r.tablename,
+       r.is_multi_docservers,
+       r.res_id,
+       r.type_id,
+       r.policy_id,
+       r.cycle_id,
+       d.description AS type_label,
+       d.doctypes_first_level_id,
+       dfl.doctypes_first_level_label,
+       dfl.css_style AS doctype_first_level_style,
+       d.doctypes_second_level_id,
+       dsl.doctypes_second_level_label,
+       dsl.css_style AS doctype_second_level_style,
+       r.format,
+       r.typist,
+       r.creation_date,
+       r.modification_date,
+       r.relation,
+       r.docserver_id,
+       r.path,
+       r.filename,
+       r.fingerprint,
+       r.offset_doc,
+       r.filesize,
+       r.scan_date,
+       r.scan_user,
+       r.scan_location,
+       r.scan_wkstation,
+       r.scan_batch,
+       r.scan_postmark,
+       r.status,
+       r.work_batch,
+       r.doc_date,
+       r.description,
+       r.source,
+       r.author,
+       r.reference_number,
+       r.external_reference,
+       r.external_id,
+       r.external_link,
+       r.departure_date,
+       r.opinion_limit_date,
+       r.department_number_id,
+       r.barcode,
+       r.external_signatory_book_id,
+       r.custom_t1 AS doc_custom_t1,
+       r.custom_t2 AS doc_custom_t2,
+       r.custom_t3 AS doc_custom_t3,
+       r.custom_t4 AS doc_custom_t4,
+       r.custom_t5 AS doc_custom_t5,
+       r.custom_t6 AS doc_custom_t6,
+       r.custom_t7 AS doc_custom_t7,
+       r.custom_t8 AS doc_custom_t8,
+       r.custom_t9 AS doc_custom_t9,
+       r.custom_t10 AS doc_custom_t10,
+       r.custom_t11 AS doc_custom_t11,
+       r.custom_t12 AS doc_custom_t12,
+       r.custom_t13 AS doc_custom_t13,
+       r.custom_t14 AS doc_custom_t14,
+       r.custom_t15 AS doc_custom_t15,
+       r.custom_d1 AS doc_custom_d1,
+       r.custom_d2 AS doc_custom_d2,
+       r.custom_d3 AS doc_custom_d3,
+       r.custom_d4 AS doc_custom_d4,
+       r.custom_d5 AS doc_custom_d5,
+       r.custom_d6 AS doc_custom_d6,
+       r.custom_d7 AS doc_custom_d7,
+       r.custom_d8 AS doc_custom_d8,
+       r.custom_d9 AS doc_custom_d9,
+       r.custom_d10 AS doc_custom_d10,
+       r.custom_n1 AS doc_custom_n1,
+       r.custom_n2 AS doc_custom_n2,
+       r.custom_n3 AS doc_custom_n3,
+       r.custom_n4 AS doc_custom_n4,
+       r.custom_n5 AS doc_custom_n5,
+       r.custom_f1 AS doc_custom_f1,
+       r.custom_f2 AS doc_custom_f2,
+       r.custom_f3 AS doc_custom_f3,
+       r.custom_f4 AS doc_custom_f4,
+       r.custom_f5 AS doc_custom_f5,
+       r.initiator,
+       r.destination,
+       r.dest_user,
+       r.confidentiality,
+       r.category_id,
+       r.exp_contact_id,
+       r.exp_user_id,
+       r.dest_user_id,
+       r.dest_contact_id,
+       r.address_id,
+       r.alt_identifier,
+       r.admission_date,
+       r.process_limit_date,
+       r.closing_date,
+       r.alarm1_date,
+       r.alarm2_date,
+       r.flag_alarm1,
+       r.flag_alarm2,
+       r.is_multicontacts,
+       r.subject,
+       r.identifier,
+       r.title,
+       r.priority,
+       r.locker_user_id,
+       r.locker_time,
+       en.entity_label,
+       en.entity_type AS entitytype,
+       cont.contact_id,
+       cont.firstname AS contact_firstname,
+       cont.lastname AS contact_lastname,
+       cont.society AS contact_society,
+       u.lastname AS user_lastname,
+       u.firstname AS user_firstname
+FROM doctypes d,
+     doctypes_first_level dfl,
+     doctypes_second_level dsl,
+     res_letterbox r
+         LEFT JOIN entities en ON r.destination::text = en.entity_id::text
+         LEFT JOIN contacts_v2 cont ON r.exp_contact_id = cont.contact_id OR r.dest_contact_id = cont.contact_id
+         LEFT JOIN users u ON r.exp_user_id::text = u.user_id::text OR r.dest_user_id::text = u.user_id::text
+WHERE r.type_id = d.type_id AND d.doctypes_first_level_id = dfl.doctypes_first_level_id AND d.doctypes_second_level_id = dsl.doctypes_second_level_id;
   
 
 --view for contacts_v2
diff --git a/src/frontend/app/indexation/indexing-form/indexing-form.component.ts b/src/frontend/app/indexation/indexing-form/indexing-form.component.ts
index 9487ac8999b..bb2bb557f1c 100644
--- a/src/frontend/app/indexation/indexing-form/indexing-form.component.ts
+++ b/src/frontend/app/indexation/indexing-form/indexing-form.component.ts
@@ -688,4 +688,4 @@ export class IndexingFormComponent implements OnInit {
     changeCategory(categoryId: string) {
         this.currentCategory = categoryId;
     }
-}
\ No newline at end of file
+}
-- 
GitLab