USE [Elyse_DB] GO /****** Object: StoredProcedure [reviewing].[usp_UPD_wf_people_output_file] Script Date: Sun 22-11-2026 9:04:49 PM ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: Silkwood Software -- Create date: 10-09-2024 -- Description: Initial creation -- Updates a record from workflow_instances.workflow_person_output_files. -- All old and new parameters must be sent. -- Output is a status message and transaction status. /* COPYRIGHT NOTICE This database schema and stored procedures are protected by copyright. Copyright. Silkwood Software Pty. Ltd. 2024 */ -- ========================================================= CREATE PROCEDURE [reviewing].[usp_UPD_wf_people_output_file] @fileid bigint = NULL, @instance_step_id bigint = NULL, @new_fileid bigint = NULL, @new_instance_step_id bigint = NULL, @message nvarchar(1000) = '' OUTPUT, @transaction_status nvarchar(50) = NULL OUTPUT AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; DECLARE @tempmessage nvarchar(300) = '', @tempuserauth_status nchar(10) = '', @connectedusersid varbinary(100) = SUSER_SID(ORIGINAL_LOGIN()), -- The SID of the connected user @username nvarchar(150) = ORIGINAL_LOGIN(), -- The username @sidid bigint, -- The user id @fileauthenticationstatus nchar(10) = '', -- Status of authorisation for the connected user to access the file -- or alternatively if the file does not exist. The result does not distinguish either. @validationmessage nvarchar(200) = '', -- Communicates that the transaction failed due to data validation @userauthentication_status nchar(10) = 'Fail', -- The outcome of the authentication check of the user @object_auth_result nvarchar(10) = 'Pass', -- Result of authentication check of the user for the given object. @docauthenticationstatus nchar(10) = '', -- Communicates if the user is not authorised to access the document -- or alternatively if the document does not exist. The result does not distinguish either. @tempvalidationstatus nchar(10) = '', @transaction_ready nchar(10) = 'Ready', @data_validation_status nchar(10) = 'Pass'; -- Parameters which have been initialised at declaration but not explicitly set might be output as null to calling function. SET @transaction_status = 'Transaction not attempted'; -- Connected user authentication -- Authenticate the connected user for the role EXEC [internal].[usp_AUTHENTICATE_user_role] @role_to_check = 'Editor', @user_authentication_result = @tempuserauth_status OUTPUT; IF @tempuserauth_status = 'Pass' SET @userauthentication_status = 'Pass'; EXEC [internal].[usp_AUTHENTICATE_user_role] @role_to_check = 'Reviewer', @user_authentication_result = @tempuserauth_status OUTPUT; IF @tempuserauth_status = 'Pass' SET @userauthentication_status = 'Pass'; IF @userauthentication_status = 'Fail' BEGIN -- The user does not have permission for this action SET @transaction_ready = 'Fail'; EXEC internal.usp_SEL_message @message_id = 'NoPermission', @message_text = @tempmessage OUTPUT; IF (@tempmessage IS NOT NULL) SET @message = CONCAT(@message, ' | ', ISNULL(@username, ''), ' ', @tempmessage); ELSE SET @message = CONCAT_WS(' | ', @message,'A database level message error occurred on NoPermission.'); END IF @userauthentication_status = 'Pass' -- Don't do anything if the user is not authorised. BEGIN -- Data validation -- Check that a file id was supplied IF (@new_fileid IS NULL OR @new_fileid = 0) BEGIN SET @data_validation_status = 'Fail'; SET @transaction_ready = 'Fail'; EXEC internal.usp_SEL_message @message_id = 'NoFileID', @message_text = @tempmessage OUTPUT; IF (@tempmessage IS NOT NULL) SET @message = CONCAT_WS(' | ', @message, @tempmessage); ELSE SET @message = CONCAT_WS(' | ', @message, 'A database level message error occurred on NoFileID.'); END ELSE -- File id has been supplied BEGIN -- Check if the file ID exists and the user has edit permission for the file EXEC [internal].[usp_AUTHENTICATE_file_ed_perm] @file_id_to_check_ep = @new_fileid, @user_authentication_result_fep = @fileauthenticationstatus OUTPUT; IF @fileauthenticationstatus = 'Fail' BEGIN -- File id does not exist or the user does not have permission to access it SET @data_validation_status = 'Fail'; SET @transaction_ready = 'Fail'; EXEC internal.usp_SEL_message @message_id = 'FileIDNotExist', @message_text = @tempmessage OUTPUT; IF @tempmessage IS NOT NULL SET @message = CONCAT(@message, ' | ', 'File ID: ', CONVERT(nvarchar(20), @new_fileid), '. ', @tempmessage); ELSE SET @message = CONCAT_WS(' | ', @message, 'A database level message error occurred on FileIDNotExist.'); END END -- End checking the file id -- Check the instance step id IF @instance_step_id = 0 SET @instance_step_id = NULL; -- Check that the user has permission for it, in relation to the linked document. BEGIN EXEC [internal].[usp_AUTHENTICATE_wf_inst_step] @instance_step_to_check =@instance_step_id, @step_authentication_result = @object_auth_result OUTPUT; IF @object_auth_result = 'Fail' BEGIN SET @data_validation_status = 'Fail'; SET @transaction_ready = 'Fail'; EXEC internal.usp_SEL_message @message_id = 'WFStepNotAuth', @message_text = @tempmessage OUTPUT; IF (@tempmessage IS NOT NULL) SET @message = CONCAT_WS(' | ', @message, @tempmessage); ELSE SET @message = CONCAT_WS(' | ', @message, 'A database level message error occurred on WFStepNotAuth.'); END END -- End checking the instance step id IF @new_instance_step_id = 0 SET @new_instance_step_id = NULL; -- Check if an instance step id was supplied. IF @new_instance_step_id IS NULL BEGIN SET @data_validation_status = 'Fail'; SET @transaction_ready = 'Fail'; EXEC internal.usp_SEL_message @message_id = 'NoWfInstanceStepID', @message_text = @tempmessage OUTPUT; IF (@tempmessage IS NOT NULL) SET @message = CONCAT_WS(' | ', @message, @tempmessage); ELSE SET @message = CONCAT_WS(' | ', @message, 'A database level message error occurred on NoWfInstanceStepID.'); END ELSE -- An instance step id has been supplied BEGIN -- Check that the instance step id exists IF NOT EXISTS (SELECT workflow_instance_step_id FROM workflow_instances.workflow_instance_steps WHERE workflow_instance_step_id = @new_instance_step_id ) BEGIN -- Record id does not exist SET @data_validation_status = 'Fail'; SET @transaction_ready = 'Fail'; EXEC internal.usp_SEL_message @message_id = 'WfInstStepIdInvalid', @message_text = @tempmessage OUTPUT; IF (@tempmessage IS NOT NULL) SET @message = CONCAT_WS(' | ', @message, @tempmessage); ELSE SET @message = CONCAT_WS(' | ', @message, 'A database level message error occurred on WfInstStepIdInvalid.'); END ELSE -- The record exists. Check that the user has permission for it, in relation to the linked document. BEGIN EXEC [internal].[usp_AUTHENTICATE_wf_inst_step] @instance_step_to_check = @new_instance_step_id, @step_authentication_result = @object_auth_result OUTPUT; IF @object_auth_result = 'Fail' BEGIN SET @data_validation_status = 'Fail'; SET @transaction_ready = 'Fail'; EXEC internal.usp_SEL_message @message_id = 'WFStepNotAuth', @message_text = @tempmessage OUTPUT; IF (@tempmessage IS NOT NULL) SET @message = CONCAT_WS(' | ', @message, @tempmessage); ELSE SET @message = CONCAT_WS(' | ', @message, 'A database level message error occurred on WFStepNotAuth.'); END END END -- Retrieve the sidid SET @sidid = (SELECT sl.sid_id FROM user_restr.sid_list AS sl WHERE sl.sid = @connectedusersid) -- Check if the record exists IF @data_validation_status = 'Pass' BEGIN IF NOT EXISTS (SELECT workflow_instance_step_id FROM workflow_instances.workflow_person_output_files WHERE file_id = @fileid AND workflow_instance_step_id = @instance_step_id AND sid_id = @sidid) BEGIN -- Record does not exist SET @data_validation_status = 'Fail'; SET @transaction_ready = 'Fail'; EXEC internal.usp_SEL_message @message_id = 'NotExist', @message_text = @tempmessage OUTPUT; IF (@tempmessage IS NOT NULL) SET @message = CONCAT_WS(' | ', @message, @tempmessage); ELSE SET @message = CONCAT_WS(' | ', @message, 'A database level message error occurred on NotExist.'); END END IF @data_validation_status = 'Pass' BEGIN -- Check if a duplicate record already exists IF EXISTS (SELECT workflow_instance_step_id FROM workflow_instances.workflow_person_output_files WHERE file_id = @new_fileid AND workflow_instance_step_id = @new_instance_step_id AND sid_id = @sidid) BEGIN -- Record already exists SET @data_validation_status = 'Fail'; SET @transaction_ready = 'Fail'; EXEC internal.usp_SEL_message @message_id = 'RecordExists', @message_text = @tempmessage OUTPUT; IF (@tempmessage IS NOT NULL) SET @message = CONCAT_WS(' | ', @message, @tempmessage); ELSE SET @message = CONCAT_WS(' | ', @message, 'A database level message error occurred on RecordExists.'); END -- Check that there is a matching record of workflow instance step id and people id in workflow_instances.workflow_step_duty_functions IF NOT EXISTS (SELECT workflow_instance_step_id FROM workflow_instances.workflow_step_participants WHERE workflow_instance_step_id = @new_instance_step_id AND sid_id = @sidid) BEGIN -- The person is not linked to the workflow step SET @data_validation_status = 'Fail'; SET @transaction_ready = 'Fail'; EXEC internal.usp_SEL_message @message_id = 'NoFunctionStepLink', @message_text = @tempmessage OUTPUT; IF (@tempmessage IS NOT NULL) SET @message = CONCAT_WS(' | ', @message, @tempmessage); ELSE SET @message = CONCAT_WS(' | ', @message, 'A database level message error occurred on NoFunctionStepLink.'); END END --END IF @data_validation_status = 'Pass' after initial validation checks. -- Output the data validation status failed message IF @data_validation_status = 'Fail' BEGIN EXEC internal.usp_SEL_message @message_id = 'FailedDataValidation', @message_text = @tempmessage OUTPUT; IF (@tempmessage IS NOT NULL) SET @message = CONCAT_WS(' | ', @message, @tempmessage); ELSE SET @message = CONCAT_WS(' | ', @message, 'A database level message error occurred on FailedDataValidation'); END -- End data validation -- Execute the delete query IF @transaction_ready = 'Ready' BEGIN BEGIN TRY UPDATE workflow_instances.workflow_person_output_files SET file_id = @new_fileid, workflow_instance_step_id = @new_instance_step_id WHERE file_id = @fileid AND workflow_instance_step_id = @instance_step_id AND sid_id = @sidid; EXEC internal.usp_SEL_message @message_id = 'Success', @message_text = @tempmessage OUTPUT; IF (@tempmessage IS NOT NULL) SET @message = CONCAT_WS(' | ', @message, @tempmessage); ELSE SET @message = CONCAT_WS(' | ', @message, 'A database level message error occurred on Success'); SET @transaction_status = 'Good'; END TRY BEGIN CATCH SET @transaction_status = 'Bad'; EXEC internal.usp_SEL_message @message_id = 'UpdateError', @message_text = @tempmessage OUTPUT; IF (@tempmessage IS NOT NULL) SET @message = CONCAT(@message, ' | ', @tempmessage, ' | ', CONVERT(nvarchar(10),ERROR_NUMBER()), ' | ', ERROR_MESSAGE()); ELSE SET @message = CONCAT_WS(' | ', @message, 'A database level message error occurred on UpdateError'); END CATCH END -- END IF @transaction_ready = 'Ready' END -- End IF @userauthentication = 'Pass' END GO