Compare commits
13 Commits
v2.1
..
cc67fb4f85
| Author | SHA1 | Date | |
|---|---|---|---|
| cc67fb4f85 | |||
| 75ff4aadc1 | |||
| 6f998d07c1 | |||
| f2bb326040 | |||
| 8c701db900 | |||
| d1664338a6 | |||
| 1a2624cd9e | |||
| a138190cc6 | |||
| 993e0cd74b | |||
| dab004a7b6 | |||
| d039336f39 | |||
| 4c781e9325 | |||
| 451e6b3646 |
@@ -113,7 +113,3 @@ secrets/
|
|||||||
keys/
|
keys/
|
||||||
|
|
||||||
ToDo.txt
|
ToDo.txt
|
||||||
|
|
||||||
|
|
||||||
# only in development branch
|
|
||||||
next-env.d.ts
|
|
||||||
+196
-217
@@ -1,32 +1,27 @@
|
|||||||
# Borrow System API Documentation
|
# Backend API (V2) Documentation
|
||||||
|
|
||||||
**Frontend:** https://insta.the1s.de
|
This document describes the current backend API routes and their real response shapes, based on the code in `backendV2`.
|
||||||
**Backend base URL:** `https://insta.the1s.de/backend/api`
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Base URLs
|
||||||
|
|
||||||
|
- Frontend: `https://insta.the1s.de`
|
||||||
|
- Backend: `https://backend.insta.the1s.de`
|
||||||
|
- Base path: `https://backend.insta.the1s.de/api`
|
||||||
|
|
||||||
|
Service status: `https://status.the1s.de`
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## Authentication
|
## Authentication
|
||||||
|
|
||||||
All API endpoints require **either**:
|
All **protected** endpoints require an API key as a path parameter `:key`.
|
||||||
|
|
||||||
### 1. Bearer Token (JWT)
|
Rules for `:key`:
|
||||||
|
|
||||||
Send an `Authorization` header:
|
- Exactly 8 characters
|
||||||
|
- Digits only (`^[0-9]{8}$`)
|
||||||
```http
|
|
||||||
Authorization: Bearer <JWT_TOKEN>
|
|
||||||
```
|
|
||||||
|
|
||||||
- Used for user-based access.
|
|
||||||
- Token must be valid and not expired.
|
|
||||||
|
|
||||||
### 2. API Key (for devices / machine-to-machine)
|
|
||||||
|
|
||||||
Include an API key in the route as `:key` parameter:
|
|
||||||
|
|
||||||
```text
|
|
||||||
/api/.../:key/...
|
|
||||||
```
|
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
@@ -34,48 +29,59 @@ Example:
|
|||||||
GET /api/items/12345678
|
GET /api/items/12345678
|
||||||
```
|
```
|
||||||
|
|
||||||
Where `12345678` is your API key.
|
On missing / invalid key:
|
||||||
The API key is validated server-side.
|
|
||||||
|
- Status: `401 Unauthorized`
|
||||||
|
- Body (exact message depends on `authenticate` in `backendV2/services/authentication.js`)
|
||||||
|
|
||||||
|
Auth-related modules:
|
||||||
|
|
||||||
|
- `backendV2/services/authentication.js`
|
||||||
|
- `backendV2/services/database.js`
|
||||||
|
|
||||||
|
Route handlers:
|
||||||
|
|
||||||
|
- `backendV2/routes/api/api.route.js`
|
||||||
|
- `backendV2/routes/api/api.database.js`
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## Common Response Codes
|
## Endpoints (Overview)
|
||||||
|
|
||||||
- `200 OK` – Request was successful.
|
1. **Public**
|
||||||
- `401 Unauthorized` – Missing or malformed credentials.
|
|
||||||
- `403 Forbidden` – Credentials invalid or not allowed to access this resource.
|
- `GET /api/all-items` – List all items (no auth; from original docs)
|
||||||
- `404 Not Found` – Resource (e.g., loan) not found.
|
|
||||||
- `500 Internal Server Error` – Unexpected server error.
|
2. **Items (authenticated)**
|
||||||
|
|
||||||
|
- `GET /api/items/:key` – List all items
|
||||||
|
- `POST /api/change-state/:key/:itemId/:state` – Toggle item safe state
|
||||||
|
|
||||||
|
3. **Loans (authenticated)**
|
||||||
|
- `GET /api/get-loan-by-code/:key/:loan_code` – Get loan by code
|
||||||
|
- `POST /api/set-take-date/:key/:loan_code` – Set “take” date and mark items as out
|
||||||
|
- `POST /api/set-return-date/:key/:loan_code` – Set “return” date and mark items as returned
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## Endpoints
|
## 1) Items
|
||||||
|
|
||||||
### 1. Get All Items
|
### 1.1 Get all items
|
||||||
|
|
||||||
**GET** `/api/items/:key`
|
**GET** `/api/items/:key`
|
||||||
|
|
||||||
Returns a list of all items.
|
Returns all items wrapped in a `data` property.
|
||||||
|
|
||||||
#### Path Parameters
|
- Handler: `getItemsFromDatabaseV2` in `api.database.js`
|
||||||
|
- SQL: `SELECT * FROM items;`
|
||||||
|
|
||||||
- `:key` – API key (8-digit number)
|
#### Example request
|
||||||
|
|
||||||
#### Authentication
|
|
||||||
|
|
||||||
- Either:
|
|
||||||
- Valid `Authorization: Bearer <token>`
|
|
||||||
- Or valid `:key` path parameter
|
|
||||||
|
|
||||||
#### Request Example
|
|
||||||
|
|
||||||
```http
|
```http
|
||||||
GET /api/items/12345678 HTTP/1.1
|
GET https://backend.insta.the1s.de/api/items/12345678
|
||||||
Host: backend.insta.the1s.de
|
|
||||||
Authorization: Bearer <JWT_TOKEN>
|
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Successful Response (200)
|
#### Successful response
|
||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
@@ -84,9 +90,8 @@ Authorization: Bearer <JWT_TOKEN>
|
|||||||
"id": 1,
|
"id": 1,
|
||||||
"item_name": "DJI 1er Mikro",
|
"item_name": "DJI 1er Mikro",
|
||||||
"can_borrow_role": 4,
|
"can_borrow_role": 4,
|
||||||
"inSafe": 1,
|
"in_safe": 1,
|
||||||
"safe_nr": 3,
|
"safe_nr": "01",
|
||||||
"door_key": "123",
|
|
||||||
"entry_created_at": "2025-08-19T22:02:16.000Z",
|
"entry_created_at": "2025-08-19T22:02:16.000Z",
|
||||||
"entry_updated_at": "2025-08-19T22:02:16.000Z",
|
"entry_updated_at": "2025-08-19T22:02:16.000Z",
|
||||||
"last_borrowed_person": "alice",
|
"last_borrowed_person": "alice",
|
||||||
@@ -96,271 +101,245 @@ Authorization: Bearer <JWT_TOKEN>
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Error Response (500)
|
#### Error response
|
||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{ "message": "Failed to fetch items" }
|
||||||
"message": "Failed to fetch items"
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### Status codes
|
||||||
|
|
||||||
|
- `200 OK` – success, `data` is an array (possibly empty)
|
||||||
|
- `401 Unauthorized` – invalid / missing key
|
||||||
|
- `500 Internal Server Error` – database error or `success: false` from DB layer
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
### 2. Toggle Item Safe State
|
### 2.2 Toggle item safe state
|
||||||
|
|
||||||
Toggles `in_safe` between `0` and `1` for a given item.
|
|
||||||
|
|
||||||
**Keep in mind that when you return a loan by code, the item states are automatically updated.**
|
|
||||||
|
|
||||||
**POST** `/api/change-state/:key/:itemId`
|
**POST** `/api/change-state/:key/:itemId`
|
||||||
|
|
||||||
#### Path Parameters
|
> You do not need this endpoint to set the states of the items when the items are taken out or returned. When you take or return a loan, the item states are set automatically by the loan endpoints. This endpoint is only for manually toggling the `inSafe` state of an item.
|
||||||
|
|
||||||
- `:key` – API key (8-digit number)
|
Path parameters:
|
||||||
- `:itemId` – Item ID (integer)
|
|
||||||
|
|
||||||
#### Authentication
|
- `:key` – API key (8 digits)
|
||||||
|
- `:itemId` – numeric `id` of the item
|
||||||
|
|
||||||
- Either Bearer token or `:key` API key.
|
Handler in `api.route.js` calls `changeInSafeStateV2(itemId)`, which executes:
|
||||||
|
|
||||||
#### Request Example
|
```sql
|
||||||
|
UPDATE items SET in_safe = NOT in_safe WHERE id = ?
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Example request
|
||||||
|
|
||||||
```http
|
```http
|
||||||
POST /api/change-state/12345678/42 HTTP/1.1
|
POST https://backend.insta.the1s.de/api/change-state/12345678/42
|
||||||
Host: backend.insta.the1s.de
|
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Successful Response (200)
|
(Will toggle `in_safe` for item `42`.)
|
||||||
|
|
||||||
|
#### Successful response (current implementation)
|
||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"data": {}
|
"data": null
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
_(Implementation currently only returns `{ success: true }`, so `data` may be empty.)_
|
#### Error responses
|
||||||
|
|
||||||
#### Error Response (500)
|
Invalid `state` (anything other than `"0"` or `"1"`):
|
||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{ "message": "Invalid state value" }
|
||||||
"message": "Failed to update item state"
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Failed update:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{ "message": "Failed to update item state" }
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Status codes
|
||||||
|
|
||||||
|
- `200 OK` – item state toggled
|
||||||
|
- `400 Bad Request` – invalid `state` parameter
|
||||||
|
- `401 Unauthorized` – invalid / missing key
|
||||||
|
- `500 Internal Server Error` – database/update failure or `success: false` from DB layer
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
### 3. Get Loan by Code
|
## 3) Loans
|
||||||
|
|
||||||
Fetch loan information by `loan_code`.
|
### 3.1 Get loan by code
|
||||||
|
|
||||||
**GET** `/api/get-loan-by-code/:key/:loan_code`
|
**GET** `/api/get-loan-by-code/:key/:loan_code`
|
||||||
|
|
||||||
#### Path Parameters
|
Path parameters:
|
||||||
|
|
||||||
- `:key` – API key (8-digit number)
|
- `:key` – API key
|
||||||
- `:loan_code` – Loan code (string)
|
- `:loan_code` – 6-digit loan code (`^[0-9]{6}$` per DB constraint)
|
||||||
|
|
||||||
#### Authentication
|
Database layer (`getLoanByCodeV2`) currently selects:
|
||||||
|
|
||||||
- Either Bearer token or `:key` API key.
|
```sql
|
||||||
|
SELECT first_name, returned_date, take_date, lockers
|
||||||
#### Request Example
|
FROM loans
|
||||||
|
WHERE loan_code = ?;
|
||||||
```http
|
|
||||||
GET /api/get-loan-by-code/12345678/12345 HTTP/1.1
|
|
||||||
Host: backend.insta.the1s.de
|
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Successful Response (200)
|
#### Example request
|
||||||
|
|
||||||
|
```http
|
||||||
|
GET https://backend.insta.the1s.de/api/get-loan-by-code/12345678/646473
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Successful response
|
||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"data": {
|
"data": {
|
||||||
"username": "john",
|
"first_name": "Theis",
|
||||||
"returned_date": null,
|
"returned_date": null,
|
||||||
"take_date": "2025-01-01T10:00:00.000Z",
|
"take_date": "2025-08-25T13:23:00.000Z",
|
||||||
"lockers": "[1, 2, 3]"
|
"lockers": ["01", "03"]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Error Response (404)
|
#### Error response
|
||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{ "message": "Loan not found" }
|
||||||
"message": "Loan not found"
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### Status codes
|
||||||
|
|
||||||
|
- `200 OK` – loan found
|
||||||
|
- `401 Unauthorized` – invalid / missing key
|
||||||
|
- `404 Not Found` – no matching loan for this `loan_code`
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
### 4. Set Loan Return Date
|
### 3.2 Set take date
|
||||||
|
|
||||||
Sets `returned_date = NOW()` on a loan and updates related items:
|
|
||||||
|
|
||||||
- `in_safe = 1`
|
|
||||||
- `currently_borrowing = NULL`
|
|
||||||
- `last_borrowed_person = username`
|
|
||||||
|
|
||||||
**POST** `/api/set-return-date/:key/:loan_code`
|
|
||||||
|
|
||||||
#### Path Parameters
|
|
||||||
|
|
||||||
- `:key` – API key (8-digit number)
|
|
||||||
- `:loan_code` – Loan code (string)
|
|
||||||
|
|
||||||
#### Authentication
|
|
||||||
|
|
||||||
- Either Bearer token or `:key` API key.
|
|
||||||
|
|
||||||
#### Request Example
|
|
||||||
|
|
||||||
```http
|
|
||||||
POST /api/set-return-date/12345678/12345 HTTP/1.1
|
|
||||||
Host: backend.insta.the1s.de
|
|
||||||
```
|
|
||||||
|
|
||||||
#### Successful Response (200)
|
|
||||||
|
|
||||||
```json
|
|
||||||
{
|
|
||||||
"data": {}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
#### Error Response (500)
|
|
||||||
|
|
||||||
```json
|
|
||||||
{
|
|
||||||
"message": "Failed to set return date"
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
### 5. Set Loan Take Date
|
|
||||||
|
|
||||||
Sets `take_date = NOW()` on a loan and updates related items:
|
|
||||||
|
|
||||||
- `in_safe = 0`
|
|
||||||
- `currently_borrowing = username`
|
|
||||||
|
|
||||||
**POST** `/api/set-take-date/:key/:loan_code`
|
**POST** `/api/set-take-date/:key/:loan_code`
|
||||||
|
|
||||||
#### Path Parameters
|
Path parameters:
|
||||||
|
|
||||||
- `:key` – API key (8-digit number)
|
- `:key` – API key
|
||||||
- `:loan_code` – Loan code (string)
|
- `:loan_code` – loan code
|
||||||
|
|
||||||
#### Authentication
|
#### Example request
|
||||||
|
|
||||||
- Either Bearer token or `:key` API key.
|
|
||||||
|
|
||||||
#### Request Example
|
|
||||||
|
|
||||||
```http
|
```http
|
||||||
POST /api/set-take-date/12345678/LOAN-12345 HTTP/1.1
|
POST https://backend.insta.the1s.de/api/set-take-date/12345678/646473
|
||||||
Host: backend.insta.the1s.de
|
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Successful Response (200)
|
#### Successful response
|
||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"data": {}
|
"data": null
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Error Response (500)
|
#### Error response
|
||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{ "message": "Failed to set take date" }
|
||||||
"message": "Failed to set take date"
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### Status codes
|
||||||
|
|
||||||
|
- `200 OK` – take date set and items marked as out
|
||||||
|
- `401 Unauthorized` – invalid / missing key
|
||||||
|
- `500 Internal Server Error` – invalid loan, missing items, or DB error / `success: false`
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
### 6. Open Door by Door Key
|
### 3.3 Set return date
|
||||||
|
|
||||||
Looks up an item by its `door_key`, toggles `in_safe`, and returns safe information.
|
**POST** `/api/set-return-date/:key/:loan_code`
|
||||||
|
|
||||||
**GET** `/api/open-door/:key/:doorKey`
|
Path parameters:
|
||||||
|
|
||||||
#### Path Parameters
|
- `:key` – API key
|
||||||
|
- `:loan_code` – loan code
|
||||||
|
|
||||||
- `:key` – API key (8-digit number)
|
#### Example request
|
||||||
- `:doorKey` – Door key/token (string) used by hardware to identify the locker.
|
|
||||||
|
|
||||||
#### Authentication
|
|
||||||
|
|
||||||
- Either Bearer token or `:key` API key.
|
|
||||||
|
|
||||||
#### Request Example
|
|
||||||
|
|
||||||
```http
|
```http
|
||||||
GET /api/open-door/12345678/123 HTTP/1.1
|
POST https://backend.insta.the1s.de/api/set-return-date/12345678/646473
|
||||||
Host: backend.insta.the1s.de
|
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Successful Response (200)
|
#### Successful response (current implementation)
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"data": null
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Error response
|
||||||
|
|
||||||
|
```json
|
||||||
|
{ "message": "Failed to set return date" }
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Status codes
|
||||||
|
|
||||||
|
- `200 OK` – return date set and items marked as returned
|
||||||
|
- `401 Unauthorized` – invalid / missing key
|
||||||
|
- `500 Internal Server Error` – invalid loan, missing items, or DB error / `success: false`
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Common Response Shapes
|
||||||
|
|
||||||
|
**Success – list (authenticated items):**
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"data": [
|
||||||
|
/* array of rows */
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Success – single loan:**
|
||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"data": {
|
"data": {
|
||||||
"safe_nr": 5,
|
/* selected loan fields */
|
||||||
"id": 42
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Error Response (500)
|
**Success – mutations (current code):**
|
||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{ "data": null }
|
||||||
"message": "Failed to open door"
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
---
|
**Errors:**
|
||||||
|
|
||||||
## Authentication Error Messages
|
|
||||||
|
|
||||||
### Missing credentials
|
|
||||||
|
|
||||||
Status: `401`
|
|
||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{ "message": "Failed to fetch items" }
|
||||||
"message": "Unauthorized"
|
{ "message": "Failed to update item state" }
|
||||||
}
|
{ "message": "Invalid state value" }
|
||||||
|
{ "message": "Loan not found" }
|
||||||
|
{ "message": "Failed to set return date" }
|
||||||
|
{ "message": "Failed to set take date" }
|
||||||
```
|
```
|
||||||
|
|
||||||
### Invalid JWT
|
**HTTP Status Codes:**
|
||||||
|
|
||||||
Status: `403`
|
- `200 OK` – operation succeeded
|
||||||
|
- `400 Bad Request` – invalid `state` parameter
|
||||||
```json
|
- `401 Unauthorized` – invalid/missing API key
|
||||||
{
|
- `404 Not Found` – loan not found
|
||||||
"message": "Present token invalid"
|
- `500 Internal Server Error` – database / server failure or `success: false` from DB layer
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### Invalid API Key
|
|
||||||
|
|
||||||
Status: `403`
|
|
||||||
|
|
||||||
```json
|
|
||||||
{
|
|
||||||
"message": "API Key invalid"
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Notes
|
|
||||||
|
|
||||||
- All responses are JSON.
|
|
||||||
- Time fields like `take_date` and `returned_date` are in the format returned by MySQL (usually ISO-like strings).
|
|
||||||
- `loaned_items_id` in the database is stored as a JSON array string (e.g. `"[1,2,3]"`) and is parsed internally; clients do not interact with this field directly via current endpoints.
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
<meta charset="UTF-8" />
|
<meta charset="UTF-8" />
|
||||||
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
|
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
<title>Ausleihsystem</title>
|
<title>frontendv2</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div id="root"></div>
|
<div id="root"></div>
|
||||||
|
|||||||
@@ -9,14 +9,6 @@ server {
|
|||||||
try_files $uri $uri/ /index.html;
|
try_files $uri $uri/ /index.html;
|
||||||
}
|
}
|
||||||
|
|
||||||
location = /backend {
|
|
||||||
return 301 /backend/;
|
|
||||||
}
|
|
||||||
|
|
||||||
location /backend/ {
|
|
||||||
proxy_pass http://borrow_system-backend_v2:8004/;
|
|
||||||
}
|
|
||||||
|
|
||||||
location ~* \.(?:js|mjs|css|png|jpg|jpeg|gif|ico|svg|woff2?)$ {
|
location ~* \.(?:js|mjs|css|png|jpg|jpeg|gif|ico|svg|woff2?)$ {
|
||||||
expires 1y;
|
expires 1y;
|
||||||
access_log off;
|
access_log off;
|
||||||
|
|||||||
@@ -16,7 +16,6 @@ import { Box, Flex } from "@chakra-ui/react";
|
|||||||
import { Footer } from "./components/footer/Footer";
|
import { Footer } from "./components/footer/Footer";
|
||||||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
||||||
import { API_BASE } from "@/config/api.config";
|
import { API_BASE } from "@/config/api.config";
|
||||||
import { ContactPage } from "./pages/ContactPage";
|
|
||||||
|
|
||||||
const queryClient = new QueryClient();
|
const queryClient = new QueryClient();
|
||||||
|
|
||||||
@@ -81,7 +80,6 @@ function App() {
|
|||||||
<Route path="/" element={<HomePage />} />
|
<Route path="/" element={<HomePage />} />
|
||||||
<Route path="/my-loans" element={<MyLoansPage />} />
|
<Route path="/my-loans" element={<MyLoansPage />} />
|
||||||
<Route path="/landingpage" element={<Landingpage />} />
|
<Route path="/landingpage" element={<Landingpage />} />
|
||||||
<Route path="/contact" element={<ContactPage />} />
|
|
||||||
</Route>
|
</Route>
|
||||||
|
|
||||||
<Route path="/login" element={<LoginPage />} />
|
<Route path="/login" element={<LoginPage />} />
|
||||||
|
|||||||
@@ -4,41 +4,98 @@ import {
|
|||||||
Heading,
|
Heading,
|
||||||
Stack,
|
Stack,
|
||||||
Text,
|
Text,
|
||||||
|
CloseButton,
|
||||||
|
Dialog,
|
||||||
|
Portal,
|
||||||
HStack,
|
HStack,
|
||||||
IconButton,
|
IconButton,
|
||||||
Menu,
|
Menu,
|
||||||
Box,
|
Box,
|
||||||
Avatar,
|
Avatar,
|
||||||
|
Card,
|
||||||
|
Grid,
|
||||||
} from "@chakra-ui/react";
|
} from "@chakra-ui/react";
|
||||||
|
import { PasswordInput } from "@/components/ui/password-input";
|
||||||
import Cookies from "js-cookie";
|
import Cookies from "js-cookie";
|
||||||
import { useAtom } from "jotai";
|
import { useAtom } from "jotai";
|
||||||
import { setIsLoggedInAtom, triggerLogoutAtom } from "@/states/Atoms";
|
import { setIsLoggedInAtom, triggerLogoutAtom } from "@/states/Atoms";
|
||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
import {
|
import {
|
||||||
CircleUserRound,
|
CircleUserRound,
|
||||||
|
RotateCcwKey,
|
||||||
|
Code,
|
||||||
LifeBuoy,
|
LifeBuoy,
|
||||||
LogOut,
|
LogOut,
|
||||||
CalendarPlus,
|
CalendarPlus,
|
||||||
MoreVertical,
|
MoreVertical,
|
||||||
Languages,
|
Languages,
|
||||||
Table,
|
Table,
|
||||||
ContactRound,
|
|
||||||
} from "lucide-react";
|
} from "lucide-react";
|
||||||
import { useUserContext } from "@/states/Context";
|
import { useUserContext } from "@/states/Context";
|
||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
|
import MyAlert from "./myChakra/MyAlert";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import { UserDialogue } from "./UserDialogue";
|
import { API_BASE } from "@/config/api.config";
|
||||||
|
|
||||||
export const Header = () => {
|
export const Header = () => {
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const userData = useUserContext();
|
const userData = useUserContext();
|
||||||
|
console.log(userData);
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
|
|
||||||
|
// Error handling states
|
||||||
|
const [isMsg, setIsMsg] = useState(false);
|
||||||
|
const [msgStatus, setMsgStatus] = useState<"error" | "success">("error");
|
||||||
|
const [msgTitle, setMsgTitle] = useState("");
|
||||||
|
const [msgDescription, setMsgDescription] = useState("");
|
||||||
|
|
||||||
|
const [oldPassword, setOldPassword] = useState("");
|
||||||
|
const [newPassword, setNewPassword] = useState("");
|
||||||
|
const [confirmPassword, setConfirmPassword] = useState("");
|
||||||
|
|
||||||
const [, setTriggerLogout] = useAtom(triggerLogoutAtom);
|
const [, setTriggerLogout] = useAtom(triggerLogoutAtom);
|
||||||
const [, setIsLoggedIn] = useAtom(setIsLoggedInAtom);
|
const [, setIsLoggedIn] = useAtom(setIsLoggedInAtom);
|
||||||
|
|
||||||
|
// Dialog control
|
||||||
|
const [isPwOpen, setPwOpen] = useState(false);
|
||||||
const [userDialog, setUserDialog] = useState(false);
|
const [userDialog, setUserDialog] = useState(false);
|
||||||
|
|
||||||
|
const changePassword = async () => {
|
||||||
|
if (newPassword !== confirmPassword) {
|
||||||
|
setMsgTitle(t("err_pw_change"));
|
||||||
|
setMsgDescription(t("pw_mismatch"));
|
||||||
|
setMsgStatus("error");
|
||||||
|
setIsMsg(true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await fetch(`${API_BASE}/api/users/change-password`, {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
Authorization: `Bearer ${Cookies.get("token")}`,
|
||||||
|
},
|
||||||
|
body: JSON.stringify({ oldPassword, newPassword }),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
setMsgTitle(t("err_pw_change"));
|
||||||
|
setMsgDescription(t("pw_mismatch"));
|
||||||
|
setMsgStatus("error");
|
||||||
|
setIsMsg(true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setMsgTitle(t("pw_success"));
|
||||||
|
setMsgDescription(t("pw_success_desc"));
|
||||||
|
setMsgStatus("success");
|
||||||
|
setIsMsg(true);
|
||||||
|
|
||||||
|
setOldPassword("");
|
||||||
|
setNewPassword("");
|
||||||
|
setConfirmPassword("");
|
||||||
|
};
|
||||||
|
|
||||||
const username = userData.first_name ? userData.first_name : "N/A";
|
const username = userData.first_name ? userData.first_name : "N/A";
|
||||||
const fullname = userData.first_name + " " + userData.last_name;
|
const fullname = userData.first_name + " " + userData.last_name;
|
||||||
const randomColor = [
|
const randomColor = [
|
||||||
@@ -144,7 +201,7 @@ export const Header = () => {
|
|||||||
window.open(
|
window.open(
|
||||||
"https://git.the1s.de/Matthias-Claudius-Schule/borrow-system/wiki",
|
"https://git.the1s.de/Matthias-Claudius-Schule/borrow-system/wiki",
|
||||||
"_blank",
|
"_blank",
|
||||||
"noopener,noreferrer",
|
"noopener,noreferrer"
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
children={
|
children={
|
||||||
@@ -155,12 +212,18 @@ export const Header = () => {
|
|||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
<Menu.Item
|
<Menu.Item
|
||||||
value="contact"
|
value="source-code"
|
||||||
onSelect={() => navigate("/contact", { replace: true })}
|
onSelect={() =>
|
||||||
|
window.open(
|
||||||
|
"https://git.the1s.de/Matthias-Claudius-Schule/borrow-system",
|
||||||
|
"_blank",
|
||||||
|
"noopener,noreferrer"
|
||||||
|
)
|
||||||
|
}
|
||||||
children={
|
children={
|
||||||
<HStack gap={3}>
|
<HStack gap={3}>
|
||||||
<ContactRound size={16} />
|
<Code size={16} />
|
||||||
<Text as="span">{t("contact")}</Text>
|
<Text as="span">{t("source-code")}</Text>
|
||||||
</HStack>
|
</HStack>
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
@@ -290,15 +353,17 @@ export const Header = () => {
|
|||||||
</Button>
|
</Button>
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
<Button
|
<a
|
||||||
variant={"outline"}
|
href="https://git.the1s.de/Matthias-Claudius-Schule/borrow-system"
|
||||||
onClick={() => navigate("/contact", { replace: true })}
|
target="_blank"
|
||||||
>
|
>
|
||||||
<HStack gap={2}>
|
<Button variant="ghost">
|
||||||
<ContactRound size={18} />
|
<HStack gap={2}>
|
||||||
<Text as="span">{t("contact")}</Text>
|
<Code size={18} />
|
||||||
</HStack>
|
<Text as="span">{t("source-code")}</Text>
|
||||||
</Button>
|
</HStack>
|
||||||
|
</Button>
|
||||||
|
</a>
|
||||||
|
|
||||||
<Button onClick={logout} variant="outline" colorScheme="red">
|
<Button onClick={logout} variant="outline" colorScheme="red">
|
||||||
<HStack gap={2}>
|
<HStack gap={2}>
|
||||||
@@ -311,12 +376,145 @@ export const Header = () => {
|
|||||||
|
|
||||||
{/* User Info Dialoge */}
|
{/* User Info Dialoge */}
|
||||||
{userDialog && (
|
{userDialog && (
|
||||||
<UserDialogue
|
<Flex
|
||||||
setUserDialog={setUserDialog}
|
position="fixed"
|
||||||
fullname={fullname}
|
inset={0}
|
||||||
randomColor={randomColor}
|
zIndex={1000}
|
||||||
/>
|
align="center"
|
||||||
|
justify="center"
|
||||||
|
bg="blackAlpha.400"
|
||||||
|
backdropFilter="blur(6px)"
|
||||||
|
>
|
||||||
|
<Card.Root maxW="sm" w="full" mx={4}>
|
||||||
|
<Card.Header>
|
||||||
|
<Card.Title>
|
||||||
|
<Flex justify="center" align="center" w="100%">
|
||||||
|
<Avatar.Root
|
||||||
|
size={"2xl"}
|
||||||
|
colorPalette={randomColor[Math.floor(Math.random() * 10)]}
|
||||||
|
>
|
||||||
|
<Avatar.Fallback name={fullname} />
|
||||||
|
</Avatar.Root>
|
||||||
|
</Flex>
|
||||||
|
</Card.Title>
|
||||||
|
<Card.Description>{t("user-info-desc")}</Card.Description>
|
||||||
|
</Card.Header>
|
||||||
|
<Card.Body>
|
||||||
|
<Stack gap="4" w="full">
|
||||||
|
<Box as="dl">
|
||||||
|
<Grid
|
||||||
|
templateColumns="auto 1fr"
|
||||||
|
rowGap={2}
|
||||||
|
columnGap={4}
|
||||||
|
alignItems="start"
|
||||||
|
>
|
||||||
|
<Text as="dt" fontWeight="bold" textAlign="left">
|
||||||
|
{t("first-name")}:
|
||||||
|
</Text>
|
||||||
|
<Text as="dd">{userData.first_name}</Text>
|
||||||
|
|
||||||
|
<Text as="dt" fontWeight="bold" textAlign="left">
|
||||||
|
{t("last-name")}:
|
||||||
|
</Text>
|
||||||
|
<Text as="dd">{userData.last_name}</Text>
|
||||||
|
|
||||||
|
<Text as="dt" fontWeight="bold" textAlign="left">
|
||||||
|
{t("username")}:
|
||||||
|
</Text>
|
||||||
|
<Text as="dd">{userData.username}</Text>
|
||||||
|
|
||||||
|
<Text as="dt" fontWeight="bold" textAlign="left">
|
||||||
|
{t("role")}:
|
||||||
|
</Text>
|
||||||
|
<Text as="dd">{userData.role}</Text>
|
||||||
|
|
||||||
|
<Text as="dt" fontWeight="bold" textAlign="left">
|
||||||
|
{t("admin-status")}:
|
||||||
|
</Text>
|
||||||
|
<Text as="dd">
|
||||||
|
{userData.is_admin ? t("yes") : t("no")}
|
||||||
|
</Text>
|
||||||
|
</Grid>
|
||||||
|
</Box>
|
||||||
|
|
||||||
|
<Button variant="solid" onClick={() => setPwOpen(true)}>
|
||||||
|
<HStack gap={2}>
|
||||||
|
<RotateCcwKey size={18} />
|
||||||
|
<Text as="span">{t("change-password")}</Text>
|
||||||
|
</HStack>
|
||||||
|
</Button>
|
||||||
|
</Stack>
|
||||||
|
</Card.Body>
|
||||||
|
<Card.Footer justifyContent="flex-end">
|
||||||
|
<Button variant="outline" onClick={() => setUserDialog(false)}>
|
||||||
|
{t("cancel")}
|
||||||
|
</Button>
|
||||||
|
</Card.Footer>
|
||||||
|
</Card.Root>
|
||||||
|
</Flex>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
|
{/* Passwort-Dialog (kontrolliert) */}
|
||||||
|
<Dialog.Root open={isPwOpen} onOpenChange={(e: any) => setPwOpen(e.open)}>
|
||||||
|
<Portal>
|
||||||
|
<Dialog.Backdrop />
|
||||||
|
<Dialog.Positioner>
|
||||||
|
<Dialog.Content maxW="md">
|
||||||
|
<Dialog.Header>
|
||||||
|
<Dialog.Title>{t("change-password")}</Dialog.Title>
|
||||||
|
</Dialog.Header>
|
||||||
|
<form
|
||||||
|
onSubmit={(e) => {
|
||||||
|
e.preventDefault();
|
||||||
|
changePassword();
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Dialog.Body>
|
||||||
|
<Stack gap={3}>
|
||||||
|
<PasswordInput
|
||||||
|
value={oldPassword}
|
||||||
|
onChange={(e) => setOldPassword(e.target.value)}
|
||||||
|
placeholder={t("old-password")}
|
||||||
|
/>
|
||||||
|
<PasswordInput
|
||||||
|
value={newPassword}
|
||||||
|
onChange={(e) => setNewPassword(e.target.value)}
|
||||||
|
placeholder={t("new-password")}
|
||||||
|
/>
|
||||||
|
<PasswordInput
|
||||||
|
value={confirmPassword}
|
||||||
|
onChange={(e) => setConfirmPassword(e.target.value)}
|
||||||
|
placeholder={t("confirm-password")}
|
||||||
|
/>
|
||||||
|
</Stack>
|
||||||
|
</Dialog.Body>
|
||||||
|
<Dialog.Footer>
|
||||||
|
<Stack w="100%" gap={3}>
|
||||||
|
{isMsg && (
|
||||||
|
<MyAlert
|
||||||
|
status={msgStatus}
|
||||||
|
title={msgTitle}
|
||||||
|
description={msgDescription}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
<HStack justify="flex-end" gap={2}>
|
||||||
|
<Dialog.ActionTrigger asChild>
|
||||||
|
<Button variant="outline">{t("cancel")}</Button>
|
||||||
|
</Dialog.ActionTrigger>
|
||||||
|
<Button type="submit" colorScheme="teal">
|
||||||
|
{t("save")}
|
||||||
|
</Button>
|
||||||
|
</HStack>
|
||||||
|
</Stack>
|
||||||
|
</Dialog.Footer>
|
||||||
|
</form>
|
||||||
|
<Dialog.CloseTrigger asChild>
|
||||||
|
<CloseButton size="sm" />
|
||||||
|
</Dialog.CloseTrigger>
|
||||||
|
</Dialog.Content>
|
||||||
|
</Dialog.Positioner>
|
||||||
|
</Portal>
|
||||||
|
</Dialog.Root>
|
||||||
</Stack>
|
</Stack>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,220 +0,0 @@
|
|||||||
import {
|
|
||||||
Button,
|
|
||||||
Flex,
|
|
||||||
Stack,
|
|
||||||
Text,
|
|
||||||
CloseButton,
|
|
||||||
Dialog,
|
|
||||||
Portal,
|
|
||||||
HStack,
|
|
||||||
Box,
|
|
||||||
Avatar,
|
|
||||||
Card,
|
|
||||||
Grid,
|
|
||||||
} from "@chakra-ui/react";
|
|
||||||
import { PasswordInput } from "@/components/ui/password-input";
|
|
||||||
import { RotateCcwKey } from "lucide-react";
|
|
||||||
import MyAlert from "./myChakra/MyAlert";
|
|
||||||
import { API_BASE } from "@/config/api.config";
|
|
||||||
import { useUserContext } from "@/states/Context";
|
|
||||||
import { useState } from "react";
|
|
||||||
import { useTranslation } from "react-i18next";
|
|
||||||
import Cookies from "js-cookie";
|
|
||||||
|
|
||||||
type UserDialogueProps = {
|
|
||||||
setUserDialog: (value: boolean) => void;
|
|
||||||
fullname: string;
|
|
||||||
randomColor: string[];
|
|
||||||
};
|
|
||||||
|
|
||||||
export const UserDialogue = (props: UserDialogueProps) => {
|
|
||||||
const userData = useUserContext();
|
|
||||||
const { t } = useTranslation();
|
|
||||||
// Error handling states
|
|
||||||
const [isMsg, setIsMsg] = useState(false);
|
|
||||||
const [msgStatus, setMsgStatus] = useState<"error" | "success">("error");
|
|
||||||
const [msgTitle, setMsgTitle] = useState("");
|
|
||||||
const [msgDescription, setMsgDescription] = useState("");
|
|
||||||
|
|
||||||
const [oldPassword, setOldPassword] = useState("");
|
|
||||||
const [newPassword, setNewPassword] = useState("");
|
|
||||||
const [confirmPassword, setConfirmPassword] = useState("");
|
|
||||||
|
|
||||||
// Dialog control
|
|
||||||
const [isPwOpen, setPwOpen] = useState(false);
|
|
||||||
|
|
||||||
const changePassword = async () => {
|
|
||||||
if (newPassword !== confirmPassword) {
|
|
||||||
setMsgTitle(t("err_pw_change"));
|
|
||||||
setMsgDescription(t("pw_mismatch"));
|
|
||||||
setMsgStatus("error");
|
|
||||||
setIsMsg(true);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const response = await fetch(`${API_BASE}/api/users/change-password`, {
|
|
||||||
method: "POST",
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
Authorization: `Bearer ${Cookies.get("token")}`,
|
|
||||||
},
|
|
||||||
body: JSON.stringify({ oldPassword, newPassword }),
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
setMsgTitle(t("err_pw_change"));
|
|
||||||
setMsgDescription(t("pw_mismatch"));
|
|
||||||
setMsgStatus("error");
|
|
||||||
setIsMsg(true);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
setMsgTitle(t("pw_success"));
|
|
||||||
setMsgDescription(t("pw_success_desc"));
|
|
||||||
setMsgStatus("success");
|
|
||||||
setIsMsg(true);
|
|
||||||
|
|
||||||
setOldPassword("");
|
|
||||||
setNewPassword("");
|
|
||||||
setConfirmPassword("");
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
|
||||||
<Flex
|
|
||||||
position="fixed"
|
|
||||||
inset={0}
|
|
||||||
zIndex={1000}
|
|
||||||
align="center"
|
|
||||||
justify="center"
|
|
||||||
bg="blackAlpha.400"
|
|
||||||
backdropFilter="blur(6px)"
|
|
||||||
>
|
|
||||||
<Card.Root maxW="sm" w="full" mx={4}>
|
|
||||||
<Card.Header>
|
|
||||||
<Card.Title>
|
|
||||||
<Flex justify="center" align="center" w="100%">
|
|
||||||
<Avatar.Root
|
|
||||||
size={"2xl"}
|
|
||||||
colorPalette={props.randomColor[Math.floor(Math.random() * 10)]}
|
|
||||||
>
|
|
||||||
<Avatar.Fallback name={props.fullname} />
|
|
||||||
</Avatar.Root>
|
|
||||||
</Flex>
|
|
||||||
</Card.Title>
|
|
||||||
<Card.Description>{t("user-info-desc")}</Card.Description>
|
|
||||||
</Card.Header>
|
|
||||||
<Card.Body>
|
|
||||||
<Stack gap="4" w="full">
|
|
||||||
<Box as="dl">
|
|
||||||
<Grid
|
|
||||||
templateColumns="auto 1fr"
|
|
||||||
rowGap={2}
|
|
||||||
columnGap={4}
|
|
||||||
alignItems="start"
|
|
||||||
>
|
|
||||||
<Text as="dt" fontWeight="bold" textAlign="left">
|
|
||||||
{t("first-name")}:
|
|
||||||
</Text>
|
|
||||||
<Text as="dd">{userData.first_name}</Text>
|
|
||||||
|
|
||||||
<Text as="dt" fontWeight="bold" textAlign="left">
|
|
||||||
{t("last-name")}:
|
|
||||||
</Text>
|
|
||||||
<Text as="dd">{userData.last_name}</Text>
|
|
||||||
|
|
||||||
<Text as="dt" fontWeight="bold" textAlign="left">
|
|
||||||
{t("username")}:
|
|
||||||
</Text>
|
|
||||||
<Text as="dd">{userData.username}</Text>
|
|
||||||
|
|
||||||
<Text as="dt" fontWeight="bold" textAlign="left">
|
|
||||||
{t("role")}:
|
|
||||||
</Text>
|
|
||||||
<Text as="dd">{userData.role}</Text>
|
|
||||||
|
|
||||||
<Text as="dt" fontWeight="bold" textAlign="left">
|
|
||||||
{t("admin-status")}:
|
|
||||||
</Text>
|
|
||||||
<Text as="dd">{userData.is_admin ? t("yes") : t("no")}</Text>
|
|
||||||
</Grid>
|
|
||||||
</Box>
|
|
||||||
|
|
||||||
<Button variant="solid" onClick={() => setPwOpen(true)}>
|
|
||||||
<HStack gap={2}>
|
|
||||||
<RotateCcwKey size={18} />
|
|
||||||
<Text as="span">{t("change-password")}</Text>
|
|
||||||
</HStack>
|
|
||||||
</Button>
|
|
||||||
</Stack>
|
|
||||||
</Card.Body>
|
|
||||||
<Card.Footer justifyContent="flex-end">
|
|
||||||
<Button variant="outline" onClick={() => props.setUserDialog(false)}>
|
|
||||||
{t("cancel")}
|
|
||||||
</Button>
|
|
||||||
</Card.Footer>
|
|
||||||
</Card.Root>
|
|
||||||
|
|
||||||
{/* Passwort-Dialog (kontrolliert) */}
|
|
||||||
<Dialog.Root open={isPwOpen} onOpenChange={(e: any) => setPwOpen(e.open)}>
|
|
||||||
<Portal>
|
|
||||||
<Dialog.Backdrop />
|
|
||||||
<Dialog.Positioner>
|
|
||||||
<Dialog.Content maxW="md">
|
|
||||||
<Dialog.Header>
|
|
||||||
<Dialog.Title>{t("change-password")}</Dialog.Title>
|
|
||||||
</Dialog.Header>
|
|
||||||
<form
|
|
||||||
onSubmit={(e) => {
|
|
||||||
e.preventDefault();
|
|
||||||
changePassword();
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
<Dialog.Body>
|
|
||||||
<Stack gap={3}>
|
|
||||||
<PasswordInput
|
|
||||||
value={oldPassword}
|
|
||||||
onChange={(e) => setOldPassword(e.target.value)}
|
|
||||||
placeholder={t("old-password")}
|
|
||||||
/>
|
|
||||||
<PasswordInput
|
|
||||||
value={newPassword}
|
|
||||||
onChange={(e) => setNewPassword(e.target.value)}
|
|
||||||
placeholder={t("new-password")}
|
|
||||||
/>
|
|
||||||
<PasswordInput
|
|
||||||
value={confirmPassword}
|
|
||||||
onChange={(e) => setConfirmPassword(e.target.value)}
|
|
||||||
placeholder={t("confirm-password")}
|
|
||||||
/>
|
|
||||||
</Stack>
|
|
||||||
</Dialog.Body>
|
|
||||||
<Dialog.Footer>
|
|
||||||
<Stack w="100%" gap={3}>
|
|
||||||
{isMsg && (
|
|
||||||
<MyAlert
|
|
||||||
status={msgStatus}
|
|
||||||
title={msgTitle}
|
|
||||||
description={msgDescription}
|
|
||||||
/>
|
|
||||||
)}
|
|
||||||
<HStack justify="flex-end" gap={2}>
|
|
||||||
<Dialog.ActionTrigger asChild>
|
|
||||||
<Button variant="outline">{t("cancel")}</Button>
|
|
||||||
</Dialog.ActionTrigger>
|
|
||||||
<Button type="submit" colorScheme="teal">
|
|
||||||
{t("save")}
|
|
||||||
</Button>
|
|
||||||
</HStack>
|
|
||||||
</Stack>
|
|
||||||
</Dialog.Footer>
|
|
||||||
</form>
|
|
||||||
<Dialog.CloseTrigger asChild>
|
|
||||||
<CloseButton size="sm" />
|
|
||||||
</Dialog.CloseTrigger>
|
|
||||||
</Dialog.Content>
|
|
||||||
</Dialog.Positioner>
|
|
||||||
</Portal>
|
|
||||||
</Dialog.Root>
|
|
||||||
</Flex>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
@@ -14,7 +14,7 @@ export const Footer = () => {
|
|||||||
left="0"
|
left="0"
|
||||||
right="0"
|
right="0"
|
||||||
>
|
>
|
||||||
Made with ❤️ by Theis Gaedigk - Class of 2019 at MCS-Bochum
|
Made with ❤️ by Theis Gaedigk - Year 2019 at MCS-Bochum
|
||||||
<br />
|
<br />
|
||||||
Frontend-Version: {info ? info["frontend-info"].version : "N/A"} |
|
Frontend-Version: {info ? info["frontend-info"].version : "N/A"} |
|
||||||
Backend-Version: {info ? info["backend-info"].version : "N/A"}
|
Backend-Version: {info ? info["backend-info"].version : "N/A"}
|
||||||
|
|||||||
@@ -1,15 +1,23 @@
|
|||||||
"use client"
|
"use client";
|
||||||
|
|
||||||
import { ChakraProvider, defaultSystem } from "@chakra-ui/react"
|
import { ChakraProvider, defaultSystem } from "@chakra-ui/react";
|
||||||
import {
|
import * as React from "react";
|
||||||
ColorModeProvider,
|
import type { ReactNode } from "react";
|
||||||
type ColorModeProviderProps,
|
import { ColorModeProvider as ThemeColorModeProvider } from "./color-mode";
|
||||||
} from "./color-mode"
|
|
||||||
|
|
||||||
export function Provider(props: ColorModeProviderProps) {
|
export interface ColorModeProviderProps {
|
||||||
|
children: React.ReactNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function ColorModeProvider({ children }: ColorModeProviderProps) {
|
||||||
|
// Wrap children with the real color-mode provider
|
||||||
|
return <ThemeColorModeProvider>{children}</ThemeColorModeProvider>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function Provider({ children }: { children: ReactNode }) {
|
||||||
return (
|
return (
|
||||||
<ChakraProvider value={defaultSystem}>
|
<ChakraProvider value={defaultSystem}>
|
||||||
<ColorModeProvider {...props} />
|
<ColorModeProvider>{children}</ColorModeProvider>
|
||||||
</ChakraProvider>
|
</ChakraProvider>
|
||||||
)
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,77 +0,0 @@
|
|||||||
import { Field, Textarea, Button, Alert, Container } from "@chakra-ui/react";
|
|
||||||
import { useTranslation } from "react-i18next";
|
|
||||||
import { useState } from "react";
|
|
||||||
import { API_BASE } from "@/config/api.config";
|
|
||||||
import Cookies from "js-cookie";
|
|
||||||
import { Header } from "@/components/Header";
|
|
||||||
|
|
||||||
interface Alert {
|
|
||||||
type: "info" | "warning" | "success" | "error" | "neutral";
|
|
||||||
headline: string;
|
|
||||||
text: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const ContactPage = () => {
|
|
||||||
const { t } = useTranslation();
|
|
||||||
const [message, setMessage] = useState("");
|
|
||||||
const [alert, setAlert] = useState<Alert | null>(null);
|
|
||||||
|
|
||||||
const sendMessage = async () => {
|
|
||||||
// Logic to send the message
|
|
||||||
const result = await fetch(`${API_BASE}/api/users/contact`, {
|
|
||||||
method: "POST",
|
|
||||||
headers: {
|
|
||||||
Authorization: `Bearer ${Cookies.get("token") || ""}`,
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
Accept: "application/json",
|
|
||||||
},
|
|
||||||
body: JSON.stringify({ message }),
|
|
||||||
});
|
|
||||||
|
|
||||||
if (result.ok) {
|
|
||||||
setAlert({
|
|
||||||
type: "success",
|
|
||||||
headline: t("contactPage_successHeadline"),
|
|
||||||
text: t("contactPage_successText"),
|
|
||||||
});
|
|
||||||
setMessage("");
|
|
||||||
} else {
|
|
||||||
setAlert({
|
|
||||||
type: "error",
|
|
||||||
headline: t("contactPage_errorHeadline"),
|
|
||||||
text: t("contactPage_errorText"),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
|
||||||
<Container className="px-6 sm:px-8 pt-10">
|
|
||||||
<Header />
|
|
||||||
<Field.Root invalid={message === ""}>
|
|
||||||
<Field.Label>
|
|
||||||
{t("contactPage_messageLabel")}
|
|
||||||
<Field.RequiredIndicator />
|
|
||||||
</Field.Label>
|
|
||||||
<Textarea
|
|
||||||
placeholder={t("contactPage_messagePlaceholder")}
|
|
||||||
variant="subtle"
|
|
||||||
value={message}
|
|
||||||
onChange={(e) => setMessage(e.target.value)}
|
|
||||||
/>
|
|
||||||
{message === "" && (
|
|
||||||
<Field.ErrorText>{t("contactPage_messageErrorText")}</Field.ErrorText>
|
|
||||||
)}
|
|
||||||
</Field.Root>
|
|
||||||
{alert && (
|
|
||||||
<Alert.Root status={alert.type}>
|
|
||||||
<Alert.Indicator />
|
|
||||||
<Alert.Content>
|
|
||||||
<Alert.Title>{alert.headline}</Alert.Title>
|
|
||||||
<Alert.Description>{alert.text}</Alert.Description>
|
|
||||||
</Alert.Content>
|
|
||||||
</Alert.Root>
|
|
||||||
)}
|
|
||||||
<Button onClick={sendMessage}>{t("contactPage_sendButton")}</Button>
|
|
||||||
</Container>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
@@ -108,6 +108,7 @@ export const HomePage = () => {
|
|||||||
}
|
}
|
||||||
setBorrowableItems(response.data);
|
setBorrowableItems(response.data);
|
||||||
setIsMsg(false);
|
setIsMsg(false);
|
||||||
|
console.log(borrowableItems);
|
||||||
});
|
});
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { Button, Card, Field, Input, Stack } from "@chakra-ui/react";
|
|||||||
import { setIsLoggedInAtom, triggerLogoutAtom } from "@/states/Atoms";
|
import { setIsLoggedInAtom, triggerLogoutAtom } from "@/states/Atoms";
|
||||||
import { useAtom } from "jotai";
|
import { useAtom } from "jotai";
|
||||||
import Cookies from "js-cookie";
|
import Cookies from "js-cookie";
|
||||||
import { Navigate, useNavigate, useLocation } from "react-router-dom";
|
import { Navigate, useNavigate } from "react-router-dom";
|
||||||
import { PasswordInput } from "@/components/ui/password-input";
|
import { PasswordInput } from "@/components/ui/password-input";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import { Footer } from "@/components/footer/Footer";
|
import { Footer } from "@/components/footer/Footer";
|
||||||
@@ -16,15 +16,13 @@ export const LoginPage = () => {
|
|||||||
const [isLoggedIn, setIsLoggedIn] = useAtom(setIsLoggedInAtom);
|
const [isLoggedIn, setIsLoggedIn] = useAtom(setIsLoggedInAtom);
|
||||||
const [triggerLogout, setTriggerLogout] = useAtom(triggerLogoutAtom);
|
const [triggerLogout, setTriggerLogout] = useAtom(triggerLogoutAtom);
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const location = useLocation();
|
|
||||||
const from = location.state?.from?.pathname || "/";
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (isLoggedIn) {
|
if (isLoggedIn) {
|
||||||
navigate(from, { replace: true });
|
navigate("/", { replace: true });
|
||||||
window.location.reload(); // if deleted, the user context is not updated in time
|
window.location.reload(); // Wenn entfernt: Seite bleibt schwarz und muss manuell neu geladen werden
|
||||||
}
|
}
|
||||||
}, [isLoggedIn, navigate, from]);
|
}, [isLoggedIn, navigate]);
|
||||||
|
|
||||||
const loginFnc = async (username: string, password: string) => {
|
const loginFnc = async (username: string, password: string) => {
|
||||||
const response = await fetch(`${API_BASE}/api/users/login`, {
|
const response = await fetch(`${API_BASE}/api/users/login`, {
|
||||||
@@ -63,11 +61,11 @@ export const LoginPage = () => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
setTriggerLogout(false);
|
setTriggerLogout(false);
|
||||||
navigate(from, { replace: true });
|
navigate("/", { replace: true });
|
||||||
};
|
};
|
||||||
|
|
||||||
if (isLoggedIn) {
|
if (isLoggedIn) {
|
||||||
return <Navigate to={from} replace />;
|
return <Navigate to="/" replace />;
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -112,86 +112,6 @@ export const MyLoansPage = () => {
|
|||||||
return `${d}.${M}.${y} ${h}:${min}`;
|
return `${d}.${M}.${y} ${h}:${min}`;
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleTakeAction = async (loanCode: string) => {
|
|
||||||
try {
|
|
||||||
const res = await fetch(
|
|
||||||
`${API_BASE}/api/loans/set-take-date/${loanCode}`,
|
|
||||||
{
|
|
||||||
method: "POST",
|
|
||||||
headers: {
|
|
||||||
Authorization: `Bearer ${Cookies.get("token")}`,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!res.ok) {
|
|
||||||
setMsgStatus("error");
|
|
||||||
setMsgTitle(t("error"));
|
|
||||||
setMsgDescription(t("error-take-loan"));
|
|
||||||
setIsMsg(true);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update the loan in state
|
|
||||||
setLoans((prev) =>
|
|
||||||
prev.map((loan) =>
|
|
||||||
loan.loan_code === loanCode
|
|
||||||
? { ...loan, take_date: new Date().toISOString() }
|
|
||||||
: loan,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
setMsgStatus("success");
|
|
||||||
setMsgTitle(t("success"));
|
|
||||||
setMsgDescription(t("take-loan-success"));
|
|
||||||
setIsMsg(true);
|
|
||||||
} catch (e) {
|
|
||||||
setMsgStatus("error");
|
|
||||||
setMsgTitle(t("error"));
|
|
||||||
setMsgDescription(t("network-error"));
|
|
||||||
setIsMsg(true);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleReturnAction = async (loanCode: string) => {
|
|
||||||
try {
|
|
||||||
const res = await fetch(
|
|
||||||
`${API_BASE}/api/loans/set-return-date/${loanCode}`,
|
|
||||||
{
|
|
||||||
method: "POST",
|
|
||||||
headers: {
|
|
||||||
Authorization: `Bearer ${Cookies.get("token")}`,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!res.ok) {
|
|
||||||
setMsgStatus("error");
|
|
||||||
setMsgTitle(t("error"));
|
|
||||||
setMsgDescription(t("error-return-loan"));
|
|
||||||
setIsMsg(true);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update the loan in state
|
|
||||||
setLoans((prev) =>
|
|
||||||
prev.map((loan) =>
|
|
||||||
loan.loan_code === loanCode
|
|
||||||
? { ...loan, returned_date: new Date().toISOString() }
|
|
||||||
: loan,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
setMsgStatus("success");
|
|
||||||
setMsgTitle(t("success"));
|
|
||||||
setMsgDescription(t("return-loan-success"));
|
|
||||||
setIsMsg(true);
|
|
||||||
} catch (e) {
|
|
||||||
setMsgStatus("error");
|
|
||||||
setMsgTitle(t("error"));
|
|
||||||
setMsgDescription(t("network-error"));
|
|
||||||
setIsMsg(true);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Container className="px-6 sm:px-8 pt-10">
|
<Container className="px-6 sm:px-8 pt-10">
|
||||||
@@ -270,33 +190,8 @@ export const MyLoansPage = () => {
|
|||||||
: "-"}
|
: "-"}
|
||||||
</Text>
|
</Text>
|
||||||
</Table.Cell>
|
</Table.Cell>
|
||||||
<Table.Cell>
|
<Table.Cell>{formatDate(loan.take_date)}</Table.Cell>
|
||||||
{loan.take_date ? (
|
<Table.Cell>{formatDate(loan.returned_date)}</Table.Cell>
|
||||||
formatDate(loan.take_date)
|
|
||||||
) : (
|
|
||||||
<Button
|
|
||||||
size="xs"
|
|
||||||
colorPalette="teal"
|
|
||||||
onClick={() => handleTakeAction(loan.loan_code)}
|
|
||||||
>
|
|
||||||
{t("take")}
|
|
||||||
</Button>
|
|
||||||
)}
|
|
||||||
</Table.Cell>
|
|
||||||
<Table.Cell>
|
|
||||||
{loan.returned_date ? (
|
|
||||||
formatDate(loan.returned_date)
|
|
||||||
) : (
|
|
||||||
<Button
|
|
||||||
size="xs"
|
|
||||||
colorPalette="blue"
|
|
||||||
onClick={() => handleReturnAction(loan.loan_code)}
|
|
||||||
disabled={!loan.take_date}
|
|
||||||
>
|
|
||||||
{t("return")}
|
|
||||||
</Button>
|
|
||||||
)}
|
|
||||||
</Table.Cell>
|
|
||||||
<Table.Cell>{loan.note}</Table.Cell>
|
<Table.Cell>{loan.note}</Table.Cell>
|
||||||
<Table.Cell>
|
<Table.Cell>
|
||||||
<Dialog.Root role="alertdialog">
|
<Dialog.Root role="alertdialog">
|
||||||
|
|||||||
@@ -63,7 +63,7 @@
|
|||||||
"timezone-info": "Die angezeigten Daten und Uhrzeiten werden in deutscher Zeitzone dargestellt und müssen auch so eingegeben werden.",
|
"timezone-info": "Die angezeigten Daten und Uhrzeiten werden in deutscher Zeitzone dargestellt und müssen auch so eingegeben werden.",
|
||||||
"optional-note": "Optionale Notiz",
|
"optional-note": "Optionale Notiz",
|
||||||
"note": "Notiz",
|
"note": "Notiz",
|
||||||
"user-info-desc": "Hier können Sie Ihre persönlichen Informationen einsehen und das Passwort ändern. Falls Sie weitere Änderungen benötigen, wenden Sie sich bitte an einen Administrator.",
|
"user-info-desc": "Hier können Sie Ihre persönlichen Informationen einsehen und ändern.",
|
||||||
"role": "Rolle",
|
"role": "Rolle",
|
||||||
"admin-status": "Admin-Status",
|
"admin-status": "Admin-Status",
|
||||||
"first-name": "Vorname",
|
"first-name": "Vorname",
|
||||||
@@ -72,19 +72,5 @@
|
|||||||
"last-borrowed-person": "Zuletzt ausgeliehen von",
|
"last-borrowed-person": "Zuletzt ausgeliehen von",
|
||||||
"currently-borrowed-by": "Derzeit ausgeliehen von",
|
"currently-borrowed-by": "Derzeit ausgeliehen von",
|
||||||
"back": "Zurückgehen",
|
"back": "Zurückgehen",
|
||||||
"landingpage": "Übersichtsseite",
|
"landingpage": "Übersichtsseite"
|
||||||
"contactPage_successHeadline": "Nachricht erfolgreich gesendet",
|
|
||||||
"contactPage_successText": "Vielen Dank, dass Sie uns kontaktiert haben. Wir werden uns so schnell wie möglich bei Ihnen melden.",
|
|
||||||
"contactPage_errorHeadline": "Fehler beim Senden der Nachricht",
|
|
||||||
"contactPage_errorText": "Beim Senden Ihrer Nachricht ist ein Fehler aufgetreten. Bitte versuchen Sie es später erneut.",
|
|
||||||
"contactPage_sendButton": "Nachricht senden",
|
|
||||||
"contactPage_messageLabel": "Nachricht",
|
|
||||||
"contactPage_messagePlaceholder": "Geben Sie hier Ihre Nachricht ein...",
|
|
||||||
"contactPage_messageErrorText": "Dieses Feld darf nicht leer sein.",
|
|
||||||
"contact": "Kontakt",
|
|
||||||
"take": "Abholen",
|
|
||||||
"return": "Zurückgeben",
|
|
||||||
"take-loan-success": "Ausleihe erfolgreich abgeholt",
|
|
||||||
"return-loan-success": "Ausleihe erfolgreich zurückgegeben",
|
|
||||||
"network-error": "Netzwerkfehler. Kontaktieren Sie den Administrator."
|
|
||||||
}
|
}
|
||||||
@@ -63,7 +63,7 @@
|
|||||||
"timezone-info": "The displayed dates and times are shown in Berlin timezone and must also be entered as such.",
|
"timezone-info": "The displayed dates and times are shown in Berlin timezone and must also be entered as such.",
|
||||||
"optional-note": "Optional note",
|
"optional-note": "Optional note",
|
||||||
"note": "Note",
|
"note": "Note",
|
||||||
"user-info-desc": "Here you can view your personal information and change your password. If you need to make further changes, please contact an administrator.",
|
"user-info-desc": "Here you can view and edit your personal information.",
|
||||||
"role": "Role",
|
"role": "Role",
|
||||||
"admin-status": "Admin status",
|
"admin-status": "Admin status",
|
||||||
"first-name": "First name",
|
"first-name": "First name",
|
||||||
@@ -72,14 +72,5 @@
|
|||||||
"last-borrowed-person": "Last borrowed by",
|
"last-borrowed-person": "Last borrowed by",
|
||||||
"currently-borrowed-by": "Currently borrowed by",
|
"currently-borrowed-by": "Currently borrowed by",
|
||||||
"back": "Go back",
|
"back": "Go back",
|
||||||
"landingpage": "Overview page",
|
"landingpage": "Overview page"
|
||||||
"contactPage_successHeadline": "Message sent successfully",
|
|
||||||
"contactPage_successText": "Thank you for contacting us. We will get back to you as soon as possible.",
|
|
||||||
"contactPage_errorHeadline": "Error sending message",
|
|
||||||
"contactPage_errorText": "An error occurred while sending your message. Please try again later.",
|
|
||||||
"contactPage_sendButton": "Send message",
|
|
||||||
"contactPage_messageLabel": "Message",
|
|
||||||
"contactPage_messagePlaceholder": "Enter your message here...",
|
|
||||||
"contactPage_messageErrorText": "This field cannot be empty.",
|
|
||||||
"contact": "Contact"
|
|
||||||
}
|
}
|
||||||
@@ -1,16 +1,23 @@
|
|||||||
import { defineConfig } from "vite";
|
import { defineConfig } from "vite";
|
||||||
import react from "@vitejs/plugin-react";
|
|
||||||
import svgr from "vite-plugin-svgr";
|
|
||||||
import tailwindcss from "@tailwindcss/vite";
|
import tailwindcss from "@tailwindcss/vite";
|
||||||
import tsconfigPaths from "vite-tsconfig-paths";
|
import path from "node:path";
|
||||||
|
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
plugins: [react(), svgr(), tailwindcss(), tsconfigPaths()],
|
plugins: [tailwindcss()],
|
||||||
|
resolve: {
|
||||||
|
alias: {
|
||||||
|
"@": path.resolve(__dirname, "src"),
|
||||||
|
},
|
||||||
|
},
|
||||||
server: {
|
server: {
|
||||||
host: "0.0.0.0",
|
host: "0.0.0.0",
|
||||||
port: 8001,
|
allowedHosts: ["insta.the1s.de"],
|
||||||
watch: {
|
port: 8101,
|
||||||
usePolling: true,
|
watch: { usePolling: true },
|
||||||
|
hmr: {
|
||||||
|
host: "insta.the1s.de",
|
||||||
|
port: 8101,
|
||||||
|
protocol: "wss",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
+2
-2
@@ -1,10 +1,10 @@
|
|||||||
<!doctype html>
|
<!DOCTYPE html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8" />
|
<meta charset="UTF-8" />
|
||||||
<link rel="icon" type="image/svg+xml" href="/user-star.svg" />
|
<link rel="icon" type="image/svg+xml" href="/user-star.svg" />
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
<title>Adminpanel</title>
|
<title>Admin panel</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div id="root"></div>
|
<div id="root"></div>
|
||||||
|
|||||||
@@ -9,14 +9,6 @@ server {
|
|||||||
try_files $uri $uri/ /index.html;
|
try_files $uri $uri/ /index.html;
|
||||||
}
|
}
|
||||||
|
|
||||||
location = /backend {
|
|
||||||
return 301 /backend/;
|
|
||||||
}
|
|
||||||
|
|
||||||
location /backend/ {
|
|
||||||
proxy_pass http://borrow_system-backend_v2:8004/;
|
|
||||||
}
|
|
||||||
|
|
||||||
location ~* \.(?:js|mjs|css|png|jpg|jpeg|gif|ico|svg|woff2?)$ {
|
location ~* \.(?:js|mjs|css|png|jpg|jpeg|gif|ico|svg|woff2?)$ {
|
||||||
expires 1y;
|
expires 1y;
|
||||||
access_log off;
|
access_log off;
|
||||||
|
|||||||
@@ -63,6 +63,7 @@ const APIKeyTable: React.FC = () => {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
|
console.log(data);
|
||||||
return data;
|
return data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
setError("error", "Failed to fetch items", "There is an error");
|
setError("error", "Failed to fetch items", "There is an error");
|
||||||
|
|||||||
@@ -29,8 +29,8 @@ const AddItemForm: React.FC<AddItemFormProps> = ({ onClose, alert }) => {
|
|||||||
<Input id="item_name" placeholder="z.B. Laptop" />
|
<Input id="item_name" placeholder="z.B. Laptop" />
|
||||||
</Field.Root>
|
</Field.Root>
|
||||||
<Field.Root>
|
<Field.Root>
|
||||||
<Field.Label>Schließfachnummer</Field.Label>
|
<Field.Label>Schließfachnummer (immer zwei Zahlen)</Field.Label>
|
||||||
<Input id="safe_nr" placeholder="Nummer 1 - 6" />
|
<Input id="safe_nr" placeholder="Nummer 01 - 06" />
|
||||||
</Field.Root>
|
</Field.Root>
|
||||||
<Field.Root>
|
<Field.Root>
|
||||||
<Field.Label>Ausleih-Berechtigung (Rolle)</Field.Label>
|
<Field.Label>Ausleih-Berechtigung (Rolle)</Field.Label>
|
||||||
@@ -64,6 +64,7 @@ const AddItemForm: React.FC<AddItemFormProps> = ({ onClose, alert }) => {
|
|||||||
const safeNr = safeNrValue === "" ? null : safeNrValue;
|
const safeNr = safeNrValue === "" ? null : safeNrValue;
|
||||||
|
|
||||||
if (!name || Number.isNaN(role)) return;
|
if (!name || Number.isNaN(role)) return;
|
||||||
|
if (safeNr !== null && !/^\d{2}$/.test(safeNr)) return;
|
||||||
|
|
||||||
const res = await createItem(name, role, safeNr);
|
const res = await createItem(name, role, safeNr);
|
||||||
if (res.success) {
|
if (res.success) {
|
||||||
|
|||||||
@@ -38,7 +38,6 @@ type Items = {
|
|||||||
can_borrow_role: string;
|
can_borrow_role: string;
|
||||||
in_safe: boolean;
|
in_safe: boolean;
|
||||||
safe_nr: string;
|
safe_nr: string;
|
||||||
door_key: string;
|
|
||||||
entry_created_at: string;
|
entry_created_at: string;
|
||||||
entry_updated_at: string;
|
entry_updated_at: string;
|
||||||
last_borrowed_person: string | null;
|
last_borrowed_person: string | null;
|
||||||
@@ -73,12 +72,6 @@ const ItemTable: React.FC = () => {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleDoorKeyChange = (id: number, value: string) => {
|
|
||||||
setItems((prev) =>
|
|
||||||
prev.map((it) => (it.id === id ? { ...it, door_key: value } : it))
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
const setError = (
|
const setError = (
|
||||||
status: "error" | "success",
|
status: "error" | "success",
|
||||||
message: string,
|
message: string,
|
||||||
@@ -193,12 +186,7 @@ const ItemTable: React.FC = () => {
|
|||||||
|
|
||||||
{/* make table fill available width, like UserTable */}
|
{/* make table fill available width, like UserTable */}
|
||||||
{!isLoading && (
|
{!isLoading && (
|
||||||
<Table.Root
|
<Table.Root size="sm" striped w="100%" style={{ tableLayout: "auto" }}>
|
||||||
size="sm"
|
|
||||||
striped
|
|
||||||
w="100%"
|
|
||||||
style={{ tableLayout: "auto" }} // Spalten nach Content
|
|
||||||
>
|
|
||||||
<Table.Header>
|
<Table.Header>
|
||||||
<Table.Row>
|
<Table.Row>
|
||||||
<Table.ColumnHeader>
|
<Table.ColumnHeader>
|
||||||
@@ -213,12 +201,9 @@ const ItemTable: React.FC = () => {
|
|||||||
<Table.ColumnHeader>
|
<Table.ColumnHeader>
|
||||||
<strong>Im Schließfach</strong>
|
<strong>Im Schließfach</strong>
|
||||||
</Table.ColumnHeader>
|
</Table.ColumnHeader>
|
||||||
<Table.ColumnHeader width="1%" whiteSpace="nowrap">
|
<Table.ColumnHeader>
|
||||||
<strong>Schließfachnummer</strong>
|
<strong>Schließfachnummer</strong>
|
||||||
</Table.ColumnHeader>
|
</Table.ColumnHeader>
|
||||||
<Table.ColumnHeader width="1%" whiteSpace="nowrap">
|
|
||||||
<strong>Schlüssel</strong>
|
|
||||||
</Table.ColumnHeader>
|
|
||||||
<Table.ColumnHeader>
|
<Table.ColumnHeader>
|
||||||
<strong>Eintrag erstellt am</strong>
|
<strong>Eintrag erstellt am</strong>
|
||||||
</Table.ColumnHeader>
|
</Table.ColumnHeader>
|
||||||
@@ -231,7 +216,7 @@ const ItemTable: React.FC = () => {
|
|||||||
<Table.ColumnHeader>
|
<Table.ColumnHeader>
|
||||||
<strong>Dav **</strong>
|
<strong>Dav **</strong>
|
||||||
</Table.ColumnHeader>
|
</Table.ColumnHeader>
|
||||||
<Table.ColumnHeader width="1%" whiteSpace="nowrap">
|
<Table.ColumnHeader>
|
||||||
<strong>Aktionen</strong>
|
<strong>Aktionen</strong>
|
||||||
</Table.ColumnHeader>
|
</Table.ColumnHeader>
|
||||||
</Table.Row>
|
</Table.Row>
|
||||||
@@ -305,28 +290,17 @@ const ItemTable: React.FC = () => {
|
|||||||
value={item.safe_nr}
|
value={item.safe_nr}
|
||||||
/>
|
/>
|
||||||
</Table.Cell>
|
</Table.Cell>
|
||||||
<Table.Cell>
|
|
||||||
<Input
|
|
||||||
size="sm"
|
|
||||||
w="max-content"
|
|
||||||
onChange={(e) =>
|
|
||||||
handleDoorKeyChange(item.id, e.target.value)
|
|
||||||
}
|
|
||||||
value={item.door_key}
|
|
||||||
/>
|
|
||||||
</Table.Cell>
|
|
||||||
<Table.Cell>{formatDateTime(item.entry_created_at)}</Table.Cell>
|
<Table.Cell>{formatDateTime(item.entry_created_at)}</Table.Cell>
|
||||||
<Table.Cell>{formatDateTime(item.entry_updated_at)}</Table.Cell>
|
<Table.Cell>{formatDateTime(item.entry_updated_at)}</Table.Cell>
|
||||||
<Table.Cell>{item.last_borrowed_person}</Table.Cell>
|
<Table.Cell>{item.last_borrowed_person}</Table.Cell>
|
||||||
<Table.Cell>{item.currently_borrowing}</Table.Cell>
|
<Table.Cell>{item.currently_borrowing}</Table.Cell>
|
||||||
<Table.Cell whiteSpace="nowrap">
|
<Table.Cell>
|
||||||
<Button
|
<Button
|
||||||
onClick={() =>
|
onClick={() =>
|
||||||
handleEditItems(
|
handleEditItems(
|
||||||
item.id,
|
item.id,
|
||||||
item.item_name,
|
item.item_name,
|
||||||
item.safe_nr,
|
item.safe_nr,
|
||||||
item.door_key,
|
|
||||||
item.can_borrow_role
|
item.can_borrow_role
|
||||||
).then((response) => {
|
).then((response) => {
|
||||||
if (response.success) {
|
if (response.success) {
|
||||||
|
|||||||
@@ -85,6 +85,7 @@ const UserTable: React.FC = () => {
|
|||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
try {
|
try {
|
||||||
const data = await fetchUserData();
|
const data = await fetchUserData();
|
||||||
|
console.log(data);
|
||||||
if (Array.isArray(data)) {
|
if (Array.isArray(data)) {
|
||||||
setUsers(data);
|
setUsers(data);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -167,6 +167,7 @@ export const createItem = async (
|
|||||||
can_borrow_role: number,
|
can_borrow_role: number,
|
||||||
lockerNumber: string | null
|
lockerNumber: string | null
|
||||||
) => {
|
) => {
|
||||||
|
console.log(JSON.stringify({ item_name, can_borrow_role, lockerNumber }));
|
||||||
try {
|
try {
|
||||||
const response = await fetch(
|
const response = await fetch(
|
||||||
`${API_BASE}/api/admin/item-data/create-item`,
|
`${API_BASE}/api/admin/item-data/create-item`,
|
||||||
@@ -183,7 +184,7 @@ export const createItem = async (
|
|||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
message:
|
message:
|
||||||
"Fehler beim Erstellen des Gegenstands. Der Name des Gegenstandes und die Schließfachnummer dürfen nicht mehrmals vergeben werden.",
|
"Fehler beim Erstellen des Gegenstands. Der Name des Gegenstandes darf nicht mehrmals vergeben werden.",
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
return { success: true };
|
return { success: true };
|
||||||
@@ -197,7 +198,6 @@ export const handleEditItems = async (
|
|||||||
itemId: number,
|
itemId: number,
|
||||||
item_name: string,
|
item_name: string,
|
||||||
safe_nr: string | null,
|
safe_nr: string | null,
|
||||||
door_key: string | null,
|
|
||||||
can_borrow_role: string
|
can_borrow_role: string
|
||||||
) => {
|
) => {
|
||||||
try {
|
try {
|
||||||
@@ -209,7 +209,7 @@ export const handleEditItems = async (
|
|||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
Authorization: `Bearer ${Cookies.get("token")}`,
|
Authorization: `Bearer ${Cookies.get("token")}`,
|
||||||
},
|
},
|
||||||
body: JSON.stringify({ item_name, safe_nr, door_key, can_borrow_role }),
|
body: JSON.stringify({ item_name, safe_nr, can_borrow_role }),
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
|
|||||||
@@ -8,9 +8,13 @@ export default defineConfig({
|
|||||||
plugins: [react(), svgr(), tailwindcss(), tsconfigPaths()],
|
plugins: [react(), svgr(), tailwindcss(), tsconfigPaths()],
|
||||||
server: {
|
server: {
|
||||||
host: "0.0.0.0",
|
host: "0.0.0.0",
|
||||||
port: 8003,
|
allowedHosts: ["admin.insta.the1s.de"],
|
||||||
watch: {
|
port: 8103,
|
||||||
usePolling: true,
|
watch: { usePolling: true },
|
||||||
|
hmr: {
|
||||||
|
host: "admin.insta.the1s.de",
|
||||||
|
port: 8103,
|
||||||
|
protocol: "wss",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
+3
-3
@@ -1,11 +1,11 @@
|
|||||||
{
|
{
|
||||||
"backend-info": {
|
"backend-info": {
|
||||||
"version": "v2.0.1 (dev)"
|
"version": "v2.0 (testing)"
|
||||||
},
|
},
|
||||||
"frontend-info": {
|
"frontend-info": {
|
||||||
"version": "v2.0 (dev)"
|
"version": "v2.0 (testing)"
|
||||||
},
|
},
|
||||||
"admin-panel-info": {
|
"admin-panel-info": {
|
||||||
"version": "v1.3 (dev)"
|
"version": "v1.2 (testing)"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -36,8 +36,7 @@ export const editItemById = async (
|
|||||||
itemId,
|
itemId,
|
||||||
item_name,
|
item_name,
|
||||||
can_borrow_role,
|
can_borrow_role,
|
||||||
safe_nr,
|
safe_nr
|
||||||
door_key
|
|
||||||
) => {
|
) => {
|
||||||
let newSafeNr;
|
let newSafeNr;
|
||||||
if (safe_nr === null || safe_nr === "") {
|
if (safe_nr === null || safe_nr === "") {
|
||||||
@@ -46,8 +45,8 @@ export const editItemById = async (
|
|||||||
newSafeNr = safe_nr;
|
newSafeNr = safe_nr;
|
||||||
}
|
}
|
||||||
const [result] = await pool.query(
|
const [result] = await pool.query(
|
||||||
"UPDATE items SET item_name = ?, can_borrow_role = ?, safe_nr = ?, door_key = ?, entry_updated_at = NOW() WHERE id = ?",
|
"UPDATE items SET item_name = ?, can_borrow_role = ?, safe_nr = ?, entry_updated_at = NOW() WHERE id = ?",
|
||||||
[item_name, can_borrow_role, newSafeNr, door_key, itemId]
|
[item_name, can_borrow_role, newSafeNr, itemId]
|
||||||
);
|
);
|
||||||
if (result.affectedRows > 0) return { success: true };
|
if (result.affectedRows > 0) return { success: true };
|
||||||
return { success: false };
|
return { success: false };
|
||||||
|
|||||||
@@ -18,11 +18,11 @@ export const createUser = async (
|
|||||||
isAdmin,
|
isAdmin,
|
||||||
email,
|
email,
|
||||||
first_name,
|
first_name,
|
||||||
last_name,
|
last_name
|
||||||
) => {
|
) => {
|
||||||
const [result] = await pool.query(
|
const [result] = await pool.query(
|
||||||
"INSERT INTO users (username, role, password, is_admin, email, first_name, last_name) VALUES (?, ?, ?, ?, ?, ?, ?)",
|
"INSERT INTO users (username, role, password, is_admin, email, first_name, last_name) VALUES (?, ?, ?, ?, ?, ?, ?)",
|
||||||
[username, role, password, isAdmin, email, first_name, last_name],
|
[username, role, password, isAdmin, email, first_name, last_name]
|
||||||
);
|
);
|
||||||
if (result.affectedRows > 0) return { success: true };
|
if (result.affectedRows > 0) return { success: true };
|
||||||
return { success: false };
|
return { success: false };
|
||||||
@@ -34,10 +34,10 @@ export const deleteUserById = async (userId) => {
|
|||||||
return { success: false };
|
return { success: false };
|
||||||
};
|
};
|
||||||
|
|
||||||
export const changePassword = async (username, newPassword) => {
|
export const changePassword = async (userId, newPassword) => {
|
||||||
const [result] = await pool.query(
|
const [result] = await pool.query(
|
||||||
"UPDATE users SET password = ?, entry_updated_at = NOW() WHERE username = ?",
|
"UPDATE users SET password = ?, entry_updated_at = NOW() WHERE id = ?",
|
||||||
[newPassword, username],
|
[newPassword, userId]
|
||||||
);
|
);
|
||||||
if (result.affectedRows > 0) return { success: true };
|
if (result.affectedRows > 0) return { success: true };
|
||||||
return { success: false };
|
return { success: false };
|
||||||
@@ -49,11 +49,11 @@ export const editUserById = async (
|
|||||||
last_name,
|
last_name,
|
||||||
role,
|
role,
|
||||||
email,
|
email,
|
||||||
is_admin,
|
is_admin
|
||||||
) => {
|
) => {
|
||||||
const [result] = await pool.query(
|
const [result] = await pool.query(
|
||||||
"UPDATE users SET first_name = ?, last_name = ?, role = ?, email = ?, is_admin = ?, entry_updated_at = NOW() WHERE id = ?",
|
"UPDATE users SET first_name = ?, last_name = ?, role = ?, email = ?, is_admin = ?, entry_updated_at = NOW() WHERE id = ?",
|
||||||
[first_name, last_name, role, email, is_admin, userId],
|
[first_name, last_name, role, email, is_admin, userId]
|
||||||
);
|
);
|
||||||
if (result.affectedRows > 0) return { success: true };
|
if (result.affectedRows > 0) return { success: true };
|
||||||
return { success: false };
|
return { success: false };
|
||||||
@@ -61,7 +61,7 @@ export const editUserById = async (
|
|||||||
|
|
||||||
export const getAllUsers = async () => {
|
export const getAllUsers = async () => {
|
||||||
const [result] = await pool.query(
|
const [result] = await pool.query(
|
||||||
"SELECT id, username, first_name, last_name, role, email, is_admin, entry_created_at, entry_updated_at FROM users",
|
"SELECT id, username, first_name, last_name, role, email, is_admin, entry_created_at, entry_updated_at FROM users"
|
||||||
);
|
);
|
||||||
if (result.length > 0) return { success: true, data: result };
|
if (result.length > 0) return { success: true, data: result };
|
||||||
return { success: false };
|
return { success: false };
|
||||||
@@ -70,7 +70,7 @@ export const getAllUsers = async () => {
|
|||||||
export const getUserById = async (userId) => {
|
export const getUserById = async (userId) => {
|
||||||
const [rows] = await pool.query(
|
const [rows] = await pool.query(
|
||||||
"SELECT id, username, first_name, last_name, role, email, is_admin FROM users WHERE id = ?",
|
"SELECT id, username, first_name, last_name, role, email, is_admin FROM users WHERE id = ?",
|
||||||
[userId],
|
[userId]
|
||||||
);
|
);
|
||||||
if (rows.length === 0) {
|
if (rows.length === 0) {
|
||||||
return { success: false };
|
return { success: false };
|
||||||
|
|||||||
@@ -41,14 +41,13 @@ router.post("/create-item", authenticateAdmin, async (req, res) => {
|
|||||||
|
|
||||||
router.post("/edit-item/:id", authenticateAdmin, async (req, res) => {
|
router.post("/edit-item/:id", authenticateAdmin, async (req, res) => {
|
||||||
const itemId = req.params.id;
|
const itemId = req.params.id;
|
||||||
const { item_name, can_borrow_role, safe_nr, door_key } = req.body;
|
const { item_name, can_borrow_role, safe_nr } = req.body;
|
||||||
|
|
||||||
const result = await editItemById(
|
const result = await editItemById(
|
||||||
itemId,
|
itemId,
|
||||||
item_name,
|
item_name,
|
||||||
can_borrow_role,
|
can_borrow_role,
|
||||||
safe_nr,
|
safe_nr
|
||||||
door_key
|
|
||||||
);
|
);
|
||||||
if (result.success) {
|
if (result.success) {
|
||||||
return res.status(200).json({ message: "Item edited successfully" });
|
return res.status(200).json({ message: "Item edited successfully" });
|
||||||
|
|||||||
@@ -114,22 +114,3 @@ export const getAllLoansV2 = async () => {
|
|||||||
}
|
}
|
||||||
return { success: false };
|
return { success: false };
|
||||||
};
|
};
|
||||||
|
|
||||||
export const openDoor = async (doorKey) => {
|
|
||||||
const [result] = await pool.query(
|
|
||||||
"SELECT safe_nr, id FROM items WHERE door_key = ?;",
|
|
||||||
[doorKey]
|
|
||||||
);
|
|
||||||
if (result.length > 0) {
|
|
||||||
const [changeItemSate] = await pool.query(
|
|
||||||
"UPDATE items SET in_safe = NOT in_safe WHERE id = ?",
|
|
||||||
[result[0].id]
|
|
||||||
);
|
|
||||||
if (changeItemSate.affectedRows > 0) {
|
|
||||||
return { success: true, data: result[0] };
|
|
||||||
} else {
|
|
||||||
return { success: false };
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return { success: false };
|
|
||||||
};
|
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ import {
|
|||||||
setTakeDateV2,
|
setTakeDateV2,
|
||||||
setReturnDateV2,
|
setReturnDateV2,
|
||||||
getLoanByCodeV2,
|
getLoanByCodeV2,
|
||||||
openDoor,
|
|
||||||
} from "./api.database.js";
|
} from "./api.database.js";
|
||||||
|
|
||||||
// Route for API to get all items from the database
|
// Route for API to get all items from the database
|
||||||
@@ -80,16 +79,4 @@ router.post(
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
// Route for API to open a door
|
|
||||||
router.get("/open-door/:key/:doorKey", authenticate, async (req, res) => {
|
|
||||||
const doorKey = req.params.doorKey;
|
|
||||||
|
|
||||||
const result = await openDoor(doorKey);
|
|
||||||
if (result.success) {
|
|
||||||
res.status(200).json({ data: result.data });
|
|
||||||
} else {
|
|
||||||
res.status(500).json({ message: "Failed to open door" });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
export default router;
|
export default router;
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ export const createLoanInDatabase = async (
|
|||||||
startDate,
|
startDate,
|
||||||
endDate,
|
endDate,
|
||||||
note,
|
note,
|
||||||
itemIds,
|
itemIds
|
||||||
) => {
|
) => {
|
||||||
if (!username)
|
if (!username)
|
||||||
return { success: false, code: "BAD_REQUEST", message: "Missing username" };
|
return { success: false, code: "BAD_REQUEST", message: "Missing username" };
|
||||||
@@ -52,7 +52,7 @@ export const createLoanInDatabase = async (
|
|||||||
// Ensure all items exist and collect names + lockers
|
// Ensure all items exist and collect names + lockers
|
||||||
const [itemsRows] = await conn.query(
|
const [itemsRows] = await conn.query(
|
||||||
"SELECT id, item_name, safe_nr FROM items WHERE id IN (?)",
|
"SELECT id, item_name, safe_nr FROM items WHERE id IN (?)",
|
||||||
[itemIds],
|
[itemIds]
|
||||||
);
|
);
|
||||||
if (!itemsRows || itemsRows.length !== itemIds.length) {
|
if (!itemsRows || itemsRows.length !== itemIds.length) {
|
||||||
await conn.rollback();
|
await conn.rollback();
|
||||||
@@ -65,24 +65,16 @@ export const createLoanInDatabase = async (
|
|||||||
|
|
||||||
const itemNames = itemIds
|
const itemNames = itemIds
|
||||||
.map(
|
.map(
|
||||||
(id) => itemsRows.find((r) => Number(r.id) === Number(id))?.item_name,
|
(id) => itemsRows.find((r) => Number(r.id) === Number(id))?.item_name
|
||||||
)
|
)
|
||||||
.filter(Boolean);
|
.filter(Boolean);
|
||||||
|
|
||||||
// Build lockers array (unique, only 2-digit numbers from safe_nr)
|
// Build lockers array (unique, only 2-digit strings)
|
||||||
const lockers = [
|
const lockers = [
|
||||||
...new Set(
|
...new Set(
|
||||||
itemsRows
|
itemsRows
|
||||||
.map((r) => r.safe_nr)
|
.map((r) => r.safe_nr)
|
||||||
.filter(
|
.filter((sn) => typeof sn === "string" && /^\d{2}$/.test(sn))
|
||||||
(sn) =>
|
|
||||||
sn !== null &&
|
|
||||||
sn !== undefined &&
|
|
||||||
Number.isInteger(Number(sn)) &&
|
|
||||||
Number(sn) >= 0 &&
|
|
||||||
Number(sn) <= 99,
|
|
||||||
)
|
|
||||||
.map((sn) => Number(sn)),
|
|
||||||
),
|
),
|
||||||
];
|
];
|
||||||
|
|
||||||
@@ -98,7 +90,7 @@ export const createLoanInDatabase = async (
|
|||||||
AND l.start_date < ?
|
AND l.start_date < ?
|
||||||
AND COALESCE(l.returned_date, l.end_date) > ?
|
AND COALESCE(l.returned_date, l.end_date) > ?
|
||||||
`,
|
`,
|
||||||
[itemIds, end, start],
|
[itemIds, end, start]
|
||||||
);
|
);
|
||||||
if (confRows?.[0]?.conflicts > 0) {
|
if (confRows?.[0]?.conflicts > 0) {
|
||||||
await conn.rollback();
|
await conn.rollback();
|
||||||
@@ -115,7 +107,7 @@ export const createLoanInDatabase = async (
|
|||||||
const candidate = Math.floor(100000 + Math.random() * 899999); // 6 digits
|
const candidate = Math.floor(100000 + Math.random() * 899999); // 6 digits
|
||||||
const [exists] = await conn.query(
|
const [exists] = await conn.query(
|
||||||
"SELECT 1 FROM loans WHERE loan_code = ? LIMIT 1",
|
"SELECT 1 FROM loans WHERE loan_code = ? LIMIT 1",
|
||||||
[candidate],
|
[candidate]
|
||||||
);
|
);
|
||||||
if (exists.length === 0) {
|
if (exists.length === 0) {
|
||||||
loanCode = candidate;
|
loanCode = candidate;
|
||||||
@@ -146,7 +138,7 @@ export const createLoanInDatabase = async (
|
|||||||
JSON.stringify(itemIds.map((n) => Number(n))),
|
JSON.stringify(itemIds.map((n) => Number(n))),
|
||||||
JSON.stringify(itemNames),
|
JSON.stringify(itemNames),
|
||||||
note,
|
note,
|
||||||
],
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
await conn.commit();
|
await conn.commit();
|
||||||
@@ -189,7 +181,7 @@ export const getLoanInfoWithID = async (loanId) => {
|
|||||||
export const getLoansFromDatabase = async (username) => {
|
export const getLoansFromDatabase = async (username) => {
|
||||||
const [result] = await pool.query(
|
const [result] = await pool.query(
|
||||||
"SELECT * FROM loans WHERE username = ? AND deleted = 0;",
|
"SELECT * FROM loans WHERE username = ? AND deleted = 0;",
|
||||||
[username],
|
[username]
|
||||||
);
|
);
|
||||||
if (result.length > 0) {
|
if (result.length > 0) {
|
||||||
return { success: true, status: true, data: result };
|
return { success: true, status: true, data: result };
|
||||||
@@ -202,7 +194,7 @@ export const getLoansFromDatabase = async (username) => {
|
|||||||
export const getBorrowableItemsFromDatabase = async (
|
export const getBorrowableItemsFromDatabase = async (
|
||||||
startDate,
|
startDate,
|
||||||
endDate,
|
endDate,
|
||||||
role = 0,
|
role = 0
|
||||||
) => {
|
) => {
|
||||||
// Overlap if: loan.start < end AND effective_end > start
|
// Overlap if: loan.start < end AND effective_end > start
|
||||||
// effective_end is returned_date if set, otherwise end_date
|
// effective_end is returned_date if set, otherwise end_date
|
||||||
@@ -236,7 +228,7 @@ export const getBorrowableItemsFromDatabase = async (
|
|||||||
export const SETdeleteLoanFromDatabase = async (loanId) => {
|
export const SETdeleteLoanFromDatabase = async (loanId) => {
|
||||||
const [result] = await pool.query(
|
const [result] = await pool.query(
|
||||||
"UPDATE loans SET deleted = 1 WHERE id = ?;",
|
"UPDATE loans SET deleted = 1 WHERE id = ?;",
|
||||||
[loanId],
|
[loanId]
|
||||||
);
|
);
|
||||||
if (result.affectedRows > 0) {
|
if (result.affectedRows > 0) {
|
||||||
return { success: true };
|
return { success: true };
|
||||||
@@ -260,69 +252,3 @@ export const getItems = async () => {
|
|||||||
}
|
}
|
||||||
return { success: false };
|
return { success: false };
|
||||||
};
|
};
|
||||||
|
|
||||||
export const setReturnDate = async (loanCode) => {
|
|
||||||
const [items] = await pool.query(
|
|
||||||
"SELECT loaned_items_id FROM loans WHERE loan_code = ?",
|
|
||||||
[loanCode],
|
|
||||||
);
|
|
||||||
|
|
||||||
const [owner] = await pool.query(
|
|
||||||
"SELECT username FROM loans WHERE loan_code = ?",
|
|
||||||
[loanCode],
|
|
||||||
);
|
|
||||||
|
|
||||||
if (items.length === 0) return { success: false };
|
|
||||||
|
|
||||||
const itemIds = Array.isArray(items[0].loaned_items_id)
|
|
||||||
? items[0].loaned_items_id
|
|
||||||
: JSON.parse(items[0].loaned_items_id || "[]");
|
|
||||||
|
|
||||||
const [setItemStates] = await pool.query(
|
|
||||||
"UPDATE items SET in_safe = 1, currently_borrowing = NULL, last_borrowed_person = (?) WHERE id IN (?)",
|
|
||||||
[owner[0].username, itemIds],
|
|
||||||
);
|
|
||||||
|
|
||||||
const [result] = await pool.query(
|
|
||||||
"UPDATE loans SET returned_date = NOW() WHERE loan_code = ?",
|
|
||||||
[loanCode],
|
|
||||||
);
|
|
||||||
|
|
||||||
if (result.affectedRows > 0 && setItemStates.affectedRows > 0) {
|
|
||||||
return { success: true };
|
|
||||||
}
|
|
||||||
return { success: false };
|
|
||||||
};
|
|
||||||
|
|
||||||
export const setTakeDate = async (loanCode) => {
|
|
||||||
const [items] = await pool.query(
|
|
||||||
"SELECT loaned_items_id FROM loans WHERE loan_code = ?",
|
|
||||||
[loanCode],
|
|
||||||
);
|
|
||||||
|
|
||||||
const [owner] = await pool.query(
|
|
||||||
"SELECT username FROM loans WHERE loan_code = ?",
|
|
||||||
[loanCode],
|
|
||||||
);
|
|
||||||
|
|
||||||
if (items.length === 0) return { success: false };
|
|
||||||
|
|
||||||
const itemIds = Array.isArray(items[0].loaned_items_id)
|
|
||||||
? items[0].loaned_items_id
|
|
||||||
: JSON.parse(items[0].loaned_items_id || "[]");
|
|
||||||
|
|
||||||
const [setItemStates] = await pool.query(
|
|
||||||
"UPDATE items SET in_safe = 0, currently_borrowing = (?) WHERE id IN (?)",
|
|
||||||
[owner[0].username, itemIds],
|
|
||||||
);
|
|
||||||
|
|
||||||
const [result] = await pool.query(
|
|
||||||
"UPDATE loans SET take_date = NOW() WHERE loan_code = ?",
|
|
||||||
[loanCode],
|
|
||||||
);
|
|
||||||
|
|
||||||
if (result.affectedRows > 0 && setItemStates.affectedRows > 0) {
|
|
||||||
return { success: true };
|
|
||||||
}
|
|
||||||
return { success: false };
|
|
||||||
};
|
|
||||||
|
|||||||
@@ -13,8 +13,6 @@ import {
|
|||||||
getALLLoans,
|
getALLLoans,
|
||||||
getItems,
|
getItems,
|
||||||
SETdeleteLoanFromDatabase,
|
SETdeleteLoanFromDatabase,
|
||||||
setReturnDate,
|
|
||||||
setTakeDate,
|
|
||||||
} from "./database/loansMgmt.database.js";
|
} from "./database/loansMgmt.database.js";
|
||||||
import { sendMailLoan } from "./services/mailer.js";
|
import { sendMailLoan } from "./services/mailer.js";
|
||||||
|
|
||||||
@@ -50,7 +48,7 @@ router.post("/createLoan", authenticate, async (req, res) => {
|
|||||||
start,
|
start,
|
||||||
end,
|
end,
|
||||||
note,
|
note,
|
||||||
itemIds,
|
itemIds
|
||||||
);
|
);
|
||||||
|
|
||||||
if (result.success) {
|
if (result.success) {
|
||||||
@@ -61,7 +59,7 @@ router.post("/createLoan", authenticate, async (req, res) => {
|
|||||||
mailInfo.data.loaned_items_name,
|
mailInfo.data.loaned_items_name,
|
||||||
mailInfo.data.start_date,
|
mailInfo.data.start_date,
|
||||||
mailInfo.data.end_date,
|
mailInfo.data.end_date,
|
||||||
mailInfo.data.created_at,
|
mailInfo.data.created_at
|
||||||
);
|
);
|
||||||
return res.status(201).json({
|
return res.status(201).json({
|
||||||
message: "Loan created successfully",
|
message: "Loan created successfully",
|
||||||
@@ -98,26 +96,6 @@ router.get("/loans", authenticate, async (req, res) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
router.post("/set-return-date/:loan_code", authenticate, async (req, res) => {
|
|
||||||
const loanCode = req.params.loan_code;
|
|
||||||
const result = await setReturnDate(loanCode);
|
|
||||||
if (result.success) {
|
|
||||||
res.status(200).json({ data: result.data });
|
|
||||||
} else {
|
|
||||||
res.status(500).json({ message: "Failed to set return date" });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
router.post("/set-take-date/:loan_code", authenticate, async (req, res) => {
|
|
||||||
const loanCode = req.params.loan_code;
|
|
||||||
const result = await setTakeDate(loanCode);
|
|
||||||
if (result.success) {
|
|
||||||
res.status(200).json({ data: result.data });
|
|
||||||
} else {
|
|
||||||
res.status(500).json({ message: "Failed to set take date" });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
router.get("/all-items", authenticate, async (req, res) => {
|
router.get("/all-items", authenticate, async (req, res) => {
|
||||||
const result = await getItems();
|
const result = await getItems();
|
||||||
if (result.success) {
|
if (result.success) {
|
||||||
@@ -157,7 +135,7 @@ router.post("/borrowable-items", authenticate, async (req, res) => {
|
|||||||
const result = await getBorrowableItemsFromDatabase(
|
const result = await getBorrowableItemsFromDatabase(
|
||||||
startDate,
|
startDate,
|
||||||
endDate,
|
endDate,
|
||||||
req.user.role,
|
req.user.role
|
||||||
);
|
);
|
||||||
if (result.success) {
|
if (result.success) {
|
||||||
// return the array directly for consistency with /items
|
// return the array directly for consistency with /items
|
||||||
|
|||||||
@@ -2,38 +2,6 @@ import nodemailer from "nodemailer";
|
|||||||
import dotenv from "dotenv";
|
import dotenv from "dotenv";
|
||||||
dotenv.config();
|
dotenv.config();
|
||||||
|
|
||||||
const formatDateTime = (value) => {
|
|
||||||
if (value == null) return "N/A";
|
|
||||||
|
|
||||||
const toOut = (d) => {
|
|
||||||
if (!(d instanceof Date) || isNaN(d.getTime())) return "N/A";
|
|
||||||
const dd = String(d.getDate()).padStart(2, "0");
|
|
||||||
const mm = String(d.getMonth() + 1).padStart(2, "0");
|
|
||||||
const yyyy = d.getFullYear();
|
|
||||||
const hh = String(d.getHours()).padStart(2, "0");
|
|
||||||
const mi = String(d.getMinutes()).padStart(2, "0");
|
|
||||||
return `${dd}.${mm}.${yyyy} ${hh}:${mi} Uhr`;
|
|
||||||
};
|
|
||||||
|
|
||||||
if (value instanceof Date) return toOut(value);
|
|
||||||
if (typeof value === "number") return toOut(new Date(value));
|
|
||||||
|
|
||||||
const s = String(value).trim();
|
|
||||||
|
|
||||||
// Direct pattern: "YYYY-MM-DD[ T]HH:mm[:ss]"
|
|
||||||
const m = s.match(/^(\d{4})-(\d{2})-(\d{2})[ T](\d{2}):(\d{2})(?::\d{2})?/);
|
|
||||||
if (m) {
|
|
||||||
const [, y, M, d, h, min] = m;
|
|
||||||
return `${d}.${M}.${y} ${h}:${min} Uhr`;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ISO or other parseable formats
|
|
||||||
const dObj = new Date(s);
|
|
||||||
if (!isNaN(dObj.getTime())) return toOut(dObj);
|
|
||||||
|
|
||||||
return "N/A";
|
|
||||||
};
|
|
||||||
|
|
||||||
function buildLoanEmail({ user, items, startDate, endDate, createdDate }) {
|
function buildLoanEmail({ user, items, startDate, endDate, createdDate }) {
|
||||||
const brand = process.env.MAIL_BRAND_COLOR || "#0ea5e9";
|
const brand = process.env.MAIL_BRAND_COLOR || "#0ea5e9";
|
||||||
const itemsList =
|
const itemsList =
|
||||||
@@ -174,8 +142,7 @@ export function sendMailLoan(user, items, startDate, endDate, createdDate) {
|
|||||||
html: buildLoanEmail({ user, items, startDate, endDate, createdDate }),
|
html: buildLoanEmail({ user, items, startDate, endDate, createdDate }),
|
||||||
});
|
});
|
||||||
|
|
||||||
// debugging logs
|
console.log("Message sent:", info.messageId);
|
||||||
// console.log("Message sent:", info.messageId);
|
|
||||||
})();
|
})();
|
||||||
// console.log("sendMailLoan called");
|
console.log("sendMailLoan called");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,45 +0,0 @@
|
|||||||
import nodemailer from "nodemailer";
|
|
||||||
import dotenv from "dotenv";
|
|
||||||
dotenv.config();
|
|
||||||
|
|
||||||
export function sendMail(username, message) {
|
|
||||||
const transporter = nodemailer.createTransport({
|
|
||||||
host: process.env.MAIL_HOST,
|
|
||||||
port: process.env.MAIL_PORT,
|
|
||||||
secure: true,
|
|
||||||
auth: {
|
|
||||||
user: process.env.MAIL_USER,
|
|
||||||
pass: process.env.MAIL_PASSWORD,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
(async () => {
|
|
||||||
const mailText = `Neue Kontaktanfrage im Ausleihsystem.\n\nBenutzername: ${username}\n\nNachricht:\n${message}`;
|
|
||||||
|
|
||||||
const mailHtml = `<!DOCTYPE html>
|
|
||||||
<html lang="de">
|
|
||||||
<head>
|
|
||||||
<meta charset="UTF-8" />
|
|
||||||
<title>Neue Nachricht im Ausleihsystem</title>
|
|
||||||
</head>
|
|
||||||
<body style="font-family: Arial, sans-serif; line-height: 1.5; color: #222;">
|
|
||||||
<h2>Neue Nachricht im Ausleihsystem</h2>
|
|
||||||
<p><strong>Benutzername:</strong> ${username}</p>
|
|
||||||
<p><strong>Nachricht:</strong></p>
|
|
||||||
<p style="white-space: pre-line;">${message}</p>
|
|
||||||
</body>
|
|
||||||
</html>`;
|
|
||||||
|
|
||||||
const info = await transporter.sendMail({
|
|
||||||
from: '"Ausleihsystem" <noreply@mcs-medien.de>',
|
|
||||||
to: process.env.MAIL_SENDEES_CONTACT,
|
|
||||||
subject: "Sie haben eine neue Nachricht!",
|
|
||||||
text: mailText,
|
|
||||||
html: mailHtml,
|
|
||||||
});
|
|
||||||
|
|
||||||
// debugging logs
|
|
||||||
// console.log("Message sent:", info.messageId);
|
|
||||||
})();
|
|
||||||
// console.log("sendMailLoan called");
|
|
||||||
}
|
|
||||||
@@ -6,7 +6,6 @@ dotenv.config();
|
|||||||
|
|
||||||
// database funcs import
|
// database funcs import
|
||||||
import { loginFunc, changePassword } from "./database/userMgmt.database.js";
|
import { loginFunc, changePassword } from "./database/userMgmt.database.js";
|
||||||
import { sendMail } from "./services/mailer_v2.js";
|
|
||||||
|
|
||||||
router.post("/login", async (req, res) => {
|
router.post("/login", async (req, res) => {
|
||||||
const result = await loginFunc(req.body.username, req.body.password);
|
const result = await loginFunc(req.body.username, req.body.password);
|
||||||
@@ -36,13 +35,4 @@ router.post("/change-password", authenticate, async (req, res) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
router.post("/contact", authenticate, async (req, res) => {
|
|
||||||
const message = req.body.message;
|
|
||||||
const username = req.user.username;
|
|
||||||
|
|
||||||
sendMail(username, message);
|
|
||||||
|
|
||||||
res.status(200).json({ message: "Contact message sent successfully" });
|
|
||||||
});
|
|
||||||
|
|
||||||
export default router;
|
export default router;
|
||||||
|
|||||||
Binary file not shown.
@@ -37,13 +37,14 @@ CREATE TABLE items (
|
|||||||
item_name varchar(255) NOT NULL UNIQUE,
|
item_name varchar(255) NOT NULL UNIQUE,
|
||||||
can_borrow_role INT NOT NULL,
|
can_borrow_role INT NOT NULL,
|
||||||
in_safe bool NOT NULL DEFAULT true,
|
in_safe bool NOT NULL DEFAULT true,
|
||||||
safe_nr INT DEFAULT NULL UNIQUE,
|
safe_nr CHAR(2) DEFAULT NULL,
|
||||||
door_key INT DEFAULT NULL UNIQUE,
|
|
||||||
entry_created_at timestamp NULL DEFAULT CURRENT_TIMESTAMP,
|
entry_created_at timestamp NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
entry_updated_at timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
entry_updated_at timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||||
last_borrowed_person varchar(255) DEFAULT NULL,
|
last_borrowed_person varchar(255) DEFAULT NULL,
|
||||||
currently_borrowing varchar(255) DEFAULT NULL,
|
currently_borrowing varchar(255) DEFAULT NULL,
|
||||||
PRIMARY KEY (id)
|
PRIMARY KEY (id),
|
||||||
|
CHECK (safe_nr REGEXP '^[0-9]{2}$' OR safe_nr IS NULL),
|
||||||
|
UNIQUE KEY ux_items_safe_nr (safe_nr)
|
||||||
) ENGINE=InnoDB;
|
) ENGINE=InnoDB;
|
||||||
|
|
||||||
CREATE TABLE apiKeys (
|
CREATE TABLE apiKeys (
|
||||||
|
|||||||
+1
-1
@@ -20,7 +20,7 @@ import apiRouter from "./routes/api/api.route.js";
|
|||||||
|
|
||||||
env.config();
|
env.config();
|
||||||
const app = express();
|
const app = express();
|
||||||
const port = 8004;
|
const port = 8102;
|
||||||
|
|
||||||
app.use(cors());
|
app.use(cors());
|
||||||
// Body-Parser VOR den Routen registrieren
|
// Body-Parser VOR den Routen registrieren
|
||||||
|
|||||||
+30
-13
@@ -1,23 +1,32 @@
|
|||||||
services:
|
services:
|
||||||
# usr-frontend_v2:
|
usr-frontend_v2:
|
||||||
# container_name: borrow_system-usr-frontend
|
container_name: borrow_system-usr-frontend
|
||||||
# build: ./FrontendV2
|
networks:
|
||||||
# ports:
|
- proxynet
|
||||||
# - "8001:80"
|
- borrow_system-internal
|
||||||
# restart: unless-stopped
|
build: ./FrontendV2
|
||||||
|
ports:
|
||||||
|
- "8101:80"
|
||||||
|
restart: unless-stopped
|
||||||
|
|
||||||
# admin-frontend:
|
admin-frontend:
|
||||||
# container_name: borrow_system-admin-frontend
|
container_name: borrow_system-admin-frontend
|
||||||
# build: ./admin
|
networks:
|
||||||
# ports:
|
- proxynet
|
||||||
# - "8003:80"
|
- borrow_system-internal
|
||||||
# restart: unless-stopped
|
build: ./admin
|
||||||
|
ports:
|
||||||
|
- "8103:80"
|
||||||
|
restart: unless-stopped
|
||||||
|
|
||||||
backend_v2:
|
backend_v2:
|
||||||
container_name: borrow_system-backend_v2
|
container_name: borrow_system-backend_v2
|
||||||
|
networks:
|
||||||
|
- proxynet
|
||||||
|
- borrow_system-internal
|
||||||
build: ./backendV2
|
build: ./backendV2
|
||||||
ports:
|
ports:
|
||||||
- "8004:8004"
|
- "8102:8102"
|
||||||
environment:
|
environment:
|
||||||
NODE_ENV: production
|
NODE_ENV: production
|
||||||
DB_HOST: mysql_v2
|
DB_HOST: mysql_v2
|
||||||
@@ -30,6 +39,8 @@ services:
|
|||||||
|
|
||||||
mysql_v2:
|
mysql_v2:
|
||||||
container_name: borrow_system-mysql-v2
|
container_name: borrow_system-mysql-v2
|
||||||
|
networks:
|
||||||
|
- borrow_system-internal
|
||||||
image: mysql:8.0
|
image: mysql:8.0
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
environment:
|
environment:
|
||||||
@@ -45,3 +56,9 @@ services:
|
|||||||
volumes:
|
volumes:
|
||||||
mysql-data:
|
mysql-data:
|
||||||
mysql-v2-data:
|
mysql-v2-data:
|
||||||
|
|
||||||
|
networks:
|
||||||
|
proxynet:
|
||||||
|
external: true
|
||||||
|
borrow_system-internal:
|
||||||
|
external: false
|
||||||
|
|||||||
Reference in New Issue
Block a user