Skip to content

Commit

Permalink
bug/59556 preserve restart state when changing attendance state (#2878)
Browse files Browse the repository at this point in the history
* add deletion reference column migrations

* capture attendance record when deleting restart

* revert restart when deleting attendance

---------

Co-authored-by: Mohsen Qureshi <[email protected]>
  • Loading branch information
GuyHarwood and activemq authored Sep 17, 2024
1 parent ab50c4e commit 35b3da2
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 8 deletions.
2 changes: 1 addition & 1 deletion admin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,4 @@
"axios": "axios/dist/node/axios.cjs"
}
}
}
}
36 changes: 29 additions & 7 deletions admin/services/data-access/pupil-attendance.data.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,37 @@ pupilAttendanceDataService.sqlDeleteOneByPupilId = async function sqlDeleteOneBy

const sql = `
--
-- capture the pupilAttendanceId for the restart undo
--
DECLARE @pupilAttendanceTable TABLE (ID INT NULL);
DECLARE @pupilAttendanceId INT;
--
-- Remove the attendance code
--
UPDATE [mtc_admin].[pupilAttendance]
SET isDeleted=1
OUTPUT INSERTED.ID INTO @pupilAttendanceTable
WHERE pupil_id = @pupilId;
--
-- Get the pupilAttendanceId
SELECT @pupilAttendanceId = ID FROM @pupilAttendanceTable;
--
-- Maintain the pupil state
--
UPDATE [mtc_admin].[pupil]
SET attendanceId = NULL,
lastModifiedBy_userId = @userId
WHERE id = @pupilId;
UPDATE [mtc_admin].[pupilRestart]
SET isDeleted = 0,
deletedByUser_id = NULL,
deletedAt = NULL,
deletedBy_pupilAttendance_id = NULL
WHERE
deletedBy_pupilAttendance_id = @pupilAttendanceId
AND isDeleted = 1
AND pupil_id = @pupilId;
`
const params = [
{
Expand Down Expand Up @@ -226,14 +244,18 @@ pupilAttendanceDataService.markAsNotAttending = async function markAsNotAttendin
SET
pr.isDeleted = 1,
pr.deletedByUser_id = @userId,
pr.deletedAt = GETUTCDATE()
pr.deletedAt = GETUTCDATE(),
pr.deletedBy_pupilAttendance_id = pa.id
FROM
[mtc_admin].[pupil] p JOIN
#pupilsToSet t1 ON (p.id = t1.id) LEFT JOIN
[mtc_admin].[pupilRestart] pr ON (p.id = pr.pupil_id)
[mtc_admin].[pupil] p
JOIN #pupilsToSet t1 ON (p.id = t1.id)
LEFT JOIN [mtc_admin].[pupilRestart] pr ON (p.id = pr.pupil_id)
LEFT JOIN [mtc_admin].[pupilAttendance] pa ON (p.id = pa.pupil_id)
WHERE
pr.isDeleted = 0
AND pr.check_id IS NULL -- unconsumed
pr.isDeleted = 0
AND pr.check_id IS NULL
AND pa.isDeleted = 0
-- unconsumed restarts & current attendance record
;`

return sqlService.modifyWithTransaction(sql, params.concat(insertParams))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
ALTER TABLE [mtc_admin].[pupilRestart]
ADD [deletedBy_pupilAttendance_id] INT NULL;

ALTER TABLE [mtc_admin].[pupilRestart] WITH NOCHECK
ADD CONSTRAINT [FK_pupilRestart_pupilAttendance_deletedBy_id] FOREIGN KEY ([deletedBy_pupilAttendance_id]) REFERENCES [mtc_admin].[pupilAttendance] ([id]);

ALTER TABLE [mtc_admin].[pupilRestart] WITH CHECK CHECK CONSTRAINT [FK_pupilRestart_pupilAttendance_deletedBy_id];
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
IF EXISTS(SELECT *
FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
WHERE CONSTRAINT_COLUMN_USAGE.TABLE_SCHEMA = 'mtc_admin'
AND CONSTRAINT_COLUMN_USAGE.TABLE_NAME = 'pupilRestart'
AND CONSTRAINT_COLUMN_USAGE.COLUMN_NAME = 'deletedBy_pupilAttendance_id'
AND CONSTRAINT_NAME = 'FK_pupilRestart_pupilAttendance_deletedBy_id')
BEGIN
ALTER TABLE [mtc_admin].[pupilRestart]
DROP CONSTRAINT [FK_pupilRestart_pupilAttendance_deletedBy_id];
END;

ALTER TABLE [mtc_admin].[pupilRestart] DROP COLUMN IF EXISTS [deletedBy_pupilAttendance_id];

0 comments on commit 35b3da2

Please sign in to comment.