# Bulk Download Appraisal Notifications

INSERT INTO ohrm_workflow_state_machine (workflow, state, role, action, resulting_state, roles_to_notify, screens_to_show, priority)
SELECT wsm.workflow, wsm.state, wsm.role, 'Download', wsm.resulting_state, wsm.roles_to_notify, 'SINGLE_VIEW,MULTIPLE_VIEW', 0
FROM ohrm_workflow_state_machine wsm WHERE wsm.workflow IN (
    SELECT id FROM ohrm_workflow WHERE module_id = (
        SELECT id FROM ohrm_module WHERE name = 'performance'
    )
    AND
    group_name = 'performance appraisals'
    AND
    id <> 'APPRAISAL CYCLE'
) AND wsm.workflow <> 'MAIN APPRAISAL' AND wsm.action = 'Read';

INSERT INTO ohrm_workflow_state_machine (workflow, state, role, action, resulting_state, roles_to_notify, screens_to_show, priority)
SELECT wsm.workflow, wsm.state, wsm.role, 'Download', wsm.resulting_state, wsm.roles_to_notify, 1, 0
FROM ohrm_workflow_state_machine wsm WHERE
wsm.workflow = 'MAIN APPRAISAL' AND
wsm.action = 'View' AND state NOT IN ('NOT INITIATED');

INSERT INTO `ohrm_workflow_state_machine` (`workflow`, `state`, `role`, `action`, `resulting_state`, `roles_to_notify`, `screens_to_show`, `priority`)
VALUES
('MAIN APPRAISAL', 'COMPLETED', 'ESS', 'Download', 'COMPLETED', '', '1', 0),
('MAIN APPRAISAL', 'INITIATED', 'ESS', 'Download', 'INITIATED', '', '1', 0);

SET @performance_module := (SELECT `id` FROM `ohrm_module` WHERE  `name` ='performance');
SET @action_notification_type := (SELECT id
                                  FROM ohrm_notification_type
                                  where `name` = 'Action Based');
SET @email_media := (SELECT id
                     FROM ohrm_notification_media
                     WHERE `name` = 'Email');
INSERT INTO `ohrm_notification`
(`name`, `is_enable`,`type_id`, `generator_class`, `parameters`, `workflow_id`, `workflow_action`, `module_id`, `hidden`) VALUES
('Bulk Download Appraisal Notification', 1, @action_notification_type, 'BulkDownloadAppraisalNotificationProcessor',
 null, null, null, @performance_module, 1);

SET @notification_id := LAST_INSERT_ID();

INSERT INTO `ohrm_notification_subscription` (notification_id, workflow_item_id)
SELECT @notification_id, id FROM ohrm_workflow_state_machine WHERE
`workflow` = 'MAIN APPRAISAL' AND `action` = 'Download';
SET @last_subscription_id := LAST_INSERT_ID();

INSERT INTO `ohrm_notification_subscriber`(media_id, parameters, type, name) VALUES
(@email_media, NULL, 'Custom', 'Bulk Downloader');
SET @subscriber_id := LAST_INSERT_ID();

INSERT INTO `ohrm_notification_supported_media` (`notification_id`, `media_id`) VALUES
(@notification_id, @email_media);


INSERT INTO `ohrm_notification_subscriber_subscription_template`
(`notification_id`,`media_id`,`locale`,`name`,`subject`,`body`) VALUES
(@notification_id, @email_media, 'en_US',
'Bulk Download Appraisal Notification',
'Appraisal Download Link - %date% | %time%',
'<p>Dear %recipientName%,<br/><br/>Please use the following link to download the selected appraisal(s).<br/><br/><a href="%fileUrl%">Download link</a><br/><br/>Note: This link will be valid for next 24 hours and only you are authorized to download.<br/><br/>Thank You.<br/><br/>(This is a system generated email, please do not reply!)<p>');

SET @template_id := LAST_INSERT_ID();

INSERT INTO `ohrm_notification_subscriber_subscription`(subscription_id, subscriber_id, template_id) SELECT
s.id, @subscriber_id, @template_id FROM ohrm_notification_subscription s WHERE s.id >= @last_subscription_id;

CREATE TABLE IF NOT EXISTS `ohrm_bulk_appraisal_archives` (
    `id` INT(11) UNSIGNED NOT NULL auto_increment,
    `file_identity` VARCHAR(100) NOT NULL,
    `created_by_user_id` INT(11) NOT NULL,
    `file` LONGBLOB,
    `file_name` VARCHAR(300) DEFAULT NULL,
    `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `updated_at` TIMESTAMP NOT NULL ON UPDATE CURRENT_TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY(id),
    CONSTRAINT `fk_ohrm_bulk_appraisal_archives_ohrm_user` FOREIGN KEY (`created_by_user_id`) REFERENCES `ohrm_user` (`id`) ON DELETE CASCADE,
    CONSTRAINT `unique_file_identity` UNIQUE (file_identity)
)
ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `ohrm_notification_subscriber_subscription_template`
(`notification_id`,`media_id`,`locale`,`name`,`subject`,`body`) VALUES
(@notification_id, @email_media, 'en_US',
 'Bulk Download Appraisal Failure Notification',
 'Appraisal Download Link - %date% | %time% - Failed',
 '<p>Dear %recipientName%,<br/><br/>An error occurred during the file generation. Please try again.<br/><br/>Thank You.<br/><br/>This is an auto-generated email.<p>');


INSERT INTO `ohrm_cleanup_entities_config` (`entity_name`, `configuration`, `delete_days_after`, `date_configuration_getter`, `delete_strategy`)
VALUES ('BulkAppraisalArchives', '{"columnNames":["file"]}', '1', 'created_at', 'BaseDataDeletionStrategyDao');

INSERT INTO `ohrm_config` (`property`, `value`) VALUES ('performance.appraisal_bulk_download_limit', '100');
