# REIN — Real-time Education Intelligence Network

**Tagline:** *Intelligence in Every Bell.*

REIN is a real-time, multi-tenant platform for K-12 institutions. It unifies student/staff attendance (RFID), bus tracking, hostel and library operations, and parent communication into one product. The platform consists of a Next.js dashboard, a React Native (Expo) mobile app, an Express + Socket.io API, and an RFID gateway service that bridges hardware readers to the cloud.

Authentication is centralized through the **auth-service-be** microservice using the application code `REIN`.

---

## At a glance

- **Domain:** https://rein.krishub.in
- **Repository:** https://github.com/Vyshwa/Rein
- **Auth provider:** auth-service-be (NestJS) at `https://rein.krishub.in/auth/`
- **Web port (dev):** 9000
- **API port (dev):** 9001
- **WebSocket port (dev):** 9002

---

## Architecture

```
rein/
├── apps/
│   ├── web/              # Next.js 15 dashboard (App Router)
│   ├── mobile/           # React Native (Expo SDK 52)
│   └── rfid-gateway/     # RFID reader TCP/HTTP adapter
├── packages/
│   ├── api/              # Express.js REST API + Socket.io
│   ├── db/               # Prisma ORM, migrations, seed
│   ├── shared/           # Shared types, enums, constants
│   └── notifications/    # Twilio SMS/WhatsApp + FCM push + BullMQ
└── docker/               # Compose stack and service Dockerfiles
```

### Tech stack

| Layer        | Technology                                                  |
| ------------ | ----------------------------------------------------------- |
| Frontend     | Next.js 15, React 19, Tailwind CSS, Recharts, Socket.io     |
| Mobile       | React Native (Expo SDK 52), Zustand, React Query            |
| Backend API  | Node.js 20+, Express, Socket.io, Zod                        |
| Auth         | auth-service-be (NestJS, JWT, multi-app code support)       |
| Database     | PostgreSQL 16/18, Prisma ORM                                |
| Cache/Queue  | Redis 7, BullMQ                                             |
| Notifications| Twilio (SMS/WhatsApp), Firebase Cloud Messaging             |
| RFID         | Custom TCP/HTTP gateway with offline queue                  |
| Infra        | Nginx reverse proxy + Let's Encrypt, GitHub Actions, GHCR   |

---

## Modules

| Module           | Route                                | Purpose                                          |
| ---------------- | ------------------------------------ | ------------------------------------------------ |
| Students         | `/dashboard/students`                | Roster, RFID card assignment, parent linking     |
| Teachers         | `/dashboard/teachers`                | Teacher directory and assignments                |
| Staff            | `/dashboard/staff`                   | Non-teaching staff and shift attendance          |
| Drivers          | `/dashboard/drivers`                 | Driver records linked to vehicles                |
| Vehicles         | `/dashboard/vehicles`                | Fleet management                                 |
| Buses            | `/dashboard/buses`                   | Live bus locations and routes                    |
| Attendance       | `/dashboard/attendance`              | Daily attendance and statistics                  |
| Dispersal        | `/dashboard/dispersal`               | End-of-day dispersal workflow                    |
| Campus view      | `/dashboard/campus-view`             | Real-time campus map and entry/exit feed         |
| Hostel           | `/dashboard/hostel`                  | Hostel occupancy and movement                    |
| Library          | `/dashboard/library`                 | Library check-in/out and inventory               |
| Washroom         | `/dashboard/washroom`                | Washroom pass tracking                           |
| Visitors         | `/dashboard/visitors`                | Visitor logs and gate passes                     |
| Timetable        | `/dashboard/timetable`               | Class schedules                                  |
| Schools          | `/dashboard/schools`                 | Multi-tenant school management (super admin)    |
| Infrastructure   | `/dashboard/infrastructure`          | Buildings, rooms, gates, RFID readers            |
| RFID cards       | `/dashboard/rfid-cards`              | Card lifecycle and assignment                    |
| System RFID      | `/dashboard/system-rfid`             | RFID reader configuration                        |
| Reports          | `/dashboard/reports`                 | Attendance, billing, and audit reports           |
| System settings  | `/dashboard/system-settings`         | Global policies                                  |
| Settings         | `/dashboard/settings`                | User and organization preferences                |

---

## Authentication

REIN uses **auth-service-be** for all authentication. The web app sends every auth request with the header `app-code: REIN`.

