diff --git a/WorkflowEngine.php b/WorkflowEngine.php index bfeebda..de54d13 100644 --- a/WorkflowEngine.php +++ b/WorkflowEngine.php @@ -763,15 +763,15 @@ class WorkflowEngine { } } - public function getOrCreateMeeting(int $groupId, string $meetingDatetime): int { - $meetingKey = $groupId . '_' . $meetingDatetime; + public function getOrCreateMeeting(int $bniGroupId, string $meetingDatetime): int { + $meetingKey = $bniGroupId . '_' . $meetingDatetime; $stmt = $this->pdo->prepare("SELECT id FROM meetings WHERE meeting_key = ?"); $stmt->execute([$meetingKey]); $meetingId = $stmt->fetchColumn(); if (!$meetingId) { - $stmt = $this->pdo->prepare("INSERT INTO meetings (bni_group_id, meeting_datetime, meeting_key) VALUES (?, ?, ?)"); - $stmt->execute([$groupId, $meetingDatetime, $meetingKey]); + $stmt = $this->pdo->prepare("INSERT INTO meetings (bni_group_id, meeting_date, meeting_datetime, meeting_key) VALUES (?, DATE(?), ?, ?)"); + $stmt->execute([$bniGroupId, $meetingDatetime, $meetingDatetime, $meetingKey]); $meetingId = $this->pdo->lastInsertId(); } @@ -793,8 +793,14 @@ class WorkflowEngine { public function updateMeetingAttendance(int $meetingId, int $personId, int $bniGroupId, string $status, int $userId, ?string $guestSurvey = null): void { $sql = "INSERT INTO meeting_attendance (meeting_id, person_id, bni_group_id, attendance_status, guest_survey, updated_by) VALUES (?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE attendance_status = VALUES(attendance_status), guest_survey = VALUES(guest_survey), updated_by = VALUES(updated_by), bni_group_id = VALUES(bni_group_id)"; + + // Log query and params + $params = [$meetingId, $personId, $bniGroupId, $status, $guestSurvey, $userId]; + error_log("SQL: $sql"); + error_log("Params: " . json_encode($params)); + $stmt = $this->pdo->prepare($sql); - $stmt->execute([$meetingId, $personId, $bniGroupId, $status, $guestSurvey, $userId]); + $stmt->execute($params); } public function getMeetingAttendanceByGroupAndDate(int $groupId, string $meetingDatetime): array { diff --git a/_update_meeting_attendance.php b/_update_meeting_attendance.php index de787bf..7289999 100644 --- a/_update_meeting_attendance.php +++ b/_update_meeting_attendance.php @@ -1,47 +1,47 @@ false, 'message' => 'An error occurred.']; - if (!isset($_SESSION['user_id'])) { - $response['message'] = 'You must be logged in to perform this action.'; - echo json_encode($response); - exit; + throw new WorkflowException('You must be logged in to perform this action.', 401); } if ($_SERVER['REQUEST_METHOD'] !== 'POST') { - $response['message'] = 'Invalid request method.'; - echo json_encode($response); - exit; + throw new WorkflowException('Invalid request method.', 405); } $personId = $_POST['person_id'] ?? null; -$groupId = $_POST['group_id'] ?? null; +$bniGroupId = $_POST['bni_group_id'] ?? $_POST['group_id'] ?? null; $meetingDate = $_POST['meeting_date'] ?? null; $status = $_POST['attendance_status'] ?? null; $guestSurvey = $_POST['guest_survey'] ?? null; $userId = $_SESSION['user_id']; -if (!$personId || !$groupId || !$meetingDate || !$status) { - $response['message'] = 'Missing required parameters.'; - echo json_encode($response); - exit; +if (!$personId || !$bniGroupId || !$meetingDate || !$status) { + $missing_params = []; + if (!$personId) $missing_params[] = 'person_id'; + if (!$bniGroupId) $missing_params[] = 'bni_group_id'; + if (!$meetingDate) $missing_params[] = 'meeting_date'; + if (!$status) $missing_params[] = 'status'; + throw new WorkflowException('Missing required parameters: ' . implode(', ', $missing_params), 400); } -try { - $workflowEngine = new WorkflowEngine(); - $meetingId = $workflowEngine->getOrCreateMeeting((int)$groupId, $meetingDate); - $workflowEngine->updateMeetingAttendance($meetingId, (int)$personId, (int)$groupId, $status, (int)$userId, $guestSurvey); +$workflowEngine = new WorkflowEngine(); - $response['success'] = true; - $response['message'] = 'Attendance updated successfully.'; -} catch (Exception $e) { - error_log($e->getMessage()); - $response['message'] = 'Error updating attendance: ' . $e->getMessage(); -} +$meetingId = $workflowEngine->getOrCreateMeeting((int)$bniGroupId, $meetingDate); +$workflowEngine->updateMeetingAttendance($meetingId, (int)$personId, (int)$bniGroupId, $status, (int)$userId, $guestSurvey); + +$response = [ + 'success' => true, + 'message' => 'Attendance updated successfully.' +]; echo json_encode($response); + diff --git a/db/migrations/035_cleanup_meetings_table.php b/db/migrations/035_cleanup_meetings_table.php new file mode 100644 index 0000000..734dd13 --- /dev/null +++ b/db/migrations/035_cleanup_meetings_table.php @@ -0,0 +1,18 @@ +exec("UPDATE meetings SET bni_group_id = group_id WHERE bni_group_id IS NULL"); + +// Step 2: Modify bni_group_id to be NOT NULL +$db->exec("ALTER TABLE meetings MODIFY COLUMN bni_group_id INT(11) NOT NULL"); + +// Step 3: Find and drop the foreign key constraint on group_id +$db->exec("ALTER TABLE meetings DROP FOREIGN KEY `meetings_ibfk_1`"); + +// Step 4: Drop the old group_id column +$db->exec("ALTER TABLE meetings DROP COLUMN group_id"); + +echo "Migration 035 cleanup meetings table applied successfully."; diff --git a/index.php b/index.php index 76f8e29..5ba0cfd 100644 --- a/index.php +++ b/index.php @@ -510,7 +510,7 @@ $status_colors = [