# Running the IGF HR Backend

## 1. Start the backend (required for the HR app to work)

From this folder (`igf-hr/backend`):

```bash
npm install
npm start
```

You should see:

- `MongoDB connected (same DB as CRM)`
- `IGF HR backend running on port 5001`
- `Routes: GET /, GET /api/health, GET /api/employees, /api/login, /api/working-hours, /api/holidays`

## 2. Check that the right server is on port 5001

- Open **http://localhost:5001/** in the browser. You should see plain text: **IGF HR Backend**.
- Open **http://localhost:5001/api/health**. You should see JSON: `{"ok":true,"service":"igf-hr-backend","database":"IGF"}`. That confirms the backend is connected to the IGF database.

If you get a different page or 404, something else is running on 5001 (e.g. another app or the React dev server). Stop that process and start the HR backend from `igf-hr/backend` as above.

## 3. Environment (`.env`)

Copy `.env.example` to `.env` and set:

- **MONGO_URI** – same as your CRM (igf-ems), e.g. `mongodb://...` with the IGF database.
- **JWT_SECRET** – must match the CRM so login tokens work.
- **PORT** – optional; default is 5001.

## 4. Where is data stored? (MongoDB)

- **Database:** same as CRM – **IGF** (set via `dbName: 'IGF'` and your `MONGO_URI`).
- **Existing collection:** **admins** – used for login; HR does not create it (CRM does).
- **HR collections:** `working_hours`, `holidays`, `public_holidays`, `holiday_allowances`.

MongoDB creates a **collection only when the first document is inserted**. So:

- Until at least one working hour entry is **saved** (employee logs in and clicks Save on a day), the **working_hours** collection will not appear in the IGF database. After the first save, you will see it.
- Same for the other HR collections – they appear when their first record is created.

**Daily working hours** are stored in the **working_hours** collection (one document per employee per day: `adminId`, `date`, `startTime`, `endTime`, `totalHours`, `entryMode`).

## 5. Troubleshooting

**Manager sees "Not found" / "No employees found"**

- The red "Not found" means the backend returned **404** for `GET /api/employees` – the route is not matched. **Restart the HR backend** from `igf-hr/backend` (`npm start`). After restart you should see in the terminal: `Routes: ... GET /api/employees ...`
- If it still happens, open **http://localhost:5001/api/health** in the browser. You should see `{"ok":true,"service":"igf-hr-backend","database":"IGF"}`. If `database` is missing or different, the backend is not connected to the IGF database.

**No `working_hours` collection in MongoDB**

- The **working_hours** collection is created in the **IGF** database the first time a working hour is **saved** (employee dashboard → pick a day → Edit → Save). If no save has ever succeeded, the collection will not exist.
- Check: (1) HR backend is running and uses the same MONGO_URI as your CRM (same IGF database). (2) Log in as an **employee**, open Working hours, add or edit a day, click Save. (3) If save fails, check the backend terminal and browser Network tab for the error. Once one save succeeds, refresh your MongoDB view – you should see the **working_hours** collection in IGF.

## 6. Run the frontend

From `igf-hr/frontend`:

```bash
npm install
npm start
```

The frontend will use `http://localhost:5001` for the API (or `REACT_APP_HR_API_URL` if set). Ensure the backend is running first so `/api/employees`, `/api/working-hours`, and other routes respond.