### Login request

```http
POST https://rein.krishub.in/auth/auth/login
Content-Type: application/json
app-code: REIN

{
  "emailOrPhone": "user@example.com",
  "password": "secret",
  "appCode": "REIN"
}
```

### Login response

```json
{
  "message": "Login successful",
  "data": {
    "accessToken": "eyJ...",
    "refreshToken": "...",
    "user": { "id": "...", "email": "...", "phone": "...", "fullName": "..." },
    "role": { "code": "SUPER_ADMIN", "name": "Super Admin" }
  }
}
```

### Other auth endpoints

| Method | Path                                | Purpose                          |
| ------ | ----------------------------------- | -------------------------------- |
| POST   | `/auth/auth/register`               | Register a user                  |
| POST   | `/auth/auth/refresh-token`          | Refresh access token             |
| POST   | `/auth/auth/logout`                 | Revoke refresh token             |
| POST   | `/auth/auth/sso/exchange`           | Exchange SSO token across apps   |
| GET    | `/auth/users/me`                    | Current user profile             |
| POST   | `/auth/otp/send`                    | Send OTP                         |
| POST   | `/auth/otp/verify`                  | Verify OTP                       |

---

## Domain APIs (Express, mounted under `/api`)

### Attendance

| Method | Path                              | Purpose                       |
| ------ | --------------------------------- | ----------------------------- |
| POST   | `/api/attendance/rfid-event`      | Ingest RFID scan event        |
| GET    | `/api/attendance/daily`           | Daily student attendance list |
| GET    | `/api/attendance/stats`           | Attendance statistics         |

### Staff attendance

| Method | Path                              | Purpose                  |
| ------ | --------------------------------- | ------------------------ |
| POST   | `/api/staff-attendance/scan`      | Staff RFID scan          |
| GET    | `/api/staff-attendance/daily`     | Daily staff records      |
| GET    | `/api/staff-attendance/stats`     | Staff attendance stats   |
| CRUD   | `/api/staff-attendance/shifts`    | Shift management         |

### Students

| Method | Path                                  | Purpose             |
| ------ | ------------------------------------- | ------------------- |
| POST   | `/api/students`                       | Create student      |
| GET    | `/api/students`                       | List students       |
| GET    | `/api/students/:id`                   | Get student         |
| PUT    | `/api/students/:id`                   | Update student      |
| POST   | `/api/students/:id/assign-card`       | Assign RFID card    |
| POST   | `/api/students/:id/link-parent`       | Link parent         |

### Organizations

| Method | Path                          | Purpose             |
| ------ | ----------------------------- | ------------------- |
| POST   | `/api/organizations`          | Create school       |
| GET    | `/api/organizations`          | List schools        |
| GET    | `/api/organizations/:id`      | Get school          |
| PUT    | `/api/organizations/:id`      | Update school       |

### Real-time

A Socket.io server is mounted at `path=/ws`. Clients receive:
- `attendance:event` — live RFID scans
- `attendance:stats` — refreshed dashboard stats
- `bus:location` — bus GPS updates

---

## Local development

### Prerequisites

- Node.js 20+
- pnpm 10+
- PostgreSQL 16+ (running locally)
- Redis 7+ (required only if running the rein API)

### Setup

```bash
git clone https://github.com/Vyshwa/Rein.git rein
cd rein
pnpm install

cp .env.example .env
# Edit .env if needed

pnpm db:generate
pnpm db:push

pnpm --filter @rein/web dev   # Web → http://localhost:9000
pnpm --filter @rein/api dev   # API → http://localhost:9001  (needs Redis)
```

The web app talks to:
- `auth-service-be` at `http://localhost:3004/api/v1` (set `NEXT_PUBLIC_AUTH_API_URL`)
- The rein Express API at `http://localhost:9001/api`

---

## Production deployment

- Nginx terminates TLS and proxies:
  - `/`        → Next.js (`127.0.0.1:9000`)
  - `/api/`    → Express (`127.0.0.1:9001`)
  - `/ws`      → Socket.io (`127.0.0.1:9001`)
  - `/auth/`   → auth-service-be (`127.0.0.1:3004/api/v1/`)
- TLS via Let's Encrypt (dedicated cert for `rein.krishub.in`).
- Process management: PM2 / systemd (per service).
