-- Kolo Bank: Triggers --

USE KoloBank1;
GO

-- ========================================================
-- DML Triggers:
-- Description:	These triggers update the Management.DatabaseOperations
--		by letting it know that a new operation has
--		taken place on a table of the database table. The trigger
--		also specifies the name of the employee
--		who performed the operation and the time
--		this occurred
-- ========================================================
CREATE TRIGGER Accounts.ForCustomers
ON Accounts.Customers
AFTER INSERT
AS
BEGIN
    INSERT INTO Management.DatabaseOperations
    VALUES(default, N'Customers', SUSER_SNAME(), N'Processed a deposit', SYSDATETIME(), SYSDATETIME())
END
GO
-- ========================================================
-- DML Triggers:Records Updates
-- Description:	These trigger adds a new record to the 
--		Management.DatabaseOperations when an existing record 
--		of a table has been updated.
-- ========================================================
CREATE TRIGGER Accounts.CustomerUpdated
ON Accounts.Customers
AFTER UPDATE
AS
BEGIN
    INSERT INTO Management.DatabaseOperations
    VALUES(default, N'Customers', SUSER_SNAME(), N'Some detail changed about a customer record.', SYSDATETIME(), SYSDATETIME())
END
GO
-- ========================================================
CREATE TRIGGER Accounts.ForDeposits
ON Accounts.Transactions
AFTER INSERT
AS
BEGIN
    INSERT INTO Management.DatabaseOperations
    VALUES(default, N'Transactions', SUSER_SNAME(), N'Processed a deposit', SYSDATETIME(), SYSDATETIME())
END
GO
CREATE TRIGGER Accounts.DepositUpdated
ON Accounts.Transactions
AFTER UPDATE
AS
BEGIN
    INSERT INTO Management.DatabaseOperations
    VALUES(default, N'Deposits', SUSER_SNAME(), N'Changed some information about a deposit of a bank account.', SYSDATETIME(), SYSDATETIME())
END
GO
-- ========================================================
-- This trigger acts on a view to update the
-- Management.DatabaseOperations to let it know that an 
-- attempt was made to create a new room
-- ========================================================
CREATE TRIGGER Accounts.AttemptedDeposit
ON Accounts.DepositsSummary
INSTEAD OF INSERT
AS
BEGIN
    INSERT INTO Management.DatabaseOperations
    VALUES(N'View', N'DepositsSummary', SUSER_SNAME(), N'Attempted to make a new deposit.', SYSDATETIME(), SYSDATETIME())
END
GO
-- ========================================================
CREATE TRIGGER Accounts.ForWithdrawals
ON Accounts.Transactions
AFTER INSERT
AS
BEGIN
    INSERT INTO Management.DatabaseOperations
    VALUES(default, N'Transactions', SUSER_SNAME(), N'Processed a withdrawal', SYSDATETIME(), SYSDATETIME())
END
GO
CREATE TRIGGER Accounts.WithdrawalUpdated
ON Accounts.Transactions
AFTER UPDATE
AS
BEGIN
    INSERT INTO Management.DatabaseOperations
    VALUES(default, N'Withdrawals', SUSER_SNAME(), N'Updated a withdrawal of a bank account.', SYSDATETIME(), SYSDATETIME())
END
GO
-- ========================================================
-- This trigger acts on a view to update the
-- Management.DatabaseOperations to let it know that an 
-- attempt was made to create a new room
-- ========================================================
CREATE TRIGGER Accounts.AttemptedWithdrawal
ON Accounts.WithdrawalsSummary
INSTEAD OF INSERT
AS
BEGIN
    INSERT INTO Management.DatabaseOperations
    VALUES(N'View', N'WithdrawalsSummary', SUSER_SNAME(), N'An attempt was made to withdraw money.', SYSDATETIME(), SYSDATETIME())
END
GO
-- ========================================================
CREATE TRIGGER Accounts.ForChecksCashed
ON Accounts.Transactions
AFTER INSERT
AS
BEGIN
    INSERT INTO Management.DatabaseOperations
    VALUES(default, N'CheckCashing', SUSER_SNAME(), N'Cashed a check', SYSDATETIME(), SYSDATETIME())
END
GO