From da8363c51ea3be773348b8178b9dce0529df7abc Mon Sep 17 00:00:00 2001 From: praju-aot Date: Mon, 21 Oct 2024 14:41:33 -0700 Subject: [PATCH 01/16] feat: ORV2-2857 - Update DDL model of Receipt and Transaction --- .../versions/revert/v_46_ddl_revert.sql | 219 ++++++++++++++++ database/mssql/scripts/versions/v_46_ddl.sql | 238 ++++++++++++++++++ 2 files changed, 457 insertions(+) create mode 100644 database/mssql/scripts/versions/revert/v_46_ddl_revert.sql create mode 100644 database/mssql/scripts/versions/v_46_ddl.sql diff --git a/database/mssql/scripts/versions/revert/v_46_ddl_revert.sql b/database/mssql/scripts/versions/revert/v_46_ddl_revert.sql new file mode 100644 index 000000000..453662f3e --- /dev/null +++ b/database/mssql/scripts/versions/revert/v_46_ddl_revert.sql @@ -0,0 +1,219 @@ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +SET NOCOUNT ON +GO + +SET XACT_ABORT ON +GO +SET TRANSACTION ISOLATION LEVEL SERIALIZABLE +GO +BEGIN TRANSACTION +GO + + +IF @@ERROR <> 0 SET NOEXEC ON +GO +ALTER TABLE [permit].[ORBC_RECEIPT] ADD [TRANSACTION_ID] [bigint] NULL + + +IF @@ERROR <> 0 SET NOEXEC ON +GO +ALTER TABLE [permit].[ORBC_RECEIPT] WITH CHECK ADD CONSTRAINT [ORBC_RECEIPT_TRANSACTION_ID_FK] FOREIGN KEY([TRANSACTION_ID]) +REFERENCES [permit].[ORBC_TRANSACTION] ([TRANSACTION_ID]) + +-- Alter trigger permit.ORBC_RCPT_A_S_IUD_TR +PRINT N'Alter trigger permit.ORBC_RCPT_A_S_IUD_TR' +GO +ALTER TRIGGER [permit].[ORBC_RCPT_A_S_IUD_TR] ON permit.[ORBC_RECEIPT] FOR INSERT, UPDATE, DELETE AS +SET NOCOUNT ON +BEGIN TRY +DECLARE @curr_date datetime; +SET @curr_date = getutcdate(); + IF NOT EXISTS(SELECT * FROM inserted) AND NOT EXISTS(SELECT * FROM deleted) + RETURN; + + -- historical + IF EXISTS(SELECT * FROM deleted) + update [permit].[ORBC_RECEIPT_HIST] set END_DATE_HIST = @curr_date where RECEIPT_ID in (select RECEIPT_ID from deleted) and END_DATE_HIST is null; + + IF EXISTS(SELECT * FROM inserted) + insert into [permit].[ORBC_RECEIPT_HIST] ([RECEIPT_ID], [RECEIPT_NUMBER], [TRANSACTION_ID], [RECEIPT_DOCUMENT_ID], [APP_CREATE_TIMESTAMP], [APP_CREATE_USERID], [APP_CREATE_USER_GUID], [APP_CREATE_USER_DIRECTORY], [APP_LAST_UPDATE_TIMESTAMP], [APP_LAST_UPDATE_USERID], [APP_LAST_UPDATE_USER_GUID], [APP_LAST_UPDATE_USER_DIRECTORY], [CONCURRENCY_CONTROL_NUMBER], [DB_CREATE_USERID], [DB_CREATE_TIMESTAMP], [DB_LAST_UPDATE_USERID], [DB_LAST_UPDATE_TIMESTAMP], _RECEIPT_HIST_ID, END_DATE_HIST, EFFECTIVE_DATE_HIST) + select [RECEIPT_ID], [RECEIPT_NUMBER], [TRANSACTION_ID], [RECEIPT_DOCUMENT_ID], [APP_CREATE_TIMESTAMP], [APP_CREATE_USERID], [APP_CREATE_USER_GUID], [APP_CREATE_USER_DIRECTORY], [APP_LAST_UPDATE_TIMESTAMP], [APP_LAST_UPDATE_USERID], [APP_LAST_UPDATE_USER_GUID], [APP_LAST_UPDATE_USER_DIRECTORY], [CONCURRENCY_CONTROL_NUMBER], [DB_CREATE_USERID], [DB_CREATE_TIMESTAMP], [DB_LAST_UPDATE_USERID], [DB_LAST_UPDATE_TIMESTAMP], (next value for [permit].[ORBC_RECEIPT_H_ID_SEQ]) as [_RECEIPT_HIST_ID], null as [END_DATE_HIST], @curr_date as [EFFECTIVE_DATE_HIST] from inserted; + +END TRY +BEGIN CATCH + IF @@trancount > 0 ROLLBACK TRANSACTION + EXEC orbc_error_handling +END CATCH; +GO + +IF @@ERROR <> 0 SET NOEXEC ON +GO + +-- Alter trigger permit.ORBC_RCPT_I_S_U_TR +PRINT N'Alter trigger permit.ORBC_RCPT_I_S_U_TR' +GO +ALTER TRIGGER [permit].[ORBC_RCPT_I_S_U_TR] ON permit.[ORBC_RECEIPT] INSTEAD OF UPDATE AS +SET NOCOUNT ON +BEGIN TRY + IF NOT EXISTS(SELECT * FROM deleted) + RETURN; + + -- validate concurrency control + if exists (select 1 from inserted, deleted where inserted.CONCURRENCY_CONTROL_NUMBER != deleted.CONCURRENCY_CONTROL_NUMBER+1 AND inserted.RECEIPT_ID = deleted.RECEIPT_ID) + raiserror('CONCURRENCY FAILURE.',16,1) + + + -- update statement + update [permit].[ORBC_RECEIPT] + set "RECEIPT_NUMBER" = inserted."RECEIPT_NUMBER", + "TRANSACTION_ID" = inserted."TRANSACTION_ID", + "RECEIPT_DOCUMENT_ID" = inserted."RECEIPT_DOCUMENT_ID", + "APP_LAST_UPDATE_TIMESTAMP" = inserted."APP_LAST_UPDATE_TIMESTAMP", + "APP_LAST_UPDATE_USERID" = inserted."APP_LAST_UPDATE_USERID", + "APP_LAST_UPDATE_USER_GUID" = inserted."APP_LAST_UPDATE_USER_GUID", + "APP_LAST_UPDATE_USER_DIRECTORY" = inserted."APP_LAST_UPDATE_USER_DIRECTORY", + "CONCURRENCY_CONTROL_NUMBER" = inserted."CONCURRENCY_CONTROL_NUMBER" + , DB_LAST_UPDATE_TIMESTAMP = getutcdate() + , DB_LAST_UPDATE_USERID = user_name() + from [permit].[ORBC_RECEIPT] + inner join inserted + on (ORBC_RECEIPT.RECEIPT_ID = inserted.RECEIPT_ID); + +END TRY +BEGIN CATCH + IF @@trancount > 0 ROLLBACK TRANSACTION + EXEC orbc_error_handling +END CATCH; +GO +IF @@ERROR <> 0 SET NOEXEC ON +GO +DISABLE TRIGGER [permit].[ORBC_RCPT_I_S_U_TR] ON permit.[ORBC_RECEIPT] +GO + +IF @@ERROR <> 0 SET NOEXEC ON +GO +UPDATE r +SET TRANSACTION_ID = t.TRANSACTION_ID +FROM [permit].[ORBC_RECEIPT] r +INNER JOIN [permit].[ORBC_TRANSACTION] t ON t.RECEIPT_ID = r.RECEIPT_ID; + +IF @@ERROR <> 0 SET NOEXEC ON +GO +ALTER TABLE [permit].[ORBC_RECEIPT] ALTER COLUMN [TRANSACTION_ID] [bigint] NOT NULL + +IF @@ERROR <> 0 SET NOEXEC ON +GO +AlTER TABLE [permit].[ORBC_RECEIPT_HIST] ALTER COLUMN [TRANSACTION_ID] [bigint] NOT NULL + + +IF @@ERROR <> 0 SET NOEXEC ON +GO +ALTER TABLE [permit].[ORBC_TRANSACTION] DROP CONSTRAINT FK_ORBC_TRANSACTION_RECEIPT_ID + +IF @@ERROR <> 0 SET NOEXEC ON +GO +ALTER TABLE [permit].[ORBC_TRANSACTION] DROP COLUMN [RECEIPT_ID] + + +IF @@ERROR <> 0 SET NOEXEC ON +GO +-- Alter trigger permit.ORBC_TXN_A_S_IUD_TR +PRINT N'Alter trigger permit.ORBC_TXN_A_S_IUD_TR' +GO +ALTER TRIGGER [permit].[ORBC_TXN_A_S_IUD_TR] ON permit.[ORBC_TRANSACTION] FOR INSERT, UPDATE, DELETE AS +SET NOCOUNT ON +BEGIN TRY +DECLARE @curr_date datetime; +SET @curr_date = getutcdate(); + IF NOT EXISTS(SELECT * FROM inserted) AND NOT EXISTS(SELECT * FROM deleted) + RETURN; + + -- historical + IF EXISTS(SELECT * FROM deleted) + update [permit].[ORBC_TRANSACTION_HIST] set END_DATE_HIST = @curr_date where TRANSACTION_ID in (select TRANSACTION_ID from deleted) and END_DATE_HIST is null; + + IF EXISTS(SELECT * FROM inserted) + insert into [permit].[ORBC_TRANSACTION_HIST] ([TRANSACTION_ID], [TRANSACTION_TYPE], [PAYMENT_METHOD_TYPE], [PAYMENT_CARD_TYPE], [TOTAL_TRANSACTION_AMOUNT], [TRANSACTION_SUBMIT_DATE], [TRANSACTION_ORDER_NUMBER], [PG_TRANSACTION_ID], [PG_TRANSACTION_APPROVED], [PG_AUTH_CODE], [PG_TRANSACTION_CARD_TYPE], [PG_TRANSACTION_DATE], [PG_CVD_ID], [PG_PAYMENT_METHOD], [PG_MESSAGE_ID], [PG_MESSAGE_TEXT], [APP_CREATE_TIMESTAMP], [APP_CREATE_USERID], [APP_CREATE_USER_GUID], [APP_CREATE_USER_DIRECTORY], [APP_LAST_UPDATE_TIMESTAMP], [APP_LAST_UPDATE_USERID], [APP_LAST_UPDATE_USER_GUID], [APP_LAST_UPDATE_USER_DIRECTORY], [CONCURRENCY_CONTROL_NUMBER], [DB_CREATE_USERID], [DB_CREATE_TIMESTAMP], [DB_LAST_UPDATE_USERID], [DB_LAST_UPDATE_TIMESTAMP], _TRANSACTION_HIST_ID, END_DATE_HIST, EFFECTIVE_DATE_HIST) + select [TRANSACTION_ID], [TRANSACTION_TYPE], [PAYMENT_METHOD_TYPE], [PAYMENT_CARD_TYPE], [TOTAL_TRANSACTION_AMOUNT], [TRANSACTION_SUBMIT_DATE], [TRANSACTION_ORDER_NUMBER], [PG_TRANSACTION_ID], [PG_TRANSACTION_APPROVED], [PG_AUTH_CODE], [PG_TRANSACTION_CARD_TYPE], [PG_TRANSACTION_DATE], [PG_CVD_ID], [PG_PAYMENT_METHOD], [PG_MESSAGE_ID], [PG_MESSAGE_TEXT], [APP_CREATE_TIMESTAMP], [APP_CREATE_USERID], [APP_CREATE_USER_GUID], [APP_CREATE_USER_DIRECTORY], [APP_LAST_UPDATE_TIMESTAMP], [APP_LAST_UPDATE_USERID], [APP_LAST_UPDATE_USER_GUID], [APP_LAST_UPDATE_USER_DIRECTORY], [CONCURRENCY_CONTROL_NUMBER], [DB_CREATE_USERID], [DB_CREATE_TIMESTAMP], [DB_LAST_UPDATE_USERID], [DB_LAST_UPDATE_TIMESTAMP], (next value for [permit].[ORBC_TRANSACTION_H_ID_SEQ]) as [_TRANSACTION_HIST_ID], null as [END_DATE_HIST], @curr_date as [EFFECTIVE_DATE_HIST] from inserted; + +END TRY +BEGIN CATCH + IF @@trancount > 0 ROLLBACK TRANSACTION + EXEC orbc_error_handling +END CATCH; +GO +IF @@ERROR <> 0 SET NOEXEC ON +GO + + +-- Alter trigger permit.ORBC_TXN_I_S_U_TR +PRINT N'Alter trigger permit.ORBC_TXN_I_S_U_TR' +GO +ALTER TRIGGER [permit].[ORBC_TXN_I_S_U_TR] ON permit.[ORBC_TRANSACTION] INSTEAD OF UPDATE AS +SET NOCOUNT ON +BEGIN TRY + IF NOT EXISTS(SELECT * FROM deleted) + RETURN; + + -- validate concurrency control + if exists (select 1 from inserted, deleted where inserted.CONCURRENCY_CONTROL_NUMBER != deleted.CONCURRENCY_CONTROL_NUMBER+1 AND inserted.TRANSACTION_ID = deleted.TRANSACTION_ID) + raiserror('CONCURRENCY FAILURE.',16,1) + + + -- update statement + update [permit].[ORBC_TRANSACTION] + set "TRANSACTION_TYPE" = inserted."TRANSACTION_TYPE", + "PAYMENT_METHOD_TYPE" = inserted."PAYMENT_METHOD_TYPE", + "PAYMENT_CARD_TYPE" = inserted."PAYMENT_CARD_TYPE", + "TOTAL_TRANSACTION_AMOUNT" = inserted."TOTAL_TRANSACTION_AMOUNT", + "TRANSACTION_SUBMIT_DATE" = inserted."TRANSACTION_SUBMIT_DATE", + "TRANSACTION_ORDER_NUMBER" = inserted."TRANSACTION_ORDER_NUMBER", + "PG_TRANSACTION_ID" = inserted."PG_TRANSACTION_ID", + "PG_TRANSACTION_APPROVED" = inserted."PG_TRANSACTION_APPROVED", + "PG_AUTH_CODE" = inserted."PG_AUTH_CODE", + "PG_TRANSACTION_CARD_TYPE" = inserted."PG_TRANSACTION_CARD_TYPE", + "PG_TRANSACTION_DATE" = inserted."PG_TRANSACTION_DATE", + "PG_CVD_ID" = inserted."PG_CVD_ID", + "PG_PAYMENT_METHOD" = inserted."PG_PAYMENT_METHOD", + "PG_MESSAGE_ID" = inserted."PG_MESSAGE_ID", + "PG_MESSAGE_TEXT" = inserted."PG_MESSAGE_TEXT", + "APP_LAST_UPDATE_TIMESTAMP" = inserted."APP_LAST_UPDATE_TIMESTAMP", + "APP_LAST_UPDATE_USERID" = inserted."APP_LAST_UPDATE_USERID", + "APP_LAST_UPDATE_USER_GUID" = inserted."APP_LAST_UPDATE_USER_GUID", + "APP_LAST_UPDATE_USER_DIRECTORY" = inserted."APP_LAST_UPDATE_USER_DIRECTORY", + "CONCURRENCY_CONTROL_NUMBER" = inserted."CONCURRENCY_CONTROL_NUMBER" + , DB_LAST_UPDATE_TIMESTAMP = getutcdate() + , DB_LAST_UPDATE_USERID = user_name() + from [permit].[ORBC_TRANSACTION] + inner join inserted + on (ORBC_TRANSACTION.TRANSACTION_ID = inserted.TRANSACTION_ID); + +END TRY +BEGIN CATCH + IF @@trancount > 0 ROLLBACK TRANSACTION + EXEC orbc_error_handling +END CATCH; +GO +IF @@ERROR <> 0 SET NOEXEC ON +GO +DISABLE TRIGGER [permit].[ORBC_TXN_I_S_U_TR] ON permit.[ORBC_TRANSACTION] +GO + + +IF @@ERROR <> 0 SET NOEXEC ON +GO +ALTER TABLE [permit].[ORBC_TRANSACTION_HIST] DROP COLUMN [RECEIPT_ID] + +IF @@ERROR <> 0 SET NOEXEC ON +GO +COMMIT TRANSACTION + +IF @@ERROR <> 0 SET NOEXEC ON +GO +DECLARE @VersionDescription VARCHAR(255) +SET @VersionDescription = 'Reverting addition of RECEIPT_ID col to ORBC_TRANSACTION and dropping TRANSACTION_ID from ORBC_RECEIPT.' + +INSERT [dbo].[ORBC_SYS_VERSION] ([VERSION_ID], [DESCRIPTION], [RELEASE_DATE]) VALUES (45, @VersionDescription, getutcdate()) diff --git a/database/mssql/scripts/versions/v_46_ddl.sql b/database/mssql/scripts/versions/v_46_ddl.sql new file mode 100644 index 000000000..37ef5076b --- /dev/null +++ b/database/mssql/scripts/versions/v_46_ddl.sql @@ -0,0 +1,238 @@ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +SET NOCOUNT ON +GO + +SET XACT_ABORT ON +GO +SET TRANSACTION ISOLATION LEVEL SERIALIZABLE +GO +BEGIN TRANSACTION +GO + +IF @@ERROR <> 0 SET NOEXEC ON +GO +ALTER TABLE [permit].[ORBC_TRANSACTION] ADD [RECEIPT_ID] [bigint] NULL + +IF @@ERROR <> 0 SET NOEXEC ON +GO +EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Receipt Id of the transaction' , @level0type=N'SCHEMA',@level0name=N'permit', @level1type=N'TABLE',@level1name=N'ORBC_TRANSACTION', @level2type=N'COLUMN',@level2name=N'RECEIPT_ID' + +IF @@ERROR <> 0 SET NOEXEC ON +GO + +IF @@ERROR <> 0 SET NOEXEC ON +GO +ALTER TABLE [permit].[ORBC_TRANSACTION] WITH CHECK ADD CONSTRAINT [FK_ORBC_TRANSACTION_RECEIPT_ID] FOREIGN KEY([RECEIPT_ID]) +REFERENCES [permit].[ORBC_RECEIPT] ([RECEIPT_ID]) +GO + +ALTER TABLE [permit].[ORBC_TRANSACTION] CHECK CONSTRAINT [FK_ORBC_TRANSACTION_RECEIPT_ID] + + +IF @@ERROR <> 0 SET NOEXEC ON +GO +ALTER TABLE [permit].[ORBC_TRANSACTION_HIST] ADD [RECEIPT_ID] [bigint] NULL + +IF @@ERROR <> 0 SET NOEXEC ON +GO +-- Alter trigger permit.ORBC_TXN_A_S_IUD_TR +PRINT N'Alter trigger permit.ORBC_TXN_A_S_IUD_TR' +GO +ALTER TRIGGER [permit].[ORBC_TXN_A_S_IUD_TR] ON permit.[ORBC_TRANSACTION] FOR INSERT, UPDATE, DELETE AS +SET NOCOUNT ON +BEGIN TRY +DECLARE @curr_date datetime; +SET @curr_date = getutcdate(); + IF NOT EXISTS(SELECT * FROM inserted) AND NOT EXISTS(SELECT * FROM deleted) + RETURN; + + -- historical + IF EXISTS(SELECT * FROM deleted) + update [permit].[ORBC_TRANSACTION_HIST] set END_DATE_HIST = @curr_date where TRANSACTION_ID in (select TRANSACTION_ID from deleted) and END_DATE_HIST is null; + + IF EXISTS(SELECT * FROM inserted) + insert into [permit].[ORBC_TRANSACTION_HIST] ([TRANSACTION_ID], [RECEIPT_ID], [TRANSACTION_TYPE], [PAYMENT_METHOD_TYPE], [PAYMENT_CARD_TYPE], [TOTAL_TRANSACTION_AMOUNT], [TRANSACTION_SUBMIT_DATE], [TRANSACTION_ORDER_NUMBER], [PG_TRANSACTION_ID], [PG_TRANSACTION_APPROVED], [PG_AUTH_CODE], [PG_TRANSACTION_CARD_TYPE], [PG_TRANSACTION_DATE], [PG_CVD_ID], [PG_PAYMENT_METHOD], [PG_MESSAGE_ID], [PG_MESSAGE_TEXT], [APP_CREATE_TIMESTAMP], [APP_CREATE_USERID], [APP_CREATE_USER_GUID], [APP_CREATE_USER_DIRECTORY], [APP_LAST_UPDATE_TIMESTAMP], [APP_LAST_UPDATE_USERID], [APP_LAST_UPDATE_USER_GUID], [APP_LAST_UPDATE_USER_DIRECTORY], [CONCURRENCY_CONTROL_NUMBER], [DB_CREATE_USERID], [DB_CREATE_TIMESTAMP], [DB_LAST_UPDATE_USERID], [DB_LAST_UPDATE_TIMESTAMP], _TRANSACTION_HIST_ID, END_DATE_HIST, EFFECTIVE_DATE_HIST) + select [TRANSACTION_ID], [RECEIPT_ID], [TRANSACTION_TYPE], [PAYMENT_METHOD_TYPE], [PAYMENT_CARD_TYPE], [TOTAL_TRANSACTION_AMOUNT], [TRANSACTION_SUBMIT_DATE], [TRANSACTION_ORDER_NUMBER], [PG_TRANSACTION_ID], [PG_TRANSACTION_APPROVED], [PG_AUTH_CODE], [PG_TRANSACTION_CARD_TYPE], [PG_TRANSACTION_DATE], [PG_CVD_ID], [PG_PAYMENT_METHOD], [PG_MESSAGE_ID], [PG_MESSAGE_TEXT], [APP_CREATE_TIMESTAMP], [APP_CREATE_USERID], [APP_CREATE_USER_GUID], [APP_CREATE_USER_DIRECTORY], [APP_LAST_UPDATE_TIMESTAMP], [APP_LAST_UPDATE_USERID], [APP_LAST_UPDATE_USER_GUID], [APP_LAST_UPDATE_USER_DIRECTORY], [CONCURRENCY_CONTROL_NUMBER], [DB_CREATE_USERID], [DB_CREATE_TIMESTAMP], [DB_LAST_UPDATE_USERID], [DB_LAST_UPDATE_TIMESTAMP], (next value for [permit].[ORBC_TRANSACTION_H_ID_SEQ]) as [_TRANSACTION_HIST_ID], null as [END_DATE_HIST], @curr_date as [EFFECTIVE_DATE_HIST] from inserted; + +END TRY +BEGIN CATCH + IF @@trancount > 0 ROLLBACK TRANSACTION + EXEC orbc_error_handling +END CATCH; +GO +IF @@ERROR <> 0 SET NOEXEC ON +GO + + +-- Alter trigger permit.ORBC_TXN_I_S_U_TR +PRINT N'Alter trigger permit.ORBC_TXN_I_S_U_TR' +GO +ALTER TRIGGER [permit].[ORBC_TXN_I_S_U_TR] ON permit.[ORBC_TRANSACTION] INSTEAD OF UPDATE AS +SET NOCOUNT ON +BEGIN TRY + IF NOT EXISTS(SELECT * FROM deleted) + RETURN; + + -- validate concurrency control + if exists (select 1 from inserted, deleted where inserted.CONCURRENCY_CONTROL_NUMBER != deleted.CONCURRENCY_CONTROL_NUMBER+1 AND inserted.TRANSACTION_ID = deleted.TRANSACTION_ID) + raiserror('CONCURRENCY FAILURE.',16,1) + + + -- update statement + update [permit].[ORBC_TRANSACTION] + set "TRANSACTION_TYPE" = inserted."TRANSACTION_TYPE", + "RECEIPT_ID" = inserted."RECEIPT_ID", + "PAYMENT_METHOD_TYPE" = inserted."PAYMENT_METHOD_TYPE", + "PAYMENT_CARD_TYPE" = inserted."PAYMENT_CARD_TYPE", + "TOTAL_TRANSACTION_AMOUNT" = inserted."TOTAL_TRANSACTION_AMOUNT", + "TRANSACTION_SUBMIT_DATE" = inserted."TRANSACTION_SUBMIT_DATE", + "TRANSACTION_ORDER_NUMBER" = inserted."TRANSACTION_ORDER_NUMBER", + "PG_TRANSACTION_ID" = inserted."PG_TRANSACTION_ID", + "PG_TRANSACTION_APPROVED" = inserted."PG_TRANSACTION_APPROVED", + "PG_AUTH_CODE" = inserted."PG_AUTH_CODE", + "PG_TRANSACTION_CARD_TYPE" = inserted."PG_TRANSACTION_CARD_TYPE", + "PG_TRANSACTION_DATE" = inserted."PG_TRANSACTION_DATE", + "PG_CVD_ID" = inserted."PG_CVD_ID", + "PG_PAYMENT_METHOD" = inserted."PG_PAYMENT_METHOD", + "PG_MESSAGE_ID" = inserted."PG_MESSAGE_ID", + "PG_MESSAGE_TEXT" = inserted."PG_MESSAGE_TEXT", + "APP_LAST_UPDATE_TIMESTAMP" = inserted."APP_LAST_UPDATE_TIMESTAMP", + "APP_LAST_UPDATE_USERID" = inserted."APP_LAST_UPDATE_USERID", + "APP_LAST_UPDATE_USER_GUID" = inserted."APP_LAST_UPDATE_USER_GUID", + "APP_LAST_UPDATE_USER_DIRECTORY" = inserted."APP_LAST_UPDATE_USER_DIRECTORY", + "CONCURRENCY_CONTROL_NUMBER" = inserted."CONCURRENCY_CONTROL_NUMBER" + , DB_LAST_UPDATE_TIMESTAMP = getutcdate() + , DB_LAST_UPDATE_USERID = user_name() + from [permit].[ORBC_TRANSACTION] + inner join inserted + on (ORBC_TRANSACTION.TRANSACTION_ID = inserted.TRANSACTION_ID); + +END TRY +BEGIN CATCH + IF @@trancount > 0 ROLLBACK TRANSACTION + EXEC orbc_error_handling +END CATCH; +GO +IF @@ERROR <> 0 SET NOEXEC ON +GO +DISABLE TRIGGER [permit].[ORBC_TXN_I_S_U_TR] ON permit.[ORBC_TRANSACTION] +GO + +IF @@ERROR <> 0 SET NOEXEC ON +GO +UPDATE t +SET RECEIPT_ID = r.RECEIPT_ID +FROM [permit].[ORBC_TRANSACTION] t +INNER JOIN [permit].[ORBC_RECEIPT] r ON t.TRANSACTION_ID = r.TRANSACTION_ID; + +IF @@ERROR <> 0 SET NOEXEC ON +GO +ALTER TABLE [permit].[ORBC_RECEIPT] DROP CONSTRAINT ORBC_RECEIPT_TRANSACTION_ID_FK + +IF @@ERROR <> 0 SET NOEXEC ON +GO +ALTER TABLE [permit].[ORBC_RECEIPT] DROP COLUMN [TRANSACTION_ID] + + +IF @@ERROR <> 0 SET NOEXEC ON +GO +AlTER TABLE [permit].[ORBC_RECEIPT_HIST] ALTER COLUMN [TRANSACTION_ID] [bigint] NULL + +IF @@ERROR <> 0 SET NOEXEC ON +GO +-- Alter trigger permit.ORBC_RCPT_A_S_IUD_TR +PRINT N'Alter trigger permit.ORBC_RCPT_A_S_IUD_TR' +GO +ALTER TRIGGER [permit].[ORBC_RCPT_A_S_IUD_TR] ON permit.[ORBC_RECEIPT] FOR INSERT, UPDATE, DELETE AS +SET NOCOUNT ON +BEGIN TRY +DECLARE @curr_date datetime; +SET @curr_date = getutcdate(); + IF NOT EXISTS(SELECT * FROM inserted) AND NOT EXISTS(SELECT * FROM deleted) + RETURN; + + -- historical + IF EXISTS(SELECT * FROM deleted) + update [permit].[ORBC_RECEIPT_HIST] set END_DATE_HIST = @curr_date where RECEIPT_ID in (select RECEIPT_ID from deleted) and END_DATE_HIST is null; + + IF EXISTS(SELECT * FROM inserted) + insert into [permit].[ORBC_RECEIPT_HIST] ([RECEIPT_ID], [RECEIPT_NUMBER], [RECEIPT_DOCUMENT_ID], [APP_CREATE_TIMESTAMP], [APP_CREATE_USERID], [APP_CREATE_USER_GUID], [APP_CREATE_USER_DIRECTORY], [APP_LAST_UPDATE_TIMESTAMP], [APP_LAST_UPDATE_USERID], [APP_LAST_UPDATE_USER_GUID], [APP_LAST_UPDATE_USER_DIRECTORY], [CONCURRENCY_CONTROL_NUMBER], [DB_CREATE_USERID], [DB_CREATE_TIMESTAMP], [DB_LAST_UPDATE_USERID], [DB_LAST_UPDATE_TIMESTAMP], _RECEIPT_HIST_ID, END_DATE_HIST, EFFECTIVE_DATE_HIST) + select [RECEIPT_ID], [RECEIPT_NUMBER], [RECEIPT_DOCUMENT_ID], [APP_CREATE_TIMESTAMP], [APP_CREATE_USERID], [APP_CREATE_USER_GUID], [APP_CREATE_USER_DIRECTORY], [APP_LAST_UPDATE_TIMESTAMP], [APP_LAST_UPDATE_USERID], [APP_LAST_UPDATE_USER_GUID], [APP_LAST_UPDATE_USER_DIRECTORY], [CONCURRENCY_CONTROL_NUMBER], [DB_CREATE_USERID], [DB_CREATE_TIMESTAMP], [DB_LAST_UPDATE_USERID], [DB_LAST_UPDATE_TIMESTAMP], (next value for [permit].[ORBC_RECEIPT_H_ID_SEQ]) as [_RECEIPT_HIST_ID], null as [END_DATE_HIST], @curr_date as [EFFECTIVE_DATE_HIST] from inserted; + +END TRY +BEGIN CATCH + IF @@trancount > 0 ROLLBACK TRANSACTION + EXEC orbc_error_handling +END CATCH; +GO + +IF @@ERROR <> 0 SET NOEXEC ON +GO + +-- Alter trigger permit.ORBC_RCPT_I_S_U_TR +PRINT N'Alter trigger permit.ORBC_RCPT_I_S_U_TR' +GO +ALTER TRIGGER [permit].[ORBC_RCPT_I_S_U_TR] ON permit.[ORBC_RECEIPT] INSTEAD OF UPDATE AS +SET NOCOUNT ON +BEGIN TRY + IF NOT EXISTS(SELECT * FROM deleted) + RETURN; + + -- validate concurrency control + if exists (select 1 from inserted, deleted where inserted.CONCURRENCY_CONTROL_NUMBER != deleted.CONCURRENCY_CONTROL_NUMBER+1 AND inserted.RECEIPT_ID = deleted.RECEIPT_ID) + raiserror('CONCURRENCY FAILURE.',16,1) + + + -- update statement + update [permit].[ORBC_RECEIPT] + set "RECEIPT_NUMBER" = inserted."RECEIPT_NUMBER", + "RECEIPT_DOCUMENT_ID" = inserted."RECEIPT_DOCUMENT_ID", + "APP_LAST_UPDATE_TIMESTAMP" = inserted."APP_LAST_UPDATE_TIMESTAMP", + "APP_LAST_UPDATE_USERID" = inserted."APP_LAST_UPDATE_USERID", + "APP_LAST_UPDATE_USER_GUID" = inserted."APP_LAST_UPDATE_USER_GUID", + "APP_LAST_UPDATE_USER_DIRECTORY" = inserted."APP_LAST_UPDATE_USER_DIRECTORY", + "CONCURRENCY_CONTROL_NUMBER" = inserted."CONCURRENCY_CONTROL_NUMBER" + , DB_LAST_UPDATE_TIMESTAMP = getutcdate() + , DB_LAST_UPDATE_USERID = user_name() + from [permit].[ORBC_RECEIPT] + inner join inserted + on (ORBC_RECEIPT.RECEIPT_ID = inserted.RECEIPT_ID); + +END TRY +BEGIN CATCH + IF @@trancount > 0 ROLLBACK TRANSACTION + EXEC orbc_error_handling +END CATCH; +GO +IF @@ERROR <> 0 SET NOEXEC ON +GO +DISABLE TRIGGER [permit].[ORBC_RCPT_I_S_U_TR] ON permit.[ORBC_RECEIPT] +GO + +IF @@ERROR <> 0 SET NOEXEC ON +GO + +DECLARE @VersionDescription VARCHAR(255) +SET @VersionDescription = 'Add RECEIPT_ID col to ORBC_TRANSACTION and Drop TRANSACTION_ID from ORBC_RECEIPT' + +INSERT [dbo].[ORBC_SYS_VERSION] ([VERSION_ID], [DESCRIPTION], [UPDATE_SCRIPT], [REVERT_SCRIPT], [RELEASE_DATE]) VALUES (46, @VersionDescription, '$(UPDATE_SCRIPT)', '$(REVERT_SCRIPT)', getutcdate()) +IF @@ERROR <> 0 SET NOEXEC ON +GO + +COMMIT TRANSACTION +GO +IF @@ERROR <> 0 SET NOEXEC ON +GO +DECLARE @Success AS BIT +SET @Success = 1 +SET NOEXEC OFF +IF (@Success = 1) PRINT 'The database update succeeded' +ELSE BEGIN + IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION + PRINT 'The database update failed' +END +GO + From ec7f9e53f1cfd57b81c901e3b02ea4b6c62d5e6a Mon Sep 17 00:00:00 2001 From: praju-aot Date: Tue, 22 Oct 2024 17:13:30 -0700 Subject: [PATCH 02/16] Formatting --- vehicles/src/common/constants/api.constant.ts | 3 +-- vehicles/src/common/helper/permit-application.helper.ts | 9 ++++++--- vehicles/src/common/helper/permit-fee.helper.ts | 4 ++-- .../application/application.service.ts | 4 ++-- 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/vehicles/src/common/constants/api.constant.ts b/vehicles/src/common/constants/api.constant.ts index b15b80cbe..ffcbeb052 100644 --- a/vehicles/src/common/constants/api.constant.ts +++ b/vehicles/src/common/constants/api.constant.ts @@ -8,6 +8,5 @@ export const CRYPTO_ALGORITHM_MD5 = 'md5'; export const CRYPTO_ALGORITHM_SHA256 = 'sha256'; export const TOKEN_EXPIRY_BUFFER = 15; export const PERMISSIONS_KEY = 'permissions'; -export const TIMEZONE_PACIFIC = "America/Vancouver"; +export const TIMEZONE_PACIFIC = 'America/Vancouver'; export const GL_PROJ_CODE_PLACEHOLDER = 'PROJECT'; - diff --git a/vehicles/src/common/helper/permit-application.helper.ts b/vehicles/src/common/helper/permit-application.helper.ts index b572f57e9..96d3b53e4 100644 --- a/vehicles/src/common/helper/permit-application.helper.ts +++ b/vehicles/src/common/helper/permit-application.helper.ts @@ -268,9 +268,12 @@ export const isPermitTypeEligibleForQueue = ( return PERMIT_TYPES_FOR_QUEUE.includes(permitType); }; -export const validApplicationDates = (application: Permit, timezone: string): boolean => { +export const validApplicationDates = ( + application: Permit, + timezone: string, +): boolean => { const todayUTC = dayjs(new Date()); - const todayPacific = todayUTC.tz(timezone).format("YYYY-MM-DD"); + const todayPacific = todayUTC.tz(timezone).format('YYYY-MM-DD'); const { startDate, expiryDate } = application.permitData; return startDate >= todayPacific && startDate <= expiryDate; -} +}; diff --git a/vehicles/src/common/helper/permit-fee.helper.ts b/vehicles/src/common/helper/permit-fee.helper.ts index 8e2314c02..dc14bf979 100644 --- a/vehicles/src/common/helper/permit-fee.helper.ts +++ b/vehicles/src/common/helper/permit-fee.helper.ts @@ -205,7 +205,7 @@ export const validAmount = ( calculatedAmount: number, receivedAmount: number, transactionType: TransactionType, -): boolean =>{ +): boolean => { const isAmountValid = receivedAmount.toFixed(2) === Math.abs(calculatedAmount).toFixed(2); @@ -218,4 +218,4 @@ export const validAmount = ( isAmountValid && (isRefundValid || transactionType !== TransactionType.REFUND) ); -} +}; diff --git a/vehicles/src/modules/permit-application-payment/application/application.service.ts b/vehicles/src/modules/permit-application-payment/application/application.service.ts index 81f29aef6..5cde7daa3 100644 --- a/vehicles/src/modules/permit-application-payment/application/application.service.ts +++ b/vehicles/src/modules/permit-application-payment/application/application.service.ts @@ -1236,8 +1236,8 @@ export class ApplicationService { company: { companyId: permit.company.companyId }, }, }); - if(loaDetails.length != loaIdsToInsert.length) - throw new BadRequestException('One or more loa(s) does not exist') + if (loaDetails.length != loaIdsToInsert.length) + throw new BadRequestException('One or more loa(s) does not exist'); // Transform the permit LOA IDs from an array of numbers into individual records. const singlePermitLoa = loaIdsToInsert.map((loaId) => ({ permitId, From c3ffe59021e4333f4a160a111c1ba1bfed164d78 Mon Sep 17 00:00:00 2001 From: praju-aot Date: Thu, 7 Nov 2024 00:14:23 -0500 Subject: [PATCH 03/16] Refund to multiple payment methods --- .../dto/common/refund-transaction.dto.ts | 67 +++++ .../request/create-refund-transaction.dto.ts | 36 +++ .../payment/entities/receipt.entity.ts | 13 +- .../payment/entities/transaction.entity.ts | 8 +- .../payment/payment.controller.ts | 29 ++ .../payment/payment.service.ts | 275 ++++++++++++++++-- 6 files changed, 393 insertions(+), 35 deletions(-) create mode 100644 vehicles/src/modules/permit-application-payment/payment/dto/common/refund-transaction.dto.ts create mode 100644 vehicles/src/modules/permit-application-payment/payment/dto/request/create-refund-transaction.dto.ts diff --git a/vehicles/src/modules/permit-application-payment/payment/dto/common/refund-transaction.dto.ts b/vehicles/src/modules/permit-application-payment/payment/dto/common/refund-transaction.dto.ts new file mode 100644 index 000000000..d749e3d51 --- /dev/null +++ b/vehicles/src/modules/permit-application-payment/payment/dto/common/refund-transaction.dto.ts @@ -0,0 +1,67 @@ +import { AutoMap } from '@automapper/classes'; +import { ApiProperty } from '@nestjs/swagger'; +import { + IsEnum, + IsNumber, + IsOptional, + IsString, + Length, + MaxLength, + Min, +} from 'class-validator'; +import { PaymentCardType } from '../../../../../common/enum/payment-card-type.enum'; +import { PaymentMethodType } from '../../../../../common/enum/payment-method-type.enum'; + +export class RefundTransactionDto { + @AutoMap() + @ApiProperty({ + example: '10000148', + required: false, + description: + 'Bambora-assigned eight-digit unique id number used to identify an individual transaction.', + }) + @IsOptional() + @IsString() + @MaxLength(20) + pgTransactionId: string; + + @AutoMap() + @ApiProperty({ + example: 'CC', + required: false, + description: 'Represents the payment method of a transaction.', + }) + @IsOptional() + @IsString() + @Length(1, 2) + pgPaymentMethod: string; + + @AutoMap() + @ApiProperty({ + example: '30.00', + description: 'Represents the amount of the transaction.', + }) + @IsNumber() + @Min(0) + transactionAmount: number; + + @AutoMap() + @ApiProperty({ + enum: PaymentMethodType, + example: PaymentMethodType.WEB, + description: 'The identifier of the user selected payment method.', + }) + @IsEnum(PaymentMethodType) + paymentMethodTypeCode: PaymentMethodType; + + @AutoMap() + @ApiProperty({ + example: PaymentCardType.VISA, + enum: PaymentCardType, + description: 'The payment types.', + required: false, + }) + @IsOptional() + @IsEnum(PaymentCardType) + paymentCardTypeCode?: PaymentCardType; +} diff --git a/vehicles/src/modules/permit-application-payment/payment/dto/request/create-refund-transaction.dto.ts b/vehicles/src/modules/permit-application-payment/payment/dto/request/create-refund-transaction.dto.ts new file mode 100644 index 000000000..6032203ec --- /dev/null +++ b/vehicles/src/modules/permit-application-payment/payment/dto/request/create-refund-transaction.dto.ts @@ -0,0 +1,36 @@ +import { AutoMap } from '@automapper/classes'; +import { ApiProperty } from '@nestjs/swagger'; +import { + ArrayMinSize, + IsArray, + IsNumberString, + MaxLength, + ValidateNested, +} from 'class-validator'; + +import { RefundTransactionDto } from '../common/refund-transaction.dto'; +import { Type } from 'class-transformer'; + +export class CreateRefundTransactionDto { + @AutoMap() + @ApiProperty({ + description: 'Application/Permit Id.', + example: '1', + }) + @IsNumberString() + @MaxLength(20) + applicationId: string; + + @AutoMap() + @ApiProperty({ + description: + 'The refund transaction details specific to application/permit.', + required: true, + type: [RefundTransactionDto], + }) + @IsArray() + @ValidateNested({ each: true }) + @ArrayMinSize(1) + @Type(() => RefundTransactionDto) + transactions: RefundTransactionDto[]; +} diff --git a/vehicles/src/modules/permit-application-payment/payment/entities/receipt.entity.ts b/vehicles/src/modules/permit-application-payment/payment/entities/receipt.entity.ts index 73e2bd31c..322371022 100644 --- a/vehicles/src/modules/permit-application-payment/payment/entities/receipt.entity.ts +++ b/vehicles/src/modules/permit-application-payment/payment/entities/receipt.entity.ts @@ -1,11 +1,5 @@ import { ApiProperty } from '@nestjs/swagger'; -import { - Entity, - Column, - PrimaryGeneratedColumn, - OneToOne, - JoinColumn, -} from 'typeorm'; +import { Entity, Column, PrimaryGeneratedColumn, OneToMany } from 'typeorm'; import { AutoMap } from '@automapper/classes'; import { Base } from '../../../common/entities/base.entity'; import { Transaction } from './transaction.entity'; @@ -43,7 +37,6 @@ export class Receipt extends Base { }) receiptDocumentId: string; - @OneToOne(() => Transaction, (transaction) => transaction.receipt) - @JoinColumn({ name: 'TRANSACTION_ID' }) - transaction: Transaction; + @OneToMany(() => Transaction, (transaction) => transaction.receipt) + public transactions: Transaction[]; } diff --git a/vehicles/src/modules/permit-application-payment/payment/entities/transaction.entity.ts b/vehicles/src/modules/permit-application-payment/payment/entities/transaction.entity.ts index ddc8cfc5d..ebb9eec84 100644 --- a/vehicles/src/modules/permit-application-payment/payment/entities/transaction.entity.ts +++ b/vehicles/src/modules/permit-application-payment/payment/entities/transaction.entity.ts @@ -3,8 +3,9 @@ import { Entity, Column, PrimaryGeneratedColumn, - OneToOne, OneToMany, + JoinColumn, + ManyToOne, } from 'typeorm'; import { AutoMap } from '@automapper/classes'; import { Base } from '../../../common/entities/base.entity'; @@ -222,8 +223,9 @@ export class Transaction extends Base { }) pgMessageText: string; - @OneToOne(() => Receipt, (receipt) => receipt.transaction) - receipt: Receipt; + @ManyToOne(() => Receipt, (receipt) => receipt.transactions) + @JoinColumn({ name: 'RECEIPT_ID' }) + public receipt: Receipt; @OneToMany( () => PermitTransaction, diff --git a/vehicles/src/modules/permit-application-payment/payment/payment.controller.ts b/vehicles/src/modules/permit-application-payment/payment/payment.controller.ts index a57526caa..cb7e6922d 100644 --- a/vehicles/src/modules/permit-application-payment/payment/payment.controller.ts +++ b/vehicles/src/modules/permit-application-payment/payment/payment.controller.ts @@ -37,6 +37,7 @@ import { CLIENT_USER_ROLE_LIST, IDIRUserRole, } from '../../../common/enum/user-role.enum'; +import { CreateRefundTransactionDto } from './dto/request/create-refund-transaction.dto'; @ApiBearerAuth() @ApiTags('Payment') @@ -90,6 +91,34 @@ export class PaymentController { return paymentDetails; } + @ApiCreatedResponse({ + description: 'The Transaction Resource', + isArray: true, + type: ReadTransactionDto, + }) + @Permissions({ + allowedIdirRoles: [ + IDIRUserRole.PPC_CLERK, + IDIRUserRole.SYSTEM_ADMINISTRATOR, + IDIRUserRole.CTPO, + ], + }) + @Post('refund') + async createRefundTransactionDetails( + @Req() request: Request, + @Body() { applicationId, transactions }: CreateRefundTransactionDto, + ): Promise { + const currentUser = request.user as IUserJWT; + + const paymentDetails = await this.paymentService.createRefundTransactions({ + currentUser, + applicationId, + transactions, + }); + + return paymentDetails; + } + @ApiOkResponse({ description: 'The Payment Gateway Transaction Resource', type: UpdatePaymentGatewayTransactionDto, diff --git a/vehicles/src/modules/permit-application-payment/payment/payment.service.ts b/vehicles/src/modules/permit-application-payment/payment/payment.service.ts index e4a061f1a..72b5926b0 100644 --- a/vehicles/src/modules/permit-application-payment/payment/payment.service.ts +++ b/vehicles/src/modules/permit-application-payment/payment/payment.service.ts @@ -23,7 +23,10 @@ import { } from 'typeorm'; import { PermitTransaction } from './entities/permit-transaction.entity'; import { IUserJWT } from 'src/common/interface/user-jwt.interface'; -import { callDatabaseSequence } from 'src/common/helper/database.helper'; +import { + callDatabaseSequence, + setBaseEntityProperties, +} from 'src/common/helper/database.helper'; import { Permit } from '../permit/entities/permit.entity'; import { ApplicationStatus } from '../../../common/enum/application-status.enum'; import { PaymentMethodType as PaymentMethodTypeEnum } from '../../../common/enum/payment-method-type.enum'; @@ -51,7 +54,10 @@ import { } from 'src/common/helper/permit-fee.helper'; import { CfsTransactionDetail } from './entities/cfs-transaction.entity'; import { CfsFileStatus } from 'src/common/enum/cfs-file-status.enum'; -import { isAmendmentApplication, validApplicationDates } from '../../../common/helper/permit-application.helper'; +import { + isAmendmentApplication, + validApplicationDates, +} from '../../../common/helper/permit-application.helper'; import { isCfsPaymentMethodType } from 'src/common/helper/payment.helper'; import { PgApprovesStatus } from 'src/common/enum/pg-approved-status-type.enum'; import { CACHE_MANAGER } from '@nestjs/cache-manager'; @@ -70,6 +76,8 @@ import { } from '../../../common/helper/common.helper'; import { SpecialAuth } from 'src/modules/special-auth/entities/special-auth.entity'; import { TIMEZONE_PACIFIC } from 'src/common/constants/api.constant'; +import { RefundTransactionDto } from './dto/common/refund-transaction.dto'; +import { Nullable } from '../../../common/types/common'; @Injectable() export class PaymentService { @@ -253,11 +261,201 @@ export class PaymentService { ); } + /** + * Creates a Refund Transaction in ORBC System. + * @param currentUser - The current user object of type {@link IUserJWT} + * @param createTransactionDto - The createTransactionDto object of type + * {@link CreateTransactionDto} for creating a new Transaction. + * @returns {ReadTransactionDto[]} The created transaction list of type {@link ReadTransactionDto}. + */ + @LogAsyncMethodExecution() + async createRefundTransactions({ + applicationId, + transactions, + currentUser, + nestedQueryRunner, + }: { + applicationId: string; + transactions: RefundTransactionDto[]; + currentUser: IUserJWT; + nestedQueryRunner?: Nullable; + }): Promise { + for (const transaction of transactions) { + if ( + !doesUserHaveRole(currentUser.orbcUserRole, IDIR_USER_ROLE_LIST) && + transaction?.paymentMethodTypeCode !== PaymentMethodTypeEnum.WEB && + transaction?.paymentMethodTypeCode !== PaymentMethodTypeEnum.ACCOUNT && + transaction?.paymentMethodTypeCode !== PaymentMethodTypeEnum.NO_PAYMENT + ) { + throwUnprocessableEntityException( + 'Invalid payment method type for the user', + ); + } else if ( + transaction?.paymentMethodTypeCode === PaymentMethodTypeEnum.ACCOUNT && + !(await isFeatureEnabled(this.cacheManager, 'CREDIT-ACCOUNT')) + ) { + throwUnprocessableEntityException('Disabled feature'); + } + + if ( + transaction?.paymentMethodTypeCode === PaymentMethodTypeEnum.POS && + !transaction?.paymentCardTypeCode + ) { + throwBadRequestException('paymentCardTypeCode', [ + `paymentCardTypeCode is required when paymentMethodTypeCode is ${transaction?.paymentMethodTypeCode}`, + ]); + } + } + + let readTransactionDto: ReadTransactionDto[]; + const queryRunner = + nestedQueryRunner || this.dataSource.createQueryRunner(); + if (!nestedQueryRunner) { + await queryRunner.connect(); + await queryRunner.startTransaction(); + } + + try { + const existingApplication: Permit = await queryRunner.manager.findOne( + Permit, + { + where: { permitId: applicationId }, + relations: { permitData: true }, + }, + ); + + if ( + !( + this.isVoidorRevoked(existingApplication.permitStatus) || + this.isApplicationInCart(existingApplication.permitStatus) || + isAmendmentApplication(existingApplication) + ) + ) { + throw new BadRequestException( + 'Application in its current status cannot be processed for payment.', + ); + } + + const totalTransactionAmount = transactions?.reduce( + (accumulator, item) => accumulator + item.transactionAmount, + 0, + ); + + await this.validateApplicationAndPayment( + totalTransactionAmount, + TransactionType.REFUND, + [existingApplication], + currentUser, + queryRunner, + ); + + let newTransactionList: Transaction[] = []; + + for (const transaction of transactions) { + const transactionOrderNumber = + await this.generateTransactionOrderNumber(); + const newTransaction = new Transaction(); + newTransaction.transactionTypeId = TransactionType.REFUND; + newTransaction.pgTransactionId = transaction.pgTransactionId; + newTransaction.totalTransactionAmount = transaction.transactionAmount; + newTransaction.paymentMethodTypeCode = + transaction.paymentMethodTypeCode; + newTransaction.paymentCardTypeCode = transaction.paymentCardTypeCode; + newTransaction.pgCardType = transaction.paymentCardTypeCode; + newTransaction.pgPaymentMethod = transaction.pgPaymentMethod; + newTransaction.transactionOrderNumber = transactionOrderNumber; + newTransaction.pgApproved = 1; + setBaseEntityProperties({ entity: newTransaction, currentUser }); + newTransactionList.push(newTransaction); + } + + newTransactionList = await queryRunner.manager.save(newTransactionList); + + const receiptNumber = await this.generateReceiptNumber(); + let receipt = new Receipt(); + receipt.receiptNumber = receiptNumber; + setBaseEntityProperties({ entity: receipt, currentUser }); + receipt = await queryRunner.manager.save(receipt); + + for (const newTransaction of newTransactionList) { + let newPermitTransactions = new PermitTransaction(); + newPermitTransactions.transaction = newTransaction; + newPermitTransactions.permit = existingApplication; + newPermitTransactions.createdDateTime = new Date(); + newPermitTransactions.createdUser = currentUser.userName; + newPermitTransactions.createdUserDirectory = + currentUser.orbcUserDirectory; + newPermitTransactions.createdUserGuid = currentUser.userGUID; + newPermitTransactions.updatedDateTime = new Date(); + newPermitTransactions.updatedUser = currentUser.userName; + newPermitTransactions.updatedUserDirectory = + currentUser.orbcUserDirectory; + newPermitTransactions.updatedUserGuid = currentUser.userGUID; + newPermitTransactions.transactionAmount = + newTransaction.totalTransactionAmount; + newPermitTransactions = await queryRunner.manager.save( + newPermitTransactions, + ); + + await queryRunner.manager.update( + Transaction, + { transactionId: newTransaction.transactionId }, + { + receipt: receipt, + updatedDateTime: new Date(), + updatedUser: currentUser.userName, + updatedUserDirectory: currentUser.orbcUserDirectory, + updatedUserGuid: currentUser.userGUID, + }, + ); + + if (isCfsPaymentMethodType(newTransaction.paymentMethodTypeCode)) { + const newCfsTransaction: CfsTransactionDetail = + new CfsTransactionDetail(); + newCfsTransaction.transaction = newTransaction; + newCfsTransaction.fileStatus = CfsFileStatus.READY; + await queryRunner.manager.save(newCfsTransaction); + } + } + + if (!nestedQueryRunner) { + await queryRunner.commitTransaction(); + } + + const createdTransaction = await queryRunner.manager.find(Transaction, { + where: { + transactionId: In( + newTransactionList?.map(({ transactionId }) => transactionId), + ), + }, + relations: ['permitTransactions', 'permitTransactions.permit'], + }); + + readTransactionDto = await this.classMapper.mapArrayAsync( + createdTransaction, + Transaction, + ReadTransactionDto, + ); + } catch (error) { + if (!nestedQueryRunner) { + await queryRunner.rollbackTransaction(); + } + this.logger.error(error); + throw error; + } finally { + if (!nestedQueryRunner) { + await queryRunner.release(); + } + } + + return readTransactionDto; + } + /** * Creates a Transaction in ORBC System. * @param currentUser - The current user object of type {@link IUserJWT} * @param createTransactionDto - The createTransactionDto object of type - * {@link CreateTransactionDto} for creating a new Transaction. + * {@link CreateTransactionDto} for creating a new Transaction. * @returns {ReadTransactionDto} The created transaction of type {@link ReadTransactionDto}. */ @LogAsyncMethodExecution() @@ -329,8 +527,16 @@ export class PaymentService { 'Application in its current status cannot be processed for payment.', ); } - const totalTransactionAmount = await this.validateApplicationAndPayment( - createTransactionDto, + + let totalTransactionAmount = + createTransactionDto.applicationDetails?.reduce( + (accumulator, item) => accumulator + item.transactionAmount, + 0, + ); + + totalTransactionAmount = await this.validateApplicationAndPayment( + totalTransactionAmount, + createTransactionDto.transactionTypeId, existingApplications, currentUser, queryRunner, @@ -443,9 +649,9 @@ export class PaymentService { ) ) { const receiptNumber = await this.generateReceiptNumber(); - const receipt = new Receipt(); + let receipt = new Receipt(); receipt.receiptNumber = receiptNumber; - receipt.transaction = createdTransaction; + //receipt.transaction = createdTransaction; receipt.createdDateTime = new Date(); receipt.createdUser = currentUser.userName; receipt.createdUserDirectory = currentUser.orbcUserDirectory; @@ -454,7 +660,19 @@ export class PaymentService { receipt.updatedUser = currentUser.userName; receipt.updatedUserDirectory = currentUser.orbcUserDirectory; receipt.updatedUserGuid = currentUser.userGUID; - await queryRunner.manager.save(receipt); + receipt = await queryRunner.manager.save(receipt); + + await queryRunner.manager.update( + Transaction, + { transactionId: createdTransaction.transactionId }, + { + receipt: receipt, + updatedDateTime: new Date(), + updatedUser: currentUser.userName, + updatedUserDirectory: currentUser.orbcUserDirectory, + updatedUserGuid: currentUser.userGUID, + }, + ); } readTransactionDto = await this.classMapper.mapAsync( @@ -495,15 +713,18 @@ export class PaymentService { * Additionally, for refund transactions, it checks if the total calculated transaction amount is negative as expected; * if not, it throws an error. * - * @param {CreateTransactionDto} createTransactionDto - The DTO containing the transaction details from the request. + * @param {number} totalTransactionAmount - The total transaction amount from the request. + * @param {TransactionType} transactionType - The transactionType. * @param {Permit[]} applications - A list of permits associated with the transaction. - * @param {QueryRunner} nestedQueryRunner - The query runner to use for database operations within the method. + * @param {IUserJWT} currentUser - The current user performing the transaction. + * @param {QueryRunner} queryRunner - The query runner to use for database operations within the method. * @returns {Promise} The total transaction amount calculated from the backend data. * @throws {BadRequestException} When the transaction amount in the request doesn't match with the calculated amount, * or if there's a transaction type and amount mismatch in case of refunds. */ private async validateApplicationAndPayment( - createTransactionDto: CreateTransactionDto, + totalTransactionAmount: number, + transactionType: TransactionType, applications: Permit[], currentUser: IUserJWT, queryRunner: QueryRunner, @@ -513,27 +734,26 @@ export class PaymentService { // Calculate and add amount for each requested application, as per the available backend data. for (const application of applications) { //Check if each application has a valid start date and valid expiry date. - if (isCVClientUser && !validApplicationDates(application, TIMEZONE_PACIFIC)) - { + if ( + isCVClientUser && + !validApplicationDates(application, TIMEZONE_PACIFIC) + ) { throw new UnprocessableEntityException( `Atleast one of the application has invalid startDate or expiryDate.`, ); } + totalTransactionAmountCalculated += await this.permitFeeCalculator( application, queryRunner, ); } - const totalTransactionAmount = - createTransactionDto.applicationDetails?.reduce( - (accumulator, item) => accumulator + item.transactionAmount, - 0, - ); + if ( !validAmount( totalTransactionAmountCalculated, totalTransactionAmount, - createTransactionDto.transactionTypeId, + transactionType, ) ) throw new BadRequestException('Transaction amount mismatch.'); @@ -690,9 +910,8 @@ export class PaymentService { if (updateTransactionTemp.pgApproved === 1) { const receiptNumber = await this.generateReceiptNumber(); - const receipt = new Receipt(); + let receipt = new Receipt(); receipt.receiptNumber = receiptNumber; - receipt.transaction = updatedTransaction; receipt.receiptNumber = receiptNumber; receipt.createdDateTime = new Date(); receipt.createdUser = currentUser.userName; @@ -702,7 +921,19 @@ export class PaymentService { receipt.updatedUser = currentUser.userName; receipt.updatedUserDirectory = currentUser.orbcUserDirectory; receipt.updatedUserGuid = currentUser.userGUID; - await queryRunner.manager.save(receipt); + receipt = await queryRunner.manager.save(receipt); + + await queryRunner.manager.update( + Transaction, + { transactionId: updatedTransaction.transactionId }, + { + receipt: receipt, + updatedDateTime: new Date(), + updatedUser: currentUser.userName, + updatedUserDirectory: currentUser.orbcUserDirectory, + updatedUserGuid: currentUser.userGUID, + }, + ); } await queryRunner.commitTransaction(); From f0dd2d8f0f5ada6dff11279c162a2062128bbdd3 Mon Sep 17 00:00:00 2001 From: praju-aot Date: Thu, 7 Nov 2024 00:17:04 -0500 Subject: [PATCH 04/16] Update DB version --- .../revert/{v_46_ddl_revert.sql => v_47_ddl_revert.sql} | 2 +- database/mssql/scripts/versions/{v_46_ddl.sql => v_47_ddl.sql} | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename database/mssql/scripts/versions/revert/{v_46_ddl_revert.sql => v_47_ddl_revert.sql} (99%) rename database/mssql/scripts/versions/{v_46_ddl.sql => v_47_ddl.sql} (99%) diff --git a/database/mssql/scripts/versions/revert/v_46_ddl_revert.sql b/database/mssql/scripts/versions/revert/v_47_ddl_revert.sql similarity index 99% rename from database/mssql/scripts/versions/revert/v_46_ddl_revert.sql rename to database/mssql/scripts/versions/revert/v_47_ddl_revert.sql index 453662f3e..e9ec94f53 100644 --- a/database/mssql/scripts/versions/revert/v_46_ddl_revert.sql +++ b/database/mssql/scripts/versions/revert/v_47_ddl_revert.sql @@ -216,4 +216,4 @@ GO DECLARE @VersionDescription VARCHAR(255) SET @VersionDescription = 'Reverting addition of RECEIPT_ID col to ORBC_TRANSACTION and dropping TRANSACTION_ID from ORBC_RECEIPT.' -INSERT [dbo].[ORBC_SYS_VERSION] ([VERSION_ID], [DESCRIPTION], [RELEASE_DATE]) VALUES (45, @VersionDescription, getutcdate()) +INSERT [dbo].[ORBC_SYS_VERSION] ([VERSION_ID], [DESCRIPTION], [RELEASE_DATE]) VALUES (46, @VersionDescription, getutcdate()) diff --git a/database/mssql/scripts/versions/v_46_ddl.sql b/database/mssql/scripts/versions/v_47_ddl.sql similarity index 99% rename from database/mssql/scripts/versions/v_46_ddl.sql rename to database/mssql/scripts/versions/v_47_ddl.sql index 37ef5076b..45c0b4270 100644 --- a/database/mssql/scripts/versions/v_46_ddl.sql +++ b/database/mssql/scripts/versions/v_47_ddl.sql @@ -218,7 +218,7 @@ GO DECLARE @VersionDescription VARCHAR(255) SET @VersionDescription = 'Add RECEIPT_ID col to ORBC_TRANSACTION and Drop TRANSACTION_ID from ORBC_RECEIPT' -INSERT [dbo].[ORBC_SYS_VERSION] ([VERSION_ID], [DESCRIPTION], [UPDATE_SCRIPT], [REVERT_SCRIPT], [RELEASE_DATE]) VALUES (46, @VersionDescription, '$(UPDATE_SCRIPT)', '$(REVERT_SCRIPT)', getutcdate()) +INSERT [dbo].[ORBC_SYS_VERSION] ([VERSION_ID], [DESCRIPTION], [UPDATE_SCRIPT], [REVERT_SCRIPT], [RELEASE_DATE]) VALUES (47, @VersionDescription, '$(UPDATE_SCRIPT)', '$(REVERT_SCRIPT)', getutcdate()) IF @@ERROR <> 0 SET NOEXEC ON GO From 5fcae06dfea5e69f4136005b917b7b46e9d47ea8 Mon Sep 17 00:00:00 2001 From: praju-aot Date: Thu, 7 Nov 2024 12:11:43 -0500 Subject: [PATCH 05/16] Receipt document generation --- .../permit-receipt-document.service.ts | 73 ++++++++++++++----- 1 file changed, 53 insertions(+), 20 deletions(-) diff --git a/vehicles/src/modules/permit-application-payment/permit-receipt-document/permit-receipt-document.service.ts b/vehicles/src/modules/permit-application-payment/permit-receipt-document/permit-receipt-document.service.ts index 7e4d2d550..942c8446f 100644 --- a/vehicles/src/modules/permit-application-payment/permit-receipt-document/permit-receipt-document.service.ts +++ b/vehicles/src/modules/permit-application-payment/permit-receipt-document/permit-receipt-document.service.ts @@ -40,6 +40,9 @@ import { validateEmailandFaxList, } from '../../../common/helper/notification.helper'; import { getPermitTemplateName } from '../../../common/helper/template.helper'; +import { Transaction } from '../payment/entities/transaction.entity'; +import { PermitTransaction } from '../payment/entities/permit-transaction.entity'; +import { TransactionType } from '../../../common/enum/transaction-type.enum'; @Injectable() export class PermitReceiptDocumentService { @@ -98,6 +101,14 @@ export class PermitReceiptDocumentService { permits: Permit[]; }[] = []; + // Function to check if a permitId already exists in transactionPermitList + // To avoid duplicate entries for Refund to Multiple Payment methods + function hasExistingPermit(permitId: string): boolean { + return transactionPermitList.some((item) => + item.permits.some((p) => p.permitId === permitId), + ); + } + for (const transaction of transactions) { const fetchedApplications = await this.findManyWithSuccessfulTransaction( null, @@ -106,7 +117,9 @@ export class PermitReceiptDocumentService { ); if ( - fetchedApplications?.length === transaction.permitCountPerTransactionId + fetchedApplications?.length === + transaction.permitCountPerTransactionId && + !fetchedApplications.some((app) => hasExistingPermit(app.permitId)) ) { transactionPermitList.push({ transactionId: transaction.transactionId, @@ -407,15 +420,20 @@ export class PermitReceiptDocumentService { return resultDto; } + console.log('fetchedPermits', fetchedPermits); + await Promise.allSettled( fetchedPermits?.map(async (fetchedPermit) => { - const permits = fetchedPermit.permits; + let permits = fetchedPermit.permits; const permitIds = permits?.map((permit) => permit.permitId); + permits = await this.findManyWithSuccessfulTransaction(permitIds,companyId) + console.log('permits', permits); if (permits?.length) { try { const permit = permits?.at(0); const company = permit?.company; const permitTransactions = permit?.permitTransactions; + console.log('permitTransactions', permitTransactions); const transaction = permitTransactions?.at(0)?.transaction; const receipt = transaction?.receipt; if (receipt.receiptDocumentId) { @@ -429,6 +447,8 @@ export class PermitReceiptDocumentService { permit, ); + + const { companyName, companyAlternateName, permitData } = formatTemplateData(permit, fullNames, company); const permitDetails = await Promise.all( @@ -439,19 +459,42 @@ export class PermitReceiptDocumentService { CacheKey.PERMIT_TYPE, permit?.permitType, ), - permitNumber: permit?.permitNumber, - transactionAmount: formatAmount( - transaction?.transactionTypeId, - permit?.permitTransactions?.at(0)?.transactionAmount, - ), + permitNumber: permit?.permitNumber, }; }), ); + const transactionList = await Promise.all(permits?.flatMap( (permit)=> permit?.permitTransactions?.map(async (permitTransaction) => { + return { + consolidatedPaymentMethod: ( + await getPaymentCodeFromCache( + this.cacheManager, + permitTransaction?.transaction?.paymentMethodTypeCode, + permitTransaction?.transaction?.paymentCardTypeCode + ) + ).consolidatedPaymentMethod, + pgTransactionId: permitTransaction?.transaction?.pgTransactionId, + transactionOrderNumber: permitTransaction?.transaction?.transactionOrderNumber, + transactionAmount: formatAmount( + transaction?.transactionTypeId, + permitTransaction?.transactionAmount, + ), + }; + }))); + console.log('transactionList',transactionList); + + const totalTransactionAmount = permits?.reduce( + (accumulator, item) => accumulator + item.permitTransactions.reduce((accumulator, item) => accumulator + item.transactionAmount,0), + 0, + ); + + console.log('totalTransactionAmount',totalTransactionAmount); + const dopsRequestData = { templateName: TemplateName.PAYMENT_RECEIPT, generatedDocumentFileName: `Receipt_No_${receiptNumber}`, templateData: { + receiptType: transaction.transactionTypeId=== TransactionType.PURCHASE?"Receipt": "Refund Receipt", receiptNo: receiptNumber, companyName: companyName, companyAlternateName: companyAlternateName, @@ -465,22 +508,12 @@ export class PermitReceiptDocumentService { : constants.SELF_ISSUED, totalTransactionAmount: formatAmount( transaction?.transactionTypeId, - transaction?.totalTransactionAmount, + totalTransactionAmount, ), permitDetails: permitDetails, - //Transaction Details - pgTransactionId: transaction?.pgTransactionId, - transactionOrderNumber: transaction?.transactionOrderNumber, - consolidatedPaymentMethod: ( - await getPaymentCodeFromCache( - this.cacheManager, - transaction?.paymentMethodTypeCode, - transaction?.paymentCardTypeCode, - ) - ).consolidatedPaymentMethod, + transactions: transactionList, transactionDate: convertUtcToPt( - permit?.permitTransactions?.at(0)?.transaction - ?.transactionSubmitDate, + transaction?.transactionSubmitDate, 'MMM. D, YYYY, hh:mm a Z', ), }, From 86c02a31e6afa89d09141b99ad56a7eca0719a83 Mon Sep 17 00:00:00 2001 From: praju-aot Date: Fri, 8 Nov 2024 11:12:31 -0500 Subject: [PATCH 06/16] Template changes and code review changes --- .../helper/format-template-data.helper.ts | 13 ++- .../payment/payment.service.ts | 29 ++++-- .../permit-receipt-document.service.ts | 92 ++++++++++++------- 3 files changed, 90 insertions(+), 44 deletions(-) diff --git a/vehicles/src/common/helper/format-template-data.helper.ts b/vehicles/src/common/helper/format-template-data.helper.ts index a82101252..f9334a727 100644 --- a/vehicles/src/common/helper/format-template-data.helper.ts +++ b/vehicles/src/common/helper/format-template-data.helper.ts @@ -9,6 +9,7 @@ import { PermitTemplateData, } from '../interface/permit.template.interface'; import { FullNamesForDgen } from '../interface/full-names-for-dgen.interface'; +import { formatAmount } from './payment.helper'; /** * Formats the permit data so that it can be used in the templated word documents @@ -86,10 +87,16 @@ export const formatTemplateData = ( template.clientNumber = companyInfo.clientNumber; template.companyAlternateName = companyInfo.alternateName; + const transcation = permit.permitTransactions?.at(0)?.transaction; + // Format Fee Summary - template.permitData.feeSummary = permit.permitTransactions - ?.at(0) - ?.transactionAmount.toString(); + template.permitData.feeSummary = formatAmount( + transcation.transactionTypeId, + permit.permitTransactions?.reduce( + (accumulator, item) => accumulator + item.transactionAmount, + 0, + ), + ).toString(); revisionHistory?.forEach((revision) => { if ( diff --git a/vehicles/src/modules/permit-application-payment/payment/payment.service.ts b/vehicles/src/modules/permit-application-payment/payment/payment.service.ts index 72b5926b0..ed9977c8f 100644 --- a/vehicles/src/modules/permit-application-payment/payment/payment.service.ts +++ b/vehicles/src/modules/permit-application-payment/payment/payment.service.ts @@ -40,6 +40,7 @@ import { PAYMENT_CURRENCY, CRYPTO_ALGORITHM_MD5, GL_PROJ_CODE_PLACEHOLDER, + PPC_FULL_TEXT, } from '../../../common/constants/api.constant'; import { convertToHash } from 'src/common/helper/crypto.helper'; import { UpdatePaymentGatewayTransactionDto } from './dto/request/update-payment-gateway-transaction.dto'; @@ -262,11 +263,17 @@ export class PaymentService { } /** - * Creates a Refund Transaction in ORBC System. - * @param currentUser - The current user object of type {@link IUserJWT} - * @param createTransactionDto - The createTransactionDto object of type - * {@link CreateTransactionDto} for creating a new Transaction. - * @returns {ReadTransactionDto[]} The created transaction list of type {@link ReadTransactionDto}. + * Creates a Refund Transaction in ORBC System, ensuring that payment methods align with user roles and enabled features. + * The method verifies transactions against application status and computes transaction amounts. + * It then creates and saves new transactions and their associated records, handling any CFS payment methods. + * + * @param applicationId - The ID of the application related to the refund transactions. + * @param transactions - An array of transactions of type {@link RefundTransactionDto} to process. + * @param currentUser - The current user object of type {@link IUserJWT}. + * @param nestedQueryRunner - An optional query runner. If not provided, a new one is created. + * @returns {Promise} The created list of transactions of type {@link ReadTransactionDto}. + * @throws UnprocessableEntityException - When the payment method type is invalid for the user or feature is disabled. + * @throws BadRequestException - When the application status is not valid for the transaction. */ @LogAsyncMethodExecution() async createRefundTransactions({ @@ -364,8 +371,14 @@ export class PaymentService { newTransaction.pgCardType = transaction.paymentCardTypeCode; newTransaction.pgPaymentMethod = transaction.pgPaymentMethod; newTransaction.transactionOrderNumber = transactionOrderNumber; - newTransaction.pgApproved = 1; - setBaseEntityProperties({ entity: newTransaction, currentUser }); + newTransaction.payerName = PPC_FULL_TEXT; + if (transaction.paymentMethodTypeCode === PaymentMethodTypeEnum.WEB) { + newTransaction.pgApproved = 1; + } + setBaseEntityProperties({ + entity: newTransaction, + currentUser, + }); newTransactionList.push(newTransaction); } @@ -374,7 +387,7 @@ export class PaymentService { const receiptNumber = await this.generateReceiptNumber(); let receipt = new Receipt(); receipt.receiptNumber = receiptNumber; - setBaseEntityProperties({ entity: receipt, currentUser }); + setBaseEntityProperties({ entity: receipt, currentUser }); receipt = await queryRunner.manager.save(receipt); for (const newTransaction of newTransactionList) { diff --git a/vehicles/src/modules/permit-application-payment/permit-receipt-document/permit-receipt-document.service.ts b/vehicles/src/modules/permit-application-payment/permit-receipt-document/permit-receipt-document.service.ts index 942c8446f..90181577a 100644 --- a/vehicles/src/modules/permit-application-payment/permit-receipt-document/permit-receipt-document.service.ts +++ b/vehicles/src/modules/permit-application-payment/permit-receipt-document/permit-receipt-document.service.ts @@ -40,8 +40,6 @@ import { validateEmailandFaxList, } from '../../../common/helper/notification.helper'; import { getPermitTemplateName } from '../../../common/helper/template.helper'; -import { Transaction } from '../payment/entities/transaction.entity'; -import { PermitTransaction } from '../payment/entities/permit-transaction.entity'; import { TransactionType } from '../../../common/enum/transaction-type.enum'; @Injectable() @@ -420,20 +418,21 @@ export class PermitReceiptDocumentService { return resultDto; } - console.log('fetchedPermits', fetchedPermits); - await Promise.allSettled( fetchedPermits?.map(async (fetchedPermit) => { let permits = fetchedPermit.permits; const permitIds = permits?.map((permit) => permit.permitId); - permits = await this.findManyWithSuccessfulTransaction(permitIds,companyId) - console.log('permits', permits); + permits = await this.findManyWithSuccessfulTransaction( + permitIds, + companyId, + ); + if (permits?.length) { try { const permit = permits?.at(0); const company = permit?.company; const permitTransactions = permit?.permitTransactions; - console.log('permitTransactions', permitTransactions); + const transaction = permitTransactions?.at(0)?.transaction; const receipt = transaction?.receipt; if (receipt.receiptDocumentId) { @@ -447,8 +446,6 @@ export class PermitReceiptDocumentService { permit, ); - - const { companyName, companyAlternateName, permitData } = formatTemplateData(permit, fullNames, company); const permitDetails = await Promise.all( @@ -459,42 +456,71 @@ export class PermitReceiptDocumentService { CacheKey.PERMIT_TYPE, permit?.permitType, ), - permitNumber: permit?.permitNumber, + permitNumber: permit?.permitNumber, + permitFee: formatAmount( + transaction?.transactionTypeId, + permit?.permitTransactions?.reduce( + (accumulator, item) => + accumulator + item.transactionAmount, + 0, + ), + ), }; }), ); - const transactionList = await Promise.all(permits?.flatMap( (permit)=> permit?.permitTransactions?.map(async (permitTransaction) => { - return { - consolidatedPaymentMethod: ( - await getPaymentCodeFromCache( - this.cacheManager, - permitTransaction?.transaction?.paymentMethodTypeCode, - permitTransaction?.transaction?.paymentCardTypeCode - ) - ).consolidatedPaymentMethod, - pgTransactionId: permitTransaction?.transaction?.pgTransactionId, - transactionOrderNumber: permitTransaction?.transaction?.transactionOrderNumber, - transactionAmount: formatAmount( - transaction?.transactionTypeId, - permitTransaction?.transactionAmount, - ), - }; - }))); - console.log('transactionList',transactionList); + const transactionList = await Promise.all( + permits?.flatMap((permit) => + permit?.permitTransactions?.map(async (permitTransaction) => { + return { + consolidatedPaymentMethod: ( + await getPaymentCodeFromCache( + this.cacheManager, + permitTransaction?.transaction?.paymentMethodTypeCode, + permitTransaction?.transaction?.paymentCardTypeCode, + ) + ).consolidatedPaymentMethod, + pgTransactionId: + permitTransaction?.transaction?.pgTransactionId, + transactionOrderNumber: + permitTransaction?.transaction?.transactionOrderNumber, + transactionAmount: formatAmount( + transaction?.transactionTypeId, + permitTransaction?.transaction?.totalTransactionAmount, + ), + }; + }), + ), + ); + + const uniqueTransactionList = Array.from( + new Map( + transactionList.map((item) => [ + item.transactionOrderNumber, + item, + ]), + ).values(), + ); const totalTransactionAmount = permits?.reduce( - (accumulator, item) => accumulator + item.permitTransactions.reduce((accumulator, item) => accumulator + item.transactionAmount,0), + (accumulator, item) => + accumulator + + item.permitTransactions.reduce( + (accumulator, item) => accumulator + item.transactionAmount, + 0, + ), 0, ); - console.log('totalTransactionAmount',totalTransactionAmount); - const dopsRequestData = { templateName: TemplateName.PAYMENT_RECEIPT, generatedDocumentFileName: `Receipt_No_${receiptNumber}`, templateData: { - receiptType: transaction.transactionTypeId=== TransactionType.PURCHASE?"Receipt": "Refund Receipt", + transactionType: transaction.transactionTypeId, + receiptType: + transaction.transactionTypeId === TransactionType.PURCHASE + ? 'Receipt' + : 'Refund Receipt', receiptNo: receiptNumber, companyName: companyName, companyAlternateName: companyAlternateName, @@ -511,7 +537,7 @@ export class PermitReceiptDocumentService { totalTransactionAmount, ), permitDetails: permitDetails, - transactions: transactionList, + transactions: uniqueTransactionList, transactionDate: convertUtcToPt( transaction?.transactionSubmitDate, 'MMM. D, YYYY, hh:mm a Z', From 7a3146b66881aca6206665c2051be1c6961b4132 Mon Sep 17 00:00:00 2001 From: praju-aot Date: Fri, 8 Nov 2024 12:08:02 -0500 Subject: [PATCH 07/16] Update void endpoint to process refunds to multiple payment methods --- ...tion.dto.ts => payment-transaction.dto.ts} | 2 +- .../request/create-refund-transaction.dto.ts | 11 ++- .../payment/payment.service.ts | 20 ++++- .../permit/dto/request/void-permit.dto.ts | 76 ++++--------------- .../permit/permit.service.ts | 64 ++++++++++------ 5 files changed, 75 insertions(+), 98 deletions(-) rename vehicles/src/modules/permit-application-payment/payment/dto/common/{refund-transaction.dto.ts => payment-transaction.dto.ts} (97%) diff --git a/vehicles/src/modules/permit-application-payment/payment/dto/common/refund-transaction.dto.ts b/vehicles/src/modules/permit-application-payment/payment/dto/common/payment-transaction.dto.ts similarity index 97% rename from vehicles/src/modules/permit-application-payment/payment/dto/common/refund-transaction.dto.ts rename to vehicles/src/modules/permit-application-payment/payment/dto/common/payment-transaction.dto.ts index d749e3d51..fb9e6183f 100644 --- a/vehicles/src/modules/permit-application-payment/payment/dto/common/refund-transaction.dto.ts +++ b/vehicles/src/modules/permit-application-payment/payment/dto/common/payment-transaction.dto.ts @@ -12,7 +12,7 @@ import { import { PaymentCardType } from '../../../../../common/enum/payment-card-type.enum'; import { PaymentMethodType } from '../../../../../common/enum/payment-method-type.enum'; -export class RefundTransactionDto { +export class PaymentTransactionDto { @AutoMap() @ApiProperty({ example: '10000148', diff --git a/vehicles/src/modules/permit-application-payment/payment/dto/request/create-refund-transaction.dto.ts b/vehicles/src/modules/permit-application-payment/payment/dto/request/create-refund-transaction.dto.ts index 6032203ec..a689c9714 100644 --- a/vehicles/src/modules/permit-application-payment/payment/dto/request/create-refund-transaction.dto.ts +++ b/vehicles/src/modules/permit-application-payment/payment/dto/request/create-refund-transaction.dto.ts @@ -8,7 +8,7 @@ import { ValidateNested, } from 'class-validator'; -import { RefundTransactionDto } from '../common/refund-transaction.dto'; +import { PaymentTransactionDto } from '../common/payment-transaction.dto'; import { Type } from 'class-transformer'; export class CreateRefundTransactionDto { @@ -23,14 +23,13 @@ export class CreateRefundTransactionDto { @AutoMap() @ApiProperty({ - description: - 'The refund transaction details specific to application/permit.', + description: 'The transaction details specific to application/permit.', required: true, - type: [RefundTransactionDto], + type: [PaymentTransactionDto], }) @IsArray() @ValidateNested({ each: true }) @ArrayMinSize(1) - @Type(() => RefundTransactionDto) - transactions: RefundTransactionDto[]; + @Type(() => PaymentTransactionDto) + transactions: PaymentTransactionDto[]; } diff --git a/vehicles/src/modules/permit-application-payment/payment/payment.service.ts b/vehicles/src/modules/permit-application-payment/payment/payment.service.ts index ed9977c8f..bd83e524d 100644 --- a/vehicles/src/modules/permit-application-payment/payment/payment.service.ts +++ b/vehicles/src/modules/permit-application-payment/payment/payment.service.ts @@ -77,7 +77,7 @@ import { } from '../../../common/helper/common.helper'; import { SpecialAuth } from 'src/modules/special-auth/entities/special-auth.entity'; import { TIMEZONE_PACIFIC } from 'src/common/constants/api.constant'; -import { RefundTransactionDto } from './dto/common/refund-transaction.dto'; +import { PaymentTransactionDto } from './dto/common/payment-transaction.dto'; import { Nullable } from '../../../common/types/common'; @Injectable() @@ -266,7 +266,7 @@ export class PaymentService { * Creates a Refund Transaction in ORBC System, ensuring that payment methods align with user roles and enabled features. * The method verifies transactions against application status and computes transaction amounts. * It then creates and saves new transactions and their associated records, handling any CFS payment methods. - * + * * @param applicationId - The ID of the application related to the refund transactions. * @param transactions - An array of transactions of type {@link RefundTransactionDto} to process. * @param currentUser - The current user object of type {@link IUserJWT}. @@ -283,7 +283,7 @@ export class PaymentService { nestedQueryRunner, }: { applicationId: string; - transactions: RefundTransactionDto[]; + transactions: PaymentTransactionDto[]; currentUser: IUserJWT; nestedQueryRunner?: Nullable; }): Promise { @@ -312,6 +312,16 @@ export class PaymentService { `paymentCardTypeCode is required when paymentMethodTypeCode is ${transaction?.paymentMethodTypeCode}`, ]); } + + if ( + transaction?.paymentMethodTypeCode !== + PaymentMethodTypeEnum.NO_PAYMENT && + transaction?.transactionAmount === 0 + ) { + throwUnprocessableEntityException( + `paymentMethodTypeCode should be ${PaymentMethodTypeEnum.NO_PAYMENT} when transaction amount is 0`, + ); + } } let readTransactionDto: ReadTransactionDto[]; @@ -477,6 +487,10 @@ export class PaymentService { createTransactionDto: CreateTransactionDto, nestedQueryRunner?: QueryRunner, ): Promise { + if (createTransactionDto.transactionTypeId !== TransactionType.PURCHASE) { + throwUnprocessableEntityException('Invalid transaction type'); + } + if ( !doesUserHaveRole(currentUser.orbcUserRole, IDIR_USER_ROLE_LIST) && createTransactionDto?.paymentMethodTypeCode !== diff --git a/vehicles/src/modules/permit-application-payment/permit/dto/request/void-permit.dto.ts b/vehicles/src/modules/permit-application-payment/permit/dto/request/void-permit.dto.ts index bcb70c0af..152de0d2c 100644 --- a/vehicles/src/modules/permit-application-payment/permit/dto/request/void-permit.dto.ts +++ b/vehicles/src/modules/permit-application-payment/permit/dto/request/void-permit.dto.ts @@ -1,20 +1,20 @@ import { AutoMap } from '@automapper/classes'; import { ApiProperty } from '@nestjs/swagger'; import { + ArrayMinSize, + IsArray, IsEmail, IsEnum, - IsNumber, IsOptional, IsString, Length, - MaxLength, - Min, MinLength, + ValidateNested, } from 'class-validator'; import { ApplicationStatus } from 'src/common/enum/application-status.enum'; -import { PaymentMethodType } from '../../../../../common/enum/payment-method-type.enum'; import { TransactionType } from '../../../../../common/enum/transaction-type.enum'; -import { PaymentCardType } from '../../../../../common/enum/payment-card-type.enum'; +import { PaymentTransactionDto } from '../../../payment/dto/common/payment-transaction.dto'; +import { Type } from 'class-transformer'; export class VoidPermitDto { @AutoMap() @@ -28,23 +28,15 @@ export class VoidPermitDto { @AutoMap() @ApiProperty({ - description: 'Provider Transaction ID.', - example: '10000148', - required: false, - }) - @IsOptional() - @IsString() - @MaxLength(20) - pgTransactionId?: string; - - @AutoMap() - @ApiProperty({ - enum: PaymentMethodType, - example: PaymentMethodType.WEB, - description: 'The identifier of the user selected payment method.', + description: 'The transaction details specific to application/permit.', + required: true, + type: [PaymentTransactionDto], }) - @IsEnum(PaymentMethodType) - paymentMethodTypeCode: PaymentMethodType; + @IsArray() + @ValidateNested({ each: true }) + @ArrayMinSize(1) + @Type(() => PaymentTransactionDto) + transactions: PaymentTransactionDto[]; @AutoMap() @ApiProperty({ @@ -56,48 +48,6 @@ export class VoidPermitDto { @IsEnum(TransactionType) transactionTypeId: TransactionType; - @AutoMap() - @ApiProperty({ - description: 'Payment Transaction Amount.', - example: 30, - }) - @IsNumber() - @Min(0) - transactionAmount: number; - - @AutoMap() - @ApiProperty({ - description: 'Payment Transaction Date.', - example: '2023-07-10T15:49:36.582Z', - required: false, - }) - @IsOptional() - @IsString() - //@MaxLength(27) // TODO Should it be Is Date? - pgTransactionDate?: string; - - @AutoMap() - @ApiProperty({ - example: 'CC', - description: 'Represents the payment method of a transaction.', - required: false, - }) - @IsOptional() - @IsString() - @Length(1, 2) - pgPaymentMethod?: string; - - @AutoMap() - @ApiProperty({ - enum: PaymentCardType, - example: PaymentCardType.VISA, - description: 'Represents the card type used for the transaction.', - required: false, - }) - @IsOptional() - @IsEnum(PaymentCardType) - pgCardType?: PaymentCardType; - @AutoMap() @ApiProperty({ example: 'This permit was voided because of so-and-so reason', diff --git a/vehicles/src/modules/permit-application-payment/permit/permit.service.ts b/vehicles/src/modules/permit-application-payment/permit/permit.service.ts index eb063ce04..6adc9fffd 100644 --- a/vehicles/src/modules/permit-application-payment/permit/permit.service.ts +++ b/vehicles/src/modules/permit-application-payment/permit/permit.service.ts @@ -60,6 +60,7 @@ import { generateFaxEmail, validateEmailandFaxList, } from '../../../common/helper/notification.helper'; +import { TransactionType } from '../../../common/enum/transaction-type.enum'; @Injectable() export class PermitService { @@ -591,33 +592,46 @@ export class PermitService { }, ); - const createTransactionDto = new CreateTransactionDto(); - createTransactionDto.transactionTypeId = voidPermitDto.transactionTypeId; - createTransactionDto.paymentMethodTypeCode = - voidPermitDto.paymentMethodTypeCode; - createTransactionDto.paymentCardTypeCode = voidPermitDto.pgCardType; - createTransactionDto.pgCardType = voidPermitDto.pgCardType; - createTransactionDto.pgTransactionId = voidPermitDto.pgTransactionId; - createTransactionDto.pgPaymentMethod = voidPermitDto.pgPaymentMethod; - - // Refund for void should automatically set this flag to approved for payment gateway payment methods - // Otherwise, the flag is not applicable - if (voidPermitDto.paymentMethodTypeCode === PaymentMethodType.WEB) { - createTransactionDto.pgApproved = 1; - } - - createTransactionDto.applicationDetails = [ - { + if (voidPermitDto.transactionTypeId === TransactionType.REFUND) { + await this.paymentService.createRefundTransactions({ + currentUser, applicationId: newPermit.permitId, - transactionAmount: voidPermitDto.transactionAmount, - }, - ]; - await this.paymentService.createTransactions( - currentUser, - createTransactionDto, - queryRunner, - ); + transactions: voidPermitDto.transactions, + nestedQueryRunner: queryRunner, + }); + } + if (voidPermitDto.transactionTypeId === TransactionType.PURCHASE) { + const transaction = voidPermitDto?.transactions?.at(0); + const createTransactionDto = new CreateTransactionDto(); + createTransactionDto.transactionTypeId = + voidPermitDto.transactionTypeId; + createTransactionDto.paymentMethodTypeCode = + transaction.paymentMethodTypeCode; + createTransactionDto.paymentCardTypeCode = + transaction.paymentCardTypeCode; + createTransactionDto.pgCardType = transaction.paymentCardTypeCode; + createTransactionDto.pgTransactionId = transaction.pgTransactionId; + createTransactionDto.pgPaymentMethod = transaction.pgPaymentMethod; + + // Refund for void should automatically set this flag to approved for payment gateway payment methods + // Otherwise, the flag is not applicable + if (transaction.paymentMethodTypeCode === PaymentMethodType.WEB) { + createTransactionDto.pgApproved = 1; + } + + createTransactionDto.applicationDetails = [ + { + applicationId: newPermit.permitId, + transactionAmount: transaction.transactionAmount, + }, + ]; + await this.paymentService.createTransactions( + currentUser, + createTransactionDto, + queryRunner, + ); + } await queryRunner.commitTransaction(); success = permitId; voidRevokedPermitId = newPermit.permitId; From cdc8d3acd9fe3a61592bf707c2d472c8a236cdc6 Mon Sep 17 00:00:00 2001 From: praju-aot Date: Fri, 8 Nov 2024 12:09:48 -0500 Subject: [PATCH 08/16] Code review Comments --- .../versions/revert/v_47_ddl_revert.sql | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/database/mssql/scripts/versions/revert/v_47_ddl_revert.sql b/database/mssql/scripts/versions/revert/v_47_ddl_revert.sql index e9ec94f53..c7ee20bc6 100644 --- a/database/mssql/scripts/versions/revert/v_47_ddl_revert.sql +++ b/database/mssql/scripts/versions/revert/v_47_ddl_revert.sql @@ -207,13 +207,24 @@ IF @@ERROR <> 0 SET NOEXEC ON GO ALTER TABLE [permit].[ORBC_TRANSACTION_HIST] DROP COLUMN [RECEIPT_ID] -IF @@ERROR <> 0 SET NOEXEC ON -GO -COMMIT TRANSACTION - IF @@ERROR <> 0 SET NOEXEC ON GO DECLARE @VersionDescription VARCHAR(255) SET @VersionDescription = 'Reverting addition of RECEIPT_ID col to ORBC_TRANSACTION and dropping TRANSACTION_ID from ORBC_RECEIPT.' INSERT [dbo].[ORBC_SYS_VERSION] ([VERSION_ID], [DESCRIPTION], [RELEASE_DATE]) VALUES (46, @VersionDescription, getutcdate()) + +IF @@ERROR <> 0 SET NOEXEC ON +GO +COMMIT TRANSACTION +IF @@ERROR <> 0 SET NOEXEC ON +GO +DECLARE @Success AS BIT +SET @Success = 1 +SET NOEXEC OFF +IF (@Success = 1) PRINT 'The database update succeeded' +ELSE BEGIN + IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION + PRINT 'The database update failed' +END +GO \ No newline at end of file From d2b5c29e0daee06ca34a12e91afc0d05212cf5a5 Mon Sep 17 00:00:00 2001 From: praju-aot Date: Fri, 8 Nov 2024 14:33:25 -0500 Subject: [PATCH 09/16] Updated the version from 47 to 48 --- .../revert/{v_47_ddl_revert.sql => v_48_ddl_revert.sql} | 2 +- database/mssql/scripts/versions/{v_47_ddl.sql => v_48_ddl.sql} | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename database/mssql/scripts/versions/revert/{v_47_ddl_revert.sql => v_48_ddl_revert.sql} (99%) rename database/mssql/scripts/versions/{v_47_ddl.sql => v_48_ddl.sql} (99%) diff --git a/database/mssql/scripts/versions/revert/v_47_ddl_revert.sql b/database/mssql/scripts/versions/revert/v_48_ddl_revert.sql similarity index 99% rename from database/mssql/scripts/versions/revert/v_47_ddl_revert.sql rename to database/mssql/scripts/versions/revert/v_48_ddl_revert.sql index c7ee20bc6..3ef5d2d06 100644 --- a/database/mssql/scripts/versions/revert/v_47_ddl_revert.sql +++ b/database/mssql/scripts/versions/revert/v_48_ddl_revert.sql @@ -212,7 +212,7 @@ GO DECLARE @VersionDescription VARCHAR(255) SET @VersionDescription = 'Reverting addition of RECEIPT_ID col to ORBC_TRANSACTION and dropping TRANSACTION_ID from ORBC_RECEIPT.' -INSERT [dbo].[ORBC_SYS_VERSION] ([VERSION_ID], [DESCRIPTION], [RELEASE_DATE]) VALUES (46, @VersionDescription, getutcdate()) +INSERT [dbo].[ORBC_SYS_VERSION] ([VERSION_ID], [DESCRIPTION], [RELEASE_DATE]) VALUES (47, @VersionDescription, getutcdate()) IF @@ERROR <> 0 SET NOEXEC ON GO diff --git a/database/mssql/scripts/versions/v_47_ddl.sql b/database/mssql/scripts/versions/v_48_ddl.sql similarity index 99% rename from database/mssql/scripts/versions/v_47_ddl.sql rename to database/mssql/scripts/versions/v_48_ddl.sql index 45c0b4270..f4e40ce1d 100644 --- a/database/mssql/scripts/versions/v_47_ddl.sql +++ b/database/mssql/scripts/versions/v_48_ddl.sql @@ -218,7 +218,7 @@ GO DECLARE @VersionDescription VARCHAR(255) SET @VersionDescription = 'Add RECEIPT_ID col to ORBC_TRANSACTION and Drop TRANSACTION_ID from ORBC_RECEIPT' -INSERT [dbo].[ORBC_SYS_VERSION] ([VERSION_ID], [DESCRIPTION], [UPDATE_SCRIPT], [REVERT_SCRIPT], [RELEASE_DATE]) VALUES (47, @VersionDescription, '$(UPDATE_SCRIPT)', '$(REVERT_SCRIPT)', getutcdate()) +INSERT [dbo].[ORBC_SYS_VERSION] ([VERSION_ID], [DESCRIPTION], [UPDATE_SCRIPT], [REVERT_SCRIPT], [RELEASE_DATE]) VALUES (48, @VersionDescription, '$(UPDATE_SCRIPT)', '$(REVERT_SCRIPT)', getutcdate()) IF @@ERROR <> 0 SET NOEXEC ON GO From 9b8920db711597bf26dd7a270be0f2ae4bc1a9ec Mon Sep 17 00:00:00 2001 From: praju-aot Date: Wed, 13 Nov 2024 15:40:08 -0500 Subject: [PATCH 10/16] Code merge conflicts --- .../revert/{v_48_ddl_revert.sql => v_49_ddl_revert.sql} | 2 +- database/mssql/scripts/versions/{v_48_ddl.sql => v_49_ddl.sql} | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename database/mssql/scripts/versions/revert/{v_48_ddl_revert.sql => v_49_ddl_revert.sql} (99%) rename database/mssql/scripts/versions/{v_48_ddl.sql => v_49_ddl.sql} (99%) diff --git a/database/mssql/scripts/versions/revert/v_48_ddl_revert.sql b/database/mssql/scripts/versions/revert/v_49_ddl_revert.sql similarity index 99% rename from database/mssql/scripts/versions/revert/v_48_ddl_revert.sql rename to database/mssql/scripts/versions/revert/v_49_ddl_revert.sql index 3ef5d2d06..a676aadea 100644 --- a/database/mssql/scripts/versions/revert/v_48_ddl_revert.sql +++ b/database/mssql/scripts/versions/revert/v_49_ddl_revert.sql @@ -212,7 +212,7 @@ GO DECLARE @VersionDescription VARCHAR(255) SET @VersionDescription = 'Reverting addition of RECEIPT_ID col to ORBC_TRANSACTION and dropping TRANSACTION_ID from ORBC_RECEIPT.' -INSERT [dbo].[ORBC_SYS_VERSION] ([VERSION_ID], [DESCRIPTION], [RELEASE_DATE]) VALUES (47, @VersionDescription, getutcdate()) +INSERT [dbo].[ORBC_SYS_VERSION] ([VERSION_ID], [DESCRIPTION], [RELEASE_DATE]) VALUES (48, @VersionDescription, getutcdate()) IF @@ERROR <> 0 SET NOEXEC ON GO diff --git a/database/mssql/scripts/versions/v_48_ddl.sql b/database/mssql/scripts/versions/v_49_ddl.sql similarity index 99% rename from database/mssql/scripts/versions/v_48_ddl.sql rename to database/mssql/scripts/versions/v_49_ddl.sql index f4e40ce1d..c354741fe 100644 --- a/database/mssql/scripts/versions/v_48_ddl.sql +++ b/database/mssql/scripts/versions/v_49_ddl.sql @@ -218,7 +218,7 @@ GO DECLARE @VersionDescription VARCHAR(255) SET @VersionDescription = 'Add RECEIPT_ID col to ORBC_TRANSACTION and Drop TRANSACTION_ID from ORBC_RECEIPT' -INSERT [dbo].[ORBC_SYS_VERSION] ([VERSION_ID], [DESCRIPTION], [UPDATE_SCRIPT], [REVERT_SCRIPT], [RELEASE_DATE]) VALUES (48, @VersionDescription, '$(UPDATE_SCRIPT)', '$(REVERT_SCRIPT)', getutcdate()) +INSERT [dbo].[ORBC_SYS_VERSION] ([VERSION_ID], [DESCRIPTION], [UPDATE_SCRIPT], [REVERT_SCRIPT], [RELEASE_DATE]) VALUES (49, @VersionDescription, '$(UPDATE_SCRIPT)', '$(REVERT_SCRIPT)', getutcdate()) IF @@ERROR <> 0 SET NOEXEC ON GO From 34cf1d191d9cf0cf9406cd3839f8cd72e5759763 Mon Sep 17 00:00:00 2001 From: praju-aot Date: Tue, 26 Nov 2024 11:04:36 -0500 Subject: [PATCH 11/16] Merge conflicts --- .../revert/{v_49_ddl_revert.sql => v_50_ddl_revert.sql} | 2 +- database/mssql/scripts/versions/{v_49_ddl.sql => v_50_ddl.sql} | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename database/mssql/scripts/versions/revert/{v_49_ddl_revert.sql => v_50_ddl_revert.sql} (99%) rename database/mssql/scripts/versions/{v_49_ddl.sql => v_50_ddl.sql} (99%) diff --git a/database/mssql/scripts/versions/revert/v_49_ddl_revert.sql b/database/mssql/scripts/versions/revert/v_50_ddl_revert.sql similarity index 99% rename from database/mssql/scripts/versions/revert/v_49_ddl_revert.sql rename to database/mssql/scripts/versions/revert/v_50_ddl_revert.sql index a676aadea..75fbcaa3d 100644 --- a/database/mssql/scripts/versions/revert/v_49_ddl_revert.sql +++ b/database/mssql/scripts/versions/revert/v_50_ddl_revert.sql @@ -212,7 +212,7 @@ GO DECLARE @VersionDescription VARCHAR(255) SET @VersionDescription = 'Reverting addition of RECEIPT_ID col to ORBC_TRANSACTION and dropping TRANSACTION_ID from ORBC_RECEIPT.' -INSERT [dbo].[ORBC_SYS_VERSION] ([VERSION_ID], [DESCRIPTION], [RELEASE_DATE]) VALUES (48, @VersionDescription, getutcdate()) +INSERT [dbo].[ORBC_SYS_VERSION] ([VERSION_ID], [DESCRIPTION], [RELEASE_DATE]) VALUES (49, @VersionDescription, getutcdate()) IF @@ERROR <> 0 SET NOEXEC ON GO diff --git a/database/mssql/scripts/versions/v_49_ddl.sql b/database/mssql/scripts/versions/v_50_ddl.sql similarity index 99% rename from database/mssql/scripts/versions/v_49_ddl.sql rename to database/mssql/scripts/versions/v_50_ddl.sql index c354741fe..fd12e96bf 100644 --- a/database/mssql/scripts/versions/v_49_ddl.sql +++ b/database/mssql/scripts/versions/v_50_ddl.sql @@ -218,7 +218,7 @@ GO DECLARE @VersionDescription VARCHAR(255) SET @VersionDescription = 'Add RECEIPT_ID col to ORBC_TRANSACTION and Drop TRANSACTION_ID from ORBC_RECEIPT' -INSERT [dbo].[ORBC_SYS_VERSION] ([VERSION_ID], [DESCRIPTION], [UPDATE_SCRIPT], [REVERT_SCRIPT], [RELEASE_DATE]) VALUES (49, @VersionDescription, '$(UPDATE_SCRIPT)', '$(REVERT_SCRIPT)', getutcdate()) +INSERT [dbo].[ORBC_SYS_VERSION] ([VERSION_ID], [DESCRIPTION], [UPDATE_SCRIPT], [REVERT_SCRIPT], [RELEASE_DATE]) VALUES (50, @VersionDescription, '$(UPDATE_SCRIPT)', '$(REVERT_SCRIPT)', getutcdate()) IF @@ERROR <> 0 SET NOEXEC ON GO From d857fdec71b1445df15e5950b94fff4a9ab34131 Mon Sep 17 00:00:00 2001 From: praju-aot Date: Wed, 4 Dec 2024 17:12:46 -0500 Subject: [PATCH 12/16] Resolve code merge conflicts --- .../versions/revert/v_51_ddl_revert.sql | 230 +++++++++++++++++ database/mssql/scripts/versions/v_51_ddl.sql | 238 ++++++++++++++++++ 2 files changed, 468 insertions(+) create mode 100644 database/mssql/scripts/versions/revert/v_51_ddl_revert.sql create mode 100644 database/mssql/scripts/versions/v_51_ddl.sql diff --git a/database/mssql/scripts/versions/revert/v_51_ddl_revert.sql b/database/mssql/scripts/versions/revert/v_51_ddl_revert.sql new file mode 100644 index 000000000..66f313b30 --- /dev/null +++ b/database/mssql/scripts/versions/revert/v_51_ddl_revert.sql @@ -0,0 +1,230 @@ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +SET NOCOUNT ON +GO + +SET XACT_ABORT ON +GO +SET TRANSACTION ISOLATION LEVEL SERIALIZABLE +GO +BEGIN TRANSACTION +GO + + +IF @@ERROR <> 0 SET NOEXEC ON +GO +ALTER TABLE [permit].[ORBC_RECEIPT] ADD [TRANSACTION_ID] [bigint] NULL + + +IF @@ERROR <> 0 SET NOEXEC ON +GO +ALTER TABLE [permit].[ORBC_RECEIPT] WITH CHECK ADD CONSTRAINT [ORBC_RECEIPT_TRANSACTION_ID_FK] FOREIGN KEY([TRANSACTION_ID]) +REFERENCES [permit].[ORBC_TRANSACTION] ([TRANSACTION_ID]) + +-- Alter trigger permit.ORBC_RCPT_A_S_IUD_TR +PRINT N'Alter trigger permit.ORBC_RCPT_A_S_IUD_TR' +GO +ALTER TRIGGER [permit].[ORBC_RCPT_A_S_IUD_TR] ON permit.[ORBC_RECEIPT] FOR INSERT, UPDATE, DELETE AS +SET NOCOUNT ON +BEGIN TRY +DECLARE @curr_date datetime; +SET @curr_date = getutcdate(); + IF NOT EXISTS(SELECT * FROM inserted) AND NOT EXISTS(SELECT * FROM deleted) + RETURN; + + -- historical + IF EXISTS(SELECT * FROM deleted) + update [permit].[ORBC_RECEIPT_HIST] set END_DATE_HIST = @curr_date where RECEIPT_ID in (select RECEIPT_ID from deleted) and END_DATE_HIST is null; + + IF EXISTS(SELECT * FROM inserted) + insert into [permit].[ORBC_RECEIPT_HIST] ([RECEIPT_ID], [RECEIPT_NUMBER], [TRANSACTION_ID], [RECEIPT_DOCUMENT_ID], [APP_CREATE_TIMESTAMP], [APP_CREATE_USERID], [APP_CREATE_USER_GUID], [APP_CREATE_USER_DIRECTORY], [APP_LAST_UPDATE_TIMESTAMP], [APP_LAST_UPDATE_USERID], [APP_LAST_UPDATE_USER_GUID], [APP_LAST_UPDATE_USER_DIRECTORY], [CONCURRENCY_CONTROL_NUMBER], [DB_CREATE_USERID], [DB_CREATE_TIMESTAMP], [DB_LAST_UPDATE_USERID], [DB_LAST_UPDATE_TIMESTAMP], _RECEIPT_HIST_ID, END_DATE_HIST, EFFECTIVE_DATE_HIST) + select [RECEIPT_ID], [RECEIPT_NUMBER], [TRANSACTION_ID], [RECEIPT_DOCUMENT_ID], [APP_CREATE_TIMESTAMP], [APP_CREATE_USERID], [APP_CREATE_USER_GUID], [APP_CREATE_USER_DIRECTORY], [APP_LAST_UPDATE_TIMESTAMP], [APP_LAST_UPDATE_USERID], [APP_LAST_UPDATE_USER_GUID], [APP_LAST_UPDATE_USER_DIRECTORY], [CONCURRENCY_CONTROL_NUMBER], [DB_CREATE_USERID], [DB_CREATE_TIMESTAMP], [DB_LAST_UPDATE_USERID], [DB_LAST_UPDATE_TIMESTAMP], (next value for [permit].[ORBC_RECEIPT_H_ID_SEQ]) as [_RECEIPT_HIST_ID], null as [END_DATE_HIST], @curr_date as [EFFECTIVE_DATE_HIST] from inserted; + +END TRY +BEGIN CATCH + IF @@trancount > 0 ROLLBACK TRANSACTION + EXEC orbc_error_handling +END CATCH; +GO + +IF @@ERROR <> 0 SET NOEXEC ON +GO + +-- Alter trigger permit.ORBC_RCPT_I_S_U_TR +PRINT N'Alter trigger permit.ORBC_RCPT_I_S_U_TR' +GO +ALTER TRIGGER [permit].[ORBC_RCPT_I_S_U_TR] ON permit.[ORBC_RECEIPT] INSTEAD OF UPDATE AS +SET NOCOUNT ON +BEGIN TRY + IF NOT EXISTS(SELECT * FROM deleted) + RETURN; + + -- validate concurrency control + if exists (select 1 from inserted, deleted where inserted.CONCURRENCY_CONTROL_NUMBER != deleted.CONCURRENCY_CONTROL_NUMBER+1 AND inserted.RECEIPT_ID = deleted.RECEIPT_ID) + raiserror('CONCURRENCY FAILURE.',16,1) + + + -- update statement + update [permit].[ORBC_RECEIPT] + set "RECEIPT_NUMBER" = inserted."RECEIPT_NUMBER", + "TRANSACTION_ID" = inserted."TRANSACTION_ID", + "RECEIPT_DOCUMENT_ID" = inserted."RECEIPT_DOCUMENT_ID", + "APP_LAST_UPDATE_TIMESTAMP" = inserted."APP_LAST_UPDATE_TIMESTAMP", + "APP_LAST_UPDATE_USERID" = inserted."APP_LAST_UPDATE_USERID", + "APP_LAST_UPDATE_USER_GUID" = inserted."APP_LAST_UPDATE_USER_GUID", + "APP_LAST_UPDATE_USER_DIRECTORY" = inserted."APP_LAST_UPDATE_USER_DIRECTORY", + "CONCURRENCY_CONTROL_NUMBER" = inserted."CONCURRENCY_CONTROL_NUMBER" + , DB_LAST_UPDATE_TIMESTAMP = getutcdate() + , DB_LAST_UPDATE_USERID = user_name() + from [permit].[ORBC_RECEIPT] + inner join inserted + on (ORBC_RECEIPT.RECEIPT_ID = inserted.RECEIPT_ID); + +END TRY +BEGIN CATCH + IF @@trancount > 0 ROLLBACK TRANSACTION + EXEC orbc_error_handling +END CATCH; +GO +IF @@ERROR <> 0 SET NOEXEC ON +GO +DISABLE TRIGGER [permit].[ORBC_RCPT_I_S_U_TR] ON permit.[ORBC_RECEIPT] +GO + +IF @@ERROR <> 0 SET NOEXEC ON +GO +UPDATE r +SET TRANSACTION_ID = t.TRANSACTION_ID +FROM [permit].[ORBC_RECEIPT] r +INNER JOIN [permit].[ORBC_TRANSACTION] t ON t.RECEIPT_ID = r.RECEIPT_ID; + +IF @@ERROR <> 0 SET NOEXEC ON +GO +ALTER TABLE [permit].[ORBC_RECEIPT] ALTER COLUMN [TRANSACTION_ID] [bigint] NOT NULL + +IF @@ERROR <> 0 SET NOEXEC ON +GO +AlTER TABLE [permit].[ORBC_RECEIPT_HIST] ALTER COLUMN [TRANSACTION_ID] [bigint] NOT NULL + + +IF @@ERROR <> 0 SET NOEXEC ON +GO +ALTER TABLE [permit].[ORBC_TRANSACTION] DROP CONSTRAINT FK_ORBC_TRANSACTION_RECEIPT_ID + +IF @@ERROR <> 0 SET NOEXEC ON +GO +ALTER TABLE [permit].[ORBC_TRANSACTION] DROP COLUMN [RECEIPT_ID] + + +IF @@ERROR <> 0 SET NOEXEC ON +GO +-- Alter trigger permit.ORBC_TXN_A_S_IUD_TR +PRINT N'Alter trigger permit.ORBC_TXN_A_S_IUD_TR' +GO +ALTER TRIGGER [permit].[ORBC_TXN_A_S_IUD_TR] ON permit.[ORBC_TRANSACTION] FOR INSERT, UPDATE, DELETE AS +SET NOCOUNT ON +BEGIN TRY +DECLARE @curr_date datetime; +SET @curr_date = getutcdate(); + IF NOT EXISTS(SELECT * FROM inserted) AND NOT EXISTS(SELECT * FROM deleted) + RETURN; + + -- historical + IF EXISTS(SELECT * FROM deleted) + update [permit].[ORBC_TRANSACTION_HIST] set END_DATE_HIST = @curr_date where TRANSACTION_ID in (select TRANSACTION_ID from deleted) and END_DATE_HIST is null; + + IF EXISTS(SELECT * FROM inserted) + insert into [permit].[ORBC_TRANSACTION_HIST] ([TRANSACTION_ID], [TRANSACTION_TYPE], [PAYMENT_METHOD_TYPE], [PAYMENT_CARD_TYPE], [TOTAL_TRANSACTION_AMOUNT], [TRANSACTION_SUBMIT_DATE], [TRANSACTION_ORDER_NUMBER], [PG_TRANSACTION_ID], [PG_TRANSACTION_APPROVED], [PG_AUTH_CODE], [PG_TRANSACTION_CARD_TYPE], [PG_TRANSACTION_DATE], [PG_CVD_ID], [PG_PAYMENT_METHOD], [PG_MESSAGE_ID], [PG_MESSAGE_TEXT], [APP_CREATE_TIMESTAMP], [APP_CREATE_USERID], [APP_CREATE_USER_GUID], [APP_CREATE_USER_DIRECTORY], [APP_LAST_UPDATE_TIMESTAMP], [APP_LAST_UPDATE_USERID], [APP_LAST_UPDATE_USER_GUID], [APP_LAST_UPDATE_USER_DIRECTORY], [CONCURRENCY_CONTROL_NUMBER], [DB_CREATE_USERID], [DB_CREATE_TIMESTAMP], [DB_LAST_UPDATE_USERID], [DB_LAST_UPDATE_TIMESTAMP], _TRANSACTION_HIST_ID, END_DATE_HIST, EFFECTIVE_DATE_HIST) + select [TRANSACTION_ID], [TRANSACTION_TYPE], [PAYMENT_METHOD_TYPE], [PAYMENT_CARD_TYPE], [TOTAL_TRANSACTION_AMOUNT], [TRANSACTION_SUBMIT_DATE], [TRANSACTION_ORDER_NUMBER], [PG_TRANSACTION_ID], [PG_TRANSACTION_APPROVED], [PG_AUTH_CODE], [PG_TRANSACTION_CARD_TYPE], [PG_TRANSACTION_DATE], [PG_CVD_ID], [PG_PAYMENT_METHOD], [PG_MESSAGE_ID], [PG_MESSAGE_TEXT], [APP_CREATE_TIMESTAMP], [APP_CREATE_USERID], [APP_CREATE_USER_GUID], [APP_CREATE_USER_DIRECTORY], [APP_LAST_UPDATE_TIMESTAMP], [APP_LAST_UPDATE_USERID], [APP_LAST_UPDATE_USER_GUID], [APP_LAST_UPDATE_USER_DIRECTORY], [CONCURRENCY_CONTROL_NUMBER], [DB_CREATE_USERID], [DB_CREATE_TIMESTAMP], [DB_LAST_UPDATE_USERID], [DB_LAST_UPDATE_TIMESTAMP], (next value for [permit].[ORBC_TRANSACTION_H_ID_SEQ]) as [_TRANSACTION_HIST_ID], null as [END_DATE_HIST], @curr_date as [EFFECTIVE_DATE_HIST] from inserted; + +END TRY +BEGIN CATCH + IF @@trancount > 0 ROLLBACK TRANSACTION + EXEC orbc_error_handling +END CATCH; +GO +IF @@ERROR <> 0 SET NOEXEC ON +GO + + +-- Alter trigger permit.ORBC_TXN_I_S_U_TR +PRINT N'Alter trigger permit.ORBC_TXN_I_S_U_TR' +GO +ALTER TRIGGER [permit].[ORBC_TXN_I_S_U_TR] ON permit.[ORBC_TRANSACTION] INSTEAD OF UPDATE AS +SET NOCOUNT ON +BEGIN TRY + IF NOT EXISTS(SELECT * FROM deleted) + RETURN; + + -- validate concurrency control + if exists (select 1 from inserted, deleted where inserted.CONCURRENCY_CONTROL_NUMBER != deleted.CONCURRENCY_CONTROL_NUMBER+1 AND inserted.TRANSACTION_ID = deleted.TRANSACTION_ID) + raiserror('CONCURRENCY FAILURE.',16,1) + + + -- update statement + update [permit].[ORBC_TRANSACTION] + set "TRANSACTION_TYPE" = inserted."TRANSACTION_TYPE", + "PAYMENT_METHOD_TYPE" = inserted."PAYMENT_METHOD_TYPE", + "PAYMENT_CARD_TYPE" = inserted."PAYMENT_CARD_TYPE", + "TOTAL_TRANSACTION_AMOUNT" = inserted."TOTAL_TRANSACTION_AMOUNT", + "TRANSACTION_SUBMIT_DATE" = inserted."TRANSACTION_SUBMIT_DATE", + "TRANSACTION_ORDER_NUMBER" = inserted."TRANSACTION_ORDER_NUMBER", + "PG_TRANSACTION_ID" = inserted."PG_TRANSACTION_ID", + "PG_TRANSACTION_APPROVED" = inserted."PG_TRANSACTION_APPROVED", + "PG_AUTH_CODE" = inserted."PG_AUTH_CODE", + "PG_TRANSACTION_CARD_TYPE" = inserted."PG_TRANSACTION_CARD_TYPE", + "PG_TRANSACTION_DATE" = inserted."PG_TRANSACTION_DATE", + "PG_CVD_ID" = inserted."PG_CVD_ID", + "PG_PAYMENT_METHOD" = inserted."PG_PAYMENT_METHOD", + "PG_MESSAGE_ID" = inserted."PG_MESSAGE_ID", + "PG_MESSAGE_TEXT" = inserted."PG_MESSAGE_TEXT", + "APP_LAST_UPDATE_TIMESTAMP" = inserted."APP_LAST_UPDATE_TIMESTAMP", + "APP_LAST_UPDATE_USERID" = inserted."APP_LAST_UPDATE_USERID", + "APP_LAST_UPDATE_USER_GUID" = inserted."APP_LAST_UPDATE_USER_GUID", + "APP_LAST_UPDATE_USER_DIRECTORY" = inserted."APP_LAST_UPDATE_USER_DIRECTORY", + "CONCURRENCY_CONTROL_NUMBER" = inserted."CONCURRENCY_CONTROL_NUMBER" + , DB_LAST_UPDATE_TIMESTAMP = getutcdate() + , DB_LAST_UPDATE_USERID = user_name() + from [permit].[ORBC_TRANSACTION] + inner join inserted + on (ORBC_TRANSACTION.TRANSACTION_ID = inserted.TRANSACTION_ID); + +END TRY +BEGIN CATCH + IF @@trancount > 0 ROLLBACK TRANSACTION + EXEC orbc_error_handling +END CATCH; +GO +IF @@ERROR <> 0 SET NOEXEC ON +GO +DISABLE TRIGGER [permit].[ORBC_TXN_I_S_U_TR] ON permit.[ORBC_TRANSACTION] +GO + + +IF @@ERROR <> 0 SET NOEXEC ON +GO +ALTER TABLE [permit].[ORBC_TRANSACTION_HIST] DROP COLUMN [RECEIPT_ID] + +IF @@ERROR <> 0 SET NOEXEC ON +GO +DECLARE @VersionDescription VARCHAR(255) +SET @VersionDescription = 'Reverting addition of RECEIPT_ID col to ORBC_TRANSACTION and dropping TRANSACTION_ID from ORBC_RECEIPT.' + +INSERT [dbo].[ORBC_SYS_VERSION] ([VERSION_ID], [DESCRIPTION], [RELEASE_DATE]) VALUES (50, @VersionDescription, getutcdate()) + +IF @@ERROR <> 0 SET NOEXEC ON +GO +COMMIT TRANSACTION +IF @@ERROR <> 0 SET NOEXEC ON +GO +DECLARE @Success AS BIT +SET @Success = 1 +SET NOEXEC OFF +IF (@Success = 1) PRINT 'The database update succeeded' +ELSE BEGIN + IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION + PRINT 'The database update failed' +END +GO \ No newline at end of file diff --git a/database/mssql/scripts/versions/v_51_ddl.sql b/database/mssql/scripts/versions/v_51_ddl.sql new file mode 100644 index 000000000..ddfb10478 --- /dev/null +++ b/database/mssql/scripts/versions/v_51_ddl.sql @@ -0,0 +1,238 @@ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +SET NOCOUNT ON +GO + +SET XACT_ABORT ON +GO +SET TRANSACTION ISOLATION LEVEL SERIALIZABLE +GO +BEGIN TRANSACTION +GO + +IF @@ERROR <> 0 SET NOEXEC ON +GO +ALTER TABLE [permit].[ORBC_TRANSACTION] ADD [RECEIPT_ID] [bigint] NULL + +IF @@ERROR <> 0 SET NOEXEC ON +GO +EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Receipt Id of the transaction' , @level0type=N'SCHEMA',@level0name=N'permit', @level1type=N'TABLE',@level1name=N'ORBC_TRANSACTION', @level2type=N'COLUMN',@level2name=N'RECEIPT_ID' + +IF @@ERROR <> 0 SET NOEXEC ON +GO + +IF @@ERROR <> 0 SET NOEXEC ON +GO +ALTER TABLE [permit].[ORBC_TRANSACTION] WITH CHECK ADD CONSTRAINT [FK_ORBC_TRANSACTION_RECEIPT_ID] FOREIGN KEY([RECEIPT_ID]) +REFERENCES [permit].[ORBC_RECEIPT] ([RECEIPT_ID]) +GO + +ALTER TABLE [permit].[ORBC_TRANSACTION] CHECK CONSTRAINT [FK_ORBC_TRANSACTION_RECEIPT_ID] + + +IF @@ERROR <> 0 SET NOEXEC ON +GO +ALTER TABLE [permit].[ORBC_TRANSACTION_HIST] ADD [RECEIPT_ID] [bigint] NULL + +IF @@ERROR <> 0 SET NOEXEC ON +GO +-- Alter trigger permit.ORBC_TXN_A_S_IUD_TR +PRINT N'Alter trigger permit.ORBC_TXN_A_S_IUD_TR' +GO +ALTER TRIGGER [permit].[ORBC_TXN_A_S_IUD_TR] ON permit.[ORBC_TRANSACTION] FOR INSERT, UPDATE, DELETE AS +SET NOCOUNT ON +BEGIN TRY +DECLARE @curr_date datetime; +SET @curr_date = getutcdate(); + IF NOT EXISTS(SELECT * FROM inserted) AND NOT EXISTS(SELECT * FROM deleted) + RETURN; + + -- historical + IF EXISTS(SELECT * FROM deleted) + update [permit].[ORBC_TRANSACTION_HIST] set END_DATE_HIST = @curr_date where TRANSACTION_ID in (select TRANSACTION_ID from deleted) and END_DATE_HIST is null; + + IF EXISTS(SELECT * FROM inserted) + insert into [permit].[ORBC_TRANSACTION_HIST] ([TRANSACTION_ID], [RECEIPT_ID], [TRANSACTION_TYPE], [PAYMENT_METHOD_TYPE], [PAYMENT_CARD_TYPE], [TOTAL_TRANSACTION_AMOUNT], [TRANSACTION_SUBMIT_DATE], [TRANSACTION_ORDER_NUMBER], [PG_TRANSACTION_ID], [PG_TRANSACTION_APPROVED], [PG_AUTH_CODE], [PG_TRANSACTION_CARD_TYPE], [PG_TRANSACTION_DATE], [PG_CVD_ID], [PG_PAYMENT_METHOD], [PG_MESSAGE_ID], [PG_MESSAGE_TEXT], [APP_CREATE_TIMESTAMP], [APP_CREATE_USERID], [APP_CREATE_USER_GUID], [APP_CREATE_USER_DIRECTORY], [APP_LAST_UPDATE_TIMESTAMP], [APP_LAST_UPDATE_USERID], [APP_LAST_UPDATE_USER_GUID], [APP_LAST_UPDATE_USER_DIRECTORY], [CONCURRENCY_CONTROL_NUMBER], [DB_CREATE_USERID], [DB_CREATE_TIMESTAMP], [DB_LAST_UPDATE_USERID], [DB_LAST_UPDATE_TIMESTAMP], _TRANSACTION_HIST_ID, END_DATE_HIST, EFFECTIVE_DATE_HIST) + select [TRANSACTION_ID], [RECEIPT_ID], [TRANSACTION_TYPE], [PAYMENT_METHOD_TYPE], [PAYMENT_CARD_TYPE], [TOTAL_TRANSACTION_AMOUNT], [TRANSACTION_SUBMIT_DATE], [TRANSACTION_ORDER_NUMBER], [PG_TRANSACTION_ID], [PG_TRANSACTION_APPROVED], [PG_AUTH_CODE], [PG_TRANSACTION_CARD_TYPE], [PG_TRANSACTION_DATE], [PG_CVD_ID], [PG_PAYMENT_METHOD], [PG_MESSAGE_ID], [PG_MESSAGE_TEXT], [APP_CREATE_TIMESTAMP], [APP_CREATE_USERID], [APP_CREATE_USER_GUID], [APP_CREATE_USER_DIRECTORY], [APP_LAST_UPDATE_TIMESTAMP], [APP_LAST_UPDATE_USERID], [APP_LAST_UPDATE_USER_GUID], [APP_LAST_UPDATE_USER_DIRECTORY], [CONCURRENCY_CONTROL_NUMBER], [DB_CREATE_USERID], [DB_CREATE_TIMESTAMP], [DB_LAST_UPDATE_USERID], [DB_LAST_UPDATE_TIMESTAMP], (next value for [permit].[ORBC_TRANSACTION_H_ID_SEQ]) as [_TRANSACTION_HIST_ID], null as [END_DATE_HIST], @curr_date as [EFFECTIVE_DATE_HIST] from inserted; + +END TRY +BEGIN CATCH + IF @@trancount > 0 ROLLBACK TRANSACTION + EXEC orbc_error_handling +END CATCH; +GO +IF @@ERROR <> 0 SET NOEXEC ON +GO + + +-- Alter trigger permit.ORBC_TXN_I_S_U_TR +PRINT N'Alter trigger permit.ORBC_TXN_I_S_U_TR' +GO +ALTER TRIGGER [permit].[ORBC_TXN_I_S_U_TR] ON permit.[ORBC_TRANSACTION] INSTEAD OF UPDATE AS +SET NOCOUNT ON +BEGIN TRY + IF NOT EXISTS(SELECT * FROM deleted) + RETURN; + + -- validate concurrency control + if exists (select 1 from inserted, deleted where inserted.CONCURRENCY_CONTROL_NUMBER != deleted.CONCURRENCY_CONTROL_NUMBER+1 AND inserted.TRANSACTION_ID = deleted.TRANSACTION_ID) + raiserror('CONCURRENCY FAILURE.',16,1) + + + -- update statement + update [permit].[ORBC_TRANSACTION] + set "TRANSACTION_TYPE" = inserted."TRANSACTION_TYPE", + "RECEIPT_ID" = inserted."RECEIPT_ID", + "PAYMENT_METHOD_TYPE" = inserted."PAYMENT_METHOD_TYPE", + "PAYMENT_CARD_TYPE" = inserted."PAYMENT_CARD_TYPE", + "TOTAL_TRANSACTION_AMOUNT" = inserted."TOTAL_TRANSACTION_AMOUNT", + "TRANSACTION_SUBMIT_DATE" = inserted."TRANSACTION_SUBMIT_DATE", + "TRANSACTION_ORDER_NUMBER" = inserted."TRANSACTION_ORDER_NUMBER", + "PG_TRANSACTION_ID" = inserted."PG_TRANSACTION_ID", + "PG_TRANSACTION_APPROVED" = inserted."PG_TRANSACTION_APPROVED", + "PG_AUTH_CODE" = inserted."PG_AUTH_CODE", + "PG_TRANSACTION_CARD_TYPE" = inserted."PG_TRANSACTION_CARD_TYPE", + "PG_TRANSACTION_DATE" = inserted."PG_TRANSACTION_DATE", + "PG_CVD_ID" = inserted."PG_CVD_ID", + "PG_PAYMENT_METHOD" = inserted."PG_PAYMENT_METHOD", + "PG_MESSAGE_ID" = inserted."PG_MESSAGE_ID", + "PG_MESSAGE_TEXT" = inserted."PG_MESSAGE_TEXT", + "APP_LAST_UPDATE_TIMESTAMP" = inserted."APP_LAST_UPDATE_TIMESTAMP", + "APP_LAST_UPDATE_USERID" = inserted."APP_LAST_UPDATE_USERID", + "APP_LAST_UPDATE_USER_GUID" = inserted."APP_LAST_UPDATE_USER_GUID", + "APP_LAST_UPDATE_USER_DIRECTORY" = inserted."APP_LAST_UPDATE_USER_DIRECTORY", + "CONCURRENCY_CONTROL_NUMBER" = inserted."CONCURRENCY_CONTROL_NUMBER" + , DB_LAST_UPDATE_TIMESTAMP = getutcdate() + , DB_LAST_UPDATE_USERID = user_name() + from [permit].[ORBC_TRANSACTION] + inner join inserted + on (ORBC_TRANSACTION.TRANSACTION_ID = inserted.TRANSACTION_ID); + +END TRY +BEGIN CATCH + IF @@trancount > 0 ROLLBACK TRANSACTION + EXEC orbc_error_handling +END CATCH; +GO +IF @@ERROR <> 0 SET NOEXEC ON +GO +DISABLE TRIGGER [permit].[ORBC_TXN_I_S_U_TR] ON permit.[ORBC_TRANSACTION] +GO + +IF @@ERROR <> 0 SET NOEXEC ON +GO +UPDATE t +SET RECEIPT_ID = r.RECEIPT_ID +FROM [permit].[ORBC_TRANSACTION] t +INNER JOIN [permit].[ORBC_RECEIPT] r ON t.TRANSACTION_ID = r.TRANSACTION_ID; + +IF @@ERROR <> 0 SET NOEXEC ON +GO +ALTER TABLE [permit].[ORBC_RECEIPT] DROP CONSTRAINT ORBC_RECEIPT_TRANSACTION_ID_FK + +IF @@ERROR <> 0 SET NOEXEC ON +GO +ALTER TABLE [permit].[ORBC_RECEIPT] DROP COLUMN [TRANSACTION_ID] + + +IF @@ERROR <> 0 SET NOEXEC ON +GO +AlTER TABLE [permit].[ORBC_RECEIPT_HIST] ALTER COLUMN [TRANSACTION_ID] [bigint] NULL + +IF @@ERROR <> 0 SET NOEXEC ON +GO +-- Alter trigger permit.ORBC_RCPT_A_S_IUD_TR +PRINT N'Alter trigger permit.ORBC_RCPT_A_S_IUD_TR' +GO +ALTER TRIGGER [permit].[ORBC_RCPT_A_S_IUD_TR] ON permit.[ORBC_RECEIPT] FOR INSERT, UPDATE, DELETE AS +SET NOCOUNT ON +BEGIN TRY +DECLARE @curr_date datetime; +SET @curr_date = getutcdate(); + IF NOT EXISTS(SELECT * FROM inserted) AND NOT EXISTS(SELECT * FROM deleted) + RETURN; + + -- historical + IF EXISTS(SELECT * FROM deleted) + update [permit].[ORBC_RECEIPT_HIST] set END_DATE_HIST = @curr_date where RECEIPT_ID in (select RECEIPT_ID from deleted) and END_DATE_HIST is null; + + IF EXISTS(SELECT * FROM inserted) + insert into [permit].[ORBC_RECEIPT_HIST] ([RECEIPT_ID], [RECEIPT_NUMBER], [RECEIPT_DOCUMENT_ID], [APP_CREATE_TIMESTAMP], [APP_CREATE_USERID], [APP_CREATE_USER_GUID], [APP_CREATE_USER_DIRECTORY], [APP_LAST_UPDATE_TIMESTAMP], [APP_LAST_UPDATE_USERID], [APP_LAST_UPDATE_USER_GUID], [APP_LAST_UPDATE_USER_DIRECTORY], [CONCURRENCY_CONTROL_NUMBER], [DB_CREATE_USERID], [DB_CREATE_TIMESTAMP], [DB_LAST_UPDATE_USERID], [DB_LAST_UPDATE_TIMESTAMP], _RECEIPT_HIST_ID, END_DATE_HIST, EFFECTIVE_DATE_HIST) + select [RECEIPT_ID], [RECEIPT_NUMBER], [RECEIPT_DOCUMENT_ID], [APP_CREATE_TIMESTAMP], [APP_CREATE_USERID], [APP_CREATE_USER_GUID], [APP_CREATE_USER_DIRECTORY], [APP_LAST_UPDATE_TIMESTAMP], [APP_LAST_UPDATE_USERID], [APP_LAST_UPDATE_USER_GUID], [APP_LAST_UPDATE_USER_DIRECTORY], [CONCURRENCY_CONTROL_NUMBER], [DB_CREATE_USERID], [DB_CREATE_TIMESTAMP], [DB_LAST_UPDATE_USERID], [DB_LAST_UPDATE_TIMESTAMP], (next value for [permit].[ORBC_RECEIPT_H_ID_SEQ]) as [_RECEIPT_HIST_ID], null as [END_DATE_HIST], @curr_date as [EFFECTIVE_DATE_HIST] from inserted; + +END TRY +BEGIN CATCH + IF @@trancount > 0 ROLLBACK TRANSACTION + EXEC orbc_error_handling +END CATCH; +GO + +IF @@ERROR <> 0 SET NOEXEC ON +GO + +-- Alter trigger permit.ORBC_RCPT_I_S_U_TR +PRINT N'Alter trigger permit.ORBC_RCPT_I_S_U_TR' +GO +ALTER TRIGGER [permit].[ORBC_RCPT_I_S_U_TR] ON permit.[ORBC_RECEIPT] INSTEAD OF UPDATE AS +SET NOCOUNT ON +BEGIN TRY + IF NOT EXISTS(SELECT * FROM deleted) + RETURN; + + -- validate concurrency control + if exists (select 1 from inserted, deleted where inserted.CONCURRENCY_CONTROL_NUMBER != deleted.CONCURRENCY_CONTROL_NUMBER+1 AND inserted.RECEIPT_ID = deleted.RECEIPT_ID) + raiserror('CONCURRENCY FAILURE.',16,1) + + + -- update statement + update [permit].[ORBC_RECEIPT] + set "RECEIPT_NUMBER" = inserted."RECEIPT_NUMBER", + "RECEIPT_DOCUMENT_ID" = inserted."RECEIPT_DOCUMENT_ID", + "APP_LAST_UPDATE_TIMESTAMP" = inserted."APP_LAST_UPDATE_TIMESTAMP", + "APP_LAST_UPDATE_USERID" = inserted."APP_LAST_UPDATE_USERID", + "APP_LAST_UPDATE_USER_GUID" = inserted."APP_LAST_UPDATE_USER_GUID", + "APP_LAST_UPDATE_USER_DIRECTORY" = inserted."APP_LAST_UPDATE_USER_DIRECTORY", + "CONCURRENCY_CONTROL_NUMBER" = inserted."CONCURRENCY_CONTROL_NUMBER" + , DB_LAST_UPDATE_TIMESTAMP = getutcdate() + , DB_LAST_UPDATE_USERID = user_name() + from [permit].[ORBC_RECEIPT] + inner join inserted + on (ORBC_RECEIPT.RECEIPT_ID = inserted.RECEIPT_ID); + +END TRY +BEGIN CATCH + IF @@trancount > 0 ROLLBACK TRANSACTION + EXEC orbc_error_handling +END CATCH; +GO +IF @@ERROR <> 0 SET NOEXEC ON +GO +DISABLE TRIGGER [permit].[ORBC_RCPT_I_S_U_TR] ON permit.[ORBC_RECEIPT] +GO + +IF @@ERROR <> 0 SET NOEXEC ON +GO + +DECLARE @VersionDescription VARCHAR(255) +SET @VersionDescription = 'Add RECEIPT_ID col to ORBC_TRANSACTION and Drop TRANSACTION_ID from ORBC_RECEIPT' + +INSERT [dbo].[ORBC_SYS_VERSION] ([VERSION_ID], [DESCRIPTION], [UPDATE_SCRIPT], [REVERT_SCRIPT], [RELEASE_DATE]) VALUES (51, @VersionDescription, '$(UPDATE_SCRIPT)', '$(REVERT_SCRIPT)', getutcdate()) +IF @@ERROR <> 0 SET NOEXEC ON +GO + +COMMIT TRANSACTION +GO +IF @@ERROR <> 0 SET NOEXEC ON +GO +DECLARE @Success AS BIT +SET @Success = 1 +SET NOEXEC OFF +IF (@Success = 1) PRINT 'The database update succeeded' +ELSE BEGIN + IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION + PRINT 'The database update failed' +END +GO + From 578e9ea0255943a9fc9321b1e30a97a112d20caf Mon Sep 17 00:00:00 2001 From: praju-aot Date: Wed, 4 Dec 2024 17:15:33 -0500 Subject: [PATCH 13/16] Code conflict review --- .../versions/revert/v_50_ddl_revert.sql | 230 ----------------- database/mssql/scripts/versions/v_50_ddl.sql | 238 ------------------ 2 files changed, 468 deletions(-) delete mode 100644 database/mssql/scripts/versions/revert/v_50_ddl_revert.sql delete mode 100644 database/mssql/scripts/versions/v_50_ddl.sql diff --git a/database/mssql/scripts/versions/revert/v_50_ddl_revert.sql b/database/mssql/scripts/versions/revert/v_50_ddl_revert.sql deleted file mode 100644 index 75fbcaa3d..000000000 --- a/database/mssql/scripts/versions/revert/v_50_ddl_revert.sql +++ /dev/null @@ -1,230 +0,0 @@ -SET ANSI_NULLS ON -GO -SET QUOTED_IDENTIFIER ON -GO -SET NOCOUNT ON -GO - -SET XACT_ABORT ON -GO -SET TRANSACTION ISOLATION LEVEL SERIALIZABLE -GO -BEGIN TRANSACTION -GO - - -IF @@ERROR <> 0 SET NOEXEC ON -GO -ALTER TABLE [permit].[ORBC_RECEIPT] ADD [TRANSACTION_ID] [bigint] NULL - - -IF @@ERROR <> 0 SET NOEXEC ON -GO -ALTER TABLE [permit].[ORBC_RECEIPT] WITH CHECK ADD CONSTRAINT [ORBC_RECEIPT_TRANSACTION_ID_FK] FOREIGN KEY([TRANSACTION_ID]) -REFERENCES [permit].[ORBC_TRANSACTION] ([TRANSACTION_ID]) - --- Alter trigger permit.ORBC_RCPT_A_S_IUD_TR -PRINT N'Alter trigger permit.ORBC_RCPT_A_S_IUD_TR' -GO -ALTER TRIGGER [permit].[ORBC_RCPT_A_S_IUD_TR] ON permit.[ORBC_RECEIPT] FOR INSERT, UPDATE, DELETE AS -SET NOCOUNT ON -BEGIN TRY -DECLARE @curr_date datetime; -SET @curr_date = getutcdate(); - IF NOT EXISTS(SELECT * FROM inserted) AND NOT EXISTS(SELECT * FROM deleted) - RETURN; - - -- historical - IF EXISTS(SELECT * FROM deleted) - update [permit].[ORBC_RECEIPT_HIST] set END_DATE_HIST = @curr_date where RECEIPT_ID in (select RECEIPT_ID from deleted) and END_DATE_HIST is null; - - IF EXISTS(SELECT * FROM inserted) - insert into [permit].[ORBC_RECEIPT_HIST] ([RECEIPT_ID], [RECEIPT_NUMBER], [TRANSACTION_ID], [RECEIPT_DOCUMENT_ID], [APP_CREATE_TIMESTAMP], [APP_CREATE_USERID], [APP_CREATE_USER_GUID], [APP_CREATE_USER_DIRECTORY], [APP_LAST_UPDATE_TIMESTAMP], [APP_LAST_UPDATE_USERID], [APP_LAST_UPDATE_USER_GUID], [APP_LAST_UPDATE_USER_DIRECTORY], [CONCURRENCY_CONTROL_NUMBER], [DB_CREATE_USERID], [DB_CREATE_TIMESTAMP], [DB_LAST_UPDATE_USERID], [DB_LAST_UPDATE_TIMESTAMP], _RECEIPT_HIST_ID, END_DATE_HIST, EFFECTIVE_DATE_HIST) - select [RECEIPT_ID], [RECEIPT_NUMBER], [TRANSACTION_ID], [RECEIPT_DOCUMENT_ID], [APP_CREATE_TIMESTAMP], [APP_CREATE_USERID], [APP_CREATE_USER_GUID], [APP_CREATE_USER_DIRECTORY], [APP_LAST_UPDATE_TIMESTAMP], [APP_LAST_UPDATE_USERID], [APP_LAST_UPDATE_USER_GUID], [APP_LAST_UPDATE_USER_DIRECTORY], [CONCURRENCY_CONTROL_NUMBER], [DB_CREATE_USERID], [DB_CREATE_TIMESTAMP], [DB_LAST_UPDATE_USERID], [DB_LAST_UPDATE_TIMESTAMP], (next value for [permit].[ORBC_RECEIPT_H_ID_SEQ]) as [_RECEIPT_HIST_ID], null as [END_DATE_HIST], @curr_date as [EFFECTIVE_DATE_HIST] from inserted; - -END TRY -BEGIN CATCH - IF @@trancount > 0 ROLLBACK TRANSACTION - EXEC orbc_error_handling -END CATCH; -GO - -IF @@ERROR <> 0 SET NOEXEC ON -GO - --- Alter trigger permit.ORBC_RCPT_I_S_U_TR -PRINT N'Alter trigger permit.ORBC_RCPT_I_S_U_TR' -GO -ALTER TRIGGER [permit].[ORBC_RCPT_I_S_U_TR] ON permit.[ORBC_RECEIPT] INSTEAD OF UPDATE AS -SET NOCOUNT ON -BEGIN TRY - IF NOT EXISTS(SELECT * FROM deleted) - RETURN; - - -- validate concurrency control - if exists (select 1 from inserted, deleted where inserted.CONCURRENCY_CONTROL_NUMBER != deleted.CONCURRENCY_CONTROL_NUMBER+1 AND inserted.RECEIPT_ID = deleted.RECEIPT_ID) - raiserror('CONCURRENCY FAILURE.',16,1) - - - -- update statement - update [permit].[ORBC_RECEIPT] - set "RECEIPT_NUMBER" = inserted."RECEIPT_NUMBER", - "TRANSACTION_ID" = inserted."TRANSACTION_ID", - "RECEIPT_DOCUMENT_ID" = inserted."RECEIPT_DOCUMENT_ID", - "APP_LAST_UPDATE_TIMESTAMP" = inserted."APP_LAST_UPDATE_TIMESTAMP", - "APP_LAST_UPDATE_USERID" = inserted."APP_LAST_UPDATE_USERID", - "APP_LAST_UPDATE_USER_GUID" = inserted."APP_LAST_UPDATE_USER_GUID", - "APP_LAST_UPDATE_USER_DIRECTORY" = inserted."APP_LAST_UPDATE_USER_DIRECTORY", - "CONCURRENCY_CONTROL_NUMBER" = inserted."CONCURRENCY_CONTROL_NUMBER" - , DB_LAST_UPDATE_TIMESTAMP = getutcdate() - , DB_LAST_UPDATE_USERID = user_name() - from [permit].[ORBC_RECEIPT] - inner join inserted - on (ORBC_RECEIPT.RECEIPT_ID = inserted.RECEIPT_ID); - -END TRY -BEGIN CATCH - IF @@trancount > 0 ROLLBACK TRANSACTION - EXEC orbc_error_handling -END CATCH; -GO -IF @@ERROR <> 0 SET NOEXEC ON -GO -DISABLE TRIGGER [permit].[ORBC_RCPT_I_S_U_TR] ON permit.[ORBC_RECEIPT] -GO - -IF @@ERROR <> 0 SET NOEXEC ON -GO -UPDATE r -SET TRANSACTION_ID = t.TRANSACTION_ID -FROM [permit].[ORBC_RECEIPT] r -INNER JOIN [permit].[ORBC_TRANSACTION] t ON t.RECEIPT_ID = r.RECEIPT_ID; - -IF @@ERROR <> 0 SET NOEXEC ON -GO -ALTER TABLE [permit].[ORBC_RECEIPT] ALTER COLUMN [TRANSACTION_ID] [bigint] NOT NULL - -IF @@ERROR <> 0 SET NOEXEC ON -GO -AlTER TABLE [permit].[ORBC_RECEIPT_HIST] ALTER COLUMN [TRANSACTION_ID] [bigint] NOT NULL - - -IF @@ERROR <> 0 SET NOEXEC ON -GO -ALTER TABLE [permit].[ORBC_TRANSACTION] DROP CONSTRAINT FK_ORBC_TRANSACTION_RECEIPT_ID - -IF @@ERROR <> 0 SET NOEXEC ON -GO -ALTER TABLE [permit].[ORBC_TRANSACTION] DROP COLUMN [RECEIPT_ID] - - -IF @@ERROR <> 0 SET NOEXEC ON -GO --- Alter trigger permit.ORBC_TXN_A_S_IUD_TR -PRINT N'Alter trigger permit.ORBC_TXN_A_S_IUD_TR' -GO -ALTER TRIGGER [permit].[ORBC_TXN_A_S_IUD_TR] ON permit.[ORBC_TRANSACTION] FOR INSERT, UPDATE, DELETE AS -SET NOCOUNT ON -BEGIN TRY -DECLARE @curr_date datetime; -SET @curr_date = getutcdate(); - IF NOT EXISTS(SELECT * FROM inserted) AND NOT EXISTS(SELECT * FROM deleted) - RETURN; - - -- historical - IF EXISTS(SELECT * FROM deleted) - update [permit].[ORBC_TRANSACTION_HIST] set END_DATE_HIST = @curr_date where TRANSACTION_ID in (select TRANSACTION_ID from deleted) and END_DATE_HIST is null; - - IF EXISTS(SELECT * FROM inserted) - insert into [permit].[ORBC_TRANSACTION_HIST] ([TRANSACTION_ID], [TRANSACTION_TYPE], [PAYMENT_METHOD_TYPE], [PAYMENT_CARD_TYPE], [TOTAL_TRANSACTION_AMOUNT], [TRANSACTION_SUBMIT_DATE], [TRANSACTION_ORDER_NUMBER], [PG_TRANSACTION_ID], [PG_TRANSACTION_APPROVED], [PG_AUTH_CODE], [PG_TRANSACTION_CARD_TYPE], [PG_TRANSACTION_DATE], [PG_CVD_ID], [PG_PAYMENT_METHOD], [PG_MESSAGE_ID], [PG_MESSAGE_TEXT], [APP_CREATE_TIMESTAMP], [APP_CREATE_USERID], [APP_CREATE_USER_GUID], [APP_CREATE_USER_DIRECTORY], [APP_LAST_UPDATE_TIMESTAMP], [APP_LAST_UPDATE_USERID], [APP_LAST_UPDATE_USER_GUID], [APP_LAST_UPDATE_USER_DIRECTORY], [CONCURRENCY_CONTROL_NUMBER], [DB_CREATE_USERID], [DB_CREATE_TIMESTAMP], [DB_LAST_UPDATE_USERID], [DB_LAST_UPDATE_TIMESTAMP], _TRANSACTION_HIST_ID, END_DATE_HIST, EFFECTIVE_DATE_HIST) - select [TRANSACTION_ID], [TRANSACTION_TYPE], [PAYMENT_METHOD_TYPE], [PAYMENT_CARD_TYPE], [TOTAL_TRANSACTION_AMOUNT], [TRANSACTION_SUBMIT_DATE], [TRANSACTION_ORDER_NUMBER], [PG_TRANSACTION_ID], [PG_TRANSACTION_APPROVED], [PG_AUTH_CODE], [PG_TRANSACTION_CARD_TYPE], [PG_TRANSACTION_DATE], [PG_CVD_ID], [PG_PAYMENT_METHOD], [PG_MESSAGE_ID], [PG_MESSAGE_TEXT], [APP_CREATE_TIMESTAMP], [APP_CREATE_USERID], [APP_CREATE_USER_GUID], [APP_CREATE_USER_DIRECTORY], [APP_LAST_UPDATE_TIMESTAMP], [APP_LAST_UPDATE_USERID], [APP_LAST_UPDATE_USER_GUID], [APP_LAST_UPDATE_USER_DIRECTORY], [CONCURRENCY_CONTROL_NUMBER], [DB_CREATE_USERID], [DB_CREATE_TIMESTAMP], [DB_LAST_UPDATE_USERID], [DB_LAST_UPDATE_TIMESTAMP], (next value for [permit].[ORBC_TRANSACTION_H_ID_SEQ]) as [_TRANSACTION_HIST_ID], null as [END_DATE_HIST], @curr_date as [EFFECTIVE_DATE_HIST] from inserted; - -END TRY -BEGIN CATCH - IF @@trancount > 0 ROLLBACK TRANSACTION - EXEC orbc_error_handling -END CATCH; -GO -IF @@ERROR <> 0 SET NOEXEC ON -GO - - --- Alter trigger permit.ORBC_TXN_I_S_U_TR -PRINT N'Alter trigger permit.ORBC_TXN_I_S_U_TR' -GO -ALTER TRIGGER [permit].[ORBC_TXN_I_S_U_TR] ON permit.[ORBC_TRANSACTION] INSTEAD OF UPDATE AS -SET NOCOUNT ON -BEGIN TRY - IF NOT EXISTS(SELECT * FROM deleted) - RETURN; - - -- validate concurrency control - if exists (select 1 from inserted, deleted where inserted.CONCURRENCY_CONTROL_NUMBER != deleted.CONCURRENCY_CONTROL_NUMBER+1 AND inserted.TRANSACTION_ID = deleted.TRANSACTION_ID) - raiserror('CONCURRENCY FAILURE.',16,1) - - - -- update statement - update [permit].[ORBC_TRANSACTION] - set "TRANSACTION_TYPE" = inserted."TRANSACTION_TYPE", - "PAYMENT_METHOD_TYPE" = inserted."PAYMENT_METHOD_TYPE", - "PAYMENT_CARD_TYPE" = inserted."PAYMENT_CARD_TYPE", - "TOTAL_TRANSACTION_AMOUNT" = inserted."TOTAL_TRANSACTION_AMOUNT", - "TRANSACTION_SUBMIT_DATE" = inserted."TRANSACTION_SUBMIT_DATE", - "TRANSACTION_ORDER_NUMBER" = inserted."TRANSACTION_ORDER_NUMBER", - "PG_TRANSACTION_ID" = inserted."PG_TRANSACTION_ID", - "PG_TRANSACTION_APPROVED" = inserted."PG_TRANSACTION_APPROVED", - "PG_AUTH_CODE" = inserted."PG_AUTH_CODE", - "PG_TRANSACTION_CARD_TYPE" = inserted."PG_TRANSACTION_CARD_TYPE", - "PG_TRANSACTION_DATE" = inserted."PG_TRANSACTION_DATE", - "PG_CVD_ID" = inserted."PG_CVD_ID", - "PG_PAYMENT_METHOD" = inserted."PG_PAYMENT_METHOD", - "PG_MESSAGE_ID" = inserted."PG_MESSAGE_ID", - "PG_MESSAGE_TEXT" = inserted."PG_MESSAGE_TEXT", - "APP_LAST_UPDATE_TIMESTAMP" = inserted."APP_LAST_UPDATE_TIMESTAMP", - "APP_LAST_UPDATE_USERID" = inserted."APP_LAST_UPDATE_USERID", - "APP_LAST_UPDATE_USER_GUID" = inserted."APP_LAST_UPDATE_USER_GUID", - "APP_LAST_UPDATE_USER_DIRECTORY" = inserted."APP_LAST_UPDATE_USER_DIRECTORY", - "CONCURRENCY_CONTROL_NUMBER" = inserted."CONCURRENCY_CONTROL_NUMBER" - , DB_LAST_UPDATE_TIMESTAMP = getutcdate() - , DB_LAST_UPDATE_USERID = user_name() - from [permit].[ORBC_TRANSACTION] - inner join inserted - on (ORBC_TRANSACTION.TRANSACTION_ID = inserted.TRANSACTION_ID); - -END TRY -BEGIN CATCH - IF @@trancount > 0 ROLLBACK TRANSACTION - EXEC orbc_error_handling -END CATCH; -GO -IF @@ERROR <> 0 SET NOEXEC ON -GO -DISABLE TRIGGER [permit].[ORBC_TXN_I_S_U_TR] ON permit.[ORBC_TRANSACTION] -GO - - -IF @@ERROR <> 0 SET NOEXEC ON -GO -ALTER TABLE [permit].[ORBC_TRANSACTION_HIST] DROP COLUMN [RECEIPT_ID] - -IF @@ERROR <> 0 SET NOEXEC ON -GO -DECLARE @VersionDescription VARCHAR(255) -SET @VersionDescription = 'Reverting addition of RECEIPT_ID col to ORBC_TRANSACTION and dropping TRANSACTION_ID from ORBC_RECEIPT.' - -INSERT [dbo].[ORBC_SYS_VERSION] ([VERSION_ID], [DESCRIPTION], [RELEASE_DATE]) VALUES (49, @VersionDescription, getutcdate()) - -IF @@ERROR <> 0 SET NOEXEC ON -GO -COMMIT TRANSACTION -IF @@ERROR <> 0 SET NOEXEC ON -GO -DECLARE @Success AS BIT -SET @Success = 1 -SET NOEXEC OFF -IF (@Success = 1) PRINT 'The database update succeeded' -ELSE BEGIN - IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION - PRINT 'The database update failed' -END -GO \ No newline at end of file diff --git a/database/mssql/scripts/versions/v_50_ddl.sql b/database/mssql/scripts/versions/v_50_ddl.sql deleted file mode 100644 index fd12e96bf..000000000 --- a/database/mssql/scripts/versions/v_50_ddl.sql +++ /dev/null @@ -1,238 +0,0 @@ -SET ANSI_NULLS ON -GO -SET QUOTED_IDENTIFIER ON -GO -SET NOCOUNT ON -GO - -SET XACT_ABORT ON -GO -SET TRANSACTION ISOLATION LEVEL SERIALIZABLE -GO -BEGIN TRANSACTION -GO - -IF @@ERROR <> 0 SET NOEXEC ON -GO -ALTER TABLE [permit].[ORBC_TRANSACTION] ADD [RECEIPT_ID] [bigint] NULL - -IF @@ERROR <> 0 SET NOEXEC ON -GO -EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Receipt Id of the transaction' , @level0type=N'SCHEMA',@level0name=N'permit', @level1type=N'TABLE',@level1name=N'ORBC_TRANSACTION', @level2type=N'COLUMN',@level2name=N'RECEIPT_ID' - -IF @@ERROR <> 0 SET NOEXEC ON -GO - -IF @@ERROR <> 0 SET NOEXEC ON -GO -ALTER TABLE [permit].[ORBC_TRANSACTION] WITH CHECK ADD CONSTRAINT [FK_ORBC_TRANSACTION_RECEIPT_ID] FOREIGN KEY([RECEIPT_ID]) -REFERENCES [permit].[ORBC_RECEIPT] ([RECEIPT_ID]) -GO - -ALTER TABLE [permit].[ORBC_TRANSACTION] CHECK CONSTRAINT [FK_ORBC_TRANSACTION_RECEIPT_ID] - - -IF @@ERROR <> 0 SET NOEXEC ON -GO -ALTER TABLE [permit].[ORBC_TRANSACTION_HIST] ADD [RECEIPT_ID] [bigint] NULL - -IF @@ERROR <> 0 SET NOEXEC ON -GO --- Alter trigger permit.ORBC_TXN_A_S_IUD_TR -PRINT N'Alter trigger permit.ORBC_TXN_A_S_IUD_TR' -GO -ALTER TRIGGER [permit].[ORBC_TXN_A_S_IUD_TR] ON permit.[ORBC_TRANSACTION] FOR INSERT, UPDATE, DELETE AS -SET NOCOUNT ON -BEGIN TRY -DECLARE @curr_date datetime; -SET @curr_date = getutcdate(); - IF NOT EXISTS(SELECT * FROM inserted) AND NOT EXISTS(SELECT * FROM deleted) - RETURN; - - -- historical - IF EXISTS(SELECT * FROM deleted) - update [permit].[ORBC_TRANSACTION_HIST] set END_DATE_HIST = @curr_date where TRANSACTION_ID in (select TRANSACTION_ID from deleted) and END_DATE_HIST is null; - - IF EXISTS(SELECT * FROM inserted) - insert into [permit].[ORBC_TRANSACTION_HIST] ([TRANSACTION_ID], [RECEIPT_ID], [TRANSACTION_TYPE], [PAYMENT_METHOD_TYPE], [PAYMENT_CARD_TYPE], [TOTAL_TRANSACTION_AMOUNT], [TRANSACTION_SUBMIT_DATE], [TRANSACTION_ORDER_NUMBER], [PG_TRANSACTION_ID], [PG_TRANSACTION_APPROVED], [PG_AUTH_CODE], [PG_TRANSACTION_CARD_TYPE], [PG_TRANSACTION_DATE], [PG_CVD_ID], [PG_PAYMENT_METHOD], [PG_MESSAGE_ID], [PG_MESSAGE_TEXT], [APP_CREATE_TIMESTAMP], [APP_CREATE_USERID], [APP_CREATE_USER_GUID], [APP_CREATE_USER_DIRECTORY], [APP_LAST_UPDATE_TIMESTAMP], [APP_LAST_UPDATE_USERID], [APP_LAST_UPDATE_USER_GUID], [APP_LAST_UPDATE_USER_DIRECTORY], [CONCURRENCY_CONTROL_NUMBER], [DB_CREATE_USERID], [DB_CREATE_TIMESTAMP], [DB_LAST_UPDATE_USERID], [DB_LAST_UPDATE_TIMESTAMP], _TRANSACTION_HIST_ID, END_DATE_HIST, EFFECTIVE_DATE_HIST) - select [TRANSACTION_ID], [RECEIPT_ID], [TRANSACTION_TYPE], [PAYMENT_METHOD_TYPE], [PAYMENT_CARD_TYPE], [TOTAL_TRANSACTION_AMOUNT], [TRANSACTION_SUBMIT_DATE], [TRANSACTION_ORDER_NUMBER], [PG_TRANSACTION_ID], [PG_TRANSACTION_APPROVED], [PG_AUTH_CODE], [PG_TRANSACTION_CARD_TYPE], [PG_TRANSACTION_DATE], [PG_CVD_ID], [PG_PAYMENT_METHOD], [PG_MESSAGE_ID], [PG_MESSAGE_TEXT], [APP_CREATE_TIMESTAMP], [APP_CREATE_USERID], [APP_CREATE_USER_GUID], [APP_CREATE_USER_DIRECTORY], [APP_LAST_UPDATE_TIMESTAMP], [APP_LAST_UPDATE_USERID], [APP_LAST_UPDATE_USER_GUID], [APP_LAST_UPDATE_USER_DIRECTORY], [CONCURRENCY_CONTROL_NUMBER], [DB_CREATE_USERID], [DB_CREATE_TIMESTAMP], [DB_LAST_UPDATE_USERID], [DB_LAST_UPDATE_TIMESTAMP], (next value for [permit].[ORBC_TRANSACTION_H_ID_SEQ]) as [_TRANSACTION_HIST_ID], null as [END_DATE_HIST], @curr_date as [EFFECTIVE_DATE_HIST] from inserted; - -END TRY -BEGIN CATCH - IF @@trancount > 0 ROLLBACK TRANSACTION - EXEC orbc_error_handling -END CATCH; -GO -IF @@ERROR <> 0 SET NOEXEC ON -GO - - --- Alter trigger permit.ORBC_TXN_I_S_U_TR -PRINT N'Alter trigger permit.ORBC_TXN_I_S_U_TR' -GO -ALTER TRIGGER [permit].[ORBC_TXN_I_S_U_TR] ON permit.[ORBC_TRANSACTION] INSTEAD OF UPDATE AS -SET NOCOUNT ON -BEGIN TRY - IF NOT EXISTS(SELECT * FROM deleted) - RETURN; - - -- validate concurrency control - if exists (select 1 from inserted, deleted where inserted.CONCURRENCY_CONTROL_NUMBER != deleted.CONCURRENCY_CONTROL_NUMBER+1 AND inserted.TRANSACTION_ID = deleted.TRANSACTION_ID) - raiserror('CONCURRENCY FAILURE.',16,1) - - - -- update statement - update [permit].[ORBC_TRANSACTION] - set "TRANSACTION_TYPE" = inserted."TRANSACTION_TYPE", - "RECEIPT_ID" = inserted."RECEIPT_ID", - "PAYMENT_METHOD_TYPE" = inserted."PAYMENT_METHOD_TYPE", - "PAYMENT_CARD_TYPE" = inserted."PAYMENT_CARD_TYPE", - "TOTAL_TRANSACTION_AMOUNT" = inserted."TOTAL_TRANSACTION_AMOUNT", - "TRANSACTION_SUBMIT_DATE" = inserted."TRANSACTION_SUBMIT_DATE", - "TRANSACTION_ORDER_NUMBER" = inserted."TRANSACTION_ORDER_NUMBER", - "PG_TRANSACTION_ID" = inserted."PG_TRANSACTION_ID", - "PG_TRANSACTION_APPROVED" = inserted."PG_TRANSACTION_APPROVED", - "PG_AUTH_CODE" = inserted."PG_AUTH_CODE", - "PG_TRANSACTION_CARD_TYPE" = inserted."PG_TRANSACTION_CARD_TYPE", - "PG_TRANSACTION_DATE" = inserted."PG_TRANSACTION_DATE", - "PG_CVD_ID" = inserted."PG_CVD_ID", - "PG_PAYMENT_METHOD" = inserted."PG_PAYMENT_METHOD", - "PG_MESSAGE_ID" = inserted."PG_MESSAGE_ID", - "PG_MESSAGE_TEXT" = inserted."PG_MESSAGE_TEXT", - "APP_LAST_UPDATE_TIMESTAMP" = inserted."APP_LAST_UPDATE_TIMESTAMP", - "APP_LAST_UPDATE_USERID" = inserted."APP_LAST_UPDATE_USERID", - "APP_LAST_UPDATE_USER_GUID" = inserted."APP_LAST_UPDATE_USER_GUID", - "APP_LAST_UPDATE_USER_DIRECTORY" = inserted."APP_LAST_UPDATE_USER_DIRECTORY", - "CONCURRENCY_CONTROL_NUMBER" = inserted."CONCURRENCY_CONTROL_NUMBER" - , DB_LAST_UPDATE_TIMESTAMP = getutcdate() - , DB_LAST_UPDATE_USERID = user_name() - from [permit].[ORBC_TRANSACTION] - inner join inserted - on (ORBC_TRANSACTION.TRANSACTION_ID = inserted.TRANSACTION_ID); - -END TRY -BEGIN CATCH - IF @@trancount > 0 ROLLBACK TRANSACTION - EXEC orbc_error_handling -END CATCH; -GO -IF @@ERROR <> 0 SET NOEXEC ON -GO -DISABLE TRIGGER [permit].[ORBC_TXN_I_S_U_TR] ON permit.[ORBC_TRANSACTION] -GO - -IF @@ERROR <> 0 SET NOEXEC ON -GO -UPDATE t -SET RECEIPT_ID = r.RECEIPT_ID -FROM [permit].[ORBC_TRANSACTION] t -INNER JOIN [permit].[ORBC_RECEIPT] r ON t.TRANSACTION_ID = r.TRANSACTION_ID; - -IF @@ERROR <> 0 SET NOEXEC ON -GO -ALTER TABLE [permit].[ORBC_RECEIPT] DROP CONSTRAINT ORBC_RECEIPT_TRANSACTION_ID_FK - -IF @@ERROR <> 0 SET NOEXEC ON -GO -ALTER TABLE [permit].[ORBC_RECEIPT] DROP COLUMN [TRANSACTION_ID] - - -IF @@ERROR <> 0 SET NOEXEC ON -GO -AlTER TABLE [permit].[ORBC_RECEIPT_HIST] ALTER COLUMN [TRANSACTION_ID] [bigint] NULL - -IF @@ERROR <> 0 SET NOEXEC ON -GO --- Alter trigger permit.ORBC_RCPT_A_S_IUD_TR -PRINT N'Alter trigger permit.ORBC_RCPT_A_S_IUD_TR' -GO -ALTER TRIGGER [permit].[ORBC_RCPT_A_S_IUD_TR] ON permit.[ORBC_RECEIPT] FOR INSERT, UPDATE, DELETE AS -SET NOCOUNT ON -BEGIN TRY -DECLARE @curr_date datetime; -SET @curr_date = getutcdate(); - IF NOT EXISTS(SELECT * FROM inserted) AND NOT EXISTS(SELECT * FROM deleted) - RETURN; - - -- historical - IF EXISTS(SELECT * FROM deleted) - update [permit].[ORBC_RECEIPT_HIST] set END_DATE_HIST = @curr_date where RECEIPT_ID in (select RECEIPT_ID from deleted) and END_DATE_HIST is null; - - IF EXISTS(SELECT * FROM inserted) - insert into [permit].[ORBC_RECEIPT_HIST] ([RECEIPT_ID], [RECEIPT_NUMBER], [RECEIPT_DOCUMENT_ID], [APP_CREATE_TIMESTAMP], [APP_CREATE_USERID], [APP_CREATE_USER_GUID], [APP_CREATE_USER_DIRECTORY], [APP_LAST_UPDATE_TIMESTAMP], [APP_LAST_UPDATE_USERID], [APP_LAST_UPDATE_USER_GUID], [APP_LAST_UPDATE_USER_DIRECTORY], [CONCURRENCY_CONTROL_NUMBER], [DB_CREATE_USERID], [DB_CREATE_TIMESTAMP], [DB_LAST_UPDATE_USERID], [DB_LAST_UPDATE_TIMESTAMP], _RECEIPT_HIST_ID, END_DATE_HIST, EFFECTIVE_DATE_HIST) - select [RECEIPT_ID], [RECEIPT_NUMBER], [RECEIPT_DOCUMENT_ID], [APP_CREATE_TIMESTAMP], [APP_CREATE_USERID], [APP_CREATE_USER_GUID], [APP_CREATE_USER_DIRECTORY], [APP_LAST_UPDATE_TIMESTAMP], [APP_LAST_UPDATE_USERID], [APP_LAST_UPDATE_USER_GUID], [APP_LAST_UPDATE_USER_DIRECTORY], [CONCURRENCY_CONTROL_NUMBER], [DB_CREATE_USERID], [DB_CREATE_TIMESTAMP], [DB_LAST_UPDATE_USERID], [DB_LAST_UPDATE_TIMESTAMP], (next value for [permit].[ORBC_RECEIPT_H_ID_SEQ]) as [_RECEIPT_HIST_ID], null as [END_DATE_HIST], @curr_date as [EFFECTIVE_DATE_HIST] from inserted; - -END TRY -BEGIN CATCH - IF @@trancount > 0 ROLLBACK TRANSACTION - EXEC orbc_error_handling -END CATCH; -GO - -IF @@ERROR <> 0 SET NOEXEC ON -GO - --- Alter trigger permit.ORBC_RCPT_I_S_U_TR -PRINT N'Alter trigger permit.ORBC_RCPT_I_S_U_TR' -GO -ALTER TRIGGER [permit].[ORBC_RCPT_I_S_U_TR] ON permit.[ORBC_RECEIPT] INSTEAD OF UPDATE AS -SET NOCOUNT ON -BEGIN TRY - IF NOT EXISTS(SELECT * FROM deleted) - RETURN; - - -- validate concurrency control - if exists (select 1 from inserted, deleted where inserted.CONCURRENCY_CONTROL_NUMBER != deleted.CONCURRENCY_CONTROL_NUMBER+1 AND inserted.RECEIPT_ID = deleted.RECEIPT_ID) - raiserror('CONCURRENCY FAILURE.',16,1) - - - -- update statement - update [permit].[ORBC_RECEIPT] - set "RECEIPT_NUMBER" = inserted."RECEIPT_NUMBER", - "RECEIPT_DOCUMENT_ID" = inserted."RECEIPT_DOCUMENT_ID", - "APP_LAST_UPDATE_TIMESTAMP" = inserted."APP_LAST_UPDATE_TIMESTAMP", - "APP_LAST_UPDATE_USERID" = inserted."APP_LAST_UPDATE_USERID", - "APP_LAST_UPDATE_USER_GUID" = inserted."APP_LAST_UPDATE_USER_GUID", - "APP_LAST_UPDATE_USER_DIRECTORY" = inserted."APP_LAST_UPDATE_USER_DIRECTORY", - "CONCURRENCY_CONTROL_NUMBER" = inserted."CONCURRENCY_CONTROL_NUMBER" - , DB_LAST_UPDATE_TIMESTAMP = getutcdate() - , DB_LAST_UPDATE_USERID = user_name() - from [permit].[ORBC_RECEIPT] - inner join inserted - on (ORBC_RECEIPT.RECEIPT_ID = inserted.RECEIPT_ID); - -END TRY -BEGIN CATCH - IF @@trancount > 0 ROLLBACK TRANSACTION - EXEC orbc_error_handling -END CATCH; -GO -IF @@ERROR <> 0 SET NOEXEC ON -GO -DISABLE TRIGGER [permit].[ORBC_RCPT_I_S_U_TR] ON permit.[ORBC_RECEIPT] -GO - -IF @@ERROR <> 0 SET NOEXEC ON -GO - -DECLARE @VersionDescription VARCHAR(255) -SET @VersionDescription = 'Add RECEIPT_ID col to ORBC_TRANSACTION and Drop TRANSACTION_ID from ORBC_RECEIPT' - -INSERT [dbo].[ORBC_SYS_VERSION] ([VERSION_ID], [DESCRIPTION], [UPDATE_SCRIPT], [REVERT_SCRIPT], [RELEASE_DATE]) VALUES (50, @VersionDescription, '$(UPDATE_SCRIPT)', '$(REVERT_SCRIPT)', getutcdate()) -IF @@ERROR <> 0 SET NOEXEC ON -GO - -COMMIT TRANSACTION -GO -IF @@ERROR <> 0 SET NOEXEC ON -GO -DECLARE @Success AS BIT -SET @Success = 1 -SET NOEXEC OFF -IF (@Success = 1) PRINT 'The database update succeeded' -ELSE BEGIN - IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION - PRINT 'The database update failed' -END -GO - From 78d73f59ae9cb7942cbb1a94e34e21e642ea334e Mon Sep 17 00:00:00 2001 From: praju-aot Date: Thu, 5 Dec 2024 14:08:29 -0500 Subject: [PATCH 14/16] Code merge conflicts --- .../mssql/scripts/versions/revert/v_51_ddl_revert.sql | 9 ++++++++- database/mssql/scripts/versions/v_51_ddl.sql | 8 ++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/database/mssql/scripts/versions/revert/v_51_ddl_revert.sql b/database/mssql/scripts/versions/revert/v_51_ddl_revert.sql index 66f313b30..d5e0f4b2f 100644 --- a/database/mssql/scripts/versions/revert/v_51_ddl_revert.sql +++ b/database/mssql/scripts/versions/revert/v_51_ddl_revert.sql @@ -17,7 +17,6 @@ IF @@ERROR <> 0 SET NOEXEC ON GO ALTER TABLE [permit].[ORBC_RECEIPT] ADD [TRANSACTION_ID] [bigint] NULL - IF @@ERROR <> 0 SET NOEXEC ON GO ALTER TABLE [permit].[ORBC_RECEIPT] WITH CHECK ADD CONSTRAINT [ORBC_RECEIPT_TRANSACTION_ID_FK] FOREIGN KEY([TRANSACTION_ID]) @@ -113,10 +112,18 @@ IF @@ERROR <> 0 SET NOEXEC ON GO ALTER TABLE [permit].[ORBC_TRANSACTION] DROP CONSTRAINT FK_ORBC_TRANSACTION_RECEIPT_ID +IF @@ERROR <> 0 SET NOEXEC ON +GO +DROP INDEX IX_ORBC_TRANSACTION_RECEIPT_ID_FK ON [permit].[ORBC_TRANSACTION]; + IF @@ERROR <> 0 SET NOEXEC ON GO ALTER TABLE [permit].[ORBC_TRANSACTION] DROP COLUMN [RECEIPT_ID] +IF @@ERROR <> 0 SET NOEXEC ON +GO +CREATE NONCLUSTERED INDEX IX_ORBC_RECEIPT_TRANSACTION_ID_FK ON [permit].[ORBC_RECEIPT] ([TRANSACTION_ID]); + IF @@ERROR <> 0 SET NOEXEC ON GO diff --git a/database/mssql/scripts/versions/v_51_ddl.sql b/database/mssql/scripts/versions/v_51_ddl.sql index ddfb10478..b44fa4f44 100644 --- a/database/mssql/scripts/versions/v_51_ddl.sql +++ b/database/mssql/scripts/versions/v_51_ddl.sql @@ -16,6 +16,10 @@ IF @@ERROR <> 0 SET NOEXEC ON GO ALTER TABLE [permit].[ORBC_TRANSACTION] ADD [RECEIPT_ID] [bigint] NULL +IF @@ERROR <> 0 SET NOEXEC ON +GO +CREATE NONCLUSTERED INDEX IX_ORBC_TRANSACTION_RECEIPT_ID_FK ON [permit].[ORBC_TRANSACTION] ([RECEIPT_ID]); + IF @@ERROR <> 0 SET NOEXEC ON GO EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Receipt Id of the transaction' , @level0type=N'SCHEMA',@level0name=N'permit', @level1type=N'TABLE',@level1name=N'ORBC_TRANSACTION', @level2type=N'COLUMN',@level2name=N'RECEIPT_ID' @@ -132,6 +136,10 @@ IF @@ERROR <> 0 SET NOEXEC ON GO ALTER TABLE [permit].[ORBC_RECEIPT] DROP CONSTRAINT ORBC_RECEIPT_TRANSACTION_ID_FK +IF @@ERROR <> 0 SET NOEXEC ON +GO + DROP INDEX IX_ORBC_RECEIPT_TRANSACTION_ID_FK ON [permit].[ORBC_RECEIPT]; + IF @@ERROR <> 0 SET NOEXEC ON GO ALTER TABLE [permit].[ORBC_RECEIPT] DROP COLUMN [TRANSACTION_ID] From 64ee0efb88aa5eeae38892d96531fd5ecbc8dc15 Mon Sep 17 00:00:00 2001 From: praju-aot Date: Tue, 17 Dec 2024 15:14:49 -0500 Subject: [PATCH 15/16] Resolve code conflicts with main --- .../revert/{v_51_ddl_revert.sql => v_52_ddl_revert.sql} | 2 +- database/mssql/scripts/versions/{v_51_ddl.sql => v_52_ddl.sql} | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename database/mssql/scripts/versions/revert/{v_51_ddl_revert.sql => v_52_ddl_revert.sql} (99%) rename database/mssql/scripts/versions/{v_51_ddl.sql => v_52_ddl.sql} (99%) diff --git a/database/mssql/scripts/versions/revert/v_51_ddl_revert.sql b/database/mssql/scripts/versions/revert/v_52_ddl_revert.sql similarity index 99% rename from database/mssql/scripts/versions/revert/v_51_ddl_revert.sql rename to database/mssql/scripts/versions/revert/v_52_ddl_revert.sql index d5e0f4b2f..b1bbc01e2 100644 --- a/database/mssql/scripts/versions/revert/v_51_ddl_revert.sql +++ b/database/mssql/scripts/versions/revert/v_52_ddl_revert.sql @@ -219,7 +219,7 @@ GO DECLARE @VersionDescription VARCHAR(255) SET @VersionDescription = 'Reverting addition of RECEIPT_ID col to ORBC_TRANSACTION and dropping TRANSACTION_ID from ORBC_RECEIPT.' -INSERT [dbo].[ORBC_SYS_VERSION] ([VERSION_ID], [DESCRIPTION], [RELEASE_DATE]) VALUES (50, @VersionDescription, getutcdate()) +INSERT [dbo].[ORBC_SYS_VERSION] ([VERSION_ID], [DESCRIPTION], [RELEASE_DATE]) VALUES (51, @VersionDescription, getutcdate()) IF @@ERROR <> 0 SET NOEXEC ON GO diff --git a/database/mssql/scripts/versions/v_51_ddl.sql b/database/mssql/scripts/versions/v_52_ddl.sql similarity index 99% rename from database/mssql/scripts/versions/v_51_ddl.sql rename to database/mssql/scripts/versions/v_52_ddl.sql index b44fa4f44..8ed0e6729 100644 --- a/database/mssql/scripts/versions/v_51_ddl.sql +++ b/database/mssql/scripts/versions/v_52_ddl.sql @@ -226,7 +226,7 @@ GO DECLARE @VersionDescription VARCHAR(255) SET @VersionDescription = 'Add RECEIPT_ID col to ORBC_TRANSACTION and Drop TRANSACTION_ID from ORBC_RECEIPT' -INSERT [dbo].[ORBC_SYS_VERSION] ([VERSION_ID], [DESCRIPTION], [UPDATE_SCRIPT], [REVERT_SCRIPT], [RELEASE_DATE]) VALUES (51, @VersionDescription, '$(UPDATE_SCRIPT)', '$(REVERT_SCRIPT)', getutcdate()) +INSERT [dbo].[ORBC_SYS_VERSION] ([VERSION_ID], [DESCRIPTION], [UPDATE_SCRIPT], [REVERT_SCRIPT], [RELEASE_DATE]) VALUES (52, @VersionDescription, '$(UPDATE_SCRIPT)', '$(REVERT_SCRIPT)', getutcdate()) IF @@ERROR <> 0 SET NOEXEC ON GO From d1feb4dc9c78d526eb2b99d6d5a0f64369aa0067 Mon Sep 17 00:00:00 2001 From: praju-aot Date: Wed, 8 Jan 2025 18:12:50 -0500 Subject: [PATCH 16/16] Resolve code conflict --- .../versions/revert/v_52_ddl_revert.sql | 237 ----------------- database/mssql/scripts/versions/v_52_ddl.sql | 246 ------------------ 2 files changed, 483 deletions(-) delete mode 100644 database/mssql/scripts/versions/revert/v_52_ddl_revert.sql delete mode 100644 database/mssql/scripts/versions/v_52_ddl.sql diff --git a/database/mssql/scripts/versions/revert/v_52_ddl_revert.sql b/database/mssql/scripts/versions/revert/v_52_ddl_revert.sql deleted file mode 100644 index b1bbc01e2..000000000 --- a/database/mssql/scripts/versions/revert/v_52_ddl_revert.sql +++ /dev/null @@ -1,237 +0,0 @@ -SET ANSI_NULLS ON -GO -SET QUOTED_IDENTIFIER ON -GO -SET NOCOUNT ON -GO - -SET XACT_ABORT ON -GO -SET TRANSACTION ISOLATION LEVEL SERIALIZABLE -GO -BEGIN TRANSACTION -GO - - -IF @@ERROR <> 0 SET NOEXEC ON -GO -ALTER TABLE [permit].[ORBC_RECEIPT] ADD [TRANSACTION_ID] [bigint] NULL - -IF @@ERROR <> 0 SET NOEXEC ON -GO -ALTER TABLE [permit].[ORBC_RECEIPT] WITH CHECK ADD CONSTRAINT [ORBC_RECEIPT_TRANSACTION_ID_FK] FOREIGN KEY([TRANSACTION_ID]) -REFERENCES [permit].[ORBC_TRANSACTION] ([TRANSACTION_ID]) - --- Alter trigger permit.ORBC_RCPT_A_S_IUD_TR -PRINT N'Alter trigger permit.ORBC_RCPT_A_S_IUD_TR' -GO -ALTER TRIGGER [permit].[ORBC_RCPT_A_S_IUD_TR] ON permit.[ORBC_RECEIPT] FOR INSERT, UPDATE, DELETE AS -SET NOCOUNT ON -BEGIN TRY -DECLARE @curr_date datetime; -SET @curr_date = getutcdate(); - IF NOT EXISTS(SELECT * FROM inserted) AND NOT EXISTS(SELECT * FROM deleted) - RETURN; - - -- historical - IF EXISTS(SELECT * FROM deleted) - update [permit].[ORBC_RECEIPT_HIST] set END_DATE_HIST = @curr_date where RECEIPT_ID in (select RECEIPT_ID from deleted) and END_DATE_HIST is null; - - IF EXISTS(SELECT * FROM inserted) - insert into [permit].[ORBC_RECEIPT_HIST] ([RECEIPT_ID], [RECEIPT_NUMBER], [TRANSACTION_ID], [RECEIPT_DOCUMENT_ID], [APP_CREATE_TIMESTAMP], [APP_CREATE_USERID], [APP_CREATE_USER_GUID], [APP_CREATE_USER_DIRECTORY], [APP_LAST_UPDATE_TIMESTAMP], [APP_LAST_UPDATE_USERID], [APP_LAST_UPDATE_USER_GUID], [APP_LAST_UPDATE_USER_DIRECTORY], [CONCURRENCY_CONTROL_NUMBER], [DB_CREATE_USERID], [DB_CREATE_TIMESTAMP], [DB_LAST_UPDATE_USERID], [DB_LAST_UPDATE_TIMESTAMP], _RECEIPT_HIST_ID, END_DATE_HIST, EFFECTIVE_DATE_HIST) - select [RECEIPT_ID], [RECEIPT_NUMBER], [TRANSACTION_ID], [RECEIPT_DOCUMENT_ID], [APP_CREATE_TIMESTAMP], [APP_CREATE_USERID], [APP_CREATE_USER_GUID], [APP_CREATE_USER_DIRECTORY], [APP_LAST_UPDATE_TIMESTAMP], [APP_LAST_UPDATE_USERID], [APP_LAST_UPDATE_USER_GUID], [APP_LAST_UPDATE_USER_DIRECTORY], [CONCURRENCY_CONTROL_NUMBER], [DB_CREATE_USERID], [DB_CREATE_TIMESTAMP], [DB_LAST_UPDATE_USERID], [DB_LAST_UPDATE_TIMESTAMP], (next value for [permit].[ORBC_RECEIPT_H_ID_SEQ]) as [_RECEIPT_HIST_ID], null as [END_DATE_HIST], @curr_date as [EFFECTIVE_DATE_HIST] from inserted; - -END TRY -BEGIN CATCH - IF @@trancount > 0 ROLLBACK TRANSACTION - EXEC orbc_error_handling -END CATCH; -GO - -IF @@ERROR <> 0 SET NOEXEC ON -GO - --- Alter trigger permit.ORBC_RCPT_I_S_U_TR -PRINT N'Alter trigger permit.ORBC_RCPT_I_S_U_TR' -GO -ALTER TRIGGER [permit].[ORBC_RCPT_I_S_U_TR] ON permit.[ORBC_RECEIPT] INSTEAD OF UPDATE AS -SET NOCOUNT ON -BEGIN TRY - IF NOT EXISTS(SELECT * FROM deleted) - RETURN; - - -- validate concurrency control - if exists (select 1 from inserted, deleted where inserted.CONCURRENCY_CONTROL_NUMBER != deleted.CONCURRENCY_CONTROL_NUMBER+1 AND inserted.RECEIPT_ID = deleted.RECEIPT_ID) - raiserror('CONCURRENCY FAILURE.',16,1) - - - -- update statement - update [permit].[ORBC_RECEIPT] - set "RECEIPT_NUMBER" = inserted."RECEIPT_NUMBER", - "TRANSACTION_ID" = inserted."TRANSACTION_ID", - "RECEIPT_DOCUMENT_ID" = inserted."RECEIPT_DOCUMENT_ID", - "APP_LAST_UPDATE_TIMESTAMP" = inserted."APP_LAST_UPDATE_TIMESTAMP", - "APP_LAST_UPDATE_USERID" = inserted."APP_LAST_UPDATE_USERID", - "APP_LAST_UPDATE_USER_GUID" = inserted."APP_LAST_UPDATE_USER_GUID", - "APP_LAST_UPDATE_USER_DIRECTORY" = inserted."APP_LAST_UPDATE_USER_DIRECTORY", - "CONCURRENCY_CONTROL_NUMBER" = inserted."CONCURRENCY_CONTROL_NUMBER" - , DB_LAST_UPDATE_TIMESTAMP = getutcdate() - , DB_LAST_UPDATE_USERID = user_name() - from [permit].[ORBC_RECEIPT] - inner join inserted - on (ORBC_RECEIPT.RECEIPT_ID = inserted.RECEIPT_ID); - -END TRY -BEGIN CATCH - IF @@trancount > 0 ROLLBACK TRANSACTION - EXEC orbc_error_handling -END CATCH; -GO -IF @@ERROR <> 0 SET NOEXEC ON -GO -DISABLE TRIGGER [permit].[ORBC_RCPT_I_S_U_TR] ON permit.[ORBC_RECEIPT] -GO - -IF @@ERROR <> 0 SET NOEXEC ON -GO -UPDATE r -SET TRANSACTION_ID = t.TRANSACTION_ID -FROM [permit].[ORBC_RECEIPT] r -INNER JOIN [permit].[ORBC_TRANSACTION] t ON t.RECEIPT_ID = r.RECEIPT_ID; - -IF @@ERROR <> 0 SET NOEXEC ON -GO -ALTER TABLE [permit].[ORBC_RECEIPT] ALTER COLUMN [TRANSACTION_ID] [bigint] NOT NULL - -IF @@ERROR <> 0 SET NOEXEC ON -GO -AlTER TABLE [permit].[ORBC_RECEIPT_HIST] ALTER COLUMN [TRANSACTION_ID] [bigint] NOT NULL - - -IF @@ERROR <> 0 SET NOEXEC ON -GO -ALTER TABLE [permit].[ORBC_TRANSACTION] DROP CONSTRAINT FK_ORBC_TRANSACTION_RECEIPT_ID - -IF @@ERROR <> 0 SET NOEXEC ON -GO -DROP INDEX IX_ORBC_TRANSACTION_RECEIPT_ID_FK ON [permit].[ORBC_TRANSACTION]; - -IF @@ERROR <> 0 SET NOEXEC ON -GO -ALTER TABLE [permit].[ORBC_TRANSACTION] DROP COLUMN [RECEIPT_ID] - -IF @@ERROR <> 0 SET NOEXEC ON -GO -CREATE NONCLUSTERED INDEX IX_ORBC_RECEIPT_TRANSACTION_ID_FK ON [permit].[ORBC_RECEIPT] ([TRANSACTION_ID]); - - -IF @@ERROR <> 0 SET NOEXEC ON -GO --- Alter trigger permit.ORBC_TXN_A_S_IUD_TR -PRINT N'Alter trigger permit.ORBC_TXN_A_S_IUD_TR' -GO -ALTER TRIGGER [permit].[ORBC_TXN_A_S_IUD_TR] ON permit.[ORBC_TRANSACTION] FOR INSERT, UPDATE, DELETE AS -SET NOCOUNT ON -BEGIN TRY -DECLARE @curr_date datetime; -SET @curr_date = getutcdate(); - IF NOT EXISTS(SELECT * FROM inserted) AND NOT EXISTS(SELECT * FROM deleted) - RETURN; - - -- historical - IF EXISTS(SELECT * FROM deleted) - update [permit].[ORBC_TRANSACTION_HIST] set END_DATE_HIST = @curr_date where TRANSACTION_ID in (select TRANSACTION_ID from deleted) and END_DATE_HIST is null; - - IF EXISTS(SELECT * FROM inserted) - insert into [permit].[ORBC_TRANSACTION_HIST] ([TRANSACTION_ID], [TRANSACTION_TYPE], [PAYMENT_METHOD_TYPE], [PAYMENT_CARD_TYPE], [TOTAL_TRANSACTION_AMOUNT], [TRANSACTION_SUBMIT_DATE], [TRANSACTION_ORDER_NUMBER], [PG_TRANSACTION_ID], [PG_TRANSACTION_APPROVED], [PG_AUTH_CODE], [PG_TRANSACTION_CARD_TYPE], [PG_TRANSACTION_DATE], [PG_CVD_ID], [PG_PAYMENT_METHOD], [PG_MESSAGE_ID], [PG_MESSAGE_TEXT], [APP_CREATE_TIMESTAMP], [APP_CREATE_USERID], [APP_CREATE_USER_GUID], [APP_CREATE_USER_DIRECTORY], [APP_LAST_UPDATE_TIMESTAMP], [APP_LAST_UPDATE_USERID], [APP_LAST_UPDATE_USER_GUID], [APP_LAST_UPDATE_USER_DIRECTORY], [CONCURRENCY_CONTROL_NUMBER], [DB_CREATE_USERID], [DB_CREATE_TIMESTAMP], [DB_LAST_UPDATE_USERID], [DB_LAST_UPDATE_TIMESTAMP], _TRANSACTION_HIST_ID, END_DATE_HIST, EFFECTIVE_DATE_HIST) - select [TRANSACTION_ID], [TRANSACTION_TYPE], [PAYMENT_METHOD_TYPE], [PAYMENT_CARD_TYPE], [TOTAL_TRANSACTION_AMOUNT], [TRANSACTION_SUBMIT_DATE], [TRANSACTION_ORDER_NUMBER], [PG_TRANSACTION_ID], [PG_TRANSACTION_APPROVED], [PG_AUTH_CODE], [PG_TRANSACTION_CARD_TYPE], [PG_TRANSACTION_DATE], [PG_CVD_ID], [PG_PAYMENT_METHOD], [PG_MESSAGE_ID], [PG_MESSAGE_TEXT], [APP_CREATE_TIMESTAMP], [APP_CREATE_USERID], [APP_CREATE_USER_GUID], [APP_CREATE_USER_DIRECTORY], [APP_LAST_UPDATE_TIMESTAMP], [APP_LAST_UPDATE_USERID], [APP_LAST_UPDATE_USER_GUID], [APP_LAST_UPDATE_USER_DIRECTORY], [CONCURRENCY_CONTROL_NUMBER], [DB_CREATE_USERID], [DB_CREATE_TIMESTAMP], [DB_LAST_UPDATE_USERID], [DB_LAST_UPDATE_TIMESTAMP], (next value for [permit].[ORBC_TRANSACTION_H_ID_SEQ]) as [_TRANSACTION_HIST_ID], null as [END_DATE_HIST], @curr_date as [EFFECTIVE_DATE_HIST] from inserted; - -END TRY -BEGIN CATCH - IF @@trancount > 0 ROLLBACK TRANSACTION - EXEC orbc_error_handling -END CATCH; -GO -IF @@ERROR <> 0 SET NOEXEC ON -GO - - --- Alter trigger permit.ORBC_TXN_I_S_U_TR -PRINT N'Alter trigger permit.ORBC_TXN_I_S_U_TR' -GO -ALTER TRIGGER [permit].[ORBC_TXN_I_S_U_TR] ON permit.[ORBC_TRANSACTION] INSTEAD OF UPDATE AS -SET NOCOUNT ON -BEGIN TRY - IF NOT EXISTS(SELECT * FROM deleted) - RETURN; - - -- validate concurrency control - if exists (select 1 from inserted, deleted where inserted.CONCURRENCY_CONTROL_NUMBER != deleted.CONCURRENCY_CONTROL_NUMBER+1 AND inserted.TRANSACTION_ID = deleted.TRANSACTION_ID) - raiserror('CONCURRENCY FAILURE.',16,1) - - - -- update statement - update [permit].[ORBC_TRANSACTION] - set "TRANSACTION_TYPE" = inserted."TRANSACTION_TYPE", - "PAYMENT_METHOD_TYPE" = inserted."PAYMENT_METHOD_TYPE", - "PAYMENT_CARD_TYPE" = inserted."PAYMENT_CARD_TYPE", - "TOTAL_TRANSACTION_AMOUNT" = inserted."TOTAL_TRANSACTION_AMOUNT", - "TRANSACTION_SUBMIT_DATE" = inserted."TRANSACTION_SUBMIT_DATE", - "TRANSACTION_ORDER_NUMBER" = inserted."TRANSACTION_ORDER_NUMBER", - "PG_TRANSACTION_ID" = inserted."PG_TRANSACTION_ID", - "PG_TRANSACTION_APPROVED" = inserted."PG_TRANSACTION_APPROVED", - "PG_AUTH_CODE" = inserted."PG_AUTH_CODE", - "PG_TRANSACTION_CARD_TYPE" = inserted."PG_TRANSACTION_CARD_TYPE", - "PG_TRANSACTION_DATE" = inserted."PG_TRANSACTION_DATE", - "PG_CVD_ID" = inserted."PG_CVD_ID", - "PG_PAYMENT_METHOD" = inserted."PG_PAYMENT_METHOD", - "PG_MESSAGE_ID" = inserted."PG_MESSAGE_ID", - "PG_MESSAGE_TEXT" = inserted."PG_MESSAGE_TEXT", - "APP_LAST_UPDATE_TIMESTAMP" = inserted."APP_LAST_UPDATE_TIMESTAMP", - "APP_LAST_UPDATE_USERID" = inserted."APP_LAST_UPDATE_USERID", - "APP_LAST_UPDATE_USER_GUID" = inserted."APP_LAST_UPDATE_USER_GUID", - "APP_LAST_UPDATE_USER_DIRECTORY" = inserted."APP_LAST_UPDATE_USER_DIRECTORY", - "CONCURRENCY_CONTROL_NUMBER" = inserted."CONCURRENCY_CONTROL_NUMBER" - , DB_LAST_UPDATE_TIMESTAMP = getutcdate() - , DB_LAST_UPDATE_USERID = user_name() - from [permit].[ORBC_TRANSACTION] - inner join inserted - on (ORBC_TRANSACTION.TRANSACTION_ID = inserted.TRANSACTION_ID); - -END TRY -BEGIN CATCH - IF @@trancount > 0 ROLLBACK TRANSACTION - EXEC orbc_error_handling -END CATCH; -GO -IF @@ERROR <> 0 SET NOEXEC ON -GO -DISABLE TRIGGER [permit].[ORBC_TXN_I_S_U_TR] ON permit.[ORBC_TRANSACTION] -GO - - -IF @@ERROR <> 0 SET NOEXEC ON -GO -ALTER TABLE [permit].[ORBC_TRANSACTION_HIST] DROP COLUMN [RECEIPT_ID] - -IF @@ERROR <> 0 SET NOEXEC ON -GO -DECLARE @VersionDescription VARCHAR(255) -SET @VersionDescription = 'Reverting addition of RECEIPT_ID col to ORBC_TRANSACTION and dropping TRANSACTION_ID from ORBC_RECEIPT.' - -INSERT [dbo].[ORBC_SYS_VERSION] ([VERSION_ID], [DESCRIPTION], [RELEASE_DATE]) VALUES (51, @VersionDescription, getutcdate()) - -IF @@ERROR <> 0 SET NOEXEC ON -GO -COMMIT TRANSACTION -IF @@ERROR <> 0 SET NOEXEC ON -GO -DECLARE @Success AS BIT -SET @Success = 1 -SET NOEXEC OFF -IF (@Success = 1) PRINT 'The database update succeeded' -ELSE BEGIN - IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION - PRINT 'The database update failed' -END -GO \ No newline at end of file diff --git a/database/mssql/scripts/versions/v_52_ddl.sql b/database/mssql/scripts/versions/v_52_ddl.sql deleted file mode 100644 index 8ed0e6729..000000000 --- a/database/mssql/scripts/versions/v_52_ddl.sql +++ /dev/null @@ -1,246 +0,0 @@ -SET ANSI_NULLS ON -GO -SET QUOTED_IDENTIFIER ON -GO -SET NOCOUNT ON -GO - -SET XACT_ABORT ON -GO -SET TRANSACTION ISOLATION LEVEL SERIALIZABLE -GO -BEGIN TRANSACTION -GO - -IF @@ERROR <> 0 SET NOEXEC ON -GO -ALTER TABLE [permit].[ORBC_TRANSACTION] ADD [RECEIPT_ID] [bigint] NULL - -IF @@ERROR <> 0 SET NOEXEC ON -GO -CREATE NONCLUSTERED INDEX IX_ORBC_TRANSACTION_RECEIPT_ID_FK ON [permit].[ORBC_TRANSACTION] ([RECEIPT_ID]); - -IF @@ERROR <> 0 SET NOEXEC ON -GO -EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Receipt Id of the transaction' , @level0type=N'SCHEMA',@level0name=N'permit', @level1type=N'TABLE',@level1name=N'ORBC_TRANSACTION', @level2type=N'COLUMN',@level2name=N'RECEIPT_ID' - -IF @@ERROR <> 0 SET NOEXEC ON -GO - -IF @@ERROR <> 0 SET NOEXEC ON -GO -ALTER TABLE [permit].[ORBC_TRANSACTION] WITH CHECK ADD CONSTRAINT [FK_ORBC_TRANSACTION_RECEIPT_ID] FOREIGN KEY([RECEIPT_ID]) -REFERENCES [permit].[ORBC_RECEIPT] ([RECEIPT_ID]) -GO - -ALTER TABLE [permit].[ORBC_TRANSACTION] CHECK CONSTRAINT [FK_ORBC_TRANSACTION_RECEIPT_ID] - - -IF @@ERROR <> 0 SET NOEXEC ON -GO -ALTER TABLE [permit].[ORBC_TRANSACTION_HIST] ADD [RECEIPT_ID] [bigint] NULL - -IF @@ERROR <> 0 SET NOEXEC ON -GO --- Alter trigger permit.ORBC_TXN_A_S_IUD_TR -PRINT N'Alter trigger permit.ORBC_TXN_A_S_IUD_TR' -GO -ALTER TRIGGER [permit].[ORBC_TXN_A_S_IUD_TR] ON permit.[ORBC_TRANSACTION] FOR INSERT, UPDATE, DELETE AS -SET NOCOUNT ON -BEGIN TRY -DECLARE @curr_date datetime; -SET @curr_date = getutcdate(); - IF NOT EXISTS(SELECT * FROM inserted) AND NOT EXISTS(SELECT * FROM deleted) - RETURN; - - -- historical - IF EXISTS(SELECT * FROM deleted) - update [permit].[ORBC_TRANSACTION_HIST] set END_DATE_HIST = @curr_date where TRANSACTION_ID in (select TRANSACTION_ID from deleted) and END_DATE_HIST is null; - - IF EXISTS(SELECT * FROM inserted) - insert into [permit].[ORBC_TRANSACTION_HIST] ([TRANSACTION_ID], [RECEIPT_ID], [TRANSACTION_TYPE], [PAYMENT_METHOD_TYPE], [PAYMENT_CARD_TYPE], [TOTAL_TRANSACTION_AMOUNT], [TRANSACTION_SUBMIT_DATE], [TRANSACTION_ORDER_NUMBER], [PG_TRANSACTION_ID], [PG_TRANSACTION_APPROVED], [PG_AUTH_CODE], [PG_TRANSACTION_CARD_TYPE], [PG_TRANSACTION_DATE], [PG_CVD_ID], [PG_PAYMENT_METHOD], [PG_MESSAGE_ID], [PG_MESSAGE_TEXT], [APP_CREATE_TIMESTAMP], [APP_CREATE_USERID], [APP_CREATE_USER_GUID], [APP_CREATE_USER_DIRECTORY], [APP_LAST_UPDATE_TIMESTAMP], [APP_LAST_UPDATE_USERID], [APP_LAST_UPDATE_USER_GUID], [APP_LAST_UPDATE_USER_DIRECTORY], [CONCURRENCY_CONTROL_NUMBER], [DB_CREATE_USERID], [DB_CREATE_TIMESTAMP], [DB_LAST_UPDATE_USERID], [DB_LAST_UPDATE_TIMESTAMP], _TRANSACTION_HIST_ID, END_DATE_HIST, EFFECTIVE_DATE_HIST) - select [TRANSACTION_ID], [RECEIPT_ID], [TRANSACTION_TYPE], [PAYMENT_METHOD_TYPE], [PAYMENT_CARD_TYPE], [TOTAL_TRANSACTION_AMOUNT], [TRANSACTION_SUBMIT_DATE], [TRANSACTION_ORDER_NUMBER], [PG_TRANSACTION_ID], [PG_TRANSACTION_APPROVED], [PG_AUTH_CODE], [PG_TRANSACTION_CARD_TYPE], [PG_TRANSACTION_DATE], [PG_CVD_ID], [PG_PAYMENT_METHOD], [PG_MESSAGE_ID], [PG_MESSAGE_TEXT], [APP_CREATE_TIMESTAMP], [APP_CREATE_USERID], [APP_CREATE_USER_GUID], [APP_CREATE_USER_DIRECTORY], [APP_LAST_UPDATE_TIMESTAMP], [APP_LAST_UPDATE_USERID], [APP_LAST_UPDATE_USER_GUID], [APP_LAST_UPDATE_USER_DIRECTORY], [CONCURRENCY_CONTROL_NUMBER], [DB_CREATE_USERID], [DB_CREATE_TIMESTAMP], [DB_LAST_UPDATE_USERID], [DB_LAST_UPDATE_TIMESTAMP], (next value for [permit].[ORBC_TRANSACTION_H_ID_SEQ]) as [_TRANSACTION_HIST_ID], null as [END_DATE_HIST], @curr_date as [EFFECTIVE_DATE_HIST] from inserted; - -END TRY -BEGIN CATCH - IF @@trancount > 0 ROLLBACK TRANSACTION - EXEC orbc_error_handling -END CATCH; -GO -IF @@ERROR <> 0 SET NOEXEC ON -GO - - --- Alter trigger permit.ORBC_TXN_I_S_U_TR -PRINT N'Alter trigger permit.ORBC_TXN_I_S_U_TR' -GO -ALTER TRIGGER [permit].[ORBC_TXN_I_S_U_TR] ON permit.[ORBC_TRANSACTION] INSTEAD OF UPDATE AS -SET NOCOUNT ON -BEGIN TRY - IF NOT EXISTS(SELECT * FROM deleted) - RETURN; - - -- validate concurrency control - if exists (select 1 from inserted, deleted where inserted.CONCURRENCY_CONTROL_NUMBER != deleted.CONCURRENCY_CONTROL_NUMBER+1 AND inserted.TRANSACTION_ID = deleted.TRANSACTION_ID) - raiserror('CONCURRENCY FAILURE.',16,1) - - - -- update statement - update [permit].[ORBC_TRANSACTION] - set "TRANSACTION_TYPE" = inserted."TRANSACTION_TYPE", - "RECEIPT_ID" = inserted."RECEIPT_ID", - "PAYMENT_METHOD_TYPE" = inserted."PAYMENT_METHOD_TYPE", - "PAYMENT_CARD_TYPE" = inserted."PAYMENT_CARD_TYPE", - "TOTAL_TRANSACTION_AMOUNT" = inserted."TOTAL_TRANSACTION_AMOUNT", - "TRANSACTION_SUBMIT_DATE" = inserted."TRANSACTION_SUBMIT_DATE", - "TRANSACTION_ORDER_NUMBER" = inserted."TRANSACTION_ORDER_NUMBER", - "PG_TRANSACTION_ID" = inserted."PG_TRANSACTION_ID", - "PG_TRANSACTION_APPROVED" = inserted."PG_TRANSACTION_APPROVED", - "PG_AUTH_CODE" = inserted."PG_AUTH_CODE", - "PG_TRANSACTION_CARD_TYPE" = inserted."PG_TRANSACTION_CARD_TYPE", - "PG_TRANSACTION_DATE" = inserted."PG_TRANSACTION_DATE", - "PG_CVD_ID" = inserted."PG_CVD_ID", - "PG_PAYMENT_METHOD" = inserted."PG_PAYMENT_METHOD", - "PG_MESSAGE_ID" = inserted."PG_MESSAGE_ID", - "PG_MESSAGE_TEXT" = inserted."PG_MESSAGE_TEXT", - "APP_LAST_UPDATE_TIMESTAMP" = inserted."APP_LAST_UPDATE_TIMESTAMP", - "APP_LAST_UPDATE_USERID" = inserted."APP_LAST_UPDATE_USERID", - "APP_LAST_UPDATE_USER_GUID" = inserted."APP_LAST_UPDATE_USER_GUID", - "APP_LAST_UPDATE_USER_DIRECTORY" = inserted."APP_LAST_UPDATE_USER_DIRECTORY", - "CONCURRENCY_CONTROL_NUMBER" = inserted."CONCURRENCY_CONTROL_NUMBER" - , DB_LAST_UPDATE_TIMESTAMP = getutcdate() - , DB_LAST_UPDATE_USERID = user_name() - from [permit].[ORBC_TRANSACTION] - inner join inserted - on (ORBC_TRANSACTION.TRANSACTION_ID = inserted.TRANSACTION_ID); - -END TRY -BEGIN CATCH - IF @@trancount > 0 ROLLBACK TRANSACTION - EXEC orbc_error_handling -END CATCH; -GO -IF @@ERROR <> 0 SET NOEXEC ON -GO -DISABLE TRIGGER [permit].[ORBC_TXN_I_S_U_TR] ON permit.[ORBC_TRANSACTION] -GO - -IF @@ERROR <> 0 SET NOEXEC ON -GO -UPDATE t -SET RECEIPT_ID = r.RECEIPT_ID -FROM [permit].[ORBC_TRANSACTION] t -INNER JOIN [permit].[ORBC_RECEIPT] r ON t.TRANSACTION_ID = r.TRANSACTION_ID; - -IF @@ERROR <> 0 SET NOEXEC ON -GO -ALTER TABLE [permit].[ORBC_RECEIPT] DROP CONSTRAINT ORBC_RECEIPT_TRANSACTION_ID_FK - -IF @@ERROR <> 0 SET NOEXEC ON -GO - DROP INDEX IX_ORBC_RECEIPT_TRANSACTION_ID_FK ON [permit].[ORBC_RECEIPT]; - -IF @@ERROR <> 0 SET NOEXEC ON -GO -ALTER TABLE [permit].[ORBC_RECEIPT] DROP COLUMN [TRANSACTION_ID] - - -IF @@ERROR <> 0 SET NOEXEC ON -GO -AlTER TABLE [permit].[ORBC_RECEIPT_HIST] ALTER COLUMN [TRANSACTION_ID] [bigint] NULL - -IF @@ERROR <> 0 SET NOEXEC ON -GO --- Alter trigger permit.ORBC_RCPT_A_S_IUD_TR -PRINT N'Alter trigger permit.ORBC_RCPT_A_S_IUD_TR' -GO -ALTER TRIGGER [permit].[ORBC_RCPT_A_S_IUD_TR] ON permit.[ORBC_RECEIPT] FOR INSERT, UPDATE, DELETE AS -SET NOCOUNT ON -BEGIN TRY -DECLARE @curr_date datetime; -SET @curr_date = getutcdate(); - IF NOT EXISTS(SELECT * FROM inserted) AND NOT EXISTS(SELECT * FROM deleted) - RETURN; - - -- historical - IF EXISTS(SELECT * FROM deleted) - update [permit].[ORBC_RECEIPT_HIST] set END_DATE_HIST = @curr_date where RECEIPT_ID in (select RECEIPT_ID from deleted) and END_DATE_HIST is null; - - IF EXISTS(SELECT * FROM inserted) - insert into [permit].[ORBC_RECEIPT_HIST] ([RECEIPT_ID], [RECEIPT_NUMBER], [RECEIPT_DOCUMENT_ID], [APP_CREATE_TIMESTAMP], [APP_CREATE_USERID], [APP_CREATE_USER_GUID], [APP_CREATE_USER_DIRECTORY], [APP_LAST_UPDATE_TIMESTAMP], [APP_LAST_UPDATE_USERID], [APP_LAST_UPDATE_USER_GUID], [APP_LAST_UPDATE_USER_DIRECTORY], [CONCURRENCY_CONTROL_NUMBER], [DB_CREATE_USERID], [DB_CREATE_TIMESTAMP], [DB_LAST_UPDATE_USERID], [DB_LAST_UPDATE_TIMESTAMP], _RECEIPT_HIST_ID, END_DATE_HIST, EFFECTIVE_DATE_HIST) - select [RECEIPT_ID], [RECEIPT_NUMBER], [RECEIPT_DOCUMENT_ID], [APP_CREATE_TIMESTAMP], [APP_CREATE_USERID], [APP_CREATE_USER_GUID], [APP_CREATE_USER_DIRECTORY], [APP_LAST_UPDATE_TIMESTAMP], [APP_LAST_UPDATE_USERID], [APP_LAST_UPDATE_USER_GUID], [APP_LAST_UPDATE_USER_DIRECTORY], [CONCURRENCY_CONTROL_NUMBER], [DB_CREATE_USERID], [DB_CREATE_TIMESTAMP], [DB_LAST_UPDATE_USERID], [DB_LAST_UPDATE_TIMESTAMP], (next value for [permit].[ORBC_RECEIPT_H_ID_SEQ]) as [_RECEIPT_HIST_ID], null as [END_DATE_HIST], @curr_date as [EFFECTIVE_DATE_HIST] from inserted; - -END TRY -BEGIN CATCH - IF @@trancount > 0 ROLLBACK TRANSACTION - EXEC orbc_error_handling -END CATCH; -GO - -IF @@ERROR <> 0 SET NOEXEC ON -GO - --- Alter trigger permit.ORBC_RCPT_I_S_U_TR -PRINT N'Alter trigger permit.ORBC_RCPT_I_S_U_TR' -GO -ALTER TRIGGER [permit].[ORBC_RCPT_I_S_U_TR] ON permit.[ORBC_RECEIPT] INSTEAD OF UPDATE AS -SET NOCOUNT ON -BEGIN TRY - IF NOT EXISTS(SELECT * FROM deleted) - RETURN; - - -- validate concurrency control - if exists (select 1 from inserted, deleted where inserted.CONCURRENCY_CONTROL_NUMBER != deleted.CONCURRENCY_CONTROL_NUMBER+1 AND inserted.RECEIPT_ID = deleted.RECEIPT_ID) - raiserror('CONCURRENCY FAILURE.',16,1) - - - -- update statement - update [permit].[ORBC_RECEIPT] - set "RECEIPT_NUMBER" = inserted."RECEIPT_NUMBER", - "RECEIPT_DOCUMENT_ID" = inserted."RECEIPT_DOCUMENT_ID", - "APP_LAST_UPDATE_TIMESTAMP" = inserted."APP_LAST_UPDATE_TIMESTAMP", - "APP_LAST_UPDATE_USERID" = inserted."APP_LAST_UPDATE_USERID", - "APP_LAST_UPDATE_USER_GUID" = inserted."APP_LAST_UPDATE_USER_GUID", - "APP_LAST_UPDATE_USER_DIRECTORY" = inserted."APP_LAST_UPDATE_USER_DIRECTORY", - "CONCURRENCY_CONTROL_NUMBER" = inserted."CONCURRENCY_CONTROL_NUMBER" - , DB_LAST_UPDATE_TIMESTAMP = getutcdate() - , DB_LAST_UPDATE_USERID = user_name() - from [permit].[ORBC_RECEIPT] - inner join inserted - on (ORBC_RECEIPT.RECEIPT_ID = inserted.RECEIPT_ID); - -END TRY -BEGIN CATCH - IF @@trancount > 0 ROLLBACK TRANSACTION - EXEC orbc_error_handling -END CATCH; -GO -IF @@ERROR <> 0 SET NOEXEC ON -GO -DISABLE TRIGGER [permit].[ORBC_RCPT_I_S_U_TR] ON permit.[ORBC_RECEIPT] -GO - -IF @@ERROR <> 0 SET NOEXEC ON -GO - -DECLARE @VersionDescription VARCHAR(255) -SET @VersionDescription = 'Add RECEIPT_ID col to ORBC_TRANSACTION and Drop TRANSACTION_ID from ORBC_RECEIPT' - -INSERT [dbo].[ORBC_SYS_VERSION] ([VERSION_ID], [DESCRIPTION], [UPDATE_SCRIPT], [REVERT_SCRIPT], [RELEASE_DATE]) VALUES (52, @VersionDescription, '$(UPDATE_SCRIPT)', '$(REVERT_SCRIPT)', getutcdate()) -IF @@ERROR <> 0 SET NOEXEC ON -GO - -COMMIT TRANSACTION -GO -IF @@ERROR <> 0 SET NOEXEC ON -GO -DECLARE @Success AS BIT -SET @Success = 1 -SET NOEXEC OFF -IF (@Success = 1) PRINT 'The database update succeeded' -ELSE BEGIN - IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION - PRINT 'The database update failed' -END -GO -