isPractitionerAssigned($interventionId, $practitionerId)) { // If exists, update the role $this->updatePractitionerRole($interventionId, $practitionerId, $role); // Return the updated record return InterventionPractitioner::where('intervention_id', $interventionId) ->where('practitioner_id', $practitionerId) ->first(); } $assignment = InterventionPractitioner::create([ 'intervention_id' => $interventionId, 'practitioner_id' => $practitionerId, 'role' => $role, 'assigned_at' => $assignedAt ?: now() ]); Log::info('Intervention-practitioner assignment created', [ 'intervention_id' => $interventionId, 'practitioner_id' => $practitionerId, 'role' => $role, 'assignment_id' => $assignment->id ]); return $assignment; } catch (\Exception $e) { Log::error('Error creating intervention-practitioner assignment', [ 'intervention_id' => $interventionId, 'practitioner_id' => $practitionerId, 'role' => $role, 'error' => $e->getMessage() ]); throw $e; } } /** * Remove all practitioner assignments for an intervention. */ public function removeAllAssignments(int $interventionId): int { $deleted = InterventionPractitioner::where('intervention_id', $interventionId)->delete(); Log::info('Removed all practitioner assignments for intervention', [ 'intervention_id' => $interventionId, 'deleted_count' => $deleted ]); return $deleted; } /** * Remove specific practitioner assignment. */ public function removeAssignment(int $interventionId, int $practitionerId): int { $deleted = InterventionPractitioner::where('intervention_id', $interventionId) ->where('practitioner_id', $practitionerId) ->delete(); if ($deleted > 0) { Log::info('Removed intervention-practitioner assignment', [ 'intervention_id' => $interventionId, 'practitioner_id' => $practitionerId ]); } return $deleted; } /** * Check if a practitioner is already assigned to an intervention. */ public function isPractitionerAssigned(int $interventionId, int $practitionerId): bool { return InterventionPractitioner::where('intervention_id', $interventionId) ->where('practitioner_id', $practitionerId) ->exists(); } /** * Get all practitioner assignments for an intervention. */ public function getAssignmentsForIntervention(int $interventionId) { return InterventionPractitioner::with('practitioner') ->where('intervention_id', $interventionId) ->get(); } /** * Get principal practitioners for an intervention. */ public function getPrincipalPractitioners(int $interventionId) { return InterventionPractitioner::with('practitioner') ->where('intervention_id', $interventionId) ->principal() ->get(); } /** * Get assistant practitioners for an intervention. */ public function getAssistantPractitioners(int $interventionId) { return InterventionPractitioner::with('practitioner') ->where('intervention_id', $interventionId) ->assistant() ->get(); } /** * Update practitioner role for an intervention. */ public function updatePractitionerRole(int $interventionId, int $practitionerId, string $role): bool { $updated = InterventionPractitioner::where('intervention_id', $interventionId) ->where('practitioner_id', $practitionerId) ->update([ 'role' => $role, 'assigned_at' => now() ]); if ($updated > 0) { Log::info('Updated practitioner role for intervention', [ 'intervention_id' => $interventionId, 'practitioner_id' => $practitionerId, 'new_role' => $role ]); } return $updated > 0; } }