INSERT INTO container.copy_bucket_type (code,label) VALUES ('misc', oils_i18n_gettext('misc', 'Miscellaneous', 'ccpbt', 'label'));
INSERT INTO container.copy_bucket_type (code,label) VALUES ('staff_client', oils_i18n_gettext('staff_client', 'General Staff Client container', 'ccpbt', 'label'));
+INSERT INTO container.copy_bucket_type (code,label) VALUES ( 'circ_history', 'Circulation History' );
INSERT INTO container.call_number_bucket_type (code,label) VALUES ('misc', oils_i18n_gettext('misc', 'Miscellaneous', 'ccnbt', 'label'));
INSERT INTO container.biblio_record_entry_bucket_type (code,label) VALUES ('misc', oils_i18n_gettext('misc', 'Miscellaneous', 'cbrebt', 'label'));
INSERT INTO container.biblio_record_entry_bucket_type (code,label) VALUES ('staff_client', oils_i18n_gettext('staff_client', 'General Staff Client container', 'cbrebt', 'label'));
*/
$$;
-
+CREATE OR REPLACE FUNCTION container.clear_expired_circ_history_items(
+ ac_usr IN INTEGER
+) RETURNS VOID AS $$
+--
+-- Delete old circulation bucket items for a specified user.
+-- "Old" means older than the interval specified by a
+-- user-level setting, if it is so specified.
+--
+DECLARE
+ threshold TIMESTAMP WITH TIME ZONE;
+BEGIN
+ -- Sanity check
+ IF ac_usr IS NULL THEN
+ RETURN;
+ END IF;
+ -- Determine the threshold date that defines "old". Subtract the
+ -- interval from the system date, then truncate to midnight.
+ SELECT
+ date_trunc(
+ 'day',
+ now() - CAST( translate( value, '"', '' ) AS INTERVAL )
+ )
+ INTO
+ threshold
+ FROM
+ actor.usr_setting
+ WHERE
+ usr = ac_usr
+ AND name = 'patron.max_reading_list_interval';
+ --
+ IF threshold is null THEN
+ -- No interval defined; don't delete anything
+ -- RAISE NOTICE 'No interval defined for user %', ac_usr;
+ return;
+ END IF;
+ --
+ -- RAISE NOTICE 'Date threshold: %', threshold;
+ --
+ -- Threshold found; do the delete
+ delete from container.copy_bucket_item
+ where
+ bucket in
+ (
+ select
+ id
+ from
+ container.copy_bucket
+ where
+ owner = ac_usr
+ and btype = 'circ_history'
+ )
+ and create_time < threshold;
+ --
+ RETURN;
+END;
+$$ LANGUAGE plpgsql;