Skip to content

Commit 7c78eb9

Browse files
Zeff01claude
andcommitted
Fix 'excused' attendance status saving error
- Added 'excused' to AttendanceRecord interface type definition - Updated attendance points logic to award points for 'excused' status - Created migration to add 'excused' to attendance table CHECK constraint - Updated database trigger to handle 'excused' status for attendance points - Excused attendance now counts as present for point tracking purposes 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 2d3ef8c commit 7c78eb9

2 files changed

Lines changed: 51 additions & 5 deletions

File tree

‎apps/codebility/app/home/my-team/[projectId]/actions.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ interface AttendanceRecord {
4848
codev_id: string;
4949
project_id: string;
5050
date: string;
51-
status: "present" | "absent" | "late" | "holiday" | "weekend";
51+
status: "present" | "absent" | "late" | "holiday" | "weekend" | "excused";
5252
check_in?: string;
5353
check_out?: string;
5454
notes?: string;
@@ -97,11 +97,11 @@ export async function saveAttendance(record: AttendanceRecord) {
9797
}
9898

9999
// Manually update attendance points (in case trigger isn't working)
100-
if (record.status === "present" || record.status === "late") {
100+
if (record.status === "present" || record.status === "late" || record.status === "excused") {
101101
await updateAttendancePoints(record.codev_id, 2);
102-
} else if (existing && (existing.status === "present" || existing.status === "late") &&
103-
record.status !== "present" && record.status !== "late") {
104-
// Deduct points if changing from present/late to absent
102+
} else if (existing && (existing.status === "present" || existing.status === "late" || existing.status === "excused") &&
103+
record.status !== "present" && record.status !== "late" && record.status !== "excused") {
104+
// Deduct points if changing from present/late/excused to absent
105105
await updateAttendancePoints(record.codev_id, -2);
106106
}
107107

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
-- Add 'excused' as a valid status to the attendance table
2+
ALTER TABLE attendance DROP CONSTRAINT IF EXISTS attendance_status_check;
3+
ALTER TABLE attendance ADD CONSTRAINT attendance_status_check
4+
CHECK (status IN ('present', 'absent', 'late', 'holiday', 'weekend', 'excused'));
5+
6+
-- Update the trigger to handle excused status for attendance points
7+
-- Excused attendance should count as present for attendance tracking purposes
8+
CREATE OR REPLACE FUNCTION update_attendance_points_on_attendance_change()
9+
RETURNS TRIGGER AS $$
10+
BEGIN
11+
-- Handle INSERT or UPDATE to present/late/excused
12+
IF (TG_OP = 'INSERT' AND NEW.status IN ('present', 'late', 'excused')) OR
13+
(TG_OP = 'UPDATE' AND OLD.status NOT IN ('present', 'late', 'excused') AND NEW.status IN ('present', 'late', 'excused')) THEN
14+
-- Add 2 points
15+
INSERT INTO attendance_points (codev_id, points, last_updated)
16+
VALUES (NEW.codev_id, 2, CURRENT_DATE)
17+
ON CONFLICT (codev_id)
18+
DO UPDATE SET
19+
points = attendance_points.points + 2,
20+
last_updated = CURRENT_DATE,
21+
updated_at = NOW();
22+
23+
-- Handle UPDATE from present/late/excused to other status
24+
ELSIF TG_OP = 'UPDATE' AND OLD.status IN ('present', 'late', 'excused') AND NEW.status NOT IN ('present', 'late', 'excused') THEN
25+
-- Deduct 2 points
26+
UPDATE attendance_points
27+
SET
28+
points = GREATEST(0, points - 2),
29+
last_updated = CURRENT_DATE,
30+
updated_at = NOW()
31+
WHERE codev_id = NEW.codev_id;
32+
33+
-- Handle DELETE of present/late/excused record
34+
ELSIF TG_OP = 'DELETE' AND OLD.status IN ('present', 'late', 'excused') THEN
35+
-- Deduct 2 points
36+
UPDATE attendance_points
37+
SET
38+
points = GREATEST(0, points - 2),
39+
last_updated = CURRENT_DATE,
40+
updated_at = NOW()
41+
WHERE codev_id = OLD.codev_id;
42+
END IF;
43+
44+
RETURN NEW;
45+
END;
46+
$$ LANGUAGE plpgsql;

0 commit comments

Comments
 (0)