diff --git a/admin/package.json b/admin/package.json index f9181d72c9..12d7d4dcb1 100644 --- a/admin/package.json +++ b/admin/package.json @@ -199,4 +199,4 @@ "axios": "axios/dist/node/axios.cjs" } } -} \ No newline at end of file +} diff --git a/admin/services/data-access/pupil-attendance.data.service.js b/admin/services/data-access/pupil-attendance.data.service.js index aa6c061cda..eea82b7e31 100644 --- a/admin/services/data-access/pupil-attendance.data.service.js +++ b/admin/services/data-access/pupil-attendance.data.service.js @@ -53,12 +53,20 @@ 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 -- @@ -66,6 +74,16 @@ pupilAttendanceDataService.sqlDeleteOneByPupilId = async function sqlDeleteOneBy 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 = [ { @@ -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)) diff --git a/db/migrations/schema-objects/20240816100652.do.attendance-restart-deletion-ref-column.sql b/db/migrations/schema-objects/20240816100652.do.attendance-restart-deletion-ref-column.sql new file mode 100644 index 0000000000..3f1f2e5581 --- /dev/null +++ b/db/migrations/schema-objects/20240816100652.do.attendance-restart-deletion-ref-column.sql @@ -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]; diff --git a/db/migrations/schema-objects/20240816100652.undo.attendance-restart-deletion-ref-column.sql b/db/migrations/schema-objects/20240816100652.undo.attendance-restart-deletion-ref-column.sql new file mode 100644 index 0000000000..209a18acc2 --- /dev/null +++ b/db/migrations/schema-objects/20240816100652.undo.attendance-restart-deletion-ref-column.sql @@ -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];