How a7med7c/rafiq works
Purpose and deployable surfaces
Rafiq is a health-management platform for patients and family-managed health profiles: records/documents, medicines and reminders, appointments, emergency contacts, AI chat/voice assistance, notifications, reviews, and administration. It has:
• ASP.NET Core API entry point: src/Rafiq.API/Program.cs
• Angular browser client: RafiqAngular/src/main.ts
• Capacitor-packaged Angular mobile client: RafiqMobile/src/main.ts, with Android host RafiqMobile/android/app/src/main/java/com/rafiq/app/MainActivity.java
The Angular and mobile app trees are largely parallel; mobile adds device-oriented services and Capacitor native shells.
Backend layering and request boundary
The backend follows clean/CQRS-style separation:
• HTTP controllers expose feature APIs and mediate requests: src/Rafiq.API/Controllers/AuthController.cs, src/Rafiq.API/Controllers/PatientProfilesController.cs, src/Rafiq.API/Controllers/AiChatController.cs.
• Application commands, queries, handlers, validators, DTOs, and cross-cutting pipeline behaviors implement use cases: src/Rafiq.Application/Features/PatientProfiles, src/Rafiq.Application/Features/AiChat, src/Rafiq.Application/Common/Behaviors/ValidationBehavior.cs.
• Domain owns entities, enums, exceptions, and repository contracts: src/Rafiq.Domain/Entities, src/Rafiq.Domain/Repositories, src/Rafiq.Domain/Constants/Roles.cs.
• Infrastructure implements persistence and external integrations: src/Rafiq.Infrastructure/Persistence/RafiqDbContext.cs, src/Rafiq.Infrastructure/DependencyInjection.cs.
Program.cs and service-registration extensions compose these layers; API middleware centralizes exception mapping, user access restrictions, and Hangfire dashboard authorization.
Identity, authorization, and profile ownership
Authentication supports registration/login, refresh/revoke token lifecycle, password recovery, phone verification, and Google login. API/client boundaries attach and retain credentials through:
src/Rafiq.Application/Features/Auth, src/Rafiq.Infrastructure/Services/auth/IdentityService.cs, RafiqAngular/src/app/Interceptors/auth.interceptor.ts.
Health data is scoped to a patient health profile rather than simply an account. Family/caregiver sharing is modeled as profile access with invitations, roles, and access requests:
src/Rafiq.Domain/Entities/User/HealthProfileAccess.cs,
src/Rafiq.Application/Features/PatientProfiles,
src/Rafiq.Infrastructure/Services/HealthProfileAuthorizationService.cs.
Client route guards enforce authenticated, guest, and admin navigation states.
Clinical-data and scheduling workflows
Feature handlers manage appointments, allergies/chronic diseases, emergency contacts, prescriptions, imaging/lab/general documents, user medicines, and reminder definitions/logs. Key data model boundaries are:
src/Rafiq.Domain/Entities/Documents,
src/Rafiq.Application/Features/MedicineReminders,
src/Rafiq.Application/Features/MedicationReminderEngine.
Uploads are stored through a file-storage abstraction, currently with API-hosted upload directories and a local implementation:
src/Rafiq.Infrastructure/Services/LocalFileStorageService.cs,
src/Rafiq.API/wwwroot/uploads,
src/Rafiq.Application/Common/Interfaces/IFileStorageService.cs.
Medication and appointment changes schedule background work; due reminders can be confirmed, skipped, or snoozed and produce log records.
AI document, chat, and voice subsystem
AI is a first-class backend capability with access controls, telemetry, request classification, usage/audit operations, persisted conversations/messages, and health-profile-aware context:
src/Rafiq.Infrastructure/Services/Ai/InstrumentedAiChatService.cs,
src/Rafiq.Application/Features/AiChat/Services/HealthQueryContextBuilder.cs,
src/Rafiq.Domain/Entities/Chat/AiConversation.cs.
Document uploads for prescriptions, labs, imaging, medicine boxes, and general documents are analyzed asynchronously using specialized prompts/DTOs:
src/Rafiq.Application/AI/Prompts,
src/Rafiq.Infrastructure/Services/BackgroundJobs/DocumentAnalysisJob.cs,
src/Rafiq.Application/Features/GeneralDocuments/Commands/UploadGeneralDocumentAsync.
Voice interactions create sessions, process messages through an agent loop, and execute registered domain tools such as booking appointments or managing medicines:
src/Rafiq.Application/VoiceAgent/Agent/VoiceAgentLoop.cs,
src/Rafiq.Application/VoiceAgent/Tools/ToolRegistry.cs,
RafiqMobile/src/app/Services/voice-agent.service.ts.
Notifications, jobs, and external services
Hangfire-backed background jobs handle document analysis, chat processing/classification, missed appointments, and medication schedules:
src/Rafiq.Infrastructure/Services/BackgroundJobs,
src/Rafiq.Infrastructure/Services/MedicationReminders/MedicationReminderScheduler.cs,
src/Rafiq.API/Middleware/HangfireDashboardAuthorizationFilter.cs.
Real-time in-app notifications use SignalR hub and connection management:
src/Rafiq.Infrastructure/Services/Notifications/NotificationHub.cs,
src/Rafiq.Infrastructure/Services/Notifications/SignalRNotificationService.cs,
RafiqAngular/src/app/Services/signalr.service.ts.
Named integrations indicate AWS Bedrock for AI, Google token validation, and email/SMS/WhatsApp delivery (including Twilio settings):
src/Rafiq.Infrastructure/Services/BedrockService.cs,
src/Rafiq.Infrastructure/Services/auth/GoogleTokenValidator.cs,
src/Rafiq.Infrastructure/Services/Notifications/WhatsAppService.cs.
Client composition
Angular routes bootstrap public/auth/onboarding flows, authenticated health-management pages, AI UI, and a separate admin feature area:
RafiqAngular/src/app/app.routes.ts,
RafiqAngular/src/app/Pages/onboarding,
RafiqAngular/src/app/Features/admin/admin.routes.ts.
Client services form the API-facing boundary and maintain selected profile, token, notification, document-analysis, and localization state:
RafiqAngular/src/app/Services/profile-selection.service.ts,
RafiqAngular/src/app/Services/document-analysis-state.service.ts,
RafiqAngular/src/app/i18n/ar.ts.
The mobile variant additionally provides media-picker/download capabilities and native deployment through Capacitor.
Main entry points: PatientProfiles CQRS application layer · MedicationReminderScheduler.cs Hangfire jobs and scheduler · Program.cs ASP.NET API host · main.ts Angular web client
Components 21
Client surfaces
- Angular web client · Health, AI, onboarding, and admin UI.
RafiqAngular/src/main.ts - Capacitor mobile client · Angular client with device services.
RafiqMobile/src/main.ts - Android native host
RafiqMobile/android/app/src/main/java/com/rafiq/app/MainActivity.java - Client auth and guards · Attaches credentials and restricts routes.
RafiqAngular/src/app/Interceptors/auth.interceptor.ts - Profile and UI state · Selected profile, analysis, notification, and localization state.
RafiqAngular/src/app/Services/profile-selection.service.ts
API and application
- ASP.NET API host · Composes services, middleware, and dashboard authorization.
src/Rafiq.API/Program.cs - HTTP controllers · Feature endpoints for auth, profiles, and AI chat.
src/Rafiq.API/Controllers/AuthController.cs - CQRS application layer · Commands, queries, handlers, DTOs, and validators.
src/Rafiq.Application/Features/PatientProfiles - Validation pipeline
src/Rafiq.Application/Common/Behaviors/ValidationBehavior.cs
Domain and data
- Domain model and contracts · Entities, roles, exceptions, and repository contracts.
src/Rafiq.Domain/Entities - Profile access control · Shares health profiles through invitations, roles, and requests.
src/Rafiq.Domain/Entities/User/HealthProfileAccess.cs - Persistence and repositories
src/Rafiq.Infrastructure/Persistence/RafiqDbContext.cs - Upload storage · Local implementation stores API-hosted uploads.
src/Rafiq.Infrastructure/Services/LocalFileStorageService.cs
Async and integrations
- Identity and token service · Account lifecycle and Google validation.
src/Rafiq.Infrastructure/Services/auth/IdentityService.cs - Hangfire jobs and scheduler · Schedules reminders, analysis, chat work, and missed appointments.
src/Rafiq.Infrastructure/Services/MedicationReminders/MedicationReminderScheduler.cs - Document analysis job · Asynchronously analyzes uploaded clinical documents.
src/Rafiq.Infrastructure/Services/BackgroundJobs/DocumentAnalysisJob.cs - AI chat and context · Profile-aware conversations, telemetry, and request classification.
src/Rafiq.Infrastructure/Services/Ai/InstrumentedAiChatService.cs - Voice agent and tools · Runs voice sessions and invokes registered domain tools.
src/Rafiq.Application/VoiceAgent/Agent/VoiceAgentLoop.cs - SignalR notifications · Pushes in-app notification events to clients.
src/Rafiq.Infrastructure/Services/Notifications/SignalRNotificationService.cs - AWS Bedrock
- Email, SMS, WhatsApp
src/Rafiq.Infrastructure/Services/Notifications/WhatsAppService.cs
Connections 22
- Angular web client → Client auth and guards: uses
- Capacitor mobile client → Client auth and guards: uses
- Capacitor mobile client → Android native host: packaged by
- Client auth and guards → ASP.NET API host: authenticated API calls
- Angular web client → Profile and UI state: maintains
- ASP.NET API host → HTTP controllers: routes requests
- HTTP controllers → CQRS application layer: dispatches use cases
- CQRS application layer → Validation pipeline: validates
- CQRS application layer → Domain model and contracts: applies domain rules
- CQRS application layer → Profile access control: authorizes profile scope
- CQRS application layer → Persistence and repositories: reads and writes
- CQRS application layer → Upload storage: stores uploads
- ASP.NET API host → Identity and token service: authenticates
- CQRS application layer → Hangfire jobs and scheduler: schedules work
- Hangfire jobs and scheduler → Document analysis job: executes
- Document analysis job → AI chat and context: uses AI extraction
- AI chat and context → AWS Bedrock: invokes
- CQRS application layer → Voice agent and tools: serves voice sessions
- Voice agent and tools → CQRS application layer: executes domain tools
- Hangfire jobs and scheduler → SignalR notifications: emits reminder events
- SignalR notifications → Angular web client: pushes realtime events
- Hangfire jobs and scheduler → Email, SMS, WhatsApp: sends delivery messages