LP#1442254: adjust how sending library set for messages created from notes
authorGalen Charlton <gmc@esilibrary.com>
Tue, 31 Mar 2015 21:23:22 +0000 (21:23 +0000)
committerBen Shum <bshum@biblio.org>
Fri, 10 Apr 2015 01:56:42 +0000 (21:56 -0400)
When creating a new user message from a public note, the library
of the workstation of the staff member who created the original
note is used if available from get_audit_info().  If it's not
available (e.g., if notes are added using direct SQL, the
home library of the note creator is used instead).

Signed-off-by: Galen Charlton <gmc@esilibrary.com>
Signed-off-by: Kathy Lussier <klussier@masslnc.org>
Signed-off-by: Ben Shum <bshum@biblio.org>
Open-ILS/src/sql/Pg/005.schema.actors.sql
Open-ILS/src/sql/Pg/upgrade/ZZZZ.schema.use-ws-lib-for-messages.sql [new file with mode: 0644]

index af27c3a..447d595 100644 (file)
@@ -759,6 +759,8 @@ CREATE RULE protect_usr_message_delete AS
        );
 
 CREATE FUNCTION actor.convert_usr_note_to_message () RETURNS TRIGGER AS $$
+DECLARE
+       sending_ou INTEGER;
 BEGIN
        IF NEW.pub THEN
                IF TG_OP = 'UPDATE' THEN
@@ -767,8 +769,16 @@ BEGIN
                        END IF;
                END IF;
 
+               SELECT INTO sending_ou aw.owning_lib
+               FROM auditor.get_audit_info() agai
+               JOIN actor.workstation aw ON (aw.id = agai.eg_ws);
+               IF sending_ou IS NULL THEN
+                       SELECT INTO sending_ou home_ou
+                       FROM actor.usr
+                       WHERE id = NEW.creator;
+               END IF;
                INSERT INTO actor.usr_message (usr, title, message, sending_lib)
-                       VALUES (NEW.usr, NEW.title, NEW.value, (SELECT home_ou FROM actor.usr WHERE id = NEW.creator));
+                       VALUES (NEW.usr, NEW.title, NEW.value, sending_ou);
        END IF;
 
        RETURN NEW;
diff --git a/Open-ILS/src/sql/Pg/upgrade/ZZZZ.schema.use-ws-lib-for-messages.sql b/Open-ILS/src/sql/Pg/upgrade/ZZZZ.schema.use-ws-lib-for-messages.sql
new file mode 100644 (file)
index 0000000..0163d0b
--- /dev/null
@@ -0,0 +1,30 @@
+BEGIN;
+
+CREATE OR REPLACE FUNCTION actor.convert_usr_note_to_message () RETURNS TRIGGER AS $$
+DECLARE
+       sending_ou INTEGER;
+BEGIN
+       IF NEW.pub THEN
+               IF TG_OP = 'UPDATE' THEN
+                       IF OLD.pub = TRUE THEN
+                               RETURN NEW;
+                       END IF;
+               END IF;
+
+               SELECT INTO sending_ou aw.owning_lib
+               FROM auditor.get_audit_info() agai
+               JOIN actor.workstation aw ON (aw.id = agai.eg_ws);
+               IF sending_ou IS NULL THEN
+                       SELECT INTO sending_ou home_ou
+                       FROM actor.usr
+                       WHERE id = NEW.creator;
+               END IF;
+               INSERT INTO actor.usr_message (usr, title, message, sending_lib)
+                       VALUES (NEW.usr, NEW.title, NEW.value, sending_ou);
+       END IF;
+
+       RETURN NEW;
+END;
+$$ LANGUAGE PLPGSQL;
+
+COMMIT;