Compare commits
1 Commits
dev_postgr
...
hostALT
| Author | SHA1 | Date | |
|---|---|---|---|
| 2145e2722a |
5
.gitignore
vendored
5
.gitignore
vendored
@@ -109,11 +109,8 @@ backend/public/uploads/
|
|||||||
*.sqlite3
|
*.sqlite3
|
||||||
|
|
||||||
# API keys and secrets (additional protection)
|
# API keys and secrets (additional protection)
|
||||||
|
config/
|
||||||
secrets/
|
secrets/
|
||||||
keys/
|
keys/
|
||||||
|
|
||||||
ToDo.txt
|
ToDo.txt
|
||||||
|
|
||||||
|
|
||||||
# only in development branch
|
|
||||||
next-env.d.ts
|
|
||||||
22
Docs/CHANGELOG.md
Normal file
22
Docs/CHANGELOG.md
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
# Changelog
|
||||||
|
v1.1
|
||||||
|
|
||||||
|
## Current hosted version
|
||||||
|
v1.1
|
||||||
|
|
||||||
|
> No changelog available.
|
||||||
|
|
||||||
|
## Upcoming changes
|
||||||
|
|
||||||
|
v1.2
|
||||||
|
|
||||||
|
### Fixes and improvements
|
||||||
|
|
||||||
|
- Implement user roles and permissions
|
||||||
|
- Improve form validation and error handling
|
||||||
|
- Add loading indicators for async actions
|
||||||
|
- Optimize performance for large datasets
|
||||||
|
|
||||||
|
### New features
|
||||||
|
|
||||||
|
- Admin panel for managing users, permissions and all of the system settings and database
|
||||||
@@ -3,5 +3,3 @@
|
|||||||
This document provides an overview of the backend API endpoints and their usage.
|
This document provides an overview of the backend API endpoints and their usage.
|
||||||
|
|
||||||
To get to that information, go to the `backend_API_docs` directory.
|
To get to that information, go to the `backend_API_docs` directory.
|
||||||
|
|
||||||
If you need help, see HELP.md file in this directory.
|
|
||||||
@@ -1,177 +1,210 @@
|
|||||||
# Borrow System API Documentation
|
# Backend API docs (apiV2)
|
||||||
|
|
||||||
## Overview
|
If you want to cooperate with me, or build something new with my backend API, feel free to reach out!
|
||||||
|
|
||||||
The Borrow System API provides endpoints for managing items, loans, and door access for a borrowing/locker system. All endpoints require authentication via an 8-digit API key passed as a URL parameter.
|
On this page you will learn how my API works.
|
||||||
|
|
||||||
|
## General information
|
||||||
|
|
||||||
|
When you look at my backend folder and file structure, you can see that I have two files called `API`. The first file called `api.js` which is for my web frontend, because this file works together with my JWT token service.
|
||||||
|
|
||||||
|
But I have built a second API. You can see the second API file in the same directory, the file is called `apiV2.js`.
|
||||||
|
|
||||||
|
But first you have to get an API Key. You can get the API key from my admin dashboard. When you don't have any access to my admin dashboard, please contact your administrator or me.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Base URL
|
||||||
|
|
||||||
|
- Frontend: `https://insta.the1s.de`
|
||||||
|
- Backend: `https://backend.insta.the1s.de`
|
||||||
|
- Base path for this API: `https://backend.insta.the1s.de/apiV2`
|
||||||
|
|
||||||
|
You can see the status of this and all my other services at `https://status.the1s.de`.
|
||||||
|
|
||||||
|
_I have also build a [fallback page](https://git.the1s.de/theis.gaedigk/fallback-page). When only the application is down, you will see a friendly message and a link to the status page. (Only if the server is not down)_
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
## Authentication
|
## Authentication
|
||||||
|
|
||||||
All requests must include a valid API key in the URL path as the `:key` parameter. API keys are 8-digit numeric strings.
|
All endpoints require an API key as a path parameter named `:key`.
|
||||||
|
|
||||||
|
Example: `/apiV2/items/:key`
|
||||||
|
|
||||||
|
If the key is missing or invalid, the API responds with `401 Unauthorized`.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
## Endpoints
|
## Endpoints
|
||||||
|
|
||||||
The Base URL for all endpoints is: `https://insta.the1s.de/backend/api`
|
### 1) Get all items
|
||||||
|
|
||||||
### Get All Items
|
GET `/apiV2/items/:key`
|
||||||
|
|
||||||
`GET /items/:key`
|
Returns a list of all items wrapped in a `data` object.
|
||||||
|
|
||||||
Returns all items in the system.
|
Example request:
|
||||||
|
|
||||||
**Response 200:**
|
```
|
||||||
|
GET https://backend.insta.the1s.de/apiV2/items/12345
|
||||||
|
```
|
||||||
|
|
||||||
```json
|
Example response:
|
||||||
|
|
||||||
|
```
|
||||||
{
|
{
|
||||||
"data": [
|
"data": [
|
||||||
{
|
{
|
||||||
"id": 1,
|
"id": 1,
|
||||||
"item_name": "Laptop",
|
"item_name": "DJI 1er Mikro",
|
||||||
"can_borrow_role": 1,
|
"can_borrow_role": 4,
|
||||||
"in_safe": true,
|
"inSafe": 1,
|
||||||
"safe_nr": 3,
|
"entry_created_at": "2025-08-19T22:02:16.000Z"
|
||||||
"door_key": 101,
|
|
||||||
"last_borrowed_person": "jdoe",
|
|
||||||
"currently_borrowing": null
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
**Response 500:**
|
Fields:
|
||||||
|
|
||||||
```json
|
- `id`: Unique identifier
|
||||||
{ "message": "Failed to fetch items" }
|
- `item_name`: Item name
|
||||||
```
|
- `can_borrow_role`: Role allowed to borrow
|
||||||
|
- `inSafe`: 1 if in locker, 0 otherwise
|
||||||
|
- `entry_created_at`: Creation timestamp
|
||||||
|
|
||||||
|
Status: 200 on success, 500 on failure.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
### Change Item Safe State
|
### 2) Change item safe state
|
||||||
|
|
||||||
`POST /change-state/:key/:itemId`
|
POST `/apiV2/controlInSafe/:key/:itemId/:state`
|
||||||
|
|
||||||
Toggles the `in_safe` boolean state of an item.
|
Updates `inSafe` (locker) state of an item.
|
||||||
|
|
||||||
**URL Parameters:**
|
- `state` must be `"1"` (in safe) or `"0"` (not in safe)
|
||||||
|
|
||||||
- **key** - API key
|
Example request:
|
||||||
- **itemId** - The item's ID
|
|
||||||
|
|
||||||
**Response 200:** Returns on successful toggle.
|
|
||||||
|
|
||||||
**Response 500:**
|
|
||||||
|
|
||||||
```json
|
|
||||||
{ "message": "Failed to update item state" }
|
|
||||||
```
|
```
|
||||||
|
POST https://backend.insta.the1s.de/apiV2/controlInSafe/12345/123/1
|
||||||
|
```
|
||||||
|
|
||||||
|
Example response (shape depends on database service):
|
||||||
|
|
||||||
|
```
|
||||||
|
{ "data": { /* update result */ } }
|
||||||
|
```
|
||||||
|
|
||||||
|
Status:
|
||||||
|
|
||||||
|
- 200 on success
|
||||||
|
- 400 if `state` is invalid
|
||||||
|
- 500 on failure
|
||||||
|
|
||||||
|
**You can get the item id on the admin panel, from your system administrator.**
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
### Get Loan by Code
|
### 3) Get loan by code
|
||||||
|
|
||||||
`GET /get-loan-by-code/:key/:loan_code`
|
GET `/apiV2/getLoanByCode/:key/:loan_code`
|
||||||
|
|
||||||
Retrieves loan details by its 6-digit loan code.
|
Retrieves the details of a specific loan.
|
||||||
|
|
||||||
**URL Parameters:**
|
Example request:
|
||||||
|
|
||||||
- **key** - API key
|
```
|
||||||
- **loan_code** - A 6-digit numeric loan code
|
GET https://backend.insta.the1s.de/apiV2/getLoanByCode/12345/123456
|
||||||
|
```
|
||||||
|
|
||||||
**Response 200:**
|
Example response:
|
||||||
|
|
||||||
```json
|
```
|
||||||
{
|
{
|
||||||
"data": {
|
"data": {
|
||||||
"username": "jdoe",
|
"id": 6,
|
||||||
|
"username": "theis",
|
||||||
|
"loan_code": 646473,
|
||||||
|
"start_date": "2025-08-25T13:23:00.000Z",
|
||||||
|
"end_date": "2025-08-26T13:23:00.000Z",
|
||||||
|
"take_date": null,
|
||||||
"returned_date": null,
|
"returned_date": null,
|
||||||
"take_date": "2024-01-15T10:30:00.000Z",
|
"created_at": "2025-08-20T11:23:40.000Z",
|
||||||
"lockers": [1, 3]
|
"loaned_items_id": [8, 9],
|
||||||
|
"loaned_items_name": ["SD Karten", "Kameragimbal"]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
**Response 404:**
|
Status:
|
||||||
|
|
||||||
```json
|
- 200 on success
|
||||||
{ "message": "Loan not found" }
|
- 404 if not found
|
||||||
```
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
### Set Take Date
|
### 4) Set return date (now) by loan code
|
||||||
|
|
||||||
`POST /set-take-date/:key/:loan_code`
|
POST `/apiV2/setReturnDate/:key/:loan_code`
|
||||||
|
|
||||||
Records when items are physically taken by setting `take_date` to the current timestamp. Updates associated items to `in_safe = false` and sets `currently_borrowing` to the loan's username.
|
Sets the `returned_date` to the current server time.
|
||||||
|
|
||||||
**URL Parameters:**
|
**Note:** I have updated this API route, so that everytime you return or take a loan, the state of the loaned items is automatically updated.
|
||||||
|
|
||||||
- **key** - API key
|
**DO NOT UPDATE THE STATE MANUALLY! (only if the item was taken with an admin key)**
|
||||||
- **loan_code** - A 6-digit numeric loan code
|
|
||||||
|
|
||||||
**Response 200:** Empty JSON object on success.
|
Example request:
|
||||||
|
|
||||||
**Response 500:**
|
```
|
||||||
|
POST https://backend.insta.the1s.de/apiV2/setReturnDate/12345/123456
|
||||||
```json
|
|
||||||
{ "message": "Loan not found or already taken" }
|
|
||||||
```
|
```
|
||||||
|
|
||||||
> **Note:** This endpoint will fail if the loan has already been taken or does not exist.
|
Example response:
|
||||||
|
|
||||||
|
```
|
||||||
|
{ "data": { /* update result */ } }
|
||||||
|
```
|
||||||
|
|
||||||
|
Status: 200 on success, 500 on failure.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
### Set Return Date
|
### 5) Set take date (now) by loan code
|
||||||
|
|
||||||
`POST /set-return-date/:key/:loan_code`
|
POST `/apiV2/setTakeDate/:key/:loan_code`
|
||||||
|
|
||||||
Marks a loan as returned by setting `returned_date` to the current timestamp. Also updates all associated items to `in_safe = true`, clears `currently_borrowing`, and sets `last_borrowed_person`. Therefore, keep in mind that you must not call other endpoints that will change the safe state of an item after or before calling this endpoint, otherwise the state of the items will be inconsistent.
|
Sets the `take_date` to the current server time.
|
||||||
|
|
||||||
**URL Parameters:**
|
**Note:** I have updated this API route, so that everytime you return or take a loan, the state of the loaned items is automatically updated.
|
||||||
|
|
||||||
- **key** - API key
|
**DO NOT UPDATE THE STATE MANUALLY! (only if the item was taken with an admin key)**
|
||||||
- **loan_code** - A 6-digit numeric loan code
|
|
||||||
|
|
||||||
**Response 200:** Empty JSON object on success.
|
Example request:
|
||||||
|
|
||||||
**Response 500:**
|
```
|
||||||
|
POST https://backend.insta.the1s.de/apiV2/setTakeDate/12345/123456
|
||||||
```json
|
|
||||||
{ "message": "Failed to set return date" }
|
|
||||||
```
|
```
|
||||||
|
|
||||||
> **Note:** This endpoint will fail if the loan has already been returned (i.e., `returned_date` is not `NULL`).
|
Example response:
|
||||||
|
|
||||||
|
```
|
||||||
|
{ "data": { /* update result */ } }
|
||||||
|
```
|
||||||
|
|
||||||
|
Status: 200 on success, 500 on failure.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
### Open Door
|
## Error handling
|
||||||
|
|
||||||
`GET /open-door/:key/:doorKey`
|
- 401 Unauthorized: Missing or invalid API key
|
||||||
|
- 400 Bad Request: Invalid parameters (e.g., wrong state value)
|
||||||
|
- 404 Not Found: Loan not found
|
||||||
|
- 500 Internal Server Error: Database or server error
|
||||||
|
|
||||||
Toggles the safe state of an item identified by its door key and returns the associated safe number.
|
---
|
||||||
|
|
||||||
**URL Parameters:**
|
If you have questions or want to collaborate, please reach out!
|
||||||
|
|
||||||
- **key** - API key
|
|
||||||
- **doorKey** - The door key identifier assigned to an item
|
|
||||||
|
|
||||||
**Response 200:**
|
|
||||||
|
|
||||||
```json
|
|
||||||
{
|
|
||||||
"data": {
|
|
||||||
"safe_nr": 3,
|
|
||||||
"id": 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
**Response 500:**
|
|
||||||
|
|
||||||
```json
|
|
||||||
{ "message": "Failed to open door" }
|
|
||||||
```
|
|
||||||
|
|
||||||
## Error Handling
|
|
||||||
|
|
||||||
All endpoints return a `500` status code for server-side failures and a JSON body with a `message` field, except for **Get Loan by Code** which returns `404` when no matching loan is found.
|
|
||||||
|
|||||||
@@ -1,19 +1,12 @@
|
|||||||
FROM node:22-alpine AS builder
|
FROM node:20-alpine
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
COPY package.json package-lock.json ./
|
COPY package*.json ./
|
||||||
RUN npm ci
|
RUN npm install
|
||||||
|
|
||||||
COPY . .
|
COPY . .
|
||||||
RUN npm run build
|
|
||||||
|
|
||||||
FROM nginx:alpine AS runner
|
EXPOSE 8001
|
||||||
|
|
||||||
WORKDIR /usr/share/nginx/html
|
CMD ["npm", "run", "dev"]
|
||||||
COPY --from=builder /app/dist .
|
|
||||||
|
|
||||||
COPY nginx.conf /etc/nginx/conf.d/default.conf
|
|
||||||
|
|
||||||
EXPOSE 80
|
|
||||||
CMD ["nginx", "-g", "daemon off;"]
|
|
||||||
@@ -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>
|
||||||
|
|||||||
@@ -1,26 +0,0 @@
|
|||||||
server {
|
|
||||||
listen 80;
|
|
||||||
server_name _;
|
|
||||||
|
|
||||||
root /usr/share/nginx/html;
|
|
||||||
index index.html;
|
|
||||||
|
|
||||||
location / {
|
|
||||||
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?)$ {
|
|
||||||
expires 1y;
|
|
||||||
access_log off;
|
|
||||||
add_header Cache-Control "public, immutable";
|
|
||||||
try_files $uri =404;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
2
FrontendV2/package-lock.json
generated
2
FrontendV2/package-lock.json
generated
@@ -11,7 +11,7 @@
|
|||||||
"@chakra-ui/react": "^3.28.0",
|
"@chakra-ui/react": "^3.28.0",
|
||||||
"@emotion/react": "^11.14.0",
|
"@emotion/react": "^11.14.0",
|
||||||
"@tailwindcss/vite": "^4.1.11",
|
"@tailwindcss/vite": "^4.1.11",
|
||||||
"@tanstack/react-query": "^5.90.5",
|
"@tanstack/react-query": "^5.85.5",
|
||||||
"i18next": "^25.6.0",
|
"i18next": "^25.6.0",
|
||||||
"jotai": "^2.15.0",
|
"jotai": "^2.15.0",
|
||||||
"js-cookie": "^3.0.5",
|
"js-cookie": "^3.0.5",
|
||||||
|
|||||||
@@ -13,7 +13,7 @@
|
|||||||
"@chakra-ui/react": "^3.28.0",
|
"@chakra-ui/react": "^3.28.0",
|
||||||
"@emotion/react": "^11.14.0",
|
"@emotion/react": "^11.14.0",
|
||||||
"@tailwindcss/vite": "^4.1.11",
|
"@tailwindcss/vite": "^4.1.11",
|
||||||
"@tanstack/react-query": "^5.90.5",
|
"@tanstack/react-query": "^5.85.5",
|
||||||
"i18next": "^25.6.0",
|
"i18next": "^25.6.0",
|
||||||
"jotai": "^2.15.0",
|
"jotai": "^2.15.0",
|
||||||
"js-cookie": "^3.0.5",
|
"js-cookie": "^3.0.5",
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-shapes-icon lucide-shapes"><path d="M8.3 10a.7.7 0 0 1-.626-1.079L11.4 3a.7.7 0 0 1 1.198-.043L16.3 8.9a.7.7 0 0 1-.572 1.1Z"/><rect x="3" y="14" width="7" height="7" rx="1"/><circle cx="17.5" cy="17.5" r="3.5"/></svg>
|
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="31.88" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 257"><defs><linearGradient id="IconifyId1813088fe1fbc01fb466" x1="-.828%" x2="57.636%" y1="7.652%" y2="78.411%"><stop offset="0%" stop-color="#41D1FF"></stop><stop offset="100%" stop-color="#BD34FE"></stop></linearGradient><linearGradient id="IconifyId1813088fe1fbc01fb467" x1="43.376%" x2="50.316%" y1="2.242%" y2="89.03%"><stop offset="0%" stop-color="#FFEA83"></stop><stop offset="8.333%" stop-color="#FFDD35"></stop><stop offset="100%" stop-color="#FFA800"></stop></linearGradient></defs><path fill="url(#IconifyId1813088fe1fbc01fb466)" d="M255.153 37.938L134.897 252.976c-2.483 4.44-8.862 4.466-11.382.048L.875 37.958c-2.746-4.814 1.371-10.646 6.827-9.67l120.385 21.517a6.537 6.537 0 0 0 2.322-.004l117.867-21.483c5.438-.991 9.574 4.796 6.877 9.62Z"></path><path fill="url(#IconifyId1813088fe1fbc01fb467)" d="M185.432.063L96.44 17.501a3.268 3.268 0 0 0-2.634 3.014l-5.474 92.456a3.268 3.268 0 0 0 3.997 3.378l24.777-5.718c2.318-.535 4.413 1.507 3.936 3.838l-7.361 36.047c-.495 2.426 1.782 4.5 4.151 3.78l15.304-4.649c2.372-.72 4.652 1.36 4.15 3.788l-11.698 56.621c-.732 3.542 3.979 5.473 5.943 2.437l1.313-2.028l72.516-144.72c1.215-2.423-.88-5.186-3.54-4.672l-25.505 4.922c-2.396.462-4.435-1.77-3.759-4.114l16.646-57.705c.677-2.35-1.37-4.583-3.769-4.113Z"></path></svg>
|
||||||
|
Before Width: | Height: | Size: 420 B After Width: | Height: | Size: 1.5 KiB |
@@ -12,13 +12,13 @@ import { triggerLogoutAtom } from "@/states/Atoms";
|
|||||||
import { MyLoansPage } from "./pages/MyLoansPage";
|
import { MyLoansPage } from "./pages/MyLoansPage";
|
||||||
import Landingpage from "./pages/Landingpage";
|
import Landingpage from "./pages/Landingpage";
|
||||||
import { changeLanguage } from "i18next";
|
import { changeLanguage } from "i18next";
|
||||||
import { Flex } from "@chakra-ui/react";
|
import { Box, Flex } from "@chakra-ui/react";
|
||||||
import { Footer } from "./components/footer/Footer";
|
import { Footer } from "./components/Footer";
|
||||||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
|
||||||
import { API_BASE } from "@/config/api.config";
|
|
||||||
import { ContactPage } from "./pages/ContactPage";
|
|
||||||
|
|
||||||
const queryClient = new QueryClient();
|
const API_BASE =
|
||||||
|
(import.meta as any).env?.VITE_BACKEND_URL ||
|
||||||
|
import.meta.env.VITE_BACKEND_URL ||
|
||||||
|
"http://localhost:8002";
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
const [user, setUser] = useState<User | undefined>(undefined);
|
const [user, setUser] = useState<User | undefined>(undefined);
|
||||||
@@ -28,7 +28,7 @@ function App() {
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (Cookies.get("token")) {
|
if (Cookies.get("token")) {
|
||||||
const verifyToken = async () => {
|
const verifyToken = async () => {
|
||||||
const response = await fetch(`${API_BASE}/verify`, {
|
const response = await fetch(`${API_BASE}/api/verifyToken`, {
|
||||||
method: "GET",
|
method: "GET",
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${Cookies.get("token")}`,
|
Authorization: `Bearer ${Cookies.get("token")}`,
|
||||||
@@ -37,13 +37,7 @@ function App() {
|
|||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
setTriggerLogout(false);
|
setTriggerLogout(false);
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
setUser({
|
setUser({ username: data.user.username, role: data.user.role });
|
||||||
username: data.user.username,
|
|
||||||
is_admin: data.user.is_admin,
|
|
||||||
first_name: data.user.first_name,
|
|
||||||
last_name: data.user.last_name,
|
|
||||||
role: data.user.role,
|
|
||||||
});
|
|
||||||
setIsLoggedIn(true);
|
setIsLoggedIn(true);
|
||||||
} else {
|
} else {
|
||||||
Cookies.remove("token");
|
Cookies.remove("token");
|
||||||
@@ -71,27 +65,24 @@ function App() {
|
|||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<QueryClientProvider client={queryClient}>
|
<Flex direction="column" minH="100vh">
|
||||||
<Flex direction="column" minH="100dvh">
|
<Box as="main" flex="1">
|
||||||
<Flex as="main" flex="1" direction="column">
|
|
||||||
<UserContext.Provider value={user}>
|
<UserContext.Provider value={user}>
|
||||||
<BrowserRouter>
|
<BrowserRouter>
|
||||||
<Routes>
|
<Routes>
|
||||||
<Route element={<ProtectedRoutes />}>
|
<Route element={<ProtectedRoutes />}>
|
||||||
<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="/landing" element={<Landingpage />} />
|
||||||
<Route path="/contact" element={<ContactPage />} />
|
|
||||||
</Route>
|
</Route>
|
||||||
|
|
||||||
<Route path="/login" element={<LoginPage />} />
|
<Route path="/login" element={<LoginPage />} />
|
||||||
</Routes>
|
</Routes>
|
||||||
</BrowserRouter>
|
</BrowserRouter>
|
||||||
</UserContext.Provider>
|
</UserContext.Provider>
|
||||||
</Flex>
|
</Box>
|
||||||
<Footer />
|
<Footer />
|
||||||
</Flex>
|
</Flex>
|
||||||
</QueryClientProvider>
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
50
FrontendV2/src/components/Changelog.json
Normal file
50
FrontendV2/src/components/Changelog.json
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
{
|
||||||
|
"title": "Changelog",
|
||||||
|
"items": [
|
||||||
|
{
|
||||||
|
"version": "v2.1.0",
|
||||||
|
"date": "2025-10-24",
|
||||||
|
"changes": [
|
||||||
|
{
|
||||||
|
"type": "Hinzugefügt",
|
||||||
|
"text": [
|
||||||
|
"Neue Changelog-Komponente mit zentriertem Layout.",
|
||||||
|
"Unterstützung für mehrsprachige Einträge (Englisch und Deutsch)."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "Verbessert",
|
||||||
|
"text": [
|
||||||
|
"Performance-Optimierungen beim Laden der Listenansichten.",
|
||||||
|
"Verbesserte Barrierefreiheit durch ARIA-Attribute."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "Behoben",
|
||||||
|
"text": [
|
||||||
|
"Fehler bei der Datumsauswahl im Safari-Browser.",
|
||||||
|
"Anzeigeprobleme bei hohen DPI-Einstellungen."
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"version": "v2.0.3",
|
||||||
|
"date": "2025-10-10",
|
||||||
|
"changes": [
|
||||||
|
{
|
||||||
|
"type": "Geändert",
|
||||||
|
"text": [
|
||||||
|
"Standard-Timeout für API-Requests auf 10s erhöht."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "Sicherheit",
|
||||||
|
"text": [
|
||||||
|
"Abhängigkeiten aktualisiert (kritische CVEs behoben)."
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
263
FrontendV2/src/components/Changelog.tsx
Normal file
263
FrontendV2/src/components/Changelog.tsx
Normal file
@@ -0,0 +1,263 @@
|
|||||||
|
import { useEffect, useRef, useState } from "react";
|
||||||
|
|
||||||
|
const STORAGE_KEY = "changelog";
|
||||||
|
|
||||||
|
type ChangeType =
|
||||||
|
| "Hinzugefügt"
|
||||||
|
| "Geändert"
|
||||||
|
| "Behoben"
|
||||||
|
| "Entfernt"
|
||||||
|
| "Verbessert"
|
||||||
|
| "Sicherheit"
|
||||||
|
| "Veraltet"
|
||||||
|
| string;
|
||||||
|
|
||||||
|
type ChangeEntry = {
|
||||||
|
type: ChangeType;
|
||||||
|
text: string | string[]; // aus localStorage kann es eine Liste sein
|
||||||
|
};
|
||||||
|
|
||||||
|
type ChangelogItem = {
|
||||||
|
version?: string;
|
||||||
|
date: string;
|
||||||
|
changes: ChangeEntry[];
|
||||||
|
};
|
||||||
|
|
||||||
|
type StoredChangelog = {
|
||||||
|
title: string;
|
||||||
|
items: ChangelogItem[];
|
||||||
|
};
|
||||||
|
|
||||||
|
const typeStyles: Record<string, string> = {
|
||||||
|
Hinzugefügt:
|
||||||
|
"bg-emerald-500/15 text-emerald-300 ring-1 ring-inset ring-emerald-500/30",
|
||||||
|
Geändert: "bg-blue-500/15 text-blue-300 ring-1 ring-inset ring-blue-500/30",
|
||||||
|
Behoben: "bg-amber-500/15 text-amber-300 ring-1 ring-inset ring-amber-500/30",
|
||||||
|
Entfernt: "bg-rose-500/15 text-rose-300 ring-1 ring-inset ring-rose-500/30",
|
||||||
|
Verbessert:
|
||||||
|
"bg-indigo-500/15 text-indigo-300 ring-1 ring-inset ring-indigo-500/30",
|
||||||
|
Sicherheit: "bg-red-500/15 text-red-300 ring-1 ring-inset ring-red-500/30",
|
||||||
|
Veraltet: "bg-zinc-700/30 text-zinc-300 ring-1 ring-inset ring-zinc-600/40",
|
||||||
|
};
|
||||||
|
|
||||||
|
export default function Changelog() {
|
||||||
|
const [open, setOpen] = useState(true);
|
||||||
|
const [mounted, setMounted] = useState(false);
|
||||||
|
const [data, setData] = useState<StoredChangelog | null>(null);
|
||||||
|
const [error, setError] = useState<string | null>(null);
|
||||||
|
const cardRef = useRef<HTMLDivElement | null>(null);
|
||||||
|
|
||||||
|
useEffect(() => setMounted(true), []);
|
||||||
|
|
||||||
|
const loadFromStorage = () => {
|
||||||
|
try {
|
||||||
|
setError(null);
|
||||||
|
const raw =
|
||||||
|
typeof window !== "undefined"
|
||||||
|
? localStorage.getItem(STORAGE_KEY)
|
||||||
|
: null;
|
||||||
|
if (!raw) {
|
||||||
|
setData(null);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const parsed = JSON.parse(raw) as StoredChangelog;
|
||||||
|
if (!parsed || !Array.isArray(parsed.items)) {
|
||||||
|
throw new Error("Ungültiges Format");
|
||||||
|
}
|
||||||
|
setData(parsed);
|
||||||
|
} catch (e) {
|
||||||
|
setError("Changelog konnte nicht aus localStorage geladen werden.");
|
||||||
|
setData(null);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
loadFromStorage();
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const onKey = (e: KeyboardEvent) => {
|
||||||
|
if (e.key === "Escape") setOpen(false);
|
||||||
|
};
|
||||||
|
const onClickOutside = (e: MouseEvent) => {
|
||||||
|
if (cardRef.current && !cardRef.current.contains(e.target as Node)) {
|
||||||
|
setOpen(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
const onStorage = (e: StorageEvent) => {
|
||||||
|
if (e.key === STORAGE_KEY) loadFromStorage();
|
||||||
|
};
|
||||||
|
window.addEventListener("keydown", onKey);
|
||||||
|
document.addEventListener("mousedown", onClickOutside);
|
||||||
|
window.addEventListener("storage", onStorage);
|
||||||
|
return () => {
|
||||||
|
window.removeEventListener("keydown", onKey);
|
||||||
|
document.removeEventListener("mousedown", onClickOutside);
|
||||||
|
window.removeEventListener("storage", onStorage);
|
||||||
|
};
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
if (!open) return null;
|
||||||
|
|
||||||
|
const title = data?.title ?? "Changelog";
|
||||||
|
const items = data?.items ?? [];
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="min-h-screen bg-zinc-950 bg-[radial-gradient(60%_60%_at_50%_0%,rgba(99,102,241,0.12),rgba(24,24,27,0))] flex items-center justify-center p-6">
|
||||||
|
<div
|
||||||
|
ref={cardRef}
|
||||||
|
className={[
|
||||||
|
"relative w-full max-w-6xl transition-all duration-300 ease-out",
|
||||||
|
mounted
|
||||||
|
? "opacity-100 translate-y-0 scale-100"
|
||||||
|
: "opacity-0 translate-y-1 scale-[0.99]",
|
||||||
|
].join(" ")}
|
||||||
|
aria-live="polite"
|
||||||
|
>
|
||||||
|
{/* Gradient border wrapper */}
|
||||||
|
<div className="rounded-2xl p-[1px] bg-gradient-to-b from-zinc-700/60 via-zinc-700/20 to-zinc-800/60 shadow-2xl">
|
||||||
|
{/* Card */}
|
||||||
|
<div className="relative rounded-[calc(theme(borderRadius.2xl)-1px)] border border-zinc-800/70 bg-zinc-900/70 supports-[backdrop-filter]:bg-zinc-900/60 backdrop-blur-xl ring-1 ring-white/10">
|
||||||
|
{/* Accent top line */}
|
||||||
|
<div className="pointer-events-none absolute inset-x-0 top-0 h-px bg-gradient-to-r from-transparent via-indigo-500/40 to-transparent" />
|
||||||
|
|
||||||
|
{/* Close button */}
|
||||||
|
<button
|
||||||
|
aria-label="Changelog schließen"
|
||||||
|
onClick={() => setOpen(false)}
|
||||||
|
className="absolute right-3 top-3 inline-flex h-9 w-9 items-center justify-center rounded-md text-zinc-400 hover:text-zinc-100 hover:bg-zinc-800/60 focus:outline-none focus-visible:ring-2 focus-visible:ring-indigo-500/70 focus-visible:ring-offset-2 focus-visible:ring-offset-zinc-900 transition"
|
||||||
|
>
|
||||||
|
<svg
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
className="h-5 w-5"
|
||||||
|
fill="none"
|
||||||
|
stroke="currentColor"
|
||||||
|
strokeWidth={1.8}
|
||||||
|
strokeLinecap="round"
|
||||||
|
strokeLinejoin="round"
|
||||||
|
>
|
||||||
|
<path d="M6 6l12 12M18 6L6 18" />
|
||||||
|
</svg>
|
||||||
|
</button>
|
||||||
|
|
||||||
|
{/* Header */}
|
||||||
|
<header className="px-10 pt-8 pb-6 border-b border-zinc-800/70">
|
||||||
|
<div className="flex items-center gap-3">
|
||||||
|
<div className="inline-flex h-9 w-9 items-center justify-center rounded-lg bg-indigo-500/15 text-indigo-300 ring-1 ring-inset ring-indigo-500/30">
|
||||||
|
<svg
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
className="h-5 w-5"
|
||||||
|
fill="none"
|
||||||
|
stroke="currentColor"
|
||||||
|
strokeWidth={1.6}
|
||||||
|
strokeLinecap="round"
|
||||||
|
strokeLinejoin="round"
|
||||||
|
>
|
||||||
|
<path d="M12 3v3M12 18v3M3 12h3M18 12h3M5.6 5.6l2.1 2.1M16.3 16.3l2.1 2.1M5.6 18.4l2.1-2.1M16.3 7.7l2.1-2.1" />
|
||||||
|
</svg>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<h1 className="text-[30px] leading-8 font-semibold text-zinc-100 tracking-[-0.01em]">
|
||||||
|
{title}
|
||||||
|
</h1>
|
||||||
|
<p className="text-sm text-zinc-400">
|
||||||
|
Aktuelle Änderungen und Updates
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</header>
|
||||||
|
|
||||||
|
{/* Body */}
|
||||||
|
<div className="relative max-h-[78vh] overflow-y-auto">
|
||||||
|
<div className="absolute pointer-events-none inset-x-0 top-0 h-8 bg-gradient-to-b from-zinc-900/70 to-transparent" />
|
||||||
|
<div className="absolute pointer-events-none inset-x-0 bottom-0 h-10 bg-gradient-to-t from-zinc-900/80 to-transparent" />
|
||||||
|
|
||||||
|
{error && (
|
||||||
|
<div className="px-10 py-8">
|
||||||
|
<div className="rounded-lg border border-red-900/40 bg-red-900/10 px-4 py-3 text-sm text-red-300">
|
||||||
|
{error}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{!error && items.length === 0 && (
|
||||||
|
<div className="px-10 py-16 text-center">
|
||||||
|
<p className="text-zinc-400">
|
||||||
|
Kein Changelog im localStorage gefunden (Key: {STORAGE_KEY}
|
||||||
|
).
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<ul className="divide-y divide-zinc-800/70">
|
||||||
|
{items.map((entry, idx) => (
|
||||||
|
<li
|
||||||
|
key={`${entry.version ?? entry.date}-${idx}`}
|
||||||
|
className="px-10 py-8"
|
||||||
|
>
|
||||||
|
{/* Kopfzeile je Release */}
|
||||||
|
<div className="flex flex-wrap items-baseline gap-x-4 gap-y-2">
|
||||||
|
{entry.version && (
|
||||||
|
<span className="inline-flex items-center rounded-md bg-gradient-to-b from-zinc-100 to-zinc-300 text-zinc-900 px-3 py-0.5 text-sm font-semibold shadow-sm">
|
||||||
|
{entry.version}
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
<time
|
||||||
|
className="text-sm text-zinc-400"
|
||||||
|
dateTime={entry.date}
|
||||||
|
>
|
||||||
|
{new Date(entry.date).toLocaleDateString("de-DE", {
|
||||||
|
year: "numeric",
|
||||||
|
month: "long",
|
||||||
|
day: "2-digit",
|
||||||
|
})}
|
||||||
|
</time>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Zweispaltiges Layout: Typ links, Text rechts (mit schöner Leselänge) */}
|
||||||
|
<dl
|
||||||
|
role="list"
|
||||||
|
className="mt-6 grid grid-cols-1 gap-x-8 gap-y-3 md:grid-cols-[max-content_1fr]"
|
||||||
|
>
|
||||||
|
{entry.changes.map((c, i) => (
|
||||||
|
<div key={i} className="contents">
|
||||||
|
<dt className="md:w-44 md:justify-end md:text-right">
|
||||||
|
<span
|
||||||
|
className={`inline-flex items-center rounded-md px-2 py-0.5 text-[11px] font-medium ${
|
||||||
|
typeStyles[c.type] ??
|
||||||
|
"bg-zinc-700/30 text-zinc-300 ring-1 ring-inset ring-zinc-600/40"
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
{c.type}
|
||||||
|
</span>
|
||||||
|
</dt>
|
||||||
|
|
||||||
|
<dd className="max-w-[74ch] text-[15px] leading-7 text-zinc-200 tracking-[0.005em]">
|
||||||
|
{Array.isArray(c.text) ? (
|
||||||
|
<ul className="ml-4 list-disc marker:text-zinc-500/70 space-y-1.5">
|
||||||
|
{c.text.map((t, k) => (
|
||||||
|
<li key={k} className="break-words">
|
||||||
|
{t}
|
||||||
|
</li>
|
||||||
|
))}
|
||||||
|
</ul>
|
||||||
|
) : (
|
||||||
|
<p className="break-words">{c.text}</p>
|
||||||
|
)}
|
||||||
|
</dd>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</dl>
|
||||||
|
</li>
|
||||||
|
))}
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* soft bottom glow */}
|
||||||
|
<div className="pointer-events-none absolute inset-x-12 -bottom-4 h-8 blur-2xl bg-indigo-600/20 rounded-full" />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
19
FrontendV2/src/components/Footer.tsx
Normal file
19
FrontendV2/src/components/Footer.tsx
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
import { Box } from "@chakra-ui/react";
|
||||||
|
|
||||||
|
export const Footer = () => {
|
||||||
|
return (
|
||||||
|
<Box
|
||||||
|
as="footer"
|
||||||
|
py={4}
|
||||||
|
textAlign="center"
|
||||||
|
position="fixed"
|
||||||
|
bottom="0"
|
||||||
|
left="0"
|
||||||
|
right="0"
|
||||||
|
>
|
||||||
|
Made with ❤️ by Theis Gaedigk - Year 2019 at MCS-Bochum
|
||||||
|
<br />
|
||||||
|
v2.0
|
||||||
|
</Box>
|
||||||
|
);
|
||||||
|
};
|
||||||
@@ -1,58 +1,103 @@
|
|||||||
import {
|
import {
|
||||||
|
Badge,
|
||||||
Button,
|
Button,
|
||||||
Flex,
|
Flex,
|
||||||
Heading,
|
Heading,
|
||||||
Stack,
|
Stack,
|
||||||
Text,
|
Text,
|
||||||
|
CloseButton,
|
||||||
|
Dialog,
|
||||||
|
Portal,
|
||||||
HStack,
|
HStack,
|
||||||
IconButton,
|
IconButton,
|
||||||
Menu,
|
Menu,
|
||||||
Box,
|
Box,
|
||||||
Avatar,
|
|
||||||
} 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,
|
Flag,
|
||||||
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";
|
|
||||||
|
const API_BASE =
|
||||||
|
(import.meta as any).env?.VITE_BACKEND_URL ||
|
||||||
|
import.meta.env.VITE_BACKEND_URL ||
|
||||||
|
"http://localhost:8002";
|
||||||
|
|
||||||
export const Header = () => {
|
export const Header = () => {
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const userData = useUserContext();
|
const userData = useUserContext();
|
||||||
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);
|
||||||
|
|
||||||
const [userDialog, setUserDialog] = useState(false);
|
// Dialog control
|
||||||
|
const [isPwOpen, setPwOpen] = useState(false);
|
||||||
|
|
||||||
const username = userData.first_name ? userData.first_name : "N/A";
|
const changePassword = async () => {
|
||||||
const fullname = userData.first_name + " " + userData.last_name;
|
if (newPassword !== confirmPassword) {
|
||||||
const randomColor = [
|
setMsgTitle(t("err_pw_change"));
|
||||||
"gray",
|
setMsgDescription(t("pw_mismatch"));
|
||||||
"red",
|
setMsgStatus("error");
|
||||||
"orange",
|
setIsMsg(true);
|
||||||
"yellow",
|
return;
|
||||||
"green",
|
}
|
||||||
"teal",
|
|
||||||
"blue",
|
const response = await fetch(`${API_BASE}/api/changePassword`, {
|
||||||
"cyan",
|
method: "POST",
|
||||||
"purple",
|
headers: {
|
||||||
"pink",
|
"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?.username
|
||||||
|
? userData.username[0].toUpperCase() + userData.username.slice(1)
|
||||||
|
: "User";
|
||||||
|
|
||||||
const logout = () => {
|
const logout = () => {
|
||||||
Cookies.remove("token");
|
Cookies.remove("token");
|
||||||
@@ -114,12 +159,12 @@ export const Header = () => {
|
|||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
<Menu.Item
|
<Menu.Item
|
||||||
value="landingpage"
|
value="change-password"
|
||||||
onSelect={() => navigate("/landingpage", { replace: true })}
|
onSelect={() => setPwOpen(true)}
|
||||||
children={
|
children={
|
||||||
<HStack gap={3}>
|
<HStack gap={3}>
|
||||||
<Table size={16} />
|
<RotateCcwKey size={16} />
|
||||||
<Text as="span">{t("landingpage")}</Text>
|
<Text as="span">{t("change-password")}</Text>
|
||||||
</HStack>
|
</HStack>
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
@@ -133,7 +178,7 @@ export const Header = () => {
|
|||||||
}}
|
}}
|
||||||
children={
|
children={
|
||||||
<HStack gap={3}>
|
<HStack gap={3}>
|
||||||
<Languages size={16} />
|
<LifeBuoy size={16} />
|
||||||
<Text as="span">{t("change-language")}</Text>
|
<Text as="span">{t("change-language")}</Text>
|
||||||
</HStack>
|
</HStack>
|
||||||
}
|
}
|
||||||
@@ -142,9 +187,9 @@ export const Header = () => {
|
|||||||
value="help"
|
value="help"
|
||||||
onSelect={() =>
|
onSelect={() =>
|
||||||
window.open(
|
window.open(
|
||||||
"https://git.the1s.de/Matthias-Claudius-Schule/borrow-system/wiki/?action=_pages",
|
"https://git.the1s.de/Matthias-Claudius-Schule/borrow-system/wiki",
|
||||||
"_blank",
|
"_blank",
|
||||||
"noopener,noreferrer",
|
"noopener,noreferrer"
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
children={
|
children={
|
||||||
@@ -155,12 +200,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>
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
@@ -194,7 +245,7 @@ export const Header = () => {
|
|||||||
size="2xl"
|
size="2xl"
|
||||||
className="tracking-tight text-slate-900 dark:text-slate-100"
|
className="tracking-tight text-slate-900 dark:text-slate-100"
|
||||||
>
|
>
|
||||||
{t("app-title")}
|
Home
|
||||||
</Heading>
|
</Heading>
|
||||||
</Flex>
|
</Flex>
|
||||||
|
|
||||||
@@ -203,21 +254,12 @@ export const Header = () => {
|
|||||||
{t("greeting")}
|
{t("greeting")}
|
||||||
<strong>{username}</strong>!
|
<strong>{username}</strong>!
|
||||||
</Text>
|
</Text>
|
||||||
|
<Badge variant="subtle" px={2} py={1} borderRadius="full">
|
||||||
|
Rolle: {userData?.role ?? "—"}
|
||||||
|
</Badge>
|
||||||
</HStack>
|
</HStack>
|
||||||
</Stack>
|
</Stack>
|
||||||
|
|
||||||
{/* Avatar: visible on mobile, hidden on desktop (desktop version is in the actions bar) */}
|
|
||||||
<HStack display={{ base: "flex", md: "none" }}>
|
|
||||||
<Avatar.Root>
|
|
||||||
<button
|
|
||||||
onClick={() => setUserDialog(true)}
|
|
||||||
style={{ cursor: "pointer" }}
|
|
||||||
>
|
|
||||||
<Avatar.Fallback name={fullname} />
|
|
||||||
</button>
|
|
||||||
</Avatar.Root>
|
|
||||||
</HStack>
|
|
||||||
|
|
||||||
{/* Right: Actions */}
|
{/* Right: Actions */}
|
||||||
{/* Desktop actions */}
|
{/* Desktop actions */}
|
||||||
<HStack
|
<HStack
|
||||||
@@ -227,18 +269,6 @@ export const Header = () => {
|
|||||||
flexWrap="wrap"
|
flexWrap="wrap"
|
||||||
display={{ base: "none", md: "flex" }}
|
display={{ base: "none", md: "flex" }}
|
||||||
>
|
>
|
||||||
{/* Desktop avatar, aligned with action buttons */}
|
|
||||||
<Avatar.Root
|
|
||||||
colorPalette={randomColor[Math.floor(Math.random() * 10)]}
|
|
||||||
>
|
|
||||||
<button
|
|
||||||
onClick={() => setUserDialog(true)}
|
|
||||||
style={{ cursor: "pointer" }}
|
|
||||||
>
|
|
||||||
<Avatar.Fallback name={fullname} />
|
|
||||||
</button>
|
|
||||||
</Avatar.Root>
|
|
||||||
|
|
||||||
<Button
|
<Button
|
||||||
colorScheme="teal"
|
colorScheme="teal"
|
||||||
onClick={() => navigate("/", { replace: true })}
|
onClick={() => navigate("/", { replace: true })}
|
||||||
@@ -256,10 +286,10 @@ export const Header = () => {
|
|||||||
</HStack>
|
</HStack>
|
||||||
</Button>
|
</Button>
|
||||||
|
|
||||||
<Button onClick={() => navigate("/landingpage", { replace: true })}>
|
<Button variant="ghost" onClick={() => setPwOpen(true)}>
|
||||||
<HStack gap={2}>
|
<HStack gap={2}>
|
||||||
<Table size={18} />
|
<RotateCcwKey size={18} />
|
||||||
<Text as="span">{t("landingpage")}</Text>
|
<Text as="span">{t("change-password")}</Text>
|
||||||
</HStack>
|
</HStack>
|
||||||
</Button>
|
</Button>
|
||||||
|
|
||||||
@@ -273,13 +303,13 @@ export const Header = () => {
|
|||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<HStack gap={2}>
|
<HStack gap={2}>
|
||||||
<Languages size={18} />
|
<Flag size={18} />
|
||||||
<Text as="span">{t("change-language")}</Text>
|
<Text as="span">{t("change-language")}</Text>
|
||||||
</HStack>
|
</HStack>
|
||||||
</Button>
|
</Button>
|
||||||
|
|
||||||
<a
|
<a
|
||||||
href="https://git.the1s.de/Matthias-Claudius-Schule/borrow-system/wiki/?action=_pages"
|
href="https://git.the1s.de/Matthias-Claudius-Schule/borrow-system/wiki"
|
||||||
target="_blank"
|
target="_blank"
|
||||||
>
|
>
|
||||||
<Button variant="ghost">
|
<Button variant="ghost">
|
||||||
@@ -290,15 +320,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"
|
||||||
>
|
>
|
||||||
|
<Button variant="ghost">
|
||||||
<HStack gap={2}>
|
<HStack gap={2}>
|
||||||
<ContactRound size={18} />
|
<Code size={18} />
|
||||||
<Text as="span">{t("contact")}</Text>
|
<Text as="span">{t("source-code")}</Text>
|
||||||
</HStack>
|
</HStack>
|
||||||
</Button>
|
</Button>
|
||||||
|
</a>
|
||||||
|
|
||||||
<Button onClick={logout} variant="outline" colorScheme="red">
|
<Button onClick={logout} variant="outline" colorScheme="red">
|
||||||
<HStack gap={2}>
|
<HStack gap={2}>
|
||||||
@@ -309,14 +341,67 @@ export const Header = () => {
|
|||||||
</HStack>
|
</HStack>
|
||||||
</Flex>
|
</Flex>
|
||||||
|
|
||||||
{/* User Info Dialoge */}
|
{/* Passwort-Dialog (kontrolliert) */}
|
||||||
{userDialog && (
|
<Dialog.Root open={isPwOpen} onOpenChange={(e: any) => setPwOpen(e.open)}>
|
||||||
<UserDialogue
|
<Portal>
|
||||||
setUserDialog={setUserDialog}
|
<Dialog.Backdrop />
|
||||||
fullname={fullname}
|
<Dialog.Positioner>
|
||||||
randomColor={randomColor}
|
<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>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
import { Box } from "@chakra-ui/react";
|
|
||||||
import { useVersionInfoQuery } from "./versionInfo.query";
|
|
||||||
|
|
||||||
export const Footer = () => {
|
|
||||||
const { data: info } = useVersionInfoQuery();
|
|
||||||
|
|
||||||
return (
|
|
||||||
<Box
|
|
||||||
as="footer"
|
|
||||||
py={4}
|
|
||||||
textAlign="center"
|
|
||||||
width="100%"
|
|
||||||
flexShrink={0}
|
|
||||||
fontSize="sm"
|
|
||||||
>
|
|
||||||
Made with ❤️ by Theis Gaedigk - Class of 2019 at MCS-Bochum
|
|
||||||
<br />
|
|
||||||
Frontend-Version: {info ? info["frontend-info"].version : "N/A"} |
|
|
||||||
Backend-Version: {info ? info["backend-info"].version : "N/A"}
|
|
||||||
</Box>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
@@ -1,29 +0,0 @@
|
|||||||
import { useQuery } from "@tanstack/react-query";
|
|
||||||
import { API_BASE } from "@/config/api.config";
|
|
||||||
|
|
||||||
export const useVersionInfoQuery = () =>
|
|
||||||
useQuery({
|
|
||||||
queryKey: ["versionInfo"],
|
|
||||||
queryFn: async () => {
|
|
||||||
const response = await fetch(`${API_BASE}/`, {
|
|
||||||
method: "GET",
|
|
||||||
});
|
|
||||||
if (response.ok) {
|
|
||||||
const data = await response.json();
|
|
||||||
return data;
|
|
||||||
} else {
|
|
||||||
console.error(
|
|
||||||
"Failed to fetch version info (versionInfo.query.ts): ",
|
|
||||||
response.statusText
|
|
||||||
);
|
|
||||||
return {
|
|
||||||
"backend-info": {
|
|
||||||
version: "N/A",
|
|
||||||
},
|
|
||||||
"frontend-info": {
|
|
||||||
version: "N/A",
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
},
|
|
||||||
});
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
export const API_BASE =
|
|
||||||
(import.meta as any).env?.VITE_BACKEND_URL ||
|
|
||||||
import.meta.env.VITE_BACKEND_URL ||
|
|
||||||
"http://localhost:8002";
|
|
||||||
@@ -5,7 +5,7 @@ import "./index.css";
|
|||||||
import App from "./App.tsx";
|
import App from "./App.tsx";
|
||||||
import i18n from "./utils/i18n"; // import i18n configuration DO NOT REMOVE
|
import i18n from "./utils/i18n"; // import i18n configuration DO NOT REMOVE
|
||||||
|
|
||||||
// Prevent unused variable tree shaking
|
// code below is to avoid linter error for unused import
|
||||||
let i18nUnused = i18n;
|
let i18nUnused = i18n;
|
||||||
console.log(i18nUnused);
|
console.log(i18nUnused);
|
||||||
|
|
||||||
|
|||||||
@@ -1,84 +0,0 @@
|
|||||||
import {
|
|
||||||
Field,
|
|
||||||
Textarea,
|
|
||||||
Button,
|
|
||||||
Alert,
|
|
||||||
Container,
|
|
||||||
Text,
|
|
||||||
} 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>
|
|
||||||
<Text>{t("contactPage_messageDescription")}</Text>
|
|
||||||
<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>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
@@ -7,8 +7,6 @@ import {
|
|||||||
Spinner,
|
Spinner,
|
||||||
VStack,
|
VStack,
|
||||||
Table,
|
Table,
|
||||||
InputGroup,
|
|
||||||
Span,
|
|
||||||
} from "@chakra-ui/react";
|
} from "@chakra-ui/react";
|
||||||
import { useAtom } from "jotai";
|
import { useAtom } from "jotai";
|
||||||
import { getBorrowableItems } from "@/utils/Fetcher";
|
import { getBorrowableItems } from "@/utils/Fetcher";
|
||||||
@@ -33,9 +31,6 @@ export const HomePage = () => {
|
|||||||
const [isLoadingA, setIsLoadingA] = useState(false);
|
const [isLoadingA, setIsLoadingA] = useState(false);
|
||||||
const [selectedItems, setSelectedItems] = useState<number[]>([]);
|
const [selectedItems, setSelectedItems] = useState<number[]>([]);
|
||||||
|
|
||||||
const MAX_CHARACTERS = 500;
|
|
||||||
const [note, setNote] = useState("");
|
|
||||||
|
|
||||||
// Error handling states
|
// Error handling states
|
||||||
const [isMsg, setIsMsg] = useState(false);
|
const [isMsg, setIsMsg] = useState(false);
|
||||||
const [msgStatus, setMsgStatus] = useState<"error" | "success">("error");
|
const [msgStatus, setMsgStatus] = useState<"error" | "success">("error");
|
||||||
@@ -61,11 +56,8 @@ export const HomePage = () => {
|
|||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
<Stack as="main">
|
<Stack as="main">
|
||||||
<Text>{t("timezone-info")}</Text>
|
|
||||||
<label htmlFor="startDate">
|
<label htmlFor="startDate">
|
||||||
<strong>
|
|
||||||
<Text>{t("start-date")}</Text>
|
<Text>{t("start-date")}</Text>
|
||||||
</strong>
|
|
||||||
</label>
|
</label>
|
||||||
<Input
|
<Input
|
||||||
id="startDate"
|
id="startDate"
|
||||||
@@ -75,9 +67,7 @@ export const HomePage = () => {
|
|||||||
onChange={(e) => setStartDate(e.target.value)}
|
onChange={(e) => setStartDate(e.target.value)}
|
||||||
/>
|
/>
|
||||||
<label htmlFor="endDate">
|
<label htmlFor="endDate">
|
||||||
<strong>
|
|
||||||
<Text>{t("end-date")}</Text>
|
<Text>{t("end-date")}</Text>
|
||||||
</strong>
|
|
||||||
</label>
|
</label>
|
||||||
<Input
|
<Input
|
||||||
id="endDate"
|
id="endDate"
|
||||||
@@ -108,6 +98,7 @@ export const HomePage = () => {
|
|||||||
}
|
}
|
||||||
setBorrowableItems(response.data);
|
setBorrowableItems(response.data);
|
||||||
setIsMsg(false);
|
setIsMsg(false);
|
||||||
|
console.log(borrowableItems);
|
||||||
});
|
});
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
@@ -143,28 +134,6 @@ export const HomePage = () => {
|
|||||||
<Table.Cell>{item.item_name}</Table.Cell>
|
<Table.Cell>{item.item_name}</Table.Cell>
|
||||||
</Table.Row>
|
</Table.Row>
|
||||||
))}
|
))}
|
||||||
<Table.Row>
|
|
||||||
<Table.Cell colSpan={2}>
|
|
||||||
<InputGroup
|
|
||||||
endElement={
|
|
||||||
<Span color="fg.muted" textStyle="xs">
|
|
||||||
{note.length} / {MAX_CHARACTERS}
|
|
||||||
</Span>
|
|
||||||
}
|
|
||||||
>
|
|
||||||
<Input
|
|
||||||
placeholder={t("optional-note")}
|
|
||||||
value={note}
|
|
||||||
maxLength={MAX_CHARACTERS}
|
|
||||||
onChange={(e) => {
|
|
||||||
setNote(
|
|
||||||
e.currentTarget.value.slice(0, MAX_CHARACTERS)
|
|
||||||
);
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
</InputGroup>
|
|
||||||
</Table.Cell>
|
|
||||||
</Table.Row>
|
|
||||||
</Table.Body>
|
</Table.Body>
|
||||||
</Table.Root>
|
</Table.Root>
|
||||||
</Table.ScrollArea>
|
</Table.ScrollArea>
|
||||||
@@ -172,14 +141,11 @@ export const HomePage = () => {
|
|||||||
{selectedItems.length >= 1 && (
|
{selectedItems.length >= 1 && (
|
||||||
<Button
|
<Button
|
||||||
onClick={() =>
|
onClick={() =>
|
||||||
createLoan(selectedItems, startDate, endDate, note).then(
|
createLoan(selectedItems, startDate, endDate).then((response) => {
|
||||||
(response) => {
|
|
||||||
if (response.status === "error") {
|
if (response.status === "error") {
|
||||||
setMsgStatus("error");
|
setMsgStatus("error");
|
||||||
setMsgTitle(response.title || t("error"));
|
setMsgTitle(response.title || t("error"));
|
||||||
setMsgDescription(
|
setMsgDescription(response.description || t("unknown-error"));
|
||||||
response.description || t("unknown-error")
|
|
||||||
);
|
|
||||||
setIsMsg(true);
|
setIsMsg(true);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -187,8 +153,7 @@ export const HomePage = () => {
|
|||||||
setMsgTitle(t("success"));
|
setMsgTitle(t("success"));
|
||||||
setMsgDescription(t("loan-success"));
|
setMsgDescription(t("loan-success"));
|
||||||
setIsMsg(true);
|
setIsMsg(true);
|
||||||
}
|
})
|
||||||
)
|
|
||||||
}
|
}
|
||||||
>
|
>
|
||||||
{t("create-loan")}
|
{t("create-loan")}
|
||||||
|
|||||||
@@ -9,13 +9,10 @@ import {
|
|||||||
Card,
|
Card,
|
||||||
SimpleGrid,
|
SimpleGrid,
|
||||||
Button,
|
Button,
|
||||||
Container,
|
|
||||||
} from "@chakra-ui/react";
|
} from "@chakra-ui/react";
|
||||||
|
import { Lock, LockOpen } from "lucide-react";
|
||||||
import MyAlert from "@/components/myChakra/MyAlert";
|
import MyAlert from "@/components/myChakra/MyAlert";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import { API_BASE } from "@/config/api.config";
|
|
||||||
import Cookies from "js-cookie";
|
|
||||||
import { Header } from "@/components/Header";
|
|
||||||
|
|
||||||
export const formatDateTime = (value: string | null | undefined) => {
|
export const formatDateTime = (value: string | null | undefined) => {
|
||||||
if (!value) return "N/A";
|
if (!value) return "N/A";
|
||||||
@@ -25,6 +22,11 @@ export const formatDateTime = (value: string | null | undefined) => {
|
|||||||
return `${d}.${M}.${y} ${h}:${min} Uhr`;
|
return `${d}.${M}.${y} ${h}:${min} Uhr`;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const API_BASE =
|
||||||
|
(import.meta as any).env?.VITE_BACKEND_URL ||
|
||||||
|
import.meta.env.VITE_BACKEND_URL ||
|
||||||
|
"http://localhost:8002";
|
||||||
|
|
||||||
type Loan = {
|
type Loan = {
|
||||||
id: number;
|
id: number;
|
||||||
username: string;
|
username: string;
|
||||||
@@ -33,17 +35,14 @@ type Loan = {
|
|||||||
returned_date: string | null;
|
returned_date: string | null;
|
||||||
take_date: string | null;
|
take_date: string | null;
|
||||||
loaned_items_name: string[] | string;
|
loaned_items_name: string[] | string;
|
||||||
note: string | null;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
type Device = {
|
type Device = {
|
||||||
id: number;
|
id: number;
|
||||||
item_name: string;
|
item_name: string;
|
||||||
can_borrow_role: string;
|
can_borrow_role: string;
|
||||||
in_safe: number;
|
inSafe: number;
|
||||||
entry_created_at: string;
|
entry_created_at: string;
|
||||||
last_borrowed_person: string | null;
|
|
||||||
currently_borrowing: string | null;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const Landingpage: React.FC = () => {
|
const Landingpage: React.FC = () => {
|
||||||
@@ -60,7 +59,7 @@ const Landingpage: React.FC = () => {
|
|||||||
const setError = (
|
const setError = (
|
||||||
status: "error" | "success",
|
status: "error" | "success",
|
||||||
message: string,
|
message: string,
|
||||||
description: string,
|
description: string
|
||||||
) => {
|
) => {
|
||||||
setIsError(false);
|
setIsError(false);
|
||||||
setErrorStatus(status);
|
setErrorStatus(status);
|
||||||
@@ -73,12 +72,7 @@ const Landingpage: React.FC = () => {
|
|||||||
const fetchData = async () => {
|
const fetchData = async () => {
|
||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
try {
|
try {
|
||||||
const loanRes = await fetch(`${API_BASE}/api/loans/all-loans`, {
|
const loanRes = await fetch(`${API_BASE}/apiV2/allLoans`);
|
||||||
method: "GET",
|
|
||||||
headers: {
|
|
||||||
Authorization: `Bearer ${Cookies.get("token")}`,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
const loanData = await loanRes.json();
|
const loanData = await loanRes.json();
|
||||||
if (Array.isArray(loanData)) {
|
if (Array.isArray(loanData)) {
|
||||||
setLoans(loanData);
|
setLoans(loanData);
|
||||||
@@ -86,16 +80,11 @@ const Landingpage: React.FC = () => {
|
|||||||
setError(
|
setError(
|
||||||
"error",
|
"error",
|
||||||
t("error-by-loading"),
|
t("error-by-loading"),
|
||||||
t("unexpected-date-format_loan"),
|
t("unexpected-date-format_loan")
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const deviceRes = await fetch(`${API_BASE}/api/loans/all-items`, {
|
const deviceRes = await fetch(`${API_BASE}/apiV2/allItems`);
|
||||||
method: "GET",
|
|
||||||
headers: {
|
|
||||||
Authorization: `Bearer ${Cookies.get("token")}`,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
const deviceData = await deviceRes.json();
|
const deviceData = await deviceRes.json();
|
||||||
if (Array.isArray(deviceData)) {
|
if (Array.isArray(deviceData)) {
|
||||||
setDevices(deviceData);
|
setDevices(deviceData);
|
||||||
@@ -103,7 +92,7 @@ const Landingpage: React.FC = () => {
|
|||||||
setError(
|
setError(
|
||||||
"error",
|
"error",
|
||||||
t("error-by-loading"),
|
t("error-by-loading"),
|
||||||
t("unexpected-date-format_device"),
|
t("unexpected-date-format_device")
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@@ -116,8 +105,10 @@ const Landingpage: React.FC = () => {
|
|||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Container className="px-6 sm:px-8 pt-10">
|
<>
|
||||||
<Header />
|
<Heading as="h1" size="lg" mb={2}>
|
||||||
|
Matthias-Claudius-Schule Technik
|
||||||
|
</Heading>
|
||||||
|
|
||||||
<Heading as="h2" size="md" mb={4}>
|
<Heading as="h2" size="md" mb={4}>
|
||||||
{t("all-loans")}
|
{t("all-loans")}
|
||||||
@@ -157,14 +148,11 @@ const Landingpage: React.FC = () => {
|
|||||||
<Table.ColumnHeader>
|
<Table.ColumnHeader>
|
||||||
<strong>{t("rented-items")}</strong>
|
<strong>{t("rented-items")}</strong>
|
||||||
</Table.ColumnHeader>
|
</Table.ColumnHeader>
|
||||||
<Table.ColumnHeader>
|
|
||||||
<strong>{t("take-date")}</strong>
|
|
||||||
</Table.ColumnHeader>
|
|
||||||
<Table.ColumnHeader>
|
<Table.ColumnHeader>
|
||||||
<strong>{t("return-date")}</strong>
|
<strong>{t("return-date")}</strong>
|
||||||
</Table.ColumnHeader>
|
</Table.ColumnHeader>
|
||||||
<Table.ColumnHeader>
|
<Table.ColumnHeader>
|
||||||
<strong>{t("note")}</strong>
|
<strong>{t("take-date")}</strong>
|
||||||
</Table.ColumnHeader>
|
</Table.ColumnHeader>
|
||||||
</Table.Row>
|
</Table.Row>
|
||||||
</Table.Header>
|
</Table.Header>
|
||||||
@@ -180,9 +168,8 @@ const Landingpage: React.FC = () => {
|
|||||||
? loan.loaned_items_name.join(", ")
|
? loan.loaned_items_name.join(", ")
|
||||||
: loan.loaned_items_name}
|
: loan.loaned_items_name}
|
||||||
</Table.Cell>
|
</Table.Cell>
|
||||||
<Table.Cell>{formatDateTime(loan.take_date)}</Table.Cell>
|
|
||||||
<Table.Cell>{formatDateTime(loan.returned_date)}</Table.Cell>
|
<Table.Cell>{formatDateTime(loan.returned_date)}</Table.Cell>
|
||||||
<Table.Cell>{loan.note}</Table.Cell>
|
<Table.Cell>{formatDateTime(loan.take_date)}</Table.Cell>
|
||||||
</Table.Row>
|
</Table.Row>
|
||||||
))}
|
))}
|
||||||
</Table.Body>
|
</Table.Body>
|
||||||
@@ -205,26 +192,17 @@ const Landingpage: React.FC = () => {
|
|||||||
<Card.Root
|
<Card.Root
|
||||||
key={device.id}
|
key={device.id}
|
||||||
size="sm"
|
size="sm"
|
||||||
bg={device.in_safe ? "green" : "red"}
|
bg={device.inSafe ? "green" : "red"}
|
||||||
h="full"
|
h="full"
|
||||||
minH="100px"
|
minH="100px"
|
||||||
>
|
>
|
||||||
<Card.Header>
|
<Card.Header>
|
||||||
<Heading size="md">
|
{device.inSafe ? <LockOpen size={16} /> : <Lock size={16} />}
|
||||||
<strong>{device.item_name}</strong>
|
<Heading size="md">{device.item_name}</Heading>
|
||||||
</Heading>
|
|
||||||
</Card.Header>
|
</Card.Header>
|
||||||
<Card.Body>
|
<Card.Body color="fg.muted">
|
||||||
<Text>
|
<Text>
|
||||||
<strong>{t("role")}</strong>: {device.can_borrow_role}
|
{t("rent-role")}: {device.can_borrow_role}
|
||||||
</Text>
|
|
||||||
<Text>
|
|
||||||
<strong>{t("last-borrowed-person")}</strong>:{" "}
|
|
||||||
{device.last_borrowed_person || "N/A"}
|
|
||||||
</Text>
|
|
||||||
<Text>
|
|
||||||
<strong>{t("currently-borrowed-by")}</strong>:{" "}
|
|
||||||
{device.currently_borrowing || "N/A"}
|
|
||||||
</Text>
|
</Text>
|
||||||
</Card.Body>
|
</Card.Body>
|
||||||
</Card.Root>
|
</Card.Root>
|
||||||
@@ -243,6 +221,7 @@ const Landingpage: React.FC = () => {
|
|||||||
borderRadius="full"
|
borderRadius="full"
|
||||||
>
|
>
|
||||||
<HStack gap={2}>
|
<HStack gap={2}>
|
||||||
|
<LockOpen size={16} />
|
||||||
<Text>{t("in-locker")}</Text>
|
<Text>{t("in-locker")}</Text>
|
||||||
</HStack>
|
</HStack>
|
||||||
</Button>
|
</Button>
|
||||||
@@ -255,11 +234,12 @@ const Landingpage: React.FC = () => {
|
|||||||
borderRadius="full"
|
borderRadius="full"
|
||||||
>
|
>
|
||||||
<HStack gap={2}>
|
<HStack gap={2}>
|
||||||
|
<Lock size={16} />
|
||||||
<Text>{t("not-in-locker")}</Text>
|
<Text>{t("not-in-locker")}</Text>
|
||||||
</HStack>
|
</HStack>
|
||||||
</Button>
|
</Button>
|
||||||
</HStack>
|
</HStack>
|
||||||
</Container>
|
</>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -4,10 +4,15 @@ 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 { API_BASE } from "@/config/api.config";
|
import { Footer } from "@/components/Footer";
|
||||||
|
|
||||||
|
const API_BASE =
|
||||||
|
(import.meta as any).env?.VITE_BACKEND_URL ||
|
||||||
|
import.meta.env.VITE_BACKEND_URL ||
|
||||||
|
"http://localhost:8002";
|
||||||
|
|
||||||
export const LoginPage = () => {
|
export const LoginPage = () => {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
@@ -15,18 +20,16 @@ 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(); // Mit Tobais besprechen, ob das so bleiben soll
|
||||||
}
|
}
|
||||||
}, [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/login`, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
body: JSON.stringify({ username, password }),
|
body: JSON.stringify({ username, password }),
|
||||||
@@ -62,15 +65,15 @@ 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 (
|
||||||
<div className="flex flex-1 items-center justify-center p-4">
|
<div className="min-h-screen flex items-center justify-center p-4">
|
||||||
<form onSubmit={(e) => e.preventDefault()}>
|
<form onSubmit={(e) => e.preventDefault()}>
|
||||||
<Card.Root maxW="sm">
|
<Card.Root maxW="sm">
|
||||||
<Card.Header>
|
<Card.Header>
|
||||||
@@ -114,6 +117,7 @@ export const LoginPage = () => {
|
|||||||
</Card.Footer>
|
</Card.Footer>
|
||||||
</Card.Root>
|
</Card.Root>
|
||||||
</form>
|
</form>
|
||||||
|
<Footer />
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -13,12 +13,15 @@ import {
|
|||||||
Dialog,
|
Dialog,
|
||||||
Portal,
|
Portal,
|
||||||
Code,
|
Code,
|
||||||
Box,
|
|
||||||
} from "@chakra-ui/react";
|
} from "@chakra-ui/react";
|
||||||
import { Header } from "@/components/Header";
|
import { Header } from "@/components/Header";
|
||||||
import { Trash2 } from "lucide-react";
|
import { Trash2 } from "lucide-react";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import { API_BASE } from "@/config/api.config";
|
|
||||||
|
const API_BASE =
|
||||||
|
(import.meta as any).env?.VITE_BACKEND_URL ||
|
||||||
|
import.meta.env.VITE_BACKEND_URL ||
|
||||||
|
"http://localhost:8002";
|
||||||
|
|
||||||
export const MyLoansPage = () => {
|
export const MyLoansPage = () => {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
@@ -44,7 +47,7 @@ export const MyLoansPage = () => {
|
|||||||
const fetchLoans = async () => {
|
const fetchLoans = async () => {
|
||||||
try {
|
try {
|
||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
const res = await fetch(`${API_BASE}/api/loans/loans`, {
|
const res = await fetch(`${API_BASE}/api/userLoans`, {
|
||||||
method: "GET",
|
method: "GET",
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${Cookies.get("token")}`,
|
Authorization: `Bearer ${Cookies.get("token")}`,
|
||||||
@@ -76,7 +79,7 @@ export const MyLoansPage = () => {
|
|||||||
|
|
||||||
const deleteLoan = async (loanId: number) => {
|
const deleteLoan = async (loanId: number) => {
|
||||||
try {
|
try {
|
||||||
const res = await fetch(`${API_BASE}/api/loans/delete-loan/${loanId}`, {
|
const res = await fetch(`${API_BASE}/api/SETdeleteLoan/${loanId}`, {
|
||||||
method: "DELETE",
|
method: "DELETE",
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${Cookies.get("token")}`,
|
Authorization: `Bearer ${Cookies.get("token")}`,
|
||||||
@@ -112,86 +115,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">
|
||||||
@@ -210,18 +133,10 @@ export const MyLoansPage = () => {
|
|||||||
</VStack>
|
</VStack>
|
||||||
)}
|
)}
|
||||||
{loans && (
|
{loans && (
|
||||||
<Box
|
|
||||||
overflowX="auto"
|
|
||||||
width="100%"
|
|
||||||
// Optional: add bottom padding to avoid scrollbar overlap
|
|
||||||
pb={2}
|
|
||||||
>
|
|
||||||
<Table.Root
|
<Table.Root
|
||||||
size="sm"
|
size="sm"
|
||||||
variant="outline"
|
variant="outline"
|
||||||
// minWidth ensures we don't cram all columns on tiny screens;
|
style={{ tableLayout: "fixed", width: "100%" }}
|
||||||
// horizontal scrolling will appear instead.
|
|
||||||
style={{ tableLayout: "fixed", width: "100%", minWidth: "800px" }}
|
|
||||||
>
|
>
|
||||||
<Table.ColumnGroup>
|
<Table.ColumnGroup>
|
||||||
{/* Ausleihcode */}
|
{/* Ausleihcode */}
|
||||||
@@ -236,8 +151,6 @@ export const MyLoansPage = () => {
|
|||||||
<Table.Column style={{ width: "14%" }} />
|
<Table.Column style={{ width: "14%" }} />
|
||||||
{/* Rückgabedatum */}
|
{/* Rückgabedatum */}
|
||||||
<Table.Column style={{ width: "14%" }} />
|
<Table.Column style={{ width: "14%" }} />
|
||||||
{/* Notiz */}
|
|
||||||
<Table.Column style={{ width: "14%" }} />
|
|
||||||
{/* Aktionen */}
|
{/* Aktionen */}
|
||||||
<Table.Column style={{ width: "8%" }} />
|
<Table.Column style={{ width: "8%" }} />
|
||||||
</Table.ColumnGroup>
|
</Table.ColumnGroup>
|
||||||
@@ -249,7 +162,6 @@ export const MyLoansPage = () => {
|
|||||||
<Table.ColumnHeader>{t("devices")}</Table.ColumnHeader>
|
<Table.ColumnHeader>{t("devices")}</Table.ColumnHeader>
|
||||||
<Table.ColumnHeader>{t("take-date")}</Table.ColumnHeader>
|
<Table.ColumnHeader>{t("take-date")}</Table.ColumnHeader>
|
||||||
<Table.ColumnHeader>{t("return-date")}</Table.ColumnHeader>
|
<Table.ColumnHeader>{t("return-date")}</Table.ColumnHeader>
|
||||||
<Table.ColumnHeader>{t("note")}</Table.ColumnHeader>
|
|
||||||
<Table.ColumnHeader>{t("actions")}</Table.ColumnHeader>
|
<Table.ColumnHeader>{t("actions")}</Table.ColumnHeader>
|
||||||
</Table.Row>
|
</Table.Row>
|
||||||
</Table.Header>
|
</Table.Header>
|
||||||
@@ -264,40 +176,12 @@ export const MyLoansPage = () => {
|
|||||||
<Table.Cell>{formatDate(loan.start_date)}</Table.Cell>
|
<Table.Cell>{formatDate(loan.start_date)}</Table.Cell>
|
||||||
<Table.Cell>{formatDate(loan.end_date)}</Table.Cell>
|
<Table.Cell>{formatDate(loan.end_date)}</Table.Cell>
|
||||||
<Table.Cell>
|
<Table.Cell>
|
||||||
<Text>
|
<Text title={loan.loaned_items_name}>
|
||||||
{Array.isArray(loan.loaned_items_name)
|
{loan.loaned_items_name}
|
||||||
? loan.loaned_items_name.join(", ")
|
|
||||||
: "-"}
|
|
||||||
</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>
|
<Table.Cell>
|
||||||
<Dialog.Root role="alertdialog">
|
<Dialog.Root role="alertdialog">
|
||||||
<Dialog.Trigger asChild>
|
<Dialog.Trigger asChild>
|
||||||
@@ -332,9 +216,7 @@ export const MyLoansPage = () => {
|
|||||||
</Dialog.Body>
|
</Dialog.Body>
|
||||||
<Dialog.Footer>
|
<Dialog.Footer>
|
||||||
<Dialog.ActionTrigger asChild>
|
<Dialog.ActionTrigger asChild>
|
||||||
<Button variant="outline">
|
<Button variant="outline">{t("cancel")}</Button>
|
||||||
{t("cancel")}
|
|
||||||
</Button>
|
|
||||||
</Dialog.ActionTrigger>
|
</Dialog.ActionTrigger>
|
||||||
<Button
|
<Button
|
||||||
colorPalette="red"
|
colorPalette="red"
|
||||||
@@ -355,7 +237,6 @@ export const MyLoansPage = () => {
|
|||||||
))}
|
))}
|
||||||
</Table.Body>
|
</Table.Body>
|
||||||
</Table.Root>
|
</Table.Root>
|
||||||
</Box>
|
|
||||||
)}
|
)}
|
||||||
</Container>
|
</Container>
|
||||||
</>
|
</>
|
||||||
|
|||||||
@@ -1,16 +1,6 @@
|
|||||||
import { atom } from "jotai";
|
import { atom } from "jotai";
|
||||||
|
|
||||||
interface Meta {
|
|
||||||
"backend-info": {
|
|
||||||
version: String;
|
|
||||||
};
|
|
||||||
"frontend-info": {
|
|
||||||
version: String;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export const testAtom = atom<number>(0);
|
export const testAtom = atom<number>(0);
|
||||||
export const setIsLoggedInAtom = atom<boolean>(false);
|
export const setIsLoggedInAtom = atom<boolean>(false);
|
||||||
export const triggerLogoutAtom = atom<boolean>(false);
|
export const triggerLogoutAtom = atom<boolean>(false);
|
||||||
export const borrowAbleItemsAtom = atom<any[]>([]);
|
export const borrowAbleItemsAtom = atom<any[]>([]);
|
||||||
export const infoAtom = atom<Meta | undefined>(undefined);
|
|
||||||
|
|||||||
@@ -3,9 +3,6 @@ import { useContext } from "react";
|
|||||||
|
|
||||||
export interface User {
|
export interface User {
|
||||||
username: string;
|
username: string;
|
||||||
is_admin: boolean;
|
|
||||||
first_name: string;
|
|
||||||
last_name: string;
|
|
||||||
role: number;
|
role: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -15,7 +12,7 @@ export function useUserContext() {
|
|||||||
const user = useContext(UserContext);
|
const user = useContext(UserContext);
|
||||||
|
|
||||||
if (user === undefined) {
|
if (user === undefined) {
|
||||||
throw new Error("useUserContext must be used with a UserContext");
|
throw new Error("useUserContext must be used with a UserContext")
|
||||||
}
|
}
|
||||||
|
|
||||||
return user;
|
return user;
|
||||||
|
|||||||
@@ -1,12 +1,15 @@
|
|||||||
import Cookies from "js-cookie";
|
import Cookies from "js-cookie";
|
||||||
import { API_BASE } from "@/config/api.config";
|
const API_BASE =
|
||||||
|
(import.meta as any).env?.VITE_BACKEND_URL ||
|
||||||
|
import.meta.env.VITE_BACKEND_URL ||
|
||||||
|
"http://localhost:8002";
|
||||||
|
|
||||||
export const getBorrowableItems = async (
|
export const getBorrowableItems = async (
|
||||||
startDate: string,
|
startDate: string,
|
||||||
endDate: string,
|
endDate: string
|
||||||
) => {
|
) => {
|
||||||
try {
|
try {
|
||||||
const response = await fetch(`${API_BASE}/api/loans/borrowable-items`, {
|
const response = await fetch(`${API_BASE}/api/borrowableItems`, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${Cookies.get("token") || ""}`,
|
Authorization: `Bearer ${Cookies.get("token") || ""}`,
|
||||||
@@ -22,11 +25,12 @@ export const getBorrowableItems = async (
|
|||||||
status: "error",
|
status: "error",
|
||||||
title: "Server error",
|
title: "Server error",
|
||||||
description:
|
description:
|
||||||
"An error occurred on the server. Sometimes reloading the page helps. Otherwise, please contact the administrator.",
|
"Ein Fehler ist auf dem Server aufgetreten. Manchmal hilft es, die Seite neu zu laden.",
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
|
console.log(data);
|
||||||
return {
|
return {
|
||||||
data: data,
|
data: data,
|
||||||
status: "success",
|
status: "success",
|
||||||
@@ -47,16 +51,15 @@ export const getBorrowableItems = async (
|
|||||||
export const createLoan = async (
|
export const createLoan = async (
|
||||||
itemIds: number[],
|
itemIds: number[],
|
||||||
startDate: string,
|
startDate: string,
|
||||||
endDate: string,
|
endDate: string
|
||||||
note: string | null,
|
|
||||||
) => {
|
) => {
|
||||||
const response = await fetch(`${API_BASE}/api/loans/createLoan`, {
|
const response = await fetch(`${API_BASE}/api/createLoan`, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
Authorization: `Bearer ${Cookies.get("token") || ""}`,
|
Authorization: `Bearer ${Cookies.get("token") || ""}`,
|
||||||
},
|
},
|
||||||
body: JSON.stringify({ items: itemIds, startDate, endDate, note }),
|
body: JSON.stringify({ items: itemIds, startDate, endDate }),
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
|
|||||||
@@ -59,34 +59,5 @@
|
|||||||
"sure-delete-loan-1": " Ausleihcode wirklich löschen?",
|
"sure-delete-loan-1": " Ausleihcode wirklich löschen?",
|
||||||
"sure-delete-loan-2": "Für den Admin bleibt sie weiterhin sichtbar.",
|
"sure-delete-loan-2": "Für den Admin bleibt sie weiterhin sichtbar.",
|
||||||
"delete": "Löschen",
|
"delete": "Löschen",
|
||||||
"change-language": "Sprache ändern",
|
"change-language": "Sprache ändern"
|
||||||
"timezone-info": "Die angezeigten Daten und Uhrzeiten werden in deutscher Zeitzone dargestellt und müssen auch so eingegeben werden.",
|
|
||||||
"optional-note": "Optionale 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.",
|
|
||||||
"role": "Rolle",
|
|
||||||
"admin-status": "Admin-Status",
|
|
||||||
"first-name": "Vorname",
|
|
||||||
"last-name": "Nachname",
|
|
||||||
"app-title": "Ausleihsystem",
|
|
||||||
"last-borrowed-person": "Zuletzt ausgeliehen von",
|
|
||||||
"currently-borrowed-by": "Derzeit ausgeliehen von",
|
|
||||||
"back": "Zurückgehen",
|
|
||||||
"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",
|
|
||||||
"serverError": "Serverfehler. Bitte versuchen Sie es später erneut, oder laden Sie die Seite neu.",
|
|
||||||
"take-loan-success": "Ausleihe erfolgreich abgeholt",
|
|
||||||
"return-loan-success": "Ausleihe erfolgreich zurückgegeben",
|
|
||||||
"network-error": "Netzwerkfehler. Kontaktieren Sie den Administrator.",
|
|
||||||
"contactPage_messageDescription": "Bitte geben Sie hier Ihre Nachricht ein. Der Systemadministrator (Theis Gaedigk) wird sich so schnell wie möglich bei Ihnen melden."
|
|
||||||
}
|
}
|
||||||
@@ -59,34 +59,5 @@
|
|||||||
"sure-delete-loan-1": " loan code?",
|
"sure-delete-loan-1": " loan code?",
|
||||||
"sure-delete-loan-2": "It will remain visible to the admin.",
|
"sure-delete-loan-2": "It will remain visible to the admin.",
|
||||||
"delete": "Delete",
|
"delete": "Delete",
|
||||||
"change-language": "Change language",
|
"change-language": "Change language"
|
||||||
"timezone-info": "The displayed dates and times are shown in Berlin timezone and must also be entered as such.",
|
|
||||||
"optional-note": "Optional 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.",
|
|
||||||
"role": "Role",
|
|
||||||
"admin-status": "Admin status",
|
|
||||||
"first-name": "First name",
|
|
||||||
"last-name": "Last name",
|
|
||||||
"app-title": "Borrow System",
|
|
||||||
"last-borrowed-person": "Last borrowed by",
|
|
||||||
"currently-borrowed-by": "Currently borrowed by",
|
|
||||||
"back": "Go back",
|
|
||||||
"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",
|
|
||||||
"serverError": "Server error. Please try again later, or refresh the page.",
|
|
||||||
"take": "Take",
|
|
||||||
"return": "Return",
|
|
||||||
"take-loan-success": "Loan taken successfully",
|
|
||||||
"return-loan-success": "Loan returned successfully",
|
|
||||||
"network-error": "Network error. Please contact the administrator.",
|
|
||||||
"contactPage_messageDescription": "Please enter your message here. The system administrator (Theis Gaedigk) will get back to you as soon as possible."
|
|
||||||
}
|
}
|
||||||
73
LICENSE
73
LICENSE
@@ -1,73 +0,0 @@
|
|||||||
Apache License
|
|
||||||
Version 2.0, January 2004
|
|
||||||
http://www.apache.org/licenses/
|
|
||||||
|
|
||||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
||||||
|
|
||||||
1. Definitions.
|
|
||||||
|
|
||||||
"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
|
|
||||||
|
|
||||||
"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
|
|
||||||
|
|
||||||
"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
|
|
||||||
|
|
||||||
"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
|
|
||||||
|
|
||||||
"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
|
|
||||||
|
|
||||||
"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
|
|
||||||
|
|
||||||
"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
|
|
||||||
|
|
||||||
"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
|
|
||||||
|
|
||||||
"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
|
|
||||||
|
|
||||||
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
|
|
||||||
|
|
||||||
2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
|
|
||||||
|
|
||||||
3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
|
|
||||||
|
|
||||||
4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
|
|
||||||
|
|
||||||
(a) You must give any other recipients of the Work or Derivative Works a copy of this License; and
|
|
||||||
|
|
||||||
(b) You must cause any modified files to carry prominent notices stating that You changed the files; and
|
|
||||||
|
|
||||||
(c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
|
|
||||||
|
|
||||||
(d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.
|
|
||||||
|
|
||||||
You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
|
|
||||||
|
|
||||||
5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
|
|
||||||
|
|
||||||
6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
|
|
||||||
|
|
||||||
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
|
|
||||||
|
|
||||||
8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
|
|
||||||
|
|
||||||
9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
|
|
||||||
|
|
||||||
END OF TERMS AND CONDITIONS
|
|
||||||
|
|
||||||
APPENDIX: How to apply the Apache License to your work.
|
|
||||||
|
|
||||||
To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives.
|
|
||||||
|
|
||||||
Copyright 2026 theis.gaedigk
|
|
||||||
|
|
||||||
Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
you may not use this file except in compliance with the License.
|
|
||||||
You may obtain a copy of the License at
|
|
||||||
|
|
||||||
http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
|
|
||||||
Unless required by applicable law or agreed to in writing, software
|
|
||||||
distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
See the License for the specific language governing permissions and
|
|
||||||
limitations under the License.
|
|
||||||
@@ -1,19 +1,12 @@
|
|||||||
FROM node:18 as builder
|
FROM node:20-alpine
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
COPY package.json package-lock.json ./
|
COPY package*.json ./
|
||||||
RUN npm ci
|
RUN npm install
|
||||||
|
|
||||||
COPY . .
|
COPY . .
|
||||||
RUN npm run build
|
|
||||||
|
|
||||||
FROM nginx:alpine AS runner
|
EXPOSE 8003
|
||||||
|
|
||||||
WORKDIR /usr/share/nginx/html
|
CMD ["npm", "run", "dev"]
|
||||||
COPY --from=builder /app/dist .
|
|
||||||
|
|
||||||
COPY nginx.conf /etc/nginx/conf.d/default.conf
|
|
||||||
|
|
||||||
EXPOSE 80
|
|
||||||
CMD ["nginx", "-g", "daemon off;"]
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
<!doctype html>
|
<!DOCTYPE html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8" />
|
<meta charset="UTF-8" />
|
||||||
|
|||||||
@@ -1,26 +0,0 @@
|
|||||||
server {
|
|
||||||
listen 80;
|
|
||||||
server_name _;
|
|
||||||
|
|
||||||
root /usr/share/nginx/html;
|
|
||||||
index index.html;
|
|
||||||
|
|
||||||
location / {
|
|
||||||
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?)$ {
|
|
||||||
expires 1y;
|
|
||||||
access_log off;
|
|
||||||
add_header Cache-Control "public, immutable";
|
|
||||||
try_files $uri =404;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
68
admin/package-lock.json
generated
68
admin/package-lock.json
generated
@@ -3675,16 +3675,12 @@
|
|||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
},
|
},
|
||||||
"node_modules/cookie": {
|
"node_modules/cookie": {
|
||||||
"version": "1.1.1",
|
"version": "1.0.2",
|
||||||
"resolved": "https://registry.npmjs.org/cookie/-/cookie-1.1.1.tgz",
|
"resolved": "https://registry.npmjs.org/cookie/-/cookie-1.0.2.tgz",
|
||||||
"integrity": "sha512-ei8Aos7ja0weRpFzJnEA9UHJ/7XQmqglbRwnf2ATjcB9Wq874VKH9kfjjirM6UhU2/E5fFYadylyhFldcqSidQ==",
|
"integrity": "sha512-9Kr/j4O16ISv8zBBhJoi4bXOYNTkFLOqSL3UDB0njXxCXNezjeyVrJyGOWtgfs/q2km1gwBcfH8q1yEGoMYunA==",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=18"
|
"node": ">=18"
|
||||||
},
|
|
||||||
"funding": {
|
|
||||||
"type": "opencollective",
|
|
||||||
"url": "https://opencollective.com/express"
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/cosmiconfig": {
|
"node_modules/cosmiconfig": {
|
||||||
@@ -4470,9 +4466,9 @@
|
|||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
},
|
},
|
||||||
"node_modules/js-yaml": {
|
"node_modules/js-yaml": {
|
||||||
"version": "4.1.1",
|
"version": "4.1.0",
|
||||||
"resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-4.1.1.tgz",
|
"resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-4.1.0.tgz",
|
||||||
"integrity": "sha512-qQKT4zQxXl8lLwBtHMWwaTcGfFOZviOJet3Oy/xmGk2gZH677CJM9EvtfdSkgWcATZhj/55JZ0rmy3myCT5lsA==",
|
"integrity": "sha512-wpxZs9NoxZaJESJGIZTyDEaYpl0FKSA+FB9aJiyemKhMwkxQg63h4T1KJgUGHpTqPDNRcmmYLugrRjJlBtWvRA==",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"argparse": "^2.0.1"
|
"argparse": "^2.0.1"
|
||||||
@@ -4908,9 +4904,9 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/minizlib": {
|
"node_modules/minizlib": {
|
||||||
"version": "3.1.0",
|
"version": "3.0.2",
|
||||||
"resolved": "https://registry.npmjs.org/minizlib/-/minizlib-3.1.0.tgz",
|
"resolved": "https://registry.npmjs.org/minizlib/-/minizlib-3.0.2.tgz",
|
||||||
"integrity": "sha512-KZxYo1BUkWD2TVFLr0MQoM8vUUigWD3LlD83a/75BqC+4qE0Hb1Vo5v1FgcfaNXvfXzr+5EhQ6ing/CaBijTlw==",
|
"integrity": "sha512-oG62iEk+CYt5Xj2YqI5Xi9xWUeZhDI8jjQmC5oThVH5JGCTgIjr7ciJDzC7MBzYd//WvR1OTmP5Q38Q8ShQtVA==",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"minipass": "^7.1.2"
|
"minipass": "^7.1.2"
|
||||||
@@ -4919,6 +4915,21 @@
|
|||||||
"node": ">= 18"
|
"node": ">= 18"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/mkdirp": {
|
||||||
|
"version": "3.0.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-3.0.1.tgz",
|
||||||
|
"integrity": "sha512-+NsyUUAZDmo6YVHzL/stxSu3t9YS1iljliy3BSDrXJ/dkn1KYdmtZODGGjLcc9XLgVVpH4KshHB8XmZgMhaBXg==",
|
||||||
|
"license": "MIT",
|
||||||
|
"bin": {
|
||||||
|
"mkdirp": "dist/cjs/src/bin.js"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=10"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"url": "https://github.com/sponsors/isaacs"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/ms": {
|
"node_modules/ms": {
|
||||||
"version": "2.1.3",
|
"version": "2.1.3",
|
||||||
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz",
|
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz",
|
||||||
@@ -5296,9 +5307,9 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/react-router": {
|
"node_modules/react-router": {
|
||||||
"version": "7.13.0",
|
"version": "7.8.2",
|
||||||
"resolved": "https://registry.npmjs.org/react-router/-/react-router-7.13.0.tgz",
|
"resolved": "https://registry.npmjs.org/react-router/-/react-router-7.8.2.tgz",
|
||||||
"integrity": "sha512-PZgus8ETambRT17BUm/LL8lX3Of+oiLaPuVTRH3l1eLvSPpKO3AvhAEb5N7ihAFZQrYDqkvvWfFh9p0z9VsjLw==",
|
"integrity": "sha512-7M2fR1JbIZ/jFWqelpvSZx+7vd7UlBTfdZqf6OSdF9g6+sfdqJDAWcak6ervbHph200ePlu+7G8LdoiC3ReyAQ==",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"cookie": "^1.0.1",
|
"cookie": "^1.0.1",
|
||||||
@@ -5318,12 +5329,12 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/react-router-dom": {
|
"node_modules/react-router-dom": {
|
||||||
"version": "7.13.0",
|
"version": "7.8.2",
|
||||||
"resolved": "https://registry.npmjs.org/react-router-dom/-/react-router-dom-7.13.0.tgz",
|
"resolved": "https://registry.npmjs.org/react-router-dom/-/react-router-dom-7.8.2.tgz",
|
||||||
"integrity": "sha512-5CO/l5Yahi2SKC6rGZ+HDEjpjkGaG/ncEP7eWFTvFxbHP8yeeI0PxTDjimtpXYlR3b3i9/WIL4VJttPrESIf2g==",
|
"integrity": "sha512-Z4VM5mKDipal2jQ385H6UBhiiEDlnJPx6jyWsTYoZQdl5TrjxEV2a9yl3Fi60NBJxYzOTGTTHXPi0pdizvTwow==",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"react-router": "7.13.0"
|
"react-router": "7.8.2"
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=20.0.0"
|
"node": ">=20.0.0"
|
||||||
@@ -5481,9 +5492,9 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/set-cookie-parser": {
|
"node_modules/set-cookie-parser": {
|
||||||
"version": "2.7.2",
|
"version": "2.7.1",
|
||||||
"resolved": "https://registry.npmjs.org/set-cookie-parser/-/set-cookie-parser-2.7.2.tgz",
|
"resolved": "https://registry.npmjs.org/set-cookie-parser/-/set-cookie-parser-2.7.1.tgz",
|
||||||
"integrity": "sha512-oeM1lpU/UvhTxw+g3cIfxXHyJRc/uidd3yK1P242gzHds0udQBYzs3y8j4gCCW+ZJ7ad0yctld8RYO+bdurlvw==",
|
"integrity": "sha512-IOc8uWeOZgnb3ptbCURJWNjWUPcO3ZnTTdzsurqERrP6nPyv+paC55vJM0LpOlT2ne+Ix+9+CRG1MNLlyZ4GjQ==",
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
},
|
},
|
||||||
"node_modules/shebang-command": {
|
"node_modules/shebang-command": {
|
||||||
@@ -5638,15 +5649,16 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/tar": {
|
"node_modules/tar": {
|
||||||
"version": "7.5.7",
|
"version": "7.4.3",
|
||||||
"resolved": "https://registry.npmjs.org/tar/-/tar-7.5.7.tgz",
|
"resolved": "https://registry.npmjs.org/tar/-/tar-7.4.3.tgz",
|
||||||
"integrity": "sha512-fov56fJiRuThVFXD6o6/Q354S7pnWMJIVlDBYijsTNx6jKSE4pvrDTs6lUnmGvNyfJwFQQwWy3owKz1ucIhveQ==",
|
"integrity": "sha512-5S7Va8hKfV7W5U6g3aYxXmlPoZVAwUMy9AOKyF2fVuZa2UD3qZjg578OrLRt8PcNN1PleVaL/5/yYATNL0ICUw==",
|
||||||
"license": "BlueOak-1.0.0",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@isaacs/fs-minipass": "^4.0.0",
|
"@isaacs/fs-minipass": "^4.0.0",
|
||||||
"chownr": "^3.0.0",
|
"chownr": "^3.0.0",
|
||||||
"minipass": "^7.1.2",
|
"minipass": "^7.1.2",
|
||||||
"minizlib": "^3.1.0",
|
"minizlib": "^3.0.1",
|
||||||
|
"mkdirp": "^3.0.1",
|
||||||
"yallist": "^5.0.0"
|
"yallist": "^5.0.0"
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
|
|||||||
@@ -3,7 +3,11 @@ import { useEffect } from "react";
|
|||||||
import Dashboard from "./Dashboard";
|
import Dashboard from "./Dashboard";
|
||||||
import Login from "./Login";
|
import Login from "./Login";
|
||||||
import Cookies from "js-cookie";
|
import Cookies from "js-cookie";
|
||||||
import { API_BASE } from "@/config/api.config";
|
|
||||||
|
const API_BASE =
|
||||||
|
(import.meta as any).env?.VITE_BACKEND_URL ||
|
||||||
|
import.meta.env.VITE_BACKEND_URL ||
|
||||||
|
"http://localhost:8002";
|
||||||
|
|
||||||
const Layout: React.FC = () => {
|
const Layout: React.FC = () => {
|
||||||
const [isLoggedIn, setIsLoggedIn] = useState(false);
|
const [isLoggedIn, setIsLoggedIn] = useState(false);
|
||||||
@@ -11,15 +15,12 @@ const Layout: React.FC = () => {
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (Cookies.get("token")) {
|
if (Cookies.get("token")) {
|
||||||
const verifyToken = async () => {
|
const verifyToken = async () => {
|
||||||
const response = await fetch(
|
const response = await fetch(`${API_BASE}/api/verifyToken`, {
|
||||||
`${API_BASE}/api/admin/user-mgmt/verify-token`,
|
|
||||||
{
|
|
||||||
method: "GET",
|
method: "GET",
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${Cookies.get("token")}`,
|
Authorization: `Bearer ${Cookies.get("token")}`,
|
||||||
},
|
},
|
||||||
}
|
});
|
||||||
);
|
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
setIsLoggedIn(true);
|
setIsLoggedIn(true);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ import { useState } from "react";
|
|||||||
import { loginFunc } from "@/utils/loginUser";
|
import { loginFunc } from "@/utils/loginUser";
|
||||||
import MyAlert from "../components/myChakra/MyAlert";
|
import MyAlert from "../components/myChakra/MyAlert";
|
||||||
import { Button, Card, Field, Input, Stack } from "@chakra-ui/react";
|
import { Button, Card, Field, Input, Stack } from "@chakra-ui/react";
|
||||||
import { PasswordInput } from "@/components/ui/password-input";
|
|
||||||
|
|
||||||
const Login: React.FC<{ onSuccess: () => void }> = ({ onSuccess }) => {
|
const Login: React.FC<{ onSuccess: () => void }> = ({ onSuccess }) => {
|
||||||
const [username, setUsername] = useState("");
|
const [username, setUsername] = useState("");
|
||||||
@@ -44,7 +43,8 @@ const Login: React.FC<{ onSuccess: () => void }> = ({ onSuccess }) => {
|
|||||||
</Field.Root>
|
</Field.Root>
|
||||||
<Field.Root>
|
<Field.Root>
|
||||||
<Field.Label>password</Field.Label>
|
<Field.Label>password</Field.Label>
|
||||||
<PasswordInput
|
<Input
|
||||||
|
type="password"
|
||||||
value={password}
|
value={password}
|
||||||
onChange={(e) => setPassword(e.target.value)}
|
onChange={(e) => setPassword(e.target.value)}
|
||||||
/>
|
/>
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
import { useEffect, useState } from "react";
|
|
||||||
import { Box, Flex, VStack, Heading, Text, Link } from "@chakra-ui/react";
|
import { Box, Flex, VStack, Heading, Text, Link } from "@chakra-ui/react";
|
||||||
import { API_BASE } from "@/config/api.config";
|
|
||||||
|
|
||||||
type SidebarProps = {
|
type SidebarProps = {
|
||||||
viewAusleihen: () => void;
|
viewAusleihen: () => void;
|
||||||
@@ -17,22 +15,10 @@ const Sidebar: React.FC<SidebarProps> = ({
|
|||||||
viewUser,
|
viewUser,
|
||||||
viewAPI,
|
viewAPI,
|
||||||
}) => {
|
}) => {
|
||||||
const [info, setInfo] = useState<any>(null);
|
|
||||||
|
|
||||||
const fetchInfo = async () => {
|
|
||||||
const response = await fetch(`${API_BASE}/`);
|
|
||||||
const data = await response.json();
|
|
||||||
setInfo(data);
|
|
||||||
};
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
fetchInfo();
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Box
|
<Box
|
||||||
as="aside"
|
as="aside"
|
||||||
w="180px"
|
w="260px"
|
||||||
minH="100vh"
|
minH="100vh"
|
||||||
bg="gray.800"
|
bg="gray.800"
|
||||||
color="gray.100"
|
color="gray.100"
|
||||||
@@ -86,33 +72,7 @@ const Sidebar: React.FC<SidebarProps> = ({
|
|||||||
</VStack>
|
</VStack>
|
||||||
|
|
||||||
<Box mt="auto" pt={8} fontSize="xs" color="gray.500">
|
<Box mt="auto" pt={8} fontSize="xs" color="gray.500">
|
||||||
<Text mb={2}>© Made with ❤️ by Theis Gaedigk</Text>
|
<Text>© Made with ❤️ by Theis Gaedigk</Text>
|
||||||
{info ? (
|
|
||||||
<Flex gap={2} wrap="wrap">
|
|
||||||
<Box
|
|
||||||
as="span"
|
|
||||||
px={2}
|
|
||||||
py={0.5}
|
|
||||||
rounded="full"
|
|
||||||
bg="gray.700"
|
|
||||||
color="gray.200"
|
|
||||||
>
|
|
||||||
Panel {info?.["admin-panel-info"]?.version ?? "—"}
|
|
||||||
</Box>
|
|
||||||
<Box
|
|
||||||
as="span"
|
|
||||||
px={2}
|
|
||||||
py={0.5}
|
|
||||||
rounded="full"
|
|
||||||
bg="gray.700"
|
|
||||||
color="gray.200"
|
|
||||||
>
|
|
||||||
Backend {info?.["backend-info"]?.version ?? "—"}
|
|
||||||
</Box>
|
|
||||||
</Flex>
|
|
||||||
) : (
|
|
||||||
<Text color="gray.600">Lade Versionsinfos…</Text>
|
|
||||||
)}
|
|
||||||
</Box>
|
</Box>
|
||||||
</Flex>
|
</Flex>
|
||||||
</Box>
|
</Box>
|
||||||
|
|||||||
@@ -17,14 +17,17 @@ import { useState, useEffect } from "react";
|
|||||||
import { deleteAPKey } from "@/utils/userActions";
|
import { deleteAPKey } from "@/utils/userActions";
|
||||||
import AddAPIKey from "./AddAPIKey";
|
import AddAPIKey from "./AddAPIKey";
|
||||||
import { formatDateTime } from "@/utils/userFuncs";
|
import { formatDateTime } from "@/utils/userFuncs";
|
||||||
import { API_BASE } from "@/config/api.config";
|
|
||||||
|
const API_BASE =
|
||||||
|
(import.meta as any).env?.VITE_BACKEND_URL ||
|
||||||
|
import.meta.env.VITE_BACKEND_URL ||
|
||||||
|
"http://localhost:8002";
|
||||||
|
|
||||||
type Items = {
|
type Items = {
|
||||||
id: number;
|
id: number;
|
||||||
api_key: string;
|
apiKey: string;
|
||||||
entry_name: string;
|
user: string;
|
||||||
entry_created_at: string;
|
entry_created_at: string;
|
||||||
last_used_at: string | null;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const APIKeyTable: React.FC = () => {
|
const APIKeyTable: React.FC = () => {
|
||||||
@@ -53,15 +56,12 @@ const APIKeyTable: React.FC = () => {
|
|||||||
const fetchData = async () => {
|
const fetchData = async () => {
|
||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
try {
|
try {
|
||||||
const response = await fetch(
|
const response = await fetch(`${API_BASE}/api/apiKeys`, {
|
||||||
`${API_BASE}/api/admin/api-data/get-api-keys`,
|
|
||||||
{
|
|
||||||
method: "GET",
|
method: "GET",
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${Cookies.get("token")}`,
|
Authorization: `Bearer ${Cookies.get("token")}`,
|
||||||
},
|
},
|
||||||
}
|
});
|
||||||
);
|
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
return data;
|
return data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -123,8 +123,8 @@ const APIKeyTable: React.FC = () => {
|
|||||||
</HStack>
|
</HStack>
|
||||||
{/* End action toolbar */}
|
{/* End action toolbar */}
|
||||||
|
|
||||||
<Heading marginBottom={4} size="2xl">
|
<Heading marginBottom={4} size="md">
|
||||||
API Keys
|
Gegenstände
|
||||||
</Heading>
|
</Heading>
|
||||||
{isError && (
|
{isError && (
|
||||||
<MyAlert
|
<MyAlert
|
||||||
@@ -149,55 +149,39 @@ const APIKeyTable: React.FC = () => {
|
|||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
<Table.Root
|
<Table.Root size="sm" striped>
|
||||||
size="sm"
|
|
||||||
striped
|
|
||||||
w="100%"
|
|
||||||
// table-layout: auto => Spaltenbreite nach Content; volle Breite nutzen
|
|
||||||
style={{ tableLayout: "auto" }}
|
|
||||||
>
|
|
||||||
<Table.Header>
|
<Table.Header>
|
||||||
<Table.Row>
|
<Table.Row>
|
||||||
<Table.ColumnHeader width="1%" whiteSpace="nowrap">
|
<Table.ColumnHeader>
|
||||||
<strong>#</strong>
|
<strong>#</strong>
|
||||||
</Table.ColumnHeader>
|
</Table.ColumnHeader>
|
||||||
<Table.ColumnHeader>
|
<Table.ColumnHeader>
|
||||||
<strong>API Key</strong>
|
<strong>API Key</strong>
|
||||||
</Table.ColumnHeader>
|
</Table.ColumnHeader>
|
||||||
<Table.ColumnHeader>
|
<Table.ColumnHeader>
|
||||||
<strong>Name</strong>
|
<strong>Benutzer</strong>
|
||||||
</Table.ColumnHeader>
|
</Table.ColumnHeader>
|
||||||
<Table.ColumnHeader whiteSpace="nowrap">
|
<Table.ColumnHeader>
|
||||||
<strong>Eintrag erstellt am</strong>
|
<strong>Eintrag erstellt am</strong>
|
||||||
</Table.ColumnHeader>
|
</Table.ColumnHeader>
|
||||||
<Table.ColumnHeader whiteSpace="nowrap">
|
<Table.ColumnHeader>
|
||||||
<strong>Zuletzt benutzt am</strong>
|
|
||||||
</Table.ColumnHeader>
|
|
||||||
<Table.ColumnHeader width="1%" whiteSpace="nowrap">
|
|
||||||
<strong>Aktionen</strong>
|
<strong>Aktionen</strong>
|
||||||
</Table.ColumnHeader>
|
</Table.ColumnHeader>
|
||||||
</Table.Row>
|
</Table.Row>
|
||||||
</Table.Header>
|
</Table.Header>
|
||||||
<Table.Body>
|
<Table.Body>
|
||||||
{items.map((item) => (
|
{items.map((apiKey) => (
|
||||||
<Table.Row key={item.id}>
|
<Table.Row key={apiKey.id}>
|
||||||
<Table.Cell whiteSpace="nowrap">{item.id}</Table.Cell>
|
<Table.Cell>{apiKey.id}</Table.Cell>
|
||||||
<Table.Cell fontFamily="mono">{item.api_key}</Table.Cell>
|
<Table.Cell>{apiKey.apiKey}</Table.Cell>
|
||||||
<Table.Cell>{item.entry_name}</Table.Cell>
|
<Table.Cell>{apiKey.user}</Table.Cell>
|
||||||
<Table.Cell whiteSpace="nowrap">
|
<Table.Cell>{formatDateTime(apiKey.entry_created_at)}</Table.Cell>
|
||||||
{formatDateTime(item.entry_created_at)}
|
<Table.Cell>
|
||||||
</Table.Cell>
|
|
||||||
<Table.Cell whiteSpace="nowrap">
|
|
||||||
{!item.last_used_at
|
|
||||||
? "Nie benutzt"
|
|
||||||
: formatDateTime(item.last_used_at)}
|
|
||||||
</Table.Cell>
|
|
||||||
<Table.Cell whiteSpace="nowrap">
|
|
||||||
<Button
|
<Button
|
||||||
onClick={() =>
|
onClick={() =>
|
||||||
deleteAPKey(item.id).then((response) => {
|
deleteAPKey(apiKey.id).then((response) => {
|
||||||
if (response.success) {
|
if (response.success) {
|
||||||
setItems(items.filter((i) => i.id !== item.id));
|
setItems(items.filter((i) => i.id !== apiKey.id));
|
||||||
setError(
|
setError(
|
||||||
"success",
|
"success",
|
||||||
"Gegenstand gelöscht",
|
"Gegenstand gelöscht",
|
||||||
|
|||||||
@@ -1,15 +1,6 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
import {
|
import { Button, Card, Field, Input, Stack } from "@chakra-ui/react";
|
||||||
Button,
|
|
||||||
Card,
|
|
||||||
Field,
|
|
||||||
Input,
|
|
||||||
Stack,
|
|
||||||
InputGroup,
|
|
||||||
Span,
|
|
||||||
} from "@chakra-ui/react";
|
|
||||||
import { createAPIentry } from "@/utils/userActions";
|
import { createAPIentry } from "@/utils/userActions";
|
||||||
import { useState } from "react";
|
|
||||||
|
|
||||||
type AddAPIKeyProps = {
|
type AddAPIKeyProps = {
|
||||||
onClose: () => void;
|
onClose: () => void;
|
||||||
@@ -21,8 +12,6 @@ type AddAPIKeyProps = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const AddAPIKey: React.FC<AddAPIKeyProps> = ({ onClose, alert }) => {
|
const AddAPIKey: React.FC<AddAPIKeyProps> = ({ onClose, alert }) => {
|
||||||
const [value, setValue] = useState("");
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/60 backdrop-blur-sm p-4">
|
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/60 backdrop-blur-sm p-4">
|
||||||
<Card.Root maxW="sm">
|
<Card.Root maxW="sm">
|
||||||
@@ -34,26 +23,13 @@ const AddAPIKey: React.FC<AddAPIKeyProps> = ({ onClose, alert }) => {
|
|||||||
</Card.Header>
|
</Card.Header>
|
||||||
<Card.Body>
|
<Card.Body>
|
||||||
<Stack gap="4" w="full">
|
<Stack gap="4" w="full">
|
||||||
<InputGroup
|
|
||||||
endElement={
|
|
||||||
<Span color="fg.muted" textStyle="xs">
|
|
||||||
{value.length} / {8}
|
|
||||||
</Span>
|
|
||||||
}
|
|
||||||
>
|
|
||||||
<Input
|
|
||||||
placeholder="Er muss 8 zahlen lang sein"
|
|
||||||
value={value}
|
|
||||||
id="apiKey"
|
|
||||||
maxLength={8}
|
|
||||||
onChange={(e) => {
|
|
||||||
setValue(e.currentTarget.value.slice(0, 8));
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
</InputGroup>
|
|
||||||
<Field.Root>
|
<Field.Root>
|
||||||
<Field.Label>Name</Field.Label>
|
<Field.Label>API key</Field.Label>
|
||||||
<Input id="name" type="text" />
|
<Input type="number" id="apiKey" />
|
||||||
|
</Field.Root>
|
||||||
|
<Field.Root>
|
||||||
|
<Field.Label>Benutzer</Field.Label>
|
||||||
|
<Input id="user" type="text" />
|
||||||
</Field.Root>
|
</Field.Root>
|
||||||
</Stack>
|
</Stack>
|
||||||
</Card.Body>
|
</Card.Body>
|
||||||
@@ -68,14 +44,14 @@ const AddAPIKey: React.FC<AddAPIKeyProps> = ({ onClose, alert }) => {
|
|||||||
(
|
(
|
||||||
document.getElementById("apiKey") as HTMLInputElement
|
document.getElementById("apiKey") as HTMLInputElement
|
||||||
)?.value.trim() || "";
|
)?.value.trim() || "";
|
||||||
const name =
|
const user =
|
||||||
(
|
(
|
||||||
document.getElementById("name") as HTMLInputElement
|
document.getElementById("user") as HTMLInputElement
|
||||||
)?.value.trim() || "";
|
)?.value.trim() || "";
|
||||||
|
|
||||||
if (!apiKey || !name) return;
|
if (!apiKey || !user) return;
|
||||||
|
|
||||||
const res = await createAPIentry(apiKey, name);
|
const res = await createAPIentry(apiKey, user);
|
||||||
if (res.success) {
|
if (res.success) {
|
||||||
alert(
|
alert(
|
||||||
"success",
|
"success",
|
||||||
|
|||||||
@@ -1,13 +1,5 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
import {
|
import { Button, Card, Field, Input, Stack } from "@chakra-ui/react";
|
||||||
Button,
|
|
||||||
Card,
|
|
||||||
Field,
|
|
||||||
Input,
|
|
||||||
Stack,
|
|
||||||
Text,
|
|
||||||
Checkbox,
|
|
||||||
} from "@chakra-ui/react";
|
|
||||||
import { createUser } from "@/utils/userActions";
|
import { createUser } from "@/utils/userActions";
|
||||||
|
|
||||||
type AddFormProps = {
|
type AddFormProps = {
|
||||||
@@ -20,71 +12,37 @@ type AddFormProps = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const AddForm: React.FC<AddFormProps> = ({ onClose, alert }) => {
|
const AddForm: React.FC<AddFormProps> = ({ onClose, alert }) => {
|
||||||
const [admin, setAdmin] = React.useState(false);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/60 backdrop-blur-sm p-4">
|
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/60 backdrop-blur-sm p-4">
|
||||||
<form
|
|
||||||
onSubmit={(e) => {
|
|
||||||
e.preventDefault();
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
<Card.Root maxW="sm">
|
<Card.Root maxW="sm">
|
||||||
<Card.Header>
|
<Card.Header>
|
||||||
<Card.Title>Neuen Nutzer erstellen</Card.Title>
|
<Card.Title>Neuen Nutzer erstellen</Card.Title>
|
||||||
<Card.Description>
|
<Card.Description>
|
||||||
Füllen Sie das folgende Formular aus, um einen Nutzer zu
|
Füllen Sie das folgende Formular aus, um einen Nutzer zu erstellen.
|
||||||
erstellen.
|
|
||||||
</Card.Description>
|
</Card.Description>
|
||||||
</Card.Header>
|
</Card.Header>
|
||||||
|
|
||||||
<Card.Body>
|
<Card.Body>
|
||||||
<Stack gap="4" w="full">
|
<Stack gap="4" w="full">
|
||||||
<Field.Root>
|
<Field.Root>
|
||||||
<Field.Label>Benutzername</Field.Label>
|
<Field.Label>Username</Field.Label>
|
||||||
<Input id="username" />
|
<Input id="username" />
|
||||||
</Field.Root>
|
</Field.Root>
|
||||||
<Field.Root>
|
<Field.Root>
|
||||||
<Field.Label>Passwort</Field.Label>
|
<Field.Label>Password</Field.Label>
|
||||||
<Input id="password" type="password" />
|
<Input id="password" type="password" />
|
||||||
</Field.Root>
|
</Field.Root>
|
||||||
<Field.Root>
|
<Field.Root>
|
||||||
<Field.Label>Vorname</Field.Label>
|
<Field.Label>Role</Field.Label>
|
||||||
<Input id="firstname" />
|
|
||||||
</Field.Root>
|
|
||||||
<Field.Root>
|
|
||||||
<Field.Label>Nachname</Field.Label>
|
|
||||||
<Input id="lastname" />
|
|
||||||
</Field.Root>
|
|
||||||
<Field.Root>
|
|
||||||
<Field.Label>E-Mail</Field.Label>
|
|
||||||
<Input id="email" type="email" />
|
|
||||||
</Field.Root>
|
|
||||||
|
|
||||||
{/* Kontrollierte Checkbox */}
|
|
||||||
<Checkbox.Root
|
|
||||||
checked={admin}
|
|
||||||
onCheckedChange={(e: any) => setAdmin(Boolean(e?.checked ?? e))}
|
|
||||||
>
|
|
||||||
<Checkbox.HiddenInput />
|
|
||||||
<Checkbox.Control />
|
|
||||||
<Checkbox.Label>Admin</Checkbox.Label>
|
|
||||||
</Checkbox.Root>
|
|
||||||
|
|
||||||
<Field.Root>
|
|
||||||
<Field.Label>Rolle</Field.Label>
|
|
||||||
<Input id="role" type="number" />
|
<Input id="role" type="number" />
|
||||||
</Field.Root>
|
</Field.Root>
|
||||||
</Stack>
|
</Stack>
|
||||||
</Card.Body>
|
</Card.Body>
|
||||||
<Card.Footer justifyContent="flex-end">
|
<Card.Footer justifyContent="flex-end">
|
||||||
<Text>Der Benutzername kann nicht mehr geändert werden.</Text>
|
|
||||||
<Button variant="outline" onClick={onClose}>
|
<Button variant="outline" onClick={onClose}>
|
||||||
Abbrechen
|
Abbrechen
|
||||||
</Button>
|
</Button>
|
||||||
<Button
|
<Button
|
||||||
variant="solid"
|
variant="solid"
|
||||||
type="submit"
|
|
||||||
onClick={async () => {
|
onClick={async () => {
|
||||||
const username =
|
const username =
|
||||||
(
|
(
|
||||||
@@ -96,30 +54,10 @@ const AddForm: React.FC<AddFormProps> = ({ onClose, alert }) => {
|
|||||||
const role = Number(
|
const role = Number(
|
||||||
(document.getElementById("role") as HTMLInputElement)?.value
|
(document.getElementById("role") as HTMLInputElement)?.value
|
||||||
);
|
);
|
||||||
const firstname =
|
|
||||||
(
|
|
||||||
document.getElementById("firstname") as HTMLInputElement
|
|
||||||
)?.value.trim() || "";
|
|
||||||
const lastname =
|
|
||||||
(
|
|
||||||
document.getElementById("lastname") as HTMLInputElement
|
|
||||||
)?.value.trim() || "";
|
|
||||||
const email =
|
|
||||||
(
|
|
||||||
document.getElementById("email") as HTMLInputElement
|
|
||||||
)?.value.trim() || "";
|
|
||||||
|
|
||||||
// admin kommt jetzt zuverlässig aus dem State
|
if (!username || !password || Number.isNaN(role)) return;
|
||||||
const res = await createUser(
|
|
||||||
username,
|
|
||||||
role,
|
|
||||||
password,
|
|
||||||
firstname,
|
|
||||||
lastname,
|
|
||||||
email,
|
|
||||||
admin
|
|
||||||
);
|
|
||||||
|
|
||||||
|
const res = await createUser(username, role, password);
|
||||||
if (res.success) {
|
if (res.success) {
|
||||||
alert(
|
alert(
|
||||||
"success",
|
"success",
|
||||||
@@ -141,7 +79,6 @@ const AddForm: React.FC<AddFormProps> = ({ onClose, alert }) => {
|
|||||||
</Button>
|
</Button>
|
||||||
</Card.Footer>
|
</Card.Footer>
|
||||||
</Card.Root>
|
</Card.Root>
|
||||||
</form>
|
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -25,19 +25,15 @@ const AddItemForm: React.FC<AddItemFormProps> = ({ onClose, alert }) => {
|
|||||||
<Card.Body>
|
<Card.Body>
|
||||||
<Stack gap="4" w="full">
|
<Stack gap="4" w="full">
|
||||||
<Field.Root>
|
<Field.Root>
|
||||||
<Field.Label>Gegenstandsname (muss einzigartig sein)</Field.Label>
|
<Field.Label>Gegenstandsname</Field.Label>
|
||||||
<Input id="item_name" placeholder="z.B. Laptop" />
|
<Input id="item_name" placeholder="z.B. Laptop" />
|
||||||
</Field.Root>
|
</Field.Root>
|
||||||
<Field.Root>
|
|
||||||
<Field.Label>Schließfachnummer</Field.Label>
|
|
||||||
<Input id="safe_nr" placeholder="Nummer 1 - 6" />
|
|
||||||
</Field.Root>
|
|
||||||
<Field.Root>
|
<Field.Root>
|
||||||
<Field.Label>Ausleih-Berechtigung (Rolle)</Field.Label>
|
<Field.Label>Ausleih-Berechtigung (Rolle)</Field.Label>
|
||||||
<Input
|
<Input
|
||||||
id="can_borrow_role"
|
id="can_borrow_role"
|
||||||
type="number"
|
type="number"
|
||||||
placeholder="Zahl (1 - 6)"
|
placeholder="Zahl (1 - 4)"
|
||||||
/>
|
/>
|
||||||
</Field.Root>
|
</Field.Root>
|
||||||
</Stack>
|
</Stack>
|
||||||
@@ -57,15 +53,10 @@ const AddItemForm: React.FC<AddItemFormProps> = ({ onClose, alert }) => {
|
|||||||
(document.getElementById("can_borrow_role") as HTMLInputElement)
|
(document.getElementById("can_borrow_role") as HTMLInputElement)
|
||||||
?.value
|
?.value
|
||||||
);
|
);
|
||||||
const safeNrValue = (
|
|
||||||
document.getElementById("safe_nr") as HTMLInputElement
|
|
||||||
)?.value.trim();
|
|
||||||
|
|
||||||
const safeNr = safeNrValue === "" ? null : safeNrValue;
|
|
||||||
|
|
||||||
if (!name || Number.isNaN(role)) return;
|
if (!name || Number.isNaN(role)) return;
|
||||||
|
|
||||||
const res = await createItem(name, role, safeNr);
|
const res = await createItem(name, role);
|
||||||
if (res.success) {
|
if (res.success) {
|
||||||
alert(
|
alert(
|
||||||
"success",
|
"success",
|
||||||
|
|||||||
@@ -30,19 +30,18 @@ import {
|
|||||||
} from "@/utils/userActions";
|
} from "@/utils/userActions";
|
||||||
import AddItemForm from "./AddItemForm";
|
import AddItemForm from "./AddItemForm";
|
||||||
import { formatDateTime } from "@/utils/userFuncs";
|
import { formatDateTime } from "@/utils/userFuncs";
|
||||||
import { API_BASE } from "@/config/api.config";
|
|
||||||
|
const API_BASE =
|
||||||
|
(import.meta as any).env?.VITE_BACKEND_URL ||
|
||||||
|
import.meta.env.VITE_BACKEND_URL ||
|
||||||
|
"http://localhost:8002";
|
||||||
|
|
||||||
type Items = {
|
type Items = {
|
||||||
id: number;
|
id: number;
|
||||||
item_name: string;
|
item_name: string;
|
||||||
can_borrow_role: string;
|
can_borrow_role: string;
|
||||||
in_safe: boolean;
|
inSafe: boolean;
|
||||||
safe_nr: string;
|
|
||||||
door_key: string;
|
|
||||||
entry_created_at: string;
|
entry_created_at: string;
|
||||||
entry_updated_at: string;
|
|
||||||
last_borrowed_person: string | null;
|
|
||||||
currently_borrowing: string | null;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const ItemTable: React.FC = () => {
|
const ItemTable: React.FC = () => {
|
||||||
@@ -67,18 +66,6 @@ const ItemTable: React.FC = () => {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleLockerNumberChange = (id: number, value: string) => {
|
|
||||||
setItems((prev) =>
|
|
||||||
prev.map((it) => (it.id === id ? { ...it, safe_nr: value } : it))
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
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,
|
||||||
@@ -95,15 +82,12 @@ const ItemTable: React.FC = () => {
|
|||||||
const fetchData = async () => {
|
const fetchData = async () => {
|
||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
try {
|
try {
|
||||||
const response = await fetch(
|
const response = await fetch(`${API_BASE}/api/allItems`, {
|
||||||
`${API_BASE}/api/admin/item-data/all-items`,
|
|
||||||
{
|
|
||||||
method: "GET",
|
method: "GET",
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${Cookies.get("token")}`,
|
Authorization: `Bearer ${Cookies.get("token")}`,
|
||||||
},
|
},
|
||||||
}
|
});
|
||||||
);
|
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
return data;
|
return data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -191,14 +175,7 @@ const ItemTable: React.FC = () => {
|
|||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{/* make table fill available width, like UserTable */}
|
<Table.Root size="sm" striped>
|
||||||
{!isLoading && (
|
|
||||||
<Table.Root
|
|
||||||
size="sm"
|
|
||||||
striped
|
|
||||||
w="100%"
|
|
||||||
style={{ tableLayout: "auto" }} // Spalten nach Content
|
|
||||||
>
|
|
||||||
<Table.Header>
|
<Table.Header>
|
||||||
<Table.Row>
|
<Table.Row>
|
||||||
<Table.ColumnHeader>
|
<Table.ColumnHeader>
|
||||||
@@ -213,25 +190,10 @@ 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">
|
|
||||||
<strong>Schließfachnummer</strong>
|
|
||||||
</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>
|
||||||
<Table.ColumnHeader>
|
<Table.ColumnHeader>
|
||||||
<strong>Eintrag aktualisiert am</strong>
|
|
||||||
</Table.ColumnHeader>
|
|
||||||
<Table.ColumnHeader>
|
|
||||||
<strong>LaP *</strong>
|
|
||||||
</Table.ColumnHeader>
|
|
||||||
<Table.ColumnHeader>
|
|
||||||
<strong>Dav **</strong>
|
|
||||||
</Table.ColumnHeader>
|
|
||||||
<Table.ColumnHeader width="1%" whiteSpace="nowrap">
|
|
||||||
<strong>Aktionen</strong>
|
<strong>Aktionen</strong>
|
||||||
</Table.ColumnHeader>
|
</Table.ColumnHeader>
|
||||||
</Table.Row>
|
</Table.Row>
|
||||||
@@ -242,8 +204,6 @@ const ItemTable: React.FC = () => {
|
|||||||
<Table.Cell>{item.id}</Table.Cell>
|
<Table.Cell>{item.id}</Table.Cell>
|
||||||
<Table.Cell>
|
<Table.Cell>
|
||||||
<Input
|
<Input
|
||||||
size="sm"
|
|
||||||
w="max-content"
|
|
||||||
onChange={(e) =>
|
onChange={(e) =>
|
||||||
handleItemNameChange(item.id, e.target.value)
|
handleItemNameChange(item.id, e.target.value)
|
||||||
}
|
}
|
||||||
@@ -252,8 +212,6 @@ const ItemTable: React.FC = () => {
|
|||||||
</Table.Cell>
|
</Table.Cell>
|
||||||
<Table.Cell>
|
<Table.Cell>
|
||||||
<Input
|
<Input
|
||||||
size="sm"
|
|
||||||
w="max-content"
|
|
||||||
onChange={(e) =>
|
onChange={(e) =>
|
||||||
handleCanBorrowRoleChange(item.id, e.target.value)
|
handleCanBorrowRoleChange(item.id, e.target.value)
|
||||||
}
|
}
|
||||||
@@ -271,62 +229,37 @@ const ItemTable: React.FC = () => {
|
|||||||
py={1}
|
py={1}
|
||||||
gap={2}
|
gap={2}
|
||||||
variant="ghost"
|
variant="ghost"
|
||||||
color={item.in_safe ? "green.600" : "red.600"}
|
color={item.inSafe ? "green.600" : "red.600"}
|
||||||
borderWidth="1px"
|
borderWidth="1px"
|
||||||
borderColor={item.in_safe ? "green.300" : "red.300"}
|
borderColor={item.inSafe ? "green.300" : "red.300"}
|
||||||
_hover={{
|
_hover={{
|
||||||
bg: item.in_safe ? "green.50" : "red.50",
|
bg: item.inSafe ? "green.50" : "red.50",
|
||||||
borderColor: item.in_safe ? "green.400" : "red.400",
|
borderColor: item.inSafe ? "green.400" : "red.400",
|
||||||
transform: "translateY(-1px)",
|
transform: "translateY(-1px)",
|
||||||
shadow: "sm",
|
shadow: "sm",
|
||||||
}}
|
}}
|
||||||
_active={{ transform: "translateY(0)" }}
|
_active={{ transform: "translateY(0)" }}
|
||||||
aria-label={
|
aria-label={
|
||||||
item.in_safe ? "Mark as not in safe" : "Mark as in safe"
|
item.inSafe ? "Mark as not in safe" : "Mark as in safe"
|
||||||
}
|
}
|
||||||
>
|
>
|
||||||
<Icon
|
<Icon
|
||||||
as={item.in_safe ? CheckCircle2 : XCircle}
|
as={item.inSafe ? CheckCircle2 : XCircle}
|
||||||
boxSize={3.5}
|
boxSize={3.5}
|
||||||
mr={2}
|
mr={2}
|
||||||
/>
|
/>
|
||||||
<Text as="span" fontSize="xs" fontWeight="semibold">
|
<Text as="span" fontSize="xs" fontWeight="semibold">
|
||||||
{item.in_safe ? "Yes" : "No"}
|
{item.inSafe ? "Yes" : "No"}
|
||||||
</Text>
|
</Text>
|
||||||
</Button>
|
</Button>
|
||||||
</Table.Cell>
|
</Table.Cell>
|
||||||
<Table.Cell>
|
|
||||||
<Input
|
|
||||||
size="sm"
|
|
||||||
w="max-content"
|
|
||||||
onChange={(e) =>
|
|
||||||
handleLockerNumberChange(item.id, e.target.value)
|
|
||||||
}
|
|
||||||
value={item.safe_nr}
|
|
||||||
/>
|
|
||||||
</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>
|
||||||
<Table.Cell>{item.last_borrowed_person}</Table.Cell>
|
|
||||||
<Table.Cell>{item.currently_borrowing}</Table.Cell>
|
|
||||||
<Table.Cell whiteSpace="nowrap">
|
|
||||||
<Button
|
<Button
|
||||||
onClick={() =>
|
onClick={() =>
|
||||||
handleEditItems(
|
handleEditItems(
|
||||||
item.id,
|
item.id,
|
||||||
item.item_name,
|
item.item_name,
|
||||||
item.safe_nr,
|
|
||||||
item.door_key,
|
|
||||||
item.can_borrow_role
|
item.can_borrow_role
|
||||||
).then((response) => {
|
).then((response) => {
|
||||||
if (response.success) {
|
if (response.success) {
|
||||||
@@ -372,9 +305,6 @@ const ItemTable: React.FC = () => {
|
|||||||
))}
|
))}
|
||||||
</Table.Body>
|
</Table.Body>
|
||||||
</Table.Root>
|
</Table.Root>
|
||||||
)}
|
|
||||||
<Text>* LaP = Letzte ausleihende Person</Text>
|
|
||||||
<Text>** Dav = Derzeit ausgeliehen von</Text>
|
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -17,7 +17,11 @@ import MyAlert from "./myChakra/MyAlert";
|
|||||||
import { formatDateTime } from "@/utils/userFuncs";
|
import { formatDateTime } from "@/utils/userFuncs";
|
||||||
import { Trash2, RefreshCcwDot } from "lucide-react";
|
import { Trash2, RefreshCcwDot } from "lucide-react";
|
||||||
import { deleteLoan } from "@/utils/userActions";
|
import { deleteLoan } from "@/utils/userActions";
|
||||||
import { API_BASE } from "@/config/api.config";
|
|
||||||
|
const API_BASE =
|
||||||
|
(import.meta as any).env?.VITE_BACKEND_URL ||
|
||||||
|
import.meta.env.VITE_BACKEND_URL ||
|
||||||
|
"http://localhost:8002";
|
||||||
|
|
||||||
const LoanTable: React.FC = () => {
|
const LoanTable: React.FC = () => {
|
||||||
const [items, setItems] = useState<Loan[]>([]);
|
const [items, setItems] = useState<Loan[]>([]);
|
||||||
@@ -51,22 +55,18 @@ const LoanTable: React.FC = () => {
|
|||||||
created_at: string;
|
created_at: string;
|
||||||
loaned_items_name: string[];
|
loaned_items_name: string[];
|
||||||
deleted: boolean;
|
deleted: boolean;
|
||||||
note: string;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const fetchData = async () => {
|
const fetchData = async () => {
|
||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
try {
|
try {
|
||||||
const response = await fetch(
|
const response = await fetch(`${API_BASE}/api/allLoans`, {
|
||||||
`${API_BASE}/api/admin/loan-data/all-loans`,
|
|
||||||
{
|
|
||||||
method: "GET",
|
method: "GET",
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${Cookies.get("token")}`,
|
Authorization: `Bearer ${Cookies.get("token")}`,
|
||||||
},
|
},
|
||||||
}
|
});
|
||||||
);
|
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
return data;
|
return data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -161,9 +161,6 @@ const LoanTable: React.FC = () => {
|
|||||||
<Table.ColumnHeader>
|
<Table.ColumnHeader>
|
||||||
<strong>Ausgeliehene Artikel</strong>
|
<strong>Ausgeliehene Artikel</strong>
|
||||||
</Table.ColumnHeader>
|
</Table.ColumnHeader>
|
||||||
<Table.ColumnHeader>
|
|
||||||
<strong>Notiz</strong>
|
|
||||||
</Table.ColumnHeader>
|
|
||||||
<Table.ColumnHeader>
|
<Table.ColumnHeader>
|
||||||
<strong>Aktionen</strong>
|
<strong>Aktionen</strong>
|
||||||
</Table.ColumnHeader>
|
</Table.ColumnHeader>
|
||||||
@@ -183,7 +180,6 @@ const LoanTable: React.FC = () => {
|
|||||||
<Table.Cell>{formatDateTime(item.returned_date)}</Table.Cell>
|
<Table.Cell>{formatDateTime(item.returned_date)}</Table.Cell>
|
||||||
<Table.Cell>{formatDateTime(item.created_at)}</Table.Cell>
|
<Table.Cell>{formatDateTime(item.created_at)}</Table.Cell>
|
||||||
<Table.Cell>{item.loaned_items_name.join(", ")}</Table.Cell>
|
<Table.Cell>{item.loaned_items_name.join(", ")}</Table.Cell>
|
||||||
<Table.Cell>{item.note}</Table.Cell>
|
|
||||||
<Table.Cell>
|
<Table.Cell>
|
||||||
<Button
|
<Button
|
||||||
onClick={() =>
|
onClick={() =>
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ import {
|
|||||||
HStack,
|
HStack,
|
||||||
IconButton,
|
IconButton,
|
||||||
Heading,
|
Heading,
|
||||||
Switch, // neu
|
|
||||||
} from "@chakra-ui/react";
|
} from "@chakra-ui/react";
|
||||||
import { Tooltip } from "@/components/ui/tooltip";
|
import { Tooltip } from "@/components/ui/tooltip";
|
||||||
import { fetchUserData } from "@/utils/fetcher";
|
import { fetchUserData } from "@/utils/fetcher";
|
||||||
@@ -24,13 +23,9 @@ import ChangePWform from "./ChangePWform";
|
|||||||
type User = {
|
type User = {
|
||||||
id: number;
|
id: number;
|
||||||
username: string;
|
username: string;
|
||||||
first_name: string;
|
password: string;
|
||||||
last_name: string;
|
role: string;
|
||||||
email: string;
|
|
||||||
is_admin: boolean;
|
|
||||||
role: number;
|
|
||||||
entry_created_at: string;
|
entry_created_at: string;
|
||||||
entry_updated_at: string;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const UserTable: React.FC = () => {
|
const UserTable: React.FC = () => {
|
||||||
@@ -57,20 +52,10 @@ const UserTable: React.FC = () => {
|
|||||||
setIsError(true);
|
setIsError(true);
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleInputChange = (userId: number, field: string, value: any) => {
|
const handleInputChange = (userId: number, field: string, value: string) => {
|
||||||
setUsers((prevUsers) =>
|
setUsers((prevUsers) =>
|
||||||
prevUsers.map((user) =>
|
prevUsers.map((user) =>
|
||||||
user.id === userId
|
user.id === userId ? { ...user, [field]: value } : user
|
||||||
? {
|
|
||||||
...user,
|
|
||||||
[field]:
|
|
||||||
field === "role"
|
|
||||||
? Number(value)
|
|
||||||
: field === "is_admin"
|
|
||||||
? value === true || value === "true" || value === 1
|
|
||||||
: value,
|
|
||||||
}
|
|
||||||
: user
|
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
@@ -85,6 +70,7 @@ const UserTable: React.FC = () => {
|
|||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
try {
|
try {
|
||||||
const data = await fetchUserData();
|
const data = await fetchUserData();
|
||||||
|
console.log("user api response", data);
|
||||||
if (Array.isArray(data)) {
|
if (Array.isArray(data)) {
|
||||||
setUsers(data);
|
setUsers(data);
|
||||||
} else {
|
} else {
|
||||||
@@ -194,45 +180,25 @@ const UserTable: React.FC = () => {
|
|||||||
</VStack>
|
</VStack>
|
||||||
)}
|
)}
|
||||||
{!isLoading && (
|
{!isLoading && (
|
||||||
<Table.Root
|
<Table.Root size="sm" striped>
|
||||||
size="sm"
|
|
||||||
striped
|
|
||||||
w="100%"
|
|
||||||
style={{ tableLayout: "auto" }} // Spalten nach Content
|
|
||||||
>
|
|
||||||
<Table.Header>
|
<Table.Header>
|
||||||
<Table.Row>
|
<Table.Row>
|
||||||
<Table.ColumnHeader width="1%" whiteSpace="nowrap">
|
<Table.ColumnHeader>
|
||||||
<strong>#</strong>
|
<strong>#</strong>
|
||||||
</Table.ColumnHeader>
|
</Table.ColumnHeader>
|
||||||
<Table.ColumnHeader>
|
<Table.ColumnHeader>
|
||||||
<strong>Benutzername</strong>
|
<strong>Benutzername</strong>
|
||||||
</Table.ColumnHeader>
|
</Table.ColumnHeader>
|
||||||
<Table.ColumnHeader>
|
<Table.ColumnHeader>
|
||||||
<strong>Vorname</strong>
|
|
||||||
</Table.ColumnHeader>
|
|
||||||
<Table.ColumnHeader>
|
|
||||||
<strong>Nachname</strong>
|
|
||||||
</Table.ColumnHeader>
|
|
||||||
<Table.ColumnHeader>
|
|
||||||
<strong>E-Mail</strong>
|
|
||||||
</Table.ColumnHeader>
|
|
||||||
<Table.ColumnHeader width="1%" whiteSpace="nowrap">
|
|
||||||
<strong>Admin</strong>
|
|
||||||
</Table.ColumnHeader>
|
|
||||||
<Table.ColumnHeader whiteSpace="nowrap">
|
|
||||||
<strong>Passwort ändern</strong>
|
<strong>Passwort ändern</strong>
|
||||||
</Table.ColumnHeader>
|
</Table.ColumnHeader>
|
||||||
<Table.ColumnHeader width="1%" whiteSpace="nowrap">
|
<Table.ColumnHeader>
|
||||||
<strong>Rolle</strong>
|
<strong>Rolle</strong>
|
||||||
</Table.ColumnHeader>
|
</Table.ColumnHeader>
|
||||||
<Table.ColumnHeader whiteSpace="nowrap">
|
<Table.ColumnHeader>
|
||||||
<strong>Eintrag erstellt am</strong>
|
<strong>Eintrag erstellt am</strong>
|
||||||
</Table.ColumnHeader>
|
</Table.ColumnHeader>
|
||||||
<Table.ColumnHeader whiteSpace="nowrap">
|
<Table.ColumnHeader>
|
||||||
<strong>Eintrag aktualisiert am</strong>
|
|
||||||
</Table.ColumnHeader>
|
|
||||||
<Table.ColumnHeader width="1%" whiteSpace="nowrap">
|
|
||||||
<strong>Aktionen</strong>
|
<strong>Aktionen</strong>
|
||||||
</Table.ColumnHeader>
|
</Table.ColumnHeader>
|
||||||
</Table.Row>
|
</Table.Row>
|
||||||
@@ -240,86 +206,37 @@ const UserTable: React.FC = () => {
|
|||||||
<Table.Body>
|
<Table.Body>
|
||||||
{users.map((user) => (
|
{users.map((user) => (
|
||||||
<Table.Row key={user.id}>
|
<Table.Row key={user.id}>
|
||||||
<Table.Cell whiteSpace="nowrap">{user.id}</Table.Cell>
|
<Table.Cell>{user.id}</Table.Cell>
|
||||||
<Table.Cell>{user.username}</Table.Cell>
|
|
||||||
<Table.Cell>
|
<Table.Cell>
|
||||||
<Input
|
<Input
|
||||||
size="sm"
|
|
||||||
value={user.first_name ?? ""}
|
|
||||||
onChange={(e) =>
|
onChange={(e) =>
|
||||||
handleInputChange(user.id, "first_name", e.target.value)
|
handleInputChange(user.id, "username", e.target.value)
|
||||||
}
|
}
|
||||||
|
value={user.username}
|
||||||
/>
|
/>
|
||||||
</Table.Cell>
|
</Table.Cell>
|
||||||
<Table.Cell>
|
<Table.Cell>
|
||||||
<Input
|
<Button onClick={() => handlePasswordChange(user.username)}>
|
||||||
size="sm"
|
|
||||||
value={user.last_name ?? ""}
|
|
||||||
onChange={(e) =>
|
|
||||||
handleInputChange(user.id, "last_name", e.target.value)
|
|
||||||
}
|
|
||||||
/>
|
|
||||||
</Table.Cell>
|
|
||||||
<Table.Cell>
|
|
||||||
<Input
|
|
||||||
type="email"
|
|
||||||
size="sm"
|
|
||||||
value={user.email ?? ""}
|
|
||||||
onChange={(e) =>
|
|
||||||
handleInputChange(user.id, "email", e.target.value)
|
|
||||||
}
|
|
||||||
/>
|
|
||||||
</Table.Cell>
|
|
||||||
<Table.Cell whiteSpace="nowrap">
|
|
||||||
<Switch.Root
|
|
||||||
size="sm"
|
|
||||||
checked={!!user.is_admin}
|
|
||||||
onCheckedChange={(d) =>
|
|
||||||
handleInputChange(user.id, "is_admin", d.checked)
|
|
||||||
}
|
|
||||||
aria-label="Adminrechte umschalten"
|
|
||||||
>
|
|
||||||
<Switch.Control>
|
|
||||||
<Switch.Thumb />
|
|
||||||
</Switch.Control>
|
|
||||||
<Switch.HiddenInput />
|
|
||||||
</Switch.Root>
|
|
||||||
</Table.Cell>
|
|
||||||
<Table.Cell whiteSpace="nowrap">
|
|
||||||
<Button
|
|
||||||
size="sm"
|
|
||||||
onClick={() => handlePasswordChange(user.username)}
|
|
||||||
>
|
|
||||||
Passwort ändern
|
Passwort ändern
|
||||||
</Button>
|
</Button>
|
||||||
</Table.Cell>
|
</Table.Cell>
|
||||||
<Table.Cell whiteSpace="nowrap">
|
<Table.Cell>
|
||||||
<Input
|
<Input
|
||||||
type="number"
|
type="number"
|
||||||
size="sm"
|
|
||||||
onChange={(e) =>
|
onChange={(e) =>
|
||||||
handleInputChange(user.id, "role", e.target.value)
|
handleInputChange(user.id, "role", e.target.value)
|
||||||
}
|
}
|
||||||
value={user.role}
|
value={user.role}
|
||||||
width="70px"
|
|
||||||
/>
|
/>
|
||||||
</Table.Cell>
|
</Table.Cell>
|
||||||
<Table.Cell whiteSpace="nowrap">
|
<Table.Cell>{formatDateTime(user.entry_created_at)}</Table.Cell>
|
||||||
{formatDateTime(user.entry_created_at)}
|
<Table.Cell>
|
||||||
</Table.Cell>
|
|
||||||
<Table.Cell whiteSpace="nowrap">
|
|
||||||
{formatDateTime(user.entry_updated_at)}
|
|
||||||
</Table.Cell>
|
|
||||||
<Table.Cell whiteSpace="nowrap">
|
|
||||||
<Button
|
<Button
|
||||||
onClick={() =>
|
onClick={() =>
|
||||||
handleEdit(
|
handleEdit(
|
||||||
user.id,
|
user.id,
|
||||||
user.first_name,
|
user.username,
|
||||||
user.last_name,
|
user.role,
|
||||||
user.email,
|
|
||||||
user.is_admin,
|
|
||||||
Number(user.role)
|
|
||||||
).then((response) => {
|
).then((response) => {
|
||||||
if (response.success) {
|
if (response.success) {
|
||||||
setError(
|
setError(
|
||||||
|
|||||||
@@ -1,159 +0,0 @@
|
|||||||
"use client"
|
|
||||||
|
|
||||||
import type {
|
|
||||||
ButtonProps,
|
|
||||||
GroupProps,
|
|
||||||
InputProps,
|
|
||||||
StackProps,
|
|
||||||
} from "@chakra-ui/react"
|
|
||||||
import {
|
|
||||||
Box,
|
|
||||||
HStack,
|
|
||||||
IconButton,
|
|
||||||
Input,
|
|
||||||
InputGroup,
|
|
||||||
Stack,
|
|
||||||
mergeRefs,
|
|
||||||
useControllableState,
|
|
||||||
} from "@chakra-ui/react"
|
|
||||||
import * as React from "react"
|
|
||||||
import { LuEye, LuEyeOff } from "react-icons/lu"
|
|
||||||
|
|
||||||
export interface PasswordVisibilityProps {
|
|
||||||
/**
|
|
||||||
* The default visibility state of the password input.
|
|
||||||
*/
|
|
||||||
defaultVisible?: boolean
|
|
||||||
/**
|
|
||||||
* The controlled visibility state of the password input.
|
|
||||||
*/
|
|
||||||
visible?: boolean
|
|
||||||
/**
|
|
||||||
* Callback invoked when the visibility state changes.
|
|
||||||
*/
|
|
||||||
onVisibleChange?: (visible: boolean) => void
|
|
||||||
/**
|
|
||||||
* Custom icons for the visibility toggle button.
|
|
||||||
*/
|
|
||||||
visibilityIcon?: { on: React.ReactNode; off: React.ReactNode }
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface PasswordInputProps
|
|
||||||
extends InputProps,
|
|
||||||
PasswordVisibilityProps {
|
|
||||||
rootProps?: GroupProps
|
|
||||||
}
|
|
||||||
|
|
||||||
export const PasswordInput = React.forwardRef<
|
|
||||||
HTMLInputElement,
|
|
||||||
PasswordInputProps
|
|
||||||
>(function PasswordInput(props, ref) {
|
|
||||||
const {
|
|
||||||
rootProps,
|
|
||||||
defaultVisible,
|
|
||||||
visible: visibleProp,
|
|
||||||
onVisibleChange,
|
|
||||||
visibilityIcon = { on: <LuEye />, off: <LuEyeOff /> },
|
|
||||||
...rest
|
|
||||||
} = props
|
|
||||||
|
|
||||||
const [visible, setVisible] = useControllableState({
|
|
||||||
value: visibleProp,
|
|
||||||
defaultValue: defaultVisible || false,
|
|
||||||
onChange: onVisibleChange,
|
|
||||||
})
|
|
||||||
|
|
||||||
const inputRef = React.useRef<HTMLInputElement>(null)
|
|
||||||
|
|
||||||
return (
|
|
||||||
<InputGroup
|
|
||||||
endElement={
|
|
||||||
<VisibilityTrigger
|
|
||||||
disabled={rest.disabled}
|
|
||||||
onPointerDown={(e) => {
|
|
||||||
if (rest.disabled) return
|
|
||||||
if (e.button !== 0) return
|
|
||||||
e.preventDefault()
|
|
||||||
setVisible(!visible)
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
{visible ? visibilityIcon.off : visibilityIcon.on}
|
|
||||||
</VisibilityTrigger>
|
|
||||||
}
|
|
||||||
{...rootProps}
|
|
||||||
>
|
|
||||||
<Input
|
|
||||||
{...rest}
|
|
||||||
ref={mergeRefs(ref, inputRef)}
|
|
||||||
type={visible ? "text" : "password"}
|
|
||||||
/>
|
|
||||||
</InputGroup>
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
const VisibilityTrigger = React.forwardRef<HTMLButtonElement, ButtonProps>(
|
|
||||||
function VisibilityTrigger(props, ref) {
|
|
||||||
return (
|
|
||||||
<IconButton
|
|
||||||
tabIndex={-1}
|
|
||||||
ref={ref}
|
|
||||||
me="-2"
|
|
||||||
aspectRatio="square"
|
|
||||||
size="sm"
|
|
||||||
variant="ghost"
|
|
||||||
height="calc(100% - {spacing.2})"
|
|
||||||
aria-label="Toggle password visibility"
|
|
||||||
{...props}
|
|
||||||
/>
|
|
||||||
)
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
interface PasswordStrengthMeterProps extends StackProps {
|
|
||||||
max?: number
|
|
||||||
value: number
|
|
||||||
}
|
|
||||||
|
|
||||||
export const PasswordStrengthMeter = React.forwardRef<
|
|
||||||
HTMLDivElement,
|
|
||||||
PasswordStrengthMeterProps
|
|
||||||
>(function PasswordStrengthMeter(props, ref) {
|
|
||||||
const { max = 4, value, ...rest } = props
|
|
||||||
|
|
||||||
const percent = (value / max) * 100
|
|
||||||
const { label, colorPalette } = getColorPalette(percent)
|
|
||||||
|
|
||||||
return (
|
|
||||||
<Stack align="flex-end" gap="1" ref={ref} {...rest}>
|
|
||||||
<HStack width="full" {...rest}>
|
|
||||||
{Array.from({ length: max }).map((_, index) => (
|
|
||||||
<Box
|
|
||||||
key={index}
|
|
||||||
height="1"
|
|
||||||
flex="1"
|
|
||||||
rounded="sm"
|
|
||||||
data-selected={index < value ? "" : undefined}
|
|
||||||
layerStyle="fill.subtle"
|
|
||||||
colorPalette="gray"
|
|
||||||
_selected={{
|
|
||||||
colorPalette,
|
|
||||||
layerStyle: "fill.solid",
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
))}
|
|
||||||
</HStack>
|
|
||||||
{label && <HStack textStyle="xs">{label}</HStack>}
|
|
||||||
</Stack>
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
function getColorPalette(percent: number) {
|
|
||||||
switch (true) {
|
|
||||||
case percent < 33:
|
|
||||||
return { label: "Low", colorPalette: "red" }
|
|
||||||
case percent < 66:
|
|
||||||
return { label: "Medium", colorPalette: "orange" }
|
|
||||||
default:
|
|
||||||
return { label: "High", colorPalette: "green" }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
export const API_BASE =
|
|
||||||
(import.meta as any).env?.VITE_BACKEND_URL ||
|
|
||||||
import.meta.env.VITE_BACKEND_URL ||
|
|
||||||
"http://localhost:8002";
|
|
||||||
@@ -1,8 +1,12 @@
|
|||||||
import Cookies from "js-cookie";
|
import Cookies from "js-cookie";
|
||||||
import { API_BASE } from "@/config/api.config";
|
|
||||||
|
const API_BASE =
|
||||||
|
(import.meta as any).env?.VITE_BACKEND_URL ||
|
||||||
|
import.meta.env.VITE_BACKEND_URL ||
|
||||||
|
"http://localhost:8002";
|
||||||
|
|
||||||
export const fetchUserData = async () => {
|
export const fetchUserData = async () => {
|
||||||
const response = await fetch(`${API_BASE}/api/admin/user-data/users`, {
|
const response = await fetch(`${API_BASE}/api/allUsers`, {
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${Cookies.get("token")}`,
|
Authorization: `Bearer ${Cookies.get("token")}`,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
import Cookies from "js-cookie";
|
import Cookies from "js-cookie";
|
||||||
import { API_BASE } from "@/config/api.config";
|
|
||||||
|
const API_BASE =
|
||||||
|
(import.meta as any).env?.VITE_BACKEND_URL ||
|
||||||
|
import.meta.env.VITE_BACKEND_URL ||
|
||||||
|
"http://localhost:8002";
|
||||||
|
|
||||||
export type LoginSuccess = { success: true };
|
export type LoginSuccess = { success: true };
|
||||||
export type LoginFailure = {
|
export type LoginFailure = {
|
||||||
@@ -14,20 +18,12 @@ export const loginFunc = async (
|
|||||||
password: string
|
password: string
|
||||||
): Promise<LoginResult> => {
|
): Promise<LoginResult> => {
|
||||||
try {
|
try {
|
||||||
const response = await fetch(`${API_BASE}/api/admin/user-mgmt/login`, {
|
const response = await fetch(`${API_BASE}/api/loginAdmin`, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
body: JSON.stringify({ username, password }),
|
body: JSON.stringify({ username, password }),
|
||||||
});
|
});
|
||||||
|
|
||||||
if (response.status === 403) {
|
|
||||||
return {
|
|
||||||
success: false,
|
|
||||||
message: "Login failed!",
|
|
||||||
description: "You are not an admin user.",
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
@@ -43,7 +39,6 @@ export const loginFunc = async (
|
|||||||
return { success: true };
|
return { success: true };
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error logging in:", error);
|
console.error("Error logging in:", error);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
message: "Login failed!",
|
message: "Login failed!",
|
||||||
|
|||||||
@@ -1,10 +1,14 @@
|
|||||||
import Cookies from "js-cookie";
|
import Cookies from "js-cookie";
|
||||||
import { API_BASE } from "@/config/api.config";
|
|
||||||
|
const API_BASE =
|
||||||
|
(import.meta as any).env?.VITE_BACKEND_URL ||
|
||||||
|
import.meta.env.VITE_BACKEND_URL ||
|
||||||
|
"http://localhost:8002";
|
||||||
|
|
||||||
export const handleDelete = async (userId: number) => {
|
export const handleDelete = async (userId: number) => {
|
||||||
try {
|
try {
|
||||||
const response = await fetch(
|
const response = await fetch(
|
||||||
`${API_BASE}/api/admin/user-data/delete-user/${userId}`,
|
`${API_BASE}/api/deleteUser/${userId}`,
|
||||||
{
|
{
|
||||||
method: "DELETE",
|
method: "DELETE",
|
||||||
headers: {
|
headers: {
|
||||||
@@ -24,28 +28,19 @@ export const handleDelete = async (userId: number) => {
|
|||||||
|
|
||||||
export const handleEdit = async (
|
export const handleEdit = async (
|
||||||
userId: number,
|
userId: number,
|
||||||
first_name: string,
|
username: string,
|
||||||
last_name: string,
|
role: string
|
||||||
email: string,
|
|
||||||
is_admin: boolean,
|
|
||||||
role: number
|
|
||||||
) => {
|
) => {
|
||||||
try {
|
try {
|
||||||
const response = await fetch(
|
const response = await fetch(
|
||||||
`${API_BASE}/api/admin/user-data/edit-user/${userId}`,
|
`${API_BASE}/api/editUser/${userId}`,
|
||||||
{
|
{
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
Authorization: `Bearer ${Cookies.get("token")}`,
|
Authorization: `Bearer ${Cookies.get("token")}`,
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({ username, role }),
|
||||||
first_name,
|
|
||||||
last_name,
|
|
||||||
role,
|
|
||||||
email,
|
|
||||||
is_admin,
|
|
||||||
}),
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
@@ -61,32 +56,17 @@ export const handleEdit = async (
|
|||||||
export const createUser = async (
|
export const createUser = async (
|
||||||
username: string,
|
username: string,
|
||||||
role: number,
|
role: number,
|
||||||
password: string,
|
password: string
|
||||||
first_name: string,
|
|
||||||
last_name: string,
|
|
||||||
email: string,
|
|
||||||
isAdmin: boolean
|
|
||||||
) => {
|
) => {
|
||||||
try {
|
try {
|
||||||
const response = await fetch(
|
const response = await fetch(`${API_BASE}/api/createUser`, {
|
||||||
`${API_BASE}/api/admin/user-data/create-user`,
|
|
||||||
{
|
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
Authorization: `Bearer ${Cookies.get("token")}`,
|
Authorization: `Bearer ${Cookies.get("token")}`,
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({ username, role, password }),
|
||||||
username,
|
});
|
||||||
role,
|
|
||||||
password,
|
|
||||||
isAdmin,
|
|
||||||
email,
|
|
||||||
first_name,
|
|
||||||
last_name,
|
|
||||||
}),
|
|
||||||
}
|
|
||||||
);
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error("Failed to create user");
|
throw new Error("Failed to create user");
|
||||||
}
|
}
|
||||||
@@ -99,17 +79,14 @@ export const createUser = async (
|
|||||||
|
|
||||||
export const changePW = async (newPassword: string, username: string) => {
|
export const changePW = async (newPassword: string, username: string) => {
|
||||||
try {
|
try {
|
||||||
const response = await fetch(
|
const response = await fetch(`${API_BASE}/api/changePWadmin`, {
|
||||||
`${API_BASE}/api/admin/user-data/change-password`,
|
|
||||||
{
|
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
Authorization: `Bearer ${Cookies.get("token")}`,
|
Authorization: `Bearer ${Cookies.get("token")}`,
|
||||||
},
|
},
|
||||||
body: JSON.stringify({ username, password: newPassword }),
|
body: JSON.stringify({ newPassword, username }),
|
||||||
}
|
});
|
||||||
);
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error("Failed to change password");
|
throw new Error("Failed to change password");
|
||||||
}
|
}
|
||||||
@@ -123,7 +100,7 @@ export const changePW = async (newPassword: string, username: string) => {
|
|||||||
export const deleteLoan = async (loanId: number) => {
|
export const deleteLoan = async (loanId: number) => {
|
||||||
try {
|
try {
|
||||||
const response = await fetch(
|
const response = await fetch(
|
||||||
`${API_BASE}/api/admin/loan-data/delete-loan/${loanId}`,
|
`${API_BASE}/api/deleteLoan/${loanId}`,
|
||||||
{
|
{
|
||||||
method: "DELETE",
|
method: "DELETE",
|
||||||
headers: {
|
headers: {
|
||||||
@@ -144,7 +121,7 @@ export const deleteLoan = async (loanId: number) => {
|
|||||||
export const deleteItem = async (itemId: number) => {
|
export const deleteItem = async (itemId: number) => {
|
||||||
try {
|
try {
|
||||||
const response = await fetch(
|
const response = await fetch(
|
||||||
`${API_BASE}/api/admin/item-data/delete-item/${itemId}`,
|
`${API_BASE}/api/deleteItem/${itemId}`,
|
||||||
{
|
{
|
||||||
method: "DELETE",
|
method: "DELETE",
|
||||||
headers: {
|
headers: {
|
||||||
@@ -164,26 +141,22 @@ export const deleteItem = async (itemId: number) => {
|
|||||||
|
|
||||||
export const createItem = async (
|
export const createItem = async (
|
||||||
item_name: string,
|
item_name: string,
|
||||||
can_borrow_role: number,
|
can_borrow_role: number
|
||||||
lockerNumber: string | null
|
|
||||||
) => {
|
) => {
|
||||||
try {
|
try {
|
||||||
const response = await fetch(
|
const response = await fetch(`${API_BASE}/api/createItem`, {
|
||||||
`${API_BASE}/api/admin/item-data/create-item`,
|
|
||||||
{
|
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
Authorization: `Bearer ${Cookies.get("token")}`,
|
Authorization: `Bearer ${Cookies.get("token")}`,
|
||||||
},
|
},
|
||||||
body: JSON.stringify({ item_name, can_borrow_role, lockerNumber }),
|
body: JSON.stringify({ item_name, can_borrow_role }),
|
||||||
}
|
});
|
||||||
);
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
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 };
|
||||||
@@ -196,22 +169,17 @@ export const createItem = async (
|
|||||||
export const handleEditItems = async (
|
export const handleEditItems = async (
|
||||||
itemId: number,
|
itemId: number,
|
||||||
item_name: string,
|
item_name: string,
|
||||||
safe_nr: string | null,
|
|
||||||
door_key: string | null,
|
|
||||||
can_borrow_role: string
|
can_borrow_role: string
|
||||||
) => {
|
) => {
|
||||||
try {
|
try {
|
||||||
const response = await fetch(
|
const response = await fetch(`${API_BASE}/api/updateItemByID`, {
|
||||||
`${API_BASE}/api/admin/item-data/edit-item/${itemId}`,
|
|
||||||
{
|
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
"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({ itemId, item_name, can_borrow_role }),
|
||||||
}
|
});
|
||||||
);
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error("Failed to edit item");
|
throw new Error("Failed to edit item");
|
||||||
}
|
}
|
||||||
@@ -225,9 +193,9 @@ export const handleEditItems = async (
|
|||||||
export const changeSafeState = async (itemId: number) => {
|
export const changeSafeState = async (itemId: number) => {
|
||||||
try {
|
try {
|
||||||
const response = await fetch(
|
const response = await fetch(
|
||||||
`${API_BASE}/api/admin/item-data/change-safe-state/${itemId}`,
|
`${API_BASE}/api/changeSafeState/${itemId}`,
|
||||||
{
|
{
|
||||||
method: "POST",
|
method: "PUT",
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${Cookies.get("token")}`,
|
Authorization: `Bearer ${Cookies.get("token")}`,
|
||||||
},
|
},
|
||||||
@@ -243,19 +211,16 @@ export const changeSafeState = async (itemId: number) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export const createAPIentry = async (apiKey: string, name: string) => {
|
export const createAPIentry = async (apiKey: string, user: string) => {
|
||||||
try {
|
try {
|
||||||
const response = await fetch(
|
const response = await fetch(`${API_BASE}/api/createAPIentry`, {
|
||||||
`${API_BASE}/api/admin/api-data/create-api-key`,
|
|
||||||
{
|
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
Authorization: `Bearer ${Cookies.get("token")}`,
|
Authorization: `Bearer ${Cookies.get("token")}`,
|
||||||
},
|
},
|
||||||
body: JSON.stringify({ apiKey, entryName: name }),
|
body: JSON.stringify({ apiKey, user }),
|
||||||
}
|
});
|
||||||
);
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
@@ -273,7 +238,7 @@ export const createAPIentry = async (apiKey: string, name: string) => {
|
|||||||
export const deleteAPKey = async (apiKeyId: number) => {
|
export const deleteAPKey = async (apiKeyId: number) => {
|
||||||
try {
|
try {
|
||||||
const response = await fetch(
|
const response = await fetch(
|
||||||
`${API_BASE}/api/admin/api-data/delete-api-key/${apiKeyId}`,
|
`${API_BASE}/api/deleteAPKey/${apiKeyId}`,
|
||||||
{
|
{
|
||||||
method: "DELETE",
|
method: "DELETE",
|
||||||
headers: {
|
headers: {
|
||||||
|
|||||||
@@ -1,11 +1,10 @@
|
|||||||
{
|
{
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.app.tsbuildinfo",
|
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.app.tsbuildinfo",
|
||||||
"target": "ES2022",
|
"target": "ESNext",
|
||||||
"useDefineForClassFields": true,
|
"useDefineForClassFields": true,
|
||||||
"lib": ["ES2022", "DOM", "DOM.Iterable"],
|
"lib": ["ES2022", "DOM", "DOM.Iterable"],
|
||||||
"module": "ESNext",
|
"module": "ESNext",
|
||||||
"types": ["vite/client"],
|
|
||||||
"skipLibCheck": true,
|
"skipLibCheck": true,
|
||||||
|
|
||||||
/* Bundler mode */
|
/* Bundler mode */
|
||||||
@@ -24,10 +23,14 @@
|
|||||||
"noFallthroughCasesInSwitch": true,
|
"noFallthroughCasesInSwitch": true,
|
||||||
"noUncheckedSideEffectImports": true,
|
"noUncheckedSideEffectImports": true,
|
||||||
|
|
||||||
/* Path aliases */
|
/* Chakra / Pfad Aliases */
|
||||||
|
"baseUrl": ".",
|
||||||
"paths": {
|
"paths": {
|
||||||
"@/*": ["./src/*"]
|
"@/*": ["./src/*"]
|
||||||
}
|
},
|
||||||
|
|
||||||
|
"forceConsistentCasingInFileNames": true,
|
||||||
|
"ignoreDeprecations": "6.0"
|
||||||
},
|
},
|
||||||
"include": ["src"]
|
"include": ["src"]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
FROM node:20-alpine
|
FROM node:20-alpine
|
||||||
|
|
||||||
ENV NODE_ENV=production
|
|
||||||
WORKDIR /backend
|
WORKDIR /backend
|
||||||
|
|
||||||
COPY package*.json ./
|
COPY package*.json ./
|
||||||
RUN npm ci --omit=dev
|
RUN npm install
|
||||||
|
|
||||||
COPY . .
|
COPY . .
|
||||||
|
|
||||||
EXPOSE 8004
|
EXPOSE 8002
|
||||||
|
|
||||||
CMD ["npm", "start"]
|
CMD ["npm", "start"]
|
||||||
@@ -1,11 +1,11 @@
|
|||||||
{
|
{
|
||||||
"name": "backendv2",
|
"name": "backend",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "backendv2",
|
"name": "backend",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
@@ -172,9 +172,9 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/debug": {
|
"node_modules/debug": {
|
||||||
"version": "4.4.3",
|
"version": "4.4.1",
|
||||||
"resolved": "https://registry.npmjs.org/debug/-/debug-4.4.3.tgz",
|
"resolved": "https://registry.npmjs.org/debug/-/debug-4.4.1.tgz",
|
||||||
"integrity": "sha512-RGwwWnwQvkVfavKVt22FGLw+xYSdzARwm0ru6DhTVA3umU5hZc28V3kO4stgYryrTlLpuvgI9GiijltAjNbcqA==",
|
"integrity": "sha512-KcKCqiftBJcZr++7ykoDIEwSa3XWowTfNPo92BYxjXiyYEVrUQh2aLyhxBCwww+heortUFxEJYcRzosstTEBYQ==",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"ms": "^2.1.3"
|
"ms": "^2.1.3"
|
||||||
@@ -207,9 +207,9 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/dotenv": {
|
"node_modules/dotenv": {
|
||||||
"version": "17.2.3",
|
"version": "17.2.1",
|
||||||
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-17.2.3.tgz",
|
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-17.2.1.tgz",
|
||||||
"integrity": "sha512-JVUnt+DUIzu87TABbhPmNfVdBDt18BLOWjMUFJMSi/Qqg7NTYtabbvSNJGOJ7afbRuv9D/lngizHtP7QyLQ+9w==",
|
"integrity": "sha512-kQhDYKZecqnM0fCnzI5eIv5L4cAe/iRI+HqMbO/hbRdTAeXDG+M9FjipUxNfbARuEg4iHIbhnhs78BCHNbSxEQ==",
|
||||||
"license": "BSD-2-Clause",
|
"license": "BSD-2-Clause",
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=12"
|
"node": ">=12"
|
||||||
@@ -566,9 +566,9 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/jose": {
|
"node_modules/jose": {
|
||||||
"version": "6.1.0",
|
"version": "6.0.12",
|
||||||
"resolved": "https://registry.npmjs.org/jose/-/jose-6.1.0.tgz",
|
"resolved": "https://registry.npmjs.org/jose/-/jose-6.0.12.tgz",
|
||||||
"integrity": "sha512-TTQJyoEoKcC1lscpVDCSsVgYzUDg/0Bt3WE//WiTPK6uOCQC2KZS4MpugbMWt/zyjkopgZoXhZuCi00gLudfUA==",
|
"integrity": "sha512-T8xypXs8CpmiIi78k0E+Lk7T2zlK4zDyg+o1CZ4AkOHgDg98ogdP2BeZ61lTFKFyoEwJ9RgAgN+SdM3iPgNonQ==",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"funding": {
|
"funding": {
|
||||||
"url": "https://github.com/sponsors/panva"
|
"url": "https://github.com/sponsors/panva"
|
||||||
@@ -674,15 +674,15 @@
|
|||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
},
|
},
|
||||||
"node_modules/mysql2": {
|
"node_modules/mysql2": {
|
||||||
"version": "3.15.3",
|
"version": "3.14.3",
|
||||||
"resolved": "https://registry.npmjs.org/mysql2/-/mysql2-3.15.3.tgz",
|
"resolved": "https://registry.npmjs.org/mysql2/-/mysql2-3.14.3.tgz",
|
||||||
"integrity": "sha512-FBrGau0IXmuqg4haEZRBfHNWB5mUARw6hNwPDXXGg0XzVJ50mr/9hb267lvpVMnhZ1FON3qNd4Xfcez1rbFwSg==",
|
"integrity": "sha512-fD6MLV8XJ1KiNFIF0bS7Msl8eZyhlTDCDl75ajU5SJtpdx9ZPEACulJcqJWr1Y8OYyxsFc4j3+nflpmhxCU5aQ==",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"aws-ssl-profiles": "^1.1.1",
|
"aws-ssl-profiles": "^1.1.1",
|
||||||
"denque": "^2.1.0",
|
"denque": "^2.1.0",
|
||||||
"generate-function": "^2.3.1",
|
"generate-function": "^2.3.1",
|
||||||
"iconv-lite": "^0.7.0",
|
"iconv-lite": "^0.6.3",
|
||||||
"long": "^5.2.1",
|
"long": "^5.2.1",
|
||||||
"lru.min": "^1.0.0",
|
"lru.min": "^1.0.0",
|
||||||
"named-placeholders": "^1.1.3",
|
"named-placeholders": "^1.1.3",
|
||||||
@@ -693,22 +693,6 @@
|
|||||||
"node": ">= 8.0"
|
"node": ">= 8.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/mysql2/node_modules/iconv-lite": {
|
|
||||||
"version": "0.7.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.7.0.tgz",
|
|
||||||
"integrity": "sha512-cf6L2Ds3h57VVmkZe+Pn+5APsT7FpqJtEhhieDCvrE2MK5Qk9MyffgQyuxQTm6BChfeZNtcOLHp9IcWRVcIcBQ==",
|
|
||||||
"license": "MIT",
|
|
||||||
"dependencies": {
|
|
||||||
"safer-buffer": ">= 2.1.2 < 3.0.0"
|
|
||||||
},
|
|
||||||
"engines": {
|
|
||||||
"node": ">=0.10.0"
|
|
||||||
},
|
|
||||||
"funding": {
|
|
||||||
"type": "opencollective",
|
|
||||||
"url": "https://opencollective.com/express"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"node_modules/named-placeholders": {
|
"node_modules/named-placeholders": {
|
||||||
"version": "1.1.3",
|
"version": "1.1.3",
|
||||||
"resolved": "https://registry.npmjs.org/named-placeholders/-/named-placeholders-1.1.3.tgz",
|
"resolved": "https://registry.npmjs.org/named-placeholders/-/named-placeholders-1.1.3.tgz",
|
||||||
@@ -731,9 +715,9 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/nodemailer": {
|
"node_modules/nodemailer": {
|
||||||
"version": "7.0.10",
|
"version": "7.0.6",
|
||||||
"resolved": "https://registry.npmjs.org/nodemailer/-/nodemailer-7.0.10.tgz",
|
"resolved": "https://registry.npmjs.org/nodemailer/-/nodemailer-7.0.6.tgz",
|
||||||
"integrity": "sha512-Us/Se1WtT0ylXgNFfyFSx4LElllVLJXQjWi2Xz17xWw7amDKO2MLtFnVp1WACy7GkVGs+oBlRopVNUzlrGSw1w==",
|
"integrity": "sha512-F44uVzgwo49xboqbFgBGkRaiMgtoBrBEWCVincJPK9+S9Adkzt/wXCLKbf7dxucmxfTI5gHGB+bEmdyzN6QKjw==",
|
||||||
"license": "MIT-0",
|
"license": "MIT-0",
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=6.0.0"
|
"node": ">=6.0.0"
|
||||||
@@ -791,13 +775,12 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/path-to-regexp": {
|
"node_modules/path-to-regexp": {
|
||||||
"version": "8.3.0",
|
"version": "8.2.0",
|
||||||
"resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-8.3.0.tgz",
|
"resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-8.2.0.tgz",
|
||||||
"integrity": "sha512-7jdwVIRtsP8MYpdXSwOS0YdD0Du+qOoF/AEPIt88PcCFrZCzx41oxku1jD88hZBwbNUIEfpqvuhjFaMAqMTWnA==",
|
"integrity": "sha512-TdrF7fW9Rphjq4RjrW0Kp2AW0Ahwu9sRGTkS6bvDi0SCwZlEZYmcfDbEsTz8RVk0EHIS/Vd1bv3JhG+1xZuAyQ==",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"funding": {
|
"engines": {
|
||||||
"type": "opencollective",
|
"node": ">=16"
|
||||||
"url": "https://opencollective.com/express"
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/picocolors": {
|
"node_modules/picocolors": {
|
||||||
@@ -844,34 +827,18 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/raw-body": {
|
"node_modules/raw-body": {
|
||||||
"version": "3.0.1",
|
"version": "3.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/raw-body/-/raw-body-3.0.1.tgz",
|
"resolved": "https://registry.npmjs.org/raw-body/-/raw-body-3.0.0.tgz",
|
||||||
"integrity": "sha512-9G8cA+tuMS75+6G/TzW8OtLzmBDMo8p1JRxN5AZ+LAp8uxGA8V8GZm4GQ4/N5QNQEnLmg6SS7wyuSmbKepiKqA==",
|
"integrity": "sha512-RmkhL8CAyCRPXCE28MMH0z2PNWQBNk2Q09ZdxM9IOOXwxwZbN+qbWaatPkdkWIKL2ZVDImrN/pK5HTRz2PcS4g==",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"bytes": "3.1.2",
|
"bytes": "3.1.2",
|
||||||
"http-errors": "2.0.0",
|
"http-errors": "2.0.0",
|
||||||
"iconv-lite": "0.7.0",
|
"iconv-lite": "0.6.3",
|
||||||
"unpipe": "1.0.0"
|
"unpipe": "1.0.0"
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">= 0.10"
|
"node": ">= 0.8"
|
||||||
}
|
|
||||||
},
|
|
||||||
"node_modules/raw-body/node_modules/iconv-lite": {
|
|
||||||
"version": "0.7.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.7.0.tgz",
|
|
||||||
"integrity": "sha512-cf6L2Ds3h57VVmkZe+Pn+5APsT7FpqJtEhhieDCvrE2MK5Qk9MyffgQyuxQTm6BChfeZNtcOLHp9IcWRVcIcBQ==",
|
|
||||||
"license": "MIT",
|
|
||||||
"dependencies": {
|
|
||||||
"safer-buffer": ">= 2.1.2 < 3.0.0"
|
|
||||||
},
|
|
||||||
"engines": {
|
|
||||||
"node": ">=0.10.0"
|
|
||||||
},
|
|
||||||
"funding": {
|
|
||||||
"type": "opencollective",
|
|
||||||
"url": "https://opencollective.com/express"
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/router": {
|
"node_modules/router": {
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "backendv2",
|
"name": "backend",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"main": "server.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "echo \"Error: no test specified\" && exit 1",
|
"test": "echo \"Error: no test specified\" && exit 1",
|
||||||
"start": "node server.js"
|
"start": "node server.js"
|
||||||
599
backend/routes/api.js
Normal file
599
backend/routes/api.js
Normal file
@@ -0,0 +1,599 @@
|
|||||||
|
import express from "express";
|
||||||
|
import {
|
||||||
|
loginFunc,
|
||||||
|
getItemsFromDatabase,
|
||||||
|
getLoansFromDatabase,
|
||||||
|
getUserLoansFromDatabase,
|
||||||
|
deleteLoanFromDatabase,
|
||||||
|
getBorrowableItemsFromDatabase,
|
||||||
|
createLoanInDatabase,
|
||||||
|
onTake,
|
||||||
|
loginAdmin,
|
||||||
|
onReturn,
|
||||||
|
getAllUsers,
|
||||||
|
deleteUserID,
|
||||||
|
handleEdit,
|
||||||
|
createUser,
|
||||||
|
getAllLoans,
|
||||||
|
getAllItems,
|
||||||
|
deleteItemID,
|
||||||
|
createItem,
|
||||||
|
changeUserPassword,
|
||||||
|
changeUserPasswordFRONTEND,
|
||||||
|
changeInSafeStateV2,
|
||||||
|
updateItemByID,
|
||||||
|
getAllApiKeys,
|
||||||
|
createAPIentry,
|
||||||
|
deleteAPKey,
|
||||||
|
getLoanInfoWithID,
|
||||||
|
SETdeleteLoanFromDatabase,
|
||||||
|
} from "../services/database.js";
|
||||||
|
import { authenticate, generateToken } from "../services/tokenService.js";
|
||||||
|
const router = express.Router();
|
||||||
|
import nodemailer from "nodemailer";
|
||||||
|
import dotenv from "dotenv";
|
||||||
|
dotenv.config();
|
||||||
|
|
||||||
|
// Nice HTML + text templates for the loan email
|
||||||
|
function buildLoanEmail({ user, items, startDate, endDate, createdDate }) {
|
||||||
|
const brand = process.env.MAIL_BRAND_COLOR || "#0ea5e9";
|
||||||
|
const itemsList =
|
||||||
|
Array.isArray(items) && items.length
|
||||||
|
? `<ul style="margin:4px 0 0 18px; padding:0;">${items
|
||||||
|
.map(
|
||||||
|
(i) =>
|
||||||
|
`<li style="margin:2px 0; color:#111827; line-height:1.3;">${i}</li>`
|
||||||
|
)
|
||||||
|
.join("")}</ul>`
|
||||||
|
: "<span style='color:#111827;'>N/A</span>";
|
||||||
|
|
||||||
|
return `<!doctype html>
|
||||||
|
<html lang="de">
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<meta name="color-scheme" content="light">
|
||||||
|
<meta name="supported-color-schemes" content="light">
|
||||||
|
<meta name="x-apple-disable-message-reformatting">
|
||||||
|
<meta name="viewport" content="width=device-width,initial-scale=1">
|
||||||
|
<style>
|
||||||
|
:root { color-scheme: light; supported-color-schemes: light; }
|
||||||
|
body { margin:0; padding:0; }
|
||||||
|
/* Mobile stacking */
|
||||||
|
@media (max-width:480px) {
|
||||||
|
.outer { width:100% !important; }
|
||||||
|
.pad-sm { padding:16px !important; }
|
||||||
|
.w-label { width:120px !important; }
|
||||||
|
}
|
||||||
|
/* Dark-mode override safety */
|
||||||
|
@media (prefers-color-scheme: dark) {
|
||||||
|
body, table, td, p, a, h1, h2, h3 { background:#ffffff !important; color:#111827 !important; }
|
||||||
|
.brand-header { background:${brand} !important; color:#ffffff !important; }
|
||||||
|
a { color:${brand} !important; }
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body bgcolor="#ffffff" style="background:#ffffff; font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,Arial,sans-serif; color:#111827; -webkit-text-size-adjust:100%;">
|
||||||
|
<!-- Preheader (hidden) -->
|
||||||
|
<div style="display:none; max-height:0; overflow:hidden; opacity:0; mso-hide:all;">
|
||||||
|
Neue Ausleihe erstellt – Übersicht der Buchung.
|
||||||
|
</div>
|
||||||
|
<div role="article" aria-roledescription="email" lang="de" style="padding:24px; background:#f2f4f7;">
|
||||||
|
<table role="presentation" cellpadding="0" cellspacing="0" width="100%" class="outer" style="max-width:600px; margin:0 auto; background:#ffffff; border:1px solid #e5e7eb; border-radius:14px; overflow:hidden;">
|
||||||
|
<tr>
|
||||||
|
<td class="brand-header" style="padding:22px 26px; background:${brand}; color:#ffffff;">
|
||||||
|
<h1 style="margin:0; font-size:18px; line-height:1.35; font-weight:600;">Neue Ausleihe erstellt</h1>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td class="pad-sm" style="padding:24px 26px; color:#111827;">
|
||||||
|
<p style="margin:0 0 14px 0; line-height:1.4;">Es wurde eine neue Ausleihe angelegt. Hier sind die Details:</p>
|
||||||
|
<table role="presentation" cellpadding="0" cellspacing="0" width="100%" style="border-collapse:collapse; font-size:14px; line-height:1.3; background:#fcfcfd; border:1px solid #e5e7eb; border-radius:10px; overflow:hidden;">
|
||||||
|
<tbody>
|
||||||
|
<tr>
|
||||||
|
<td class="w-label" style="padding:10px 14px; color:#6b7280; width:170px; border-bottom:1px solid #ececec;">Benutzer</td>
|
||||||
|
<td style="padding:10px 14px; font-weight:600; border-bottom:1px solid #ececec; color:#111827;">${
|
||||||
|
user || "N/A"
|
||||||
|
}</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td style="padding:10px 14px; color:#6b7280; vertical-align:top; border-bottom:1px solid #ececec;">Ausgeliehene Gegenstände</td>
|
||||||
|
<td style="padding:10px 14px; font-weight:600; border-bottom:1px solid #ececec; color:#111827;">${itemsList}</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td style="padding:10px 14px; color:#6b7280; border-bottom:1px solid #ececec;">Startdatum</td>
|
||||||
|
<td style="padding:10px 14px; font-weight:600; border-bottom:1px solid #ececec; color:#111827;">${formatDateTime(
|
||||||
|
startDate
|
||||||
|
)}</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td style="padding:10px 14px; color:#6b7280; border-bottom:1px solid #ececec;">Enddatum</td>
|
||||||
|
<td style="padding:10px 14px; font-weight:600; border-bottom:1px solid #ececec; color:#111827;">${formatDateTime(
|
||||||
|
endDate
|
||||||
|
)}</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td style="padding:10px 14px; color:#6b7280;">Erstellt am</td>
|
||||||
|
<td style="padding:10px 14px; font-weight:600; color:#111827;">${formatDateTime(
|
||||||
|
createdDate
|
||||||
|
)}</td>
|
||||||
|
</tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
<p style="margin:22px 0 0 0; font-size:14px;">
|
||||||
|
<a href="https://admin.insta.the1s.de/api" style="display:inline-block; background:${brand}; color:#ffffff; text-decoration:none; padding:10px 16px; border-radius:6px; font-weight:600; font-size:14px;" target="_blank" rel="noopener noreferrer">
|
||||||
|
Übersicht öffnen
|
||||||
|
</a>
|
||||||
|
</p>
|
||||||
|
<p style="margin:18px 0 0 0; font-size:12px; color:#6b7280; line-height:1.4;">
|
||||||
|
Diese E-Mail wurde automatisch vom Ausleihsystem gesendet. Bitte nicht antworten.
|
||||||
|
</p>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>`;
|
||||||
|
}
|
||||||
|
|
||||||
|
function buildLoanEmailText({ user, items, startDate, endDate, createdDate }) {
|
||||||
|
const itemsText =
|
||||||
|
Array.isArray(items) && items.length ? items.join(", ") : "N/A";
|
||||||
|
return [
|
||||||
|
"Neue Ausleihe erstellt",
|
||||||
|
"",
|
||||||
|
`Benutzer: ${user || "N/A"}`,
|
||||||
|
`Gegenstände: ${itemsText}`,
|
||||||
|
`Start: ${formatDateTime(startDate)}`,
|
||||||
|
`Ende: ${formatDateTime(endDate)}`,
|
||||||
|
`Erstellt am: ${formatDateTime(createdDate)}`,
|
||||||
|
].join("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
function sendMailLoan(user, items, startDate, endDate, createdDate) {
|
||||||
|
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 info = await transporter.sendMail({
|
||||||
|
from: '"Ausleihsystem" <noreply@mcs-medien.de>',
|
||||||
|
to: process.env.MAIL_SENDEES,
|
||||||
|
subject: "Eine neue Ausleihe wurde erstellt!",
|
||||||
|
text: buildLoanEmailText({
|
||||||
|
user,
|
||||||
|
items,
|
||||||
|
startDate,
|
||||||
|
endDate,
|
||||||
|
createdDate,
|
||||||
|
}),
|
||||||
|
html: buildLoanEmail({ user, items, startDate, endDate, createdDate }),
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log("Message sent:", info.messageId);
|
||||||
|
})();
|
||||||
|
console.log("sendMailLoan called");
|
||||||
|
}
|
||||||
|
|
||||||
|
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";
|
||||||
|
};
|
||||||
|
|
||||||
|
router.post("/login", async (req, res) => {
|
||||||
|
const result = await loginFunc(req.body.username, req.body.password);
|
||||||
|
if (result.success) {
|
||||||
|
const token = await generateToken({
|
||||||
|
username: result.data.username,
|
||||||
|
role: result.data.role,
|
||||||
|
});
|
||||||
|
res.status(200).json({ message: "Login successful", token });
|
||||||
|
} else {
|
||||||
|
res.status(401).json({ message: "Invalid credentials" });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
router.get("/items", authenticate, async (req, res) => {
|
||||||
|
const result = await getItemsFromDatabase(req.user.role);
|
||||||
|
if (result.success) {
|
||||||
|
res.status(200).json(result.data);
|
||||||
|
} else {
|
||||||
|
res.status(500).json({ message: "Failed to fetch items" });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
router.get("/loans", authenticate, async (req, res) => {
|
||||||
|
const result = await getLoansFromDatabase();
|
||||||
|
if (result.success) {
|
||||||
|
res.status(200).json(result.data);
|
||||||
|
} else {
|
||||||
|
res.status(500).json({ message: "Failed to fetch loans" });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
router.get("/userLoans", authenticate, async (req, res) => {
|
||||||
|
const result = await getUserLoansFromDatabase(req.user.username);
|
||||||
|
if (result.success) {
|
||||||
|
res.status(200).json(result.data);
|
||||||
|
} else {
|
||||||
|
res.status(500).json({ message: "Failed to fetch user loans" });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
router.delete("/deleteLoan/:id", authenticate, async (req, res) => {
|
||||||
|
const loanId = req.params.id;
|
||||||
|
const result = await deleteLoanFromDatabase(loanId);
|
||||||
|
if (result.success) {
|
||||||
|
res.status(200).json({ message: "Loan deleted successfully" });
|
||||||
|
} else {
|
||||||
|
res.status(500).json({ message: "Failed to delete loan" });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
router.delete("/SETdeleteLoan/:id", authenticate, async (req, res) => {
|
||||||
|
const loanId = req.params.id;
|
||||||
|
const result = await SETdeleteLoanFromDatabase(loanId);
|
||||||
|
if (result.success) {
|
||||||
|
res.status(200).json({ message: "Loan deleted successfully" });
|
||||||
|
} else {
|
||||||
|
res.status(500).json({ message: "Failed to delete loan" });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
router.post("/borrowableItems", authenticate, async (req, res) => {
|
||||||
|
const { startDate, endDate } = req.body || {};
|
||||||
|
if (!startDate || !endDate) {
|
||||||
|
return res
|
||||||
|
.status(400)
|
||||||
|
.json({ message: "startDate and endDate are required" });
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = await getBorrowableItemsFromDatabase(
|
||||||
|
startDate,
|
||||||
|
endDate,
|
||||||
|
req.user.role
|
||||||
|
);
|
||||||
|
if (result.success) {
|
||||||
|
// return the array directly for consistency with /items
|
||||||
|
return res.status(200).json(result.data);
|
||||||
|
} else {
|
||||||
|
return res
|
||||||
|
.status(500)
|
||||||
|
.json({ message: "Failed to fetch borrowable items" });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
router.post("/takeLoan/:id", authenticate, async (req, res) => {
|
||||||
|
const loanId = req.params.id;
|
||||||
|
const result = await onTake(loanId);
|
||||||
|
if (result.success) {
|
||||||
|
res.status(200).json({ message: "Loan taken successfully" });
|
||||||
|
} else {
|
||||||
|
res.status(500).json({ message: "Failed to take loan" });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
router.post("/returnLoan/:id", authenticate, async (req, res) => {
|
||||||
|
const loanId = req.params.id;
|
||||||
|
const result = await onReturn(loanId);
|
||||||
|
if (result.success) {
|
||||||
|
res.status(200).json({ message: "Loan returned successfully" });
|
||||||
|
} else {
|
||||||
|
res.status(500).json({ message: "Failed to return loan" });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
router.post("/createLoan", authenticate, async (req, res) => {
|
||||||
|
try {
|
||||||
|
const { items, startDate, endDate } = req.body || {};
|
||||||
|
|
||||||
|
if (!Array.isArray(items) || items.length === 0) {
|
||||||
|
return res.status(400).json({ message: "Items array is required" });
|
||||||
|
}
|
||||||
|
|
||||||
|
// If dates are not provided, default to now .. +7 days
|
||||||
|
const start =
|
||||||
|
startDate ?? new Date().toISOString().slice(0, 19).replace("T", " ");
|
||||||
|
const end =
|
||||||
|
endDate ??
|
||||||
|
new Date(Date.now() + 7 * 24 * 60 * 60 * 1000)
|
||||||
|
.toISOString()
|
||||||
|
.slice(0, 19)
|
||||||
|
.replace("T", " ");
|
||||||
|
|
||||||
|
// Coerce item IDs to numbers and filter invalids
|
||||||
|
const itemIds = items
|
||||||
|
.map((v) => Number(v))
|
||||||
|
.filter((n) => Number.isFinite(n));
|
||||||
|
|
||||||
|
if (itemIds.length === 0) {
|
||||||
|
return res.status(400).json({ message: "No valid item IDs provided" });
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = await createLoanInDatabase(
|
||||||
|
req.user.username,
|
||||||
|
start,
|
||||||
|
end,
|
||||||
|
itemIds
|
||||||
|
);
|
||||||
|
|
||||||
|
if (result.success) {
|
||||||
|
const mailInfo = await getLoanInfoWithID(result.data.id);
|
||||||
|
console.log(mailInfo);
|
||||||
|
sendMailLoan(
|
||||||
|
mailInfo.data.username,
|
||||||
|
mailInfo.data.loaned_items_name,
|
||||||
|
mailInfo.data.start_date,
|
||||||
|
mailInfo.data.end_date,
|
||||||
|
mailInfo.data.created_at
|
||||||
|
);
|
||||||
|
return res.status(201).json({
|
||||||
|
message: "Loan created successfully",
|
||||||
|
loanId: result.data.id,
|
||||||
|
loanCode: result.data.loan_code,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (result.code === "CONFLICT") {
|
||||||
|
return res
|
||||||
|
.status(409)
|
||||||
|
.json({ message: "Items not available in the selected period" });
|
||||||
|
}
|
||||||
|
|
||||||
|
if (result.code === "BAD_REQUEST") {
|
||||||
|
return res.status(400).json({ message: result.message });
|
||||||
|
}
|
||||||
|
|
||||||
|
return res.status(500).json({ message: "Failed to create loan" });
|
||||||
|
} catch (err) {
|
||||||
|
console.error("createLoan error:", err);
|
||||||
|
return res.status(500).json({ message: "Failed to create loan" });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
router.post("/changePassword", authenticate, async (req, res) => {
|
||||||
|
const { oldPassword, newPassword } = req.body || {};
|
||||||
|
const username = req.user.username;
|
||||||
|
const result = await changeUserPasswordFRONTEND(
|
||||||
|
username,
|
||||||
|
oldPassword,
|
||||||
|
newPassword
|
||||||
|
);
|
||||||
|
if (result.success) {
|
||||||
|
res.status(200).json({ message: "Password changed successfully" });
|
||||||
|
} else {
|
||||||
|
res.status(500).json({ message: "Failed to change password" });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Admin panel functions
|
||||||
|
|
||||||
|
router.post("/loginAdmin", async (req, res) => {
|
||||||
|
const { username, password } = req.body || {};
|
||||||
|
if (!username || !password) {
|
||||||
|
return res
|
||||||
|
.status(400)
|
||||||
|
.json({ message: "Username and password are required" });
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = await loginAdmin(username, password);
|
||||||
|
if (result.success) {
|
||||||
|
const token = await generateToken({
|
||||||
|
username: result.data.username,
|
||||||
|
role: result.data.role,
|
||||||
|
});
|
||||||
|
|
||||||
|
return res.status(200).json({
|
||||||
|
message: "Login successful",
|
||||||
|
first_name: result.data.first_name,
|
||||||
|
token,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return res.status(401).json({ message: "Invalid credentials" });
|
||||||
|
});
|
||||||
|
|
||||||
|
router.get("/allUsers", authenticate, async (req, res) => {
|
||||||
|
const result = await getAllUsers();
|
||||||
|
if (result.success) {
|
||||||
|
return res.status(200).json(result.data);
|
||||||
|
}
|
||||||
|
return res.status(500).json({ message: "Failed to fetch users" });
|
||||||
|
});
|
||||||
|
|
||||||
|
router.delete("/deleteUser/:id", authenticate, async (req, res) => {
|
||||||
|
const userId = req.params.id;
|
||||||
|
const result = await deleteUserID(userId);
|
||||||
|
if (result.success) {
|
||||||
|
return res.status(200).json({ message: "User deleted successfully" });
|
||||||
|
}
|
||||||
|
return res.status(500).json({ message: "Failed to delete user" });
|
||||||
|
});
|
||||||
|
|
||||||
|
router.get("/verifyToken", authenticate, async (req, res) => {
|
||||||
|
res.status(200).json({ message: "Token is valid", user: req.user });
|
||||||
|
});
|
||||||
|
|
||||||
|
router.post("/editUser/:id", authenticate, async (req, res) => {
|
||||||
|
const userId = req.params.id;
|
||||||
|
const { username, role } = req.body || {};
|
||||||
|
const result = await handleEdit(userId, username, role);
|
||||||
|
if (result.success) {
|
||||||
|
return res.status(200).json({ message: "User edited successfully" });
|
||||||
|
}
|
||||||
|
return res.status(500).json({ message: "Failed to edit user" });
|
||||||
|
});
|
||||||
|
|
||||||
|
router.post("/createUser", authenticate, async (req, res) => {
|
||||||
|
const { username, role, password } = req.body || {};
|
||||||
|
const result = await createUser(username, role, password);
|
||||||
|
if (result.success) {
|
||||||
|
return res.status(201).json({ message: "User created successfully" });
|
||||||
|
}
|
||||||
|
return res.status(500).json({ message: "Failed to create user" });
|
||||||
|
});
|
||||||
|
|
||||||
|
router.get("/allLoans", authenticate, async (req, res) => {
|
||||||
|
const result = await getAllLoans();
|
||||||
|
if (result.success) {
|
||||||
|
return res.status(200).json(result.data);
|
||||||
|
}
|
||||||
|
return res.status(500).json({ message: "Failed to fetch loans" });
|
||||||
|
});
|
||||||
|
|
||||||
|
router.get("/allItems", authenticate, async (req, res) => {
|
||||||
|
const result = await getAllItems();
|
||||||
|
if (result.success) {
|
||||||
|
return res.status(200).json(result.data);
|
||||||
|
}
|
||||||
|
return res.status(500).json({ message: "Failed to fetch items" });
|
||||||
|
});
|
||||||
|
|
||||||
|
router.delete("/deleteItem/:id", authenticate, async (req, res) => {
|
||||||
|
const itemId = req.params.id;
|
||||||
|
const result = await deleteItemID(itemId);
|
||||||
|
if (result.success) {
|
||||||
|
return res.status(200).json({ message: "Item deleted successfully" });
|
||||||
|
}
|
||||||
|
return res.status(500).json({ message: "Failed to delete item" });
|
||||||
|
});
|
||||||
|
|
||||||
|
router.post("/createItem", authenticate, async (req, res) => {
|
||||||
|
const { item_name, can_borrow_role } = req.body || {};
|
||||||
|
const result = await createItem(item_name, can_borrow_role);
|
||||||
|
if (result.success) {
|
||||||
|
return res.status(201).json({ message: "Item created successfully" });
|
||||||
|
}
|
||||||
|
return res.status(500).json({ message: "Failed to create item" });
|
||||||
|
});
|
||||||
|
|
||||||
|
router.post("/changePWadmin", authenticate, async (req, res) => {
|
||||||
|
const newPassword = req.body.newPassword;
|
||||||
|
if (!newPassword) {
|
||||||
|
return res.status(400).json({ message: "New password is required" });
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = await changeUserPassword(req.body.username, newPassword);
|
||||||
|
if (result.success) {
|
||||||
|
return res.status(200).json({ message: "Password changed successfully" });
|
||||||
|
}
|
||||||
|
return res.status(500).json({ message: "Failed to change password" });
|
||||||
|
});
|
||||||
|
|
||||||
|
router.post("/updateItemByID", authenticate, async (req, res) => {
|
||||||
|
const role = req.body.can_borrow_role;
|
||||||
|
const itemId = req.body.itemId;
|
||||||
|
const item_name = req.body.item_name;
|
||||||
|
const result = await updateItemByID(itemId, item_name, role);
|
||||||
|
if (result.success) {
|
||||||
|
return res.status(200).json({ message: "Item updated successfully" });
|
||||||
|
}
|
||||||
|
return res.status(500).json({ message: "Failed to update item" });
|
||||||
|
});
|
||||||
|
|
||||||
|
router.put("/changeSafeState/:itemId", authenticate, async (req, res) => {
|
||||||
|
const itemId = req.params.itemId;
|
||||||
|
const result = await changeInSafeStateV2(itemId);
|
||||||
|
if (result.success) {
|
||||||
|
return res
|
||||||
|
.status(200)
|
||||||
|
.json({ message: "Item safe state updated successfully" });
|
||||||
|
}
|
||||||
|
return res.status(500).json({ message: "Failed to update item safe state" });
|
||||||
|
});
|
||||||
|
|
||||||
|
router.get("/apiKeys", authenticate, async (req, res) => {
|
||||||
|
const result = await getAllApiKeys();
|
||||||
|
if (result.success) {
|
||||||
|
return res.status(200).json(result.data);
|
||||||
|
}
|
||||||
|
return res.status(500).json({ message: "Failed to fetch API keys" });
|
||||||
|
});
|
||||||
|
|
||||||
|
router.delete("/deleteAPKey/:id", authenticate, async (req, res) => {
|
||||||
|
const apiKeyId = req.params.id;
|
||||||
|
const result = await deleteAPKey(apiKeyId);
|
||||||
|
if (result.success) {
|
||||||
|
return res.status(200).json({ message: "API key deleted successfully" });
|
||||||
|
}
|
||||||
|
return res.status(500).json({ message: "Failed to delete API key" });
|
||||||
|
});
|
||||||
|
|
||||||
|
router.post("/createAPIentry", authenticate, async (req, res) => {
|
||||||
|
const apiKey = req.body.apiKey;
|
||||||
|
const user = req.body.user;
|
||||||
|
if (!apiKey || !user) {
|
||||||
|
return res.status(400).json({ message: "API key and user are required" });
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure apiKey is a number
|
||||||
|
const apiKeyNum = Number(apiKey);
|
||||||
|
if (!Number.isFinite(apiKeyNum)) {
|
||||||
|
return res.status(400).json({ message: "API key must be a number" });
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = await createAPIentry(apiKeyNum, user);
|
||||||
|
if (result.success) {
|
||||||
|
return res.status(201).json({ message: "API key created successfully" });
|
||||||
|
}
|
||||||
|
if (result.code === "DUPLICATE") {
|
||||||
|
return res.status(409).json({ message: "API key already exists" });
|
||||||
|
}
|
||||||
|
return res.status(500).json({ message: "Failed to create API key" });
|
||||||
|
});
|
||||||
|
|
||||||
|
router.get("/apiKeys/validate/:key", async (req, res) => {
|
||||||
|
try {
|
||||||
|
const rawKey = req.params.key;
|
||||||
|
const result = await getAllApiKeys();
|
||||||
|
if (!result.success || !Array.isArray(result.data)) {
|
||||||
|
return res.status(500).json({ valid: false });
|
||||||
|
}
|
||||||
|
|
||||||
|
const isValid = result.data.some((entry) => {
|
||||||
|
const val = String(
|
||||||
|
entry?.key ?? entry?.apiKey ?? entry?.api_key ?? entry
|
||||||
|
);
|
||||||
|
return val === String(rawKey);
|
||||||
|
});
|
||||||
|
|
||||||
|
return res.status(200).json({ valid: isValid });
|
||||||
|
} catch (err) {
|
||||||
|
console.error("validate api key error:", err);
|
||||||
|
return res.status(500).json({ valid: false });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
export default router;
|
||||||
133
backend/routes/apiV2.js
Normal file
133
backend/routes/apiV2.js
Normal file
@@ -0,0 +1,133 @@
|
|||||||
|
import express from "express";
|
||||||
|
import dotenv from "dotenv";
|
||||||
|
import {
|
||||||
|
getItemsFromDatabaseV2,
|
||||||
|
changeInSafeStateV2,
|
||||||
|
setTakeDateV2,
|
||||||
|
setReturnDateV2,
|
||||||
|
getLoanByCodeV2,
|
||||||
|
getAllLoansV2,
|
||||||
|
getAPIkey,
|
||||||
|
} from "../services/database.js";
|
||||||
|
|
||||||
|
dotenv.config();
|
||||||
|
const router = express.Router();
|
||||||
|
|
||||||
|
async function validateAPIKey(apiKey) {
|
||||||
|
try {
|
||||||
|
if (!apiKey) return false;
|
||||||
|
const result = await getAPIkey();
|
||||||
|
if (!result?.success || !Array.isArray(result.data)) return false;
|
||||||
|
return result.data.some((row) => String(row.apiKey) === String(apiKey));
|
||||||
|
} catch (err) {
|
||||||
|
console.error("validateAPIKey error:", err);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add a guard that returns Access Denied instead of hanging
|
||||||
|
const apiKeyGuard = async (req, res, next) => {
|
||||||
|
try {
|
||||||
|
const key = req.params.key;
|
||||||
|
if (!key) {
|
||||||
|
return res
|
||||||
|
.status(401)
|
||||||
|
.json({ message: "Access denied: missing API key" });
|
||||||
|
}
|
||||||
|
const ok = await validateAPIKey(key);
|
||||||
|
if (!ok) {
|
||||||
|
return res
|
||||||
|
.status(401)
|
||||||
|
.json({ message: "Access denied: invalid API key" });
|
||||||
|
}
|
||||||
|
next();
|
||||||
|
} catch (e) {
|
||||||
|
console.error("apiKeyGuard error:", e);
|
||||||
|
res.status(500).json({ message: "Internal server error" });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Route for API to get ALL items from the database
|
||||||
|
router.get("/items/:key", apiKeyGuard, async (req, res) => {
|
||||||
|
const result = await getItemsFromDatabaseV2();
|
||||||
|
if (result.success) {
|
||||||
|
res.status(200).json({ data: result.data });
|
||||||
|
} else {
|
||||||
|
res.status(500).json({ message: "Failed to fetch items" });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Route for API to control the position of an item
|
||||||
|
router.post(
|
||||||
|
"/controlInSafe/:key/:itemId/:state",
|
||||||
|
apiKeyGuard,
|
||||||
|
async (req, res) => {
|
||||||
|
const itemId = req.params.itemId;
|
||||||
|
const state = req.params.state;
|
||||||
|
|
||||||
|
if (state === "1" || state === "0") {
|
||||||
|
const result = await changeInSafeStateV2(itemId, state);
|
||||||
|
if (result.success) {
|
||||||
|
res.status(200).json({ data: result.data });
|
||||||
|
} else {
|
||||||
|
res.status(500).json({ message: "Failed to update item state" });
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
res.status(400).json({ message: "Invalid state value" });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
// Route for API to get a loan by its code
|
||||||
|
router.get("/getLoanByCode/:key/:loan_code", apiKeyGuard, async (req, res) => {
|
||||||
|
const loan_code = req.params.loan_code;
|
||||||
|
const result = await getLoanByCodeV2(loan_code);
|
||||||
|
if (result.success) {
|
||||||
|
res.status(200).json({ data: result.data });
|
||||||
|
} else {
|
||||||
|
res.status(404).json({ message: "Loan not found" });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Route for API to set the return date by the loan code
|
||||||
|
router.post("/setReturnDate/:key/:loan_code", apiKeyGuard, async (req, res) => {
|
||||||
|
const loanCode = req.params.loan_code;
|
||||||
|
const result = await setReturnDateV2(loanCode);
|
||||||
|
if (result.success) {
|
||||||
|
res.status(200).json({ data: result.data });
|
||||||
|
} else {
|
||||||
|
res.status(500).json({ message: "Failed to set return date" });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Route for API to set the take away date by the loan code
|
||||||
|
router.post("/setTakeDate/:key/:loan_code", apiKeyGuard, async (req, res) => {
|
||||||
|
const loanCode = req.params.loan_code;
|
||||||
|
const result = await setTakeDateV2(loanCode);
|
||||||
|
if (result.success) {
|
||||||
|
res.status(200).json({ data: result.data });
|
||||||
|
} else {
|
||||||
|
res.status(500).json({ message: "Failed to set take date" });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Route for API to get ALL loans from the database without sensitive info (only for landingpage)
|
||||||
|
router.get("/allLoans", async (req, res) => {
|
||||||
|
const result = await getAllLoansV2();
|
||||||
|
if (result.success) {
|
||||||
|
return res.status(200).json(result.data);
|
||||||
|
}
|
||||||
|
return res.status(500).json({ message: "Failed to fetch loans" });
|
||||||
|
});
|
||||||
|
|
||||||
|
// Route for API to get ALL items from the database (only for landingpage)
|
||||||
|
router.get("/allItems", async (req, res) => {
|
||||||
|
const result = await getItemsFromDatabaseV2();
|
||||||
|
if (result.success) {
|
||||||
|
res.status(200).json(result.data);
|
||||||
|
} else {
|
||||||
|
res.status(500).json({ message: "Failed to fetch items" });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
export default router;
|
||||||
100
backend/scheme.sql
Normal file
100
backend/scheme.sql
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
-- All necessary tables for the borrowing system
|
||||||
|
|
||||||
|
-- IMPORTANT: You need mySQL version 8.0 or newer!
|
||||||
|
|
||||||
|
CREATE TABLE `users` (
|
||||||
|
`id` int NOT NULL AUTO_INCREMENT,
|
||||||
|
`username` varchar(100) NOT NULL,
|
||||||
|
`password` varchar(255) NOT NULL,
|
||||||
|
`role` int DEFAULT NULL,
|
||||||
|
`entry_created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
PRIMARY KEY (`id`),
|
||||||
|
UNIQUE KEY `username` (`username`)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE `admins` (
|
||||||
|
`id` int NOT NULL AUTO_INCREMENT,
|
||||||
|
`username` varchar(100) NOT NULL,
|
||||||
|
`password` varchar(255) NOT NULL,
|
||||||
|
`first_name` varchar(255) NOT NULL,
|
||||||
|
`last_name` varchar(255) NOT NULL,
|
||||||
|
`entry_created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
PRIMARY KEY (`id`),
|
||||||
|
UNIQUE KEY `username` (`username`)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE `loans` (
|
||||||
|
`id` int NOT NULL AUTO_INCREMENT,
|
||||||
|
`username` varchar(100) NOT NULL,
|
||||||
|
`loan_code` int NOT NULL,
|
||||||
|
`start_date` timestamp NOT NULL,
|
||||||
|
`end_date` timestamp NOT NULL,
|
||||||
|
`take_date` timestamp NULL DEFAULT NULL,
|
||||||
|
`returned_date` timestamp NULL DEFAULT NULL,
|
||||||
|
`created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
`loaned_items_id` json NOT NULL DEFAULT ('[]'),
|
||||||
|
`loaned_items_name` json NOT NULL DEFAULT ('[]'),
|
||||||
|
`deleted` bool NOT NULL DEFAULT false,
|
||||||
|
PRIMARY KEY (`id`),
|
||||||
|
UNIQUE KEY `loan_code` (`loan_code`)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE `items` (
|
||||||
|
`id` int NOT NULL AUTO_INCREMENT,
|
||||||
|
`item_name` varchar(255) NOT NULL,
|
||||||
|
`can_borrow_role` INT NOT NULL,
|
||||||
|
`inSafe` tinyint(1) NOT NULL DEFAULT '1',
|
||||||
|
`entry_created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
PRIMARY KEY (`id`),
|
||||||
|
UNIQUE KEY `item_name` (`item_name`)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE `lockers` (
|
||||||
|
`id` int NOT NULL AUTO_INCREMENT,
|
||||||
|
`item` varchar(255) NOT NULL,
|
||||||
|
`locker_number` int NOT NULL,
|
||||||
|
`entry_created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
PRIMARY KEY (`id`),
|
||||||
|
UNIQUE KEY `item` (`item`),
|
||||||
|
UNIQUE KEY `locker_number` (`locker_number`)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE `apiKeys` (
|
||||||
|
`id` int NOT NULL AUTO_INCREMENT,
|
||||||
|
`apiKey` int NOT NULL UNIQUE,
|
||||||
|
`user` VARCHAR(255) NOT NULL,
|
||||||
|
`entry_created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
PRIMARY KEY (`id`)
|
||||||
|
);
|
||||||
|
|
||||||
|
INSERT INTO `items` (`item_name`, `can_borrow_role`, `inSafe`) VALUES
|
||||||
|
('DJI 1er Mikro', 4, 1),
|
||||||
|
('DJI 2er Mikro 1', 4, 1),
|
||||||
|
('DJI 2er Mikro 2', 4, 1),
|
||||||
|
('Rode Richt Mikrofon', 2, 1),
|
||||||
|
('Kamera Stativ', 1, 0),
|
||||||
|
('SONY Kamera - inkl. Akkus und Objektiv', 1, 1),
|
||||||
|
('MacBook inkl. Adapter', 2, 0),
|
||||||
|
('SD Karten', 3, 0),
|
||||||
|
('Kameragimbal', 1, 0),
|
||||||
|
('ATEM MINI PRO', 1, 1),
|
||||||
|
('Handygimbal', 4, 0),
|
||||||
|
('Kameralüfter', 1, 1),
|
||||||
|
('Kleine Kamera 1 - inkl. Objektiv', 2, 1),
|
||||||
|
('Kleine Kamera 2 - inkl. Objektiv', 2, 1);
|
||||||
|
|
||||||
|
INSERT INTO `lockers` (`item`, `locker_number`) VALUES
|
||||||
|
('DJI 1er Mikro', 1),
|
||||||
|
('DJI 2er Mikro 1', 2),
|
||||||
|
('DJI 2er Mikro 2', 3),
|
||||||
|
('Rode Richt Mikrofon', 4),
|
||||||
|
('Kamera Stativ', 5),
|
||||||
|
('SONY Kamera - inkl. Akkus und Objektiv', 6),
|
||||||
|
('MacBook inkl. Adapter', 7),
|
||||||
|
('SD Karten', 8),
|
||||||
|
('Kameragimbal', 9),
|
||||||
|
('ATEM MINI PRO', 10),
|
||||||
|
('Handygimbal', 11),
|
||||||
|
('Kameralüfter', 12),
|
||||||
|
('Kleine Kamera 1 - inkl. Objektiv', 13),
|
||||||
|
('Kleine Kamera 2 - inkl. Objektiv', 14);
|
||||||
32
backend/server.js
Normal file
32
backend/server.js
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
import express from "express";
|
||||||
|
import cors from "cors";
|
||||||
|
import env from "dotenv";
|
||||||
|
import apiRouter from "./routes/api.js";
|
||||||
|
import apiRouterV2 from "./routes/apiV2.js";
|
||||||
|
env.config();
|
||||||
|
const app = express();
|
||||||
|
const port = 8002;
|
||||||
|
|
||||||
|
app.use(cors());
|
||||||
|
// Increase body size limits to support large CSV JSON payloads
|
||||||
|
app.use(express.urlencoded({ extended: true, limit: "10mb" }));
|
||||||
|
app.set("view engine", "ejs");
|
||||||
|
app.use(express.json({ limit: "10mb" }));
|
||||||
|
|
||||||
|
app.use("/api", apiRouter);
|
||||||
|
app.use("/apiV2", apiRouterV2);
|
||||||
|
|
||||||
|
app.get("/", (req, res) => {
|
||||||
|
res.render("index.ejs");
|
||||||
|
});
|
||||||
|
|
||||||
|
app.listen(port, () => {
|
||||||
|
console.log(`Server is running on port: ${port}`);
|
||||||
|
});
|
||||||
|
|
||||||
|
// error handling code
|
||||||
|
app.use((err, req, res, next) => {
|
||||||
|
// Log the error stack and send a generic error response
|
||||||
|
console.error(err.stack);
|
||||||
|
res.status(500).send("Something broke!");
|
||||||
|
});
|
||||||
551
backend/services/database.js
Normal file
551
backend/services/database.js
Normal file
@@ -0,0 +1,551 @@
|
|||||||
|
import mysql from "mysql2";
|
||||||
|
import dotenv from "dotenv";
|
||||||
|
dotenv.config();
|
||||||
|
|
||||||
|
const pool = mysql
|
||||||
|
.createPool({
|
||||||
|
host: process.env.DB_HOST,
|
||||||
|
user: process.env.DB_USER,
|
||||||
|
password: process.env.DB_PASSWORD,
|
||||||
|
database: process.env.DB_NAME,
|
||||||
|
})
|
||||||
|
.promise();
|
||||||
|
|
||||||
|
export const loginFunc = async (username, password) => {
|
||||||
|
const [result] = await pool.query(
|
||||||
|
"SELECT * FROM users WHERE username = ? AND password = ?",
|
||||||
|
[username, password]
|
||||||
|
);
|
||||||
|
if (result.length > 0) return { success: true, data: result[0] };
|
||||||
|
return { success: false };
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getItemsFromDatabaseV2 = async () => {
|
||||||
|
const [rows] = await pool.query("SELECT * FROM items;");
|
||||||
|
if (rows.length > 0) {
|
||||||
|
return { success: true, data: rows };
|
||||||
|
}
|
||||||
|
return { success: false };
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getLoanByCodeV2 = async (loan_code) => {
|
||||||
|
const [result] = await pool.query(
|
||||||
|
"SELECT * FROM loans WHERE loan_code = ?;",
|
||||||
|
[loan_code]
|
||||||
|
);
|
||||||
|
if (result.length > 0) {
|
||||||
|
return { success: true, data: result[0] };
|
||||||
|
}
|
||||||
|
return { success: false };
|
||||||
|
};
|
||||||
|
|
||||||
|
export const changeInSafeStateV2 = async (itemId) => {
|
||||||
|
const [result] = await pool.query(
|
||||||
|
"UPDATE items SET inSafe = NOT inSafe WHERE id = ?",
|
||||||
|
[itemId]
|
||||||
|
);
|
||||||
|
if (result.affectedRows > 0) {
|
||||||
|
return { success: true };
|
||||||
|
}
|
||||||
|
return { success: false };
|
||||||
|
};
|
||||||
|
|
||||||
|
export const setReturnDateV2 = async (loanCode) => {
|
||||||
|
const [items] = await pool.query(
|
||||||
|
"SELECT loaned_items_id 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 inSafe = 1 WHERE id IN (?)",
|
||||||
|
[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 setTakeDateV2 = async (loanCode) => {
|
||||||
|
const [items] = await pool.query(
|
||||||
|
"SELECT loaned_items_id 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 inSafe = 0 WHERE id IN (?)",
|
||||||
|
[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 };
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getItemsFromDatabase = async (role) => {
|
||||||
|
const sql =
|
||||||
|
role == 0
|
||||||
|
? "SELECT * FROM items;"
|
||||||
|
: "SELECT * FROM items WHERE can_borrow_role >= ?";
|
||||||
|
const params = role == 0 ? [] : [role];
|
||||||
|
|
||||||
|
const [rows] = await pool.query(sql, params);
|
||||||
|
if (rows.length > 0) {
|
||||||
|
return { success: true, data: rows };
|
||||||
|
}
|
||||||
|
return { success: false };
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getLoansFromDatabase = async () => {
|
||||||
|
const [rows] = await pool.query("SELECT * FROM loans;");
|
||||||
|
return { success: true, data: rows.length > 0 ? rows : null };
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getUserLoansFromDatabase = async (username) => {
|
||||||
|
const [result] = await pool.query(
|
||||||
|
"SELECT * FROM loans WHERE username = ? AND deleted = 0;",
|
||||||
|
[username]
|
||||||
|
);
|
||||||
|
if (result.length > 0) {
|
||||||
|
return { success: true, data: result };
|
||||||
|
} else if (result.length == 0) {
|
||||||
|
return { success: true, data: "No loans found for this user" };
|
||||||
|
} else {
|
||||||
|
return { success: false };
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const deleteLoanFromDatabase = async (loanId) => {
|
||||||
|
const [result] = await pool.query("DELETE FROM loans WHERE id = ?;", [
|
||||||
|
loanId,
|
||||||
|
]);
|
||||||
|
if (result.affectedRows > 0) {
|
||||||
|
return { success: true };
|
||||||
|
} else {
|
||||||
|
return { success: false };
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const SETdeleteLoanFromDatabase = async (loanId) => {
|
||||||
|
const [result] = await pool.query(
|
||||||
|
"UPDATE loans SET deleted = 1 WHERE id = ?;",
|
||||||
|
[loanId]
|
||||||
|
);
|
||||||
|
if (result.affectedRows > 0) {
|
||||||
|
return { success: true };
|
||||||
|
} else {
|
||||||
|
return { success: false };
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getBorrowableItemsFromDatabase = async (
|
||||||
|
startDate,
|
||||||
|
endDate,
|
||||||
|
role = 0
|
||||||
|
) => {
|
||||||
|
// Overlap if: loan.start < end AND effective_end > start
|
||||||
|
// effective_end is returned_date if set, otherwise end_date
|
||||||
|
const hasRoleFilter = Number(role) > 0;
|
||||||
|
|
||||||
|
const sql = `
|
||||||
|
SELECT i.*
|
||||||
|
FROM items i
|
||||||
|
WHERE ${hasRoleFilter ? "i.can_borrow_role >= ? AND " : ""}NOT EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM loans l
|
||||||
|
JOIN JSON_TABLE(l.loaned_items_id, '$[*]' COLUMNS (item_id INT PATH '$')) jt
|
||||||
|
WHERE jt.item_id = i.id
|
||||||
|
AND l.deleted = 0
|
||||||
|
AND l.start_date < ?
|
||||||
|
AND COALESCE(l.returned_date, l.end_date) > ?
|
||||||
|
);
|
||||||
|
`;
|
||||||
|
|
||||||
|
const params = hasRoleFilter
|
||||||
|
? [role, endDate, startDate]
|
||||||
|
: [endDate, startDate];
|
||||||
|
|
||||||
|
const [rows] = await pool.query(sql, params);
|
||||||
|
if (rows.length > 0) {
|
||||||
|
return { success: true, data: rows };
|
||||||
|
}
|
||||||
|
return { success: false };
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getLoanInfoWithID = async (loanId) => {
|
||||||
|
const [rows] = await pool.query("SELECT * FROM loans WHERE id = ?;", [
|
||||||
|
loanId,
|
||||||
|
]);
|
||||||
|
if (rows.length > 0) {
|
||||||
|
return { success: true, data: rows[0] };
|
||||||
|
}
|
||||||
|
return { success: false };
|
||||||
|
};
|
||||||
|
|
||||||
|
export const createLoanInDatabase = async (
|
||||||
|
username,
|
||||||
|
startDate,
|
||||||
|
endDate,
|
||||||
|
itemIds
|
||||||
|
) => {
|
||||||
|
if (!username)
|
||||||
|
return { success: false, code: "BAD_REQUEST", message: "Missing username" };
|
||||||
|
if (!Array.isArray(itemIds) || itemIds.length === 0)
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
code: "BAD_REQUEST",
|
||||||
|
message: "No items provided",
|
||||||
|
};
|
||||||
|
if (!startDate || !endDate)
|
||||||
|
return { success: false, code: "BAD_REQUEST", message: "Missing dates" };
|
||||||
|
|
||||||
|
const start = new Date(startDate);
|
||||||
|
const end = new Date(endDate);
|
||||||
|
if (
|
||||||
|
!(start instanceof Date) ||
|
||||||
|
isNaN(start.getTime()) ||
|
||||||
|
!(end instanceof Date) ||
|
||||||
|
isNaN(end.getTime()) ||
|
||||||
|
start >= end
|
||||||
|
) {
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
code: "BAD_REQUEST",
|
||||||
|
message: "Invalid date range",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const conn = await pool.getConnection();
|
||||||
|
try {
|
||||||
|
await conn.beginTransaction();
|
||||||
|
|
||||||
|
// Ensure all items exist and collect names
|
||||||
|
const [itemsRows] = await conn.query(
|
||||||
|
"SELECT id, item_name FROM items WHERE id IN (?)",
|
||||||
|
[itemIds]
|
||||||
|
);
|
||||||
|
if (!itemsRows || itemsRows.length !== itemIds.length) {
|
||||||
|
await conn.rollback();
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
code: "BAD_REQUEST",
|
||||||
|
message: "One or more items not found",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
const itemNames = itemIds
|
||||||
|
.map(
|
||||||
|
(id) => itemsRows.find((r) => Number(r.id) === Number(id))?.item_name
|
||||||
|
)
|
||||||
|
.filter(Boolean);
|
||||||
|
|
||||||
|
// Check availability (no overlap with existing loans)
|
||||||
|
const [confRows] = await conn.query(
|
||||||
|
`
|
||||||
|
SELECT COUNT(*) AS conflicts
|
||||||
|
FROM loans l
|
||||||
|
JOIN JSON_TABLE(l.loaned_items_id, '$[*]' COLUMNS (item_id INT PATH '$')) jt
|
||||||
|
ON TRUE
|
||||||
|
WHERE jt.item_id IN (?)
|
||||||
|
AND l.deleted = 0
|
||||||
|
AND l.start_date < ?
|
||||||
|
AND COALESCE(l.returned_date, l.end_date) > ?
|
||||||
|
`,
|
||||||
|
[itemIds, end, start]
|
||||||
|
);
|
||||||
|
if (confRows?.[0]?.conflicts > 0) {
|
||||||
|
await conn.rollback();
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
code: "CONFLICT",
|
||||||
|
message: "One or more items are not available in the selected period",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generate unique loan_code (retry a few times)
|
||||||
|
let loanCode = null;
|
||||||
|
for (let i = 0; i < 6; i++) {
|
||||||
|
const candidate = Math.floor(100000 + Math.random() * 899999); // 6 digits
|
||||||
|
const [exists] = await conn.query(
|
||||||
|
"SELECT 1 FROM loans WHERE loan_code = ? LIMIT 1",
|
||||||
|
[candidate]
|
||||||
|
);
|
||||||
|
if (exists.length === 0) {
|
||||||
|
loanCode = candidate;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!loanCode) {
|
||||||
|
await conn.rollback();
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
code: "SERVER_ERROR",
|
||||||
|
message: "Failed to generate unique loan code",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Insert loan
|
||||||
|
const [insertRes] = await conn.query(
|
||||||
|
`
|
||||||
|
INSERT INTO loans (username, loan_code, start_date, end_date, loaned_items_id, loaned_items_name)
|
||||||
|
VALUES (?, ?, ?, ?, CAST(? AS JSON), CAST(? AS JSON))
|
||||||
|
`,
|
||||||
|
[
|
||||||
|
username,
|
||||||
|
loanCode,
|
||||||
|
// Use DATETIME/TIMESTAMP friendly format
|
||||||
|
new Date(start).toISOString().slice(0, 19).replace("T", " "),
|
||||||
|
new Date(end).toISOString().slice(0, 19).replace("T", " "),
|
||||||
|
JSON.stringify(itemIds.map((n) => Number(n))),
|
||||||
|
JSON.stringify(itemNames),
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
await conn.commit();
|
||||||
|
return {
|
||||||
|
success: true,
|
||||||
|
data: {
|
||||||
|
id: insertRes.insertId,
|
||||||
|
loan_code: loanCode,
|
||||||
|
username,
|
||||||
|
start_date: start,
|
||||||
|
end_date: end,
|
||||||
|
items: itemIds,
|
||||||
|
item_names: itemNames,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
} catch (err) {
|
||||||
|
await conn.rollback();
|
||||||
|
console.error("createLoanInDatabase error:", err);
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
code: "SERVER_ERROR",
|
||||||
|
message: "Failed to create loan",
|
||||||
|
};
|
||||||
|
} finally {
|
||||||
|
conn.release();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// These functions are only temporary, and will be deleted when the full bin is set up.
|
||||||
|
export const onTake = async (loanId) => {
|
||||||
|
const [items] = await pool.query(
|
||||||
|
"SELECT loaned_items_id FROM loans WHERE id = ?",
|
||||||
|
[loanId]
|
||||||
|
);
|
||||||
|
|
||||||
|
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 inSafe = 0 WHERE id IN (?)",
|
||||||
|
[itemIds]
|
||||||
|
);
|
||||||
|
|
||||||
|
const [result] = await pool.query(
|
||||||
|
"UPDATE loans SET take_date = NOW() WHERE id = ?",
|
||||||
|
[loanId]
|
||||||
|
);
|
||||||
|
|
||||||
|
if (result.affectedRows > 0 && setItemStates.affectedRows > 0) {
|
||||||
|
return { success: true };
|
||||||
|
}
|
||||||
|
return { success: false };
|
||||||
|
};
|
||||||
|
|
||||||
|
export const onReturn = async (loanId) => {
|
||||||
|
const [items] = await pool.query(
|
||||||
|
"SELECT loaned_items_id FROM loans WHERE id = ?",
|
||||||
|
[loanId]
|
||||||
|
);
|
||||||
|
|
||||||
|
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 inSafe = 1 WHERE id IN (?)",
|
||||||
|
[itemIds]
|
||||||
|
);
|
||||||
|
|
||||||
|
const [result] = await pool.query(
|
||||||
|
"UPDATE loans SET returned_date = NOW() WHERE id = ?",
|
||||||
|
[loanId]
|
||||||
|
);
|
||||||
|
|
||||||
|
if (result.affectedRows > 0 && setItemStates.affectedRows > 0) {
|
||||||
|
return { success: true };
|
||||||
|
}
|
||||||
|
return { success: false };
|
||||||
|
};
|
||||||
|
// Temporary functions end here.
|
||||||
|
|
||||||
|
export const loginAdmin = async (username, password) => {
|
||||||
|
const [result] = await pool.query(
|
||||||
|
"SELECT * FROM admins WHERE username = ? AND password = ?",
|
||||||
|
[username, password]
|
||||||
|
);
|
||||||
|
if (result.length > 0) return { success: true, data: result[0] };
|
||||||
|
return { success: false };
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getAllUsers = async () => {
|
||||||
|
const [result] = await pool.query(
|
||||||
|
"SELECT id, username, role, entry_created_at FROM users"
|
||||||
|
);
|
||||||
|
if (result.length > 0) return { success: true, data: result };
|
||||||
|
return { success: false };
|
||||||
|
};
|
||||||
|
|
||||||
|
export const deleteUserID = async (userId) => {
|
||||||
|
const [result] = await pool.query("DELETE FROM users WHERE id = ?", [userId]);
|
||||||
|
if (result.affectedRows > 0) return { success: true };
|
||||||
|
return { success: false };
|
||||||
|
};
|
||||||
|
|
||||||
|
export const handleEdit = async (userId, username, role) => {
|
||||||
|
const [result] = await pool.query(
|
||||||
|
"UPDATE users SET username = ?, role = ? WHERE id = ?",
|
||||||
|
[username, role, userId]
|
||||||
|
);
|
||||||
|
if (result.affectedRows > 0) return { success: true };
|
||||||
|
return { success: false };
|
||||||
|
};
|
||||||
|
|
||||||
|
export const createUser = async (username, role, password) => {
|
||||||
|
const [result] = await pool.query(
|
||||||
|
"INSERT INTO users (username, role, password) VALUES (?, ?, ?)",
|
||||||
|
[username, role, password]
|
||||||
|
);
|
||||||
|
if (result.affectedRows > 0) return { success: true };
|
||||||
|
return { success: false };
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getAllLoans = async () => {
|
||||||
|
const [result] = await pool.query("SELECT * FROM loans");
|
||||||
|
if (result.length > 0) return { success: true, data: result };
|
||||||
|
return { success: false };
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getAllItems = async () => {
|
||||||
|
const [result] = await pool.query("SELECT * FROM items");
|
||||||
|
if (result.length > 0) return { success: true, data: result };
|
||||||
|
return { success: false };
|
||||||
|
};
|
||||||
|
|
||||||
|
export const deleteItemID = async (itemId) => {
|
||||||
|
const [result] = await pool.query("DELETE FROM items WHERE id = ?", [itemId]);
|
||||||
|
if (result.affectedRows > 0) return { success: true };
|
||||||
|
return { success: false };
|
||||||
|
};
|
||||||
|
|
||||||
|
export const createItem = async (item_name, can_borrow_role) => {
|
||||||
|
const [result] = await pool.query(
|
||||||
|
"INSERT INTO items (item_name, can_borrow_role) VALUES (?, ?)",
|
||||||
|
[item_name, can_borrow_role]
|
||||||
|
);
|
||||||
|
if (result.affectedRows > 0) return { success: true };
|
||||||
|
return { success: false };
|
||||||
|
};
|
||||||
|
|
||||||
|
export const changeUserPassword = async (username, newPassword) => {
|
||||||
|
const [result] = await pool.query(
|
||||||
|
"UPDATE users SET password = ? WHERE username = ?",
|
||||||
|
[newPassword, username]
|
||||||
|
);
|
||||||
|
if (result.affectedRows > 0) return { success: true };
|
||||||
|
return { success: false };
|
||||||
|
};
|
||||||
|
|
||||||
|
export const changeUserPasswordFRONTEND = async (
|
||||||
|
username,
|
||||||
|
oldPassword,
|
||||||
|
newPassword
|
||||||
|
) => {
|
||||||
|
const [result] = await pool.query(
|
||||||
|
"UPDATE users SET password = ? WHERE username = ? AND password = ?",
|
||||||
|
[newPassword, username, oldPassword]
|
||||||
|
);
|
||||||
|
if (result.affectedRows > 0) return { success: true };
|
||||||
|
return { success: false };
|
||||||
|
};
|
||||||
|
|
||||||
|
export const updateItemByID = async (itemId, item_name, can_borrow_role) => {
|
||||||
|
const [result] = await pool.query(
|
||||||
|
"UPDATE items SET item_name = ?, can_borrow_role = ? WHERE id = ?",
|
||||||
|
[item_name, can_borrow_role, itemId]
|
||||||
|
);
|
||||||
|
if (result.affectedRows > 0) return { success: true };
|
||||||
|
return { success: false };
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getAllLoansV2 = async () => {
|
||||||
|
const [rows] = await pool.query(
|
||||||
|
"SELECT id, username, start_date, end_date, loaned_items_name, returned_date, take_date FROM loans"
|
||||||
|
);
|
||||||
|
if (rows.length > 0) {
|
||||||
|
return { success: true, data: rows };
|
||||||
|
}
|
||||||
|
return { success: false };
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getAllApiKeys = async () => {
|
||||||
|
const [rows] = await pool.query("SELECT * FROM apiKeys");
|
||||||
|
if (rows.length > 0) {
|
||||||
|
return { success: true, data: rows };
|
||||||
|
}
|
||||||
|
return { success: false };
|
||||||
|
};
|
||||||
|
|
||||||
|
export const createAPIentry = async (apiKey, user) => {
|
||||||
|
const [result] = await pool.query(
|
||||||
|
"INSERT INTO apiKeys (apiKey, user) VALUES (?, ?)",
|
||||||
|
[apiKey, user]
|
||||||
|
);
|
||||||
|
if (result.affectedRows > 0) return { success: true };
|
||||||
|
return { success: false };
|
||||||
|
};
|
||||||
|
|
||||||
|
export const deleteAPKey = async (apiKeyId) => {
|
||||||
|
const [result] = await pool.query("DELETE FROM apiKeys WHERE id = ?", [
|
||||||
|
apiKeyId,
|
||||||
|
]);
|
||||||
|
if (result.affectedRows > 0) return { success: true };
|
||||||
|
return { success: false };
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getAPIkey = async () => {
|
||||||
|
const [rows] = await pool.query("SELECT apiKey FROM apiKeys");
|
||||||
|
if (rows.length > 0) {
|
||||||
|
return { success: true, data: rows };
|
||||||
|
}
|
||||||
|
return { success: false };
|
||||||
|
};
|
||||||
25
backend/services/tokenService.js
Normal file
25
backend/services/tokenService.js
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
import { SignJWT, jwtVerify } from "jose";
|
||||||
|
import env from "dotenv";
|
||||||
|
env.config();
|
||||||
|
const secret = new TextEncoder().encode(process.env.SECRET_KEY);
|
||||||
|
|
||||||
|
export async function generateToken(payload) {
|
||||||
|
const newToken = await new SignJWT(payload)
|
||||||
|
.setProtectedHeader({ alg: "HS256" })
|
||||||
|
.setIssuedAt()
|
||||||
|
.setExpirationTime("2h") // Token valid for 2 hours
|
||||||
|
.sign(secret);
|
||||||
|
return newToken;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function authenticate(req, res, next) {
|
||||||
|
const authHeader = req.headers["authorization"];
|
||||||
|
const token = authHeader && authHeader.split(" ")[1]; // Bearer <token>
|
||||||
|
|
||||||
|
if (token == null) return res.sendStatus(401); // No token present
|
||||||
|
|
||||||
|
const { payload } = await jwtVerify(token, secret);
|
||||||
|
req.user = payload;
|
||||||
|
|
||||||
|
next();
|
||||||
|
}
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
{
|
|
||||||
"backend-info": {
|
|
||||||
"version": "v2.1.1 (dev)"
|
|
||||||
},
|
|
||||||
"frontend-info": {
|
|
||||||
"version": "v2.1.2 (dev)"
|
|
||||||
},
|
|
||||||
"admin-panel-info": {
|
|
||||||
"version": "v1.3.2 (dev)"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,41 +0,0 @@
|
|||||||
import express from "express";
|
|
||||||
import { authenticateAdmin } from "../../services/authentication.js";
|
|
||||||
const router = express.Router();
|
|
||||||
import dotenv from "dotenv";
|
|
||||||
dotenv.config();
|
|
||||||
|
|
||||||
// database funcs import
|
|
||||||
import {
|
|
||||||
getAllApiKeys,
|
|
||||||
createAPIentry,
|
|
||||||
deleteAPKey,
|
|
||||||
} from "./database/apiDataMgmt.database.js";
|
|
||||||
|
|
||||||
router.get("/get-api-keys", authenticateAdmin, async (req, res) => {
|
|
||||||
const result = await getAllApiKeys();
|
|
||||||
if (result.success) {
|
|
||||||
return res.status(200).json(result.data);
|
|
||||||
}
|
|
||||||
return res.status(500).json({ message: "Failed to retrieve API keys" });
|
|
||||||
});
|
|
||||||
|
|
||||||
router.post("/create-api-key", authenticateAdmin, async (req, res) => {
|
|
||||||
const apiKey = req.body.apiKey;
|
|
||||||
const entryName = req.body.entryName;
|
|
||||||
const result = await createAPIentry(apiKey, entryName);
|
|
||||||
if (result.success) {
|
|
||||||
return res.status(201).json({ message: "API key created successfully" });
|
|
||||||
}
|
|
||||||
return res.status(500).json({ message: "Failed to create API key" });
|
|
||||||
});
|
|
||||||
|
|
||||||
router.delete("/delete-api-key/:id", authenticateAdmin, async (req, res) => {
|
|
||||||
const apiKeyId = req.params.id;
|
|
||||||
const result = await deleteAPKey(apiKeyId);
|
|
||||||
if (result.success) {
|
|
||||||
return res.status(200).json({ message: "API key deleted successfully" });
|
|
||||||
}
|
|
||||||
return res.status(500).json({ message: "Failed to delete API key" });
|
|
||||||
});
|
|
||||||
|
|
||||||
export default router;
|
|
||||||
@@ -1,37 +0,0 @@
|
|||||||
import mysql from "mysql2";
|
|
||||||
import dotenv from "dotenv";
|
|
||||||
dotenv.config();
|
|
||||||
|
|
||||||
const pool = mysql
|
|
||||||
.createPool({
|
|
||||||
host: process.env.DB_HOST,
|
|
||||||
user: process.env.DB_USER,
|
|
||||||
password: process.env.DB_PASSWORD,
|
|
||||||
database: process.env.DB_NAME,
|
|
||||||
})
|
|
||||||
.promise();
|
|
||||||
|
|
||||||
export const getAllApiKeys = async () => {
|
|
||||||
const [rows] = await pool.query("SELECT * FROM apiKeys");
|
|
||||||
if (rows.length > 0) {
|
|
||||||
return { success: true, data: rows };
|
|
||||||
}
|
|
||||||
return { success: false };
|
|
||||||
};
|
|
||||||
|
|
||||||
export const createAPIentry = async (apiKey, entryName) => {
|
|
||||||
const [result] = await pool.query(
|
|
||||||
"INSERT INTO apiKeys (api_key, entry_name) VALUES (?, ?)",
|
|
||||||
[apiKey, entryName]
|
|
||||||
);
|
|
||||||
if (result.affectedRows > 0) return { success: true };
|
|
||||||
return { success: false };
|
|
||||||
};
|
|
||||||
|
|
||||||
export const deleteAPKey = async (apiKeyId) => {
|
|
||||||
const [result] = await pool.query("DELETE FROM apiKeys WHERE id = ?", [
|
|
||||||
apiKeyId,
|
|
||||||
]);
|
|
||||||
if (result.affectedRows > 0) return { success: true };
|
|
||||||
return { success: false };
|
|
||||||
};
|
|
||||||
@@ -1,82 +0,0 @@
|
|||||||
import mysql from "mysql2";
|
|
||||||
import dotenv from "dotenv";
|
|
||||||
dotenv.config();
|
|
||||||
|
|
||||||
const pool = mysql
|
|
||||||
.createPool({
|
|
||||||
host: process.env.DB_HOST,
|
|
||||||
user: process.env.DB_USER,
|
|
||||||
password: process.env.DB_PASSWORD,
|
|
||||||
database: process.env.DB_NAME,
|
|
||||||
})
|
|
||||||
.promise();
|
|
||||||
|
|
||||||
export const getAllItems = async () => {
|
|
||||||
const [result] = await pool.query("SELECT * FROM items");
|
|
||||||
if (result.length > 0) return { success: true, data: result };
|
|
||||||
return { success: false };
|
|
||||||
};
|
|
||||||
|
|
||||||
export const deleteItemById = async (itemId) => {
|
|
||||||
const [result] = await pool.query("DELETE FROM items WHERE id = ?", [itemId]);
|
|
||||||
if (result.affectedRows > 0) return { success: true };
|
|
||||||
return { success: false };
|
|
||||||
};
|
|
||||||
|
|
||||||
export const createItem = async (item_name, can_borrow_role, lockerNumber) => {
|
|
||||||
const [result] = await pool.query(
|
|
||||||
"INSERT INTO items (item_name, can_borrow_role, in_safe, safe_nr) VALUES (?, ?, ?, ?)",
|
|
||||||
[item_name, can_borrow_role, true, lockerNumber]
|
|
||||||
);
|
|
||||||
if (result.affectedRows > 0) return { success: true };
|
|
||||||
return { success: false };
|
|
||||||
};
|
|
||||||
|
|
||||||
export const editItemById = async (
|
|
||||||
itemId,
|
|
||||||
item_name,
|
|
||||||
can_borrow_role,
|
|
||||||
safe_nr,
|
|
||||||
door_key
|
|
||||||
) => {
|
|
||||||
let newSafeNr;
|
|
||||||
if (safe_nr === null || safe_nr === "") {
|
|
||||||
newSafeNr = null;
|
|
||||||
} else {
|
|
||||||
newSafeNr = safe_nr;
|
|
||||||
}
|
|
||||||
const [result] = await pool.query(
|
|
||||||
"UPDATE items SET item_name = ?, can_borrow_role = ?, safe_nr = ?, door_key = ?, entry_updated_at = NOW() WHERE id = ?",
|
|
||||||
[item_name, can_borrow_role, newSafeNr, door_key, itemId]
|
|
||||||
);
|
|
||||||
if (result.affectedRows > 0) return { success: true };
|
|
||||||
return { success: false };
|
|
||||||
};
|
|
||||||
|
|
||||||
export const changeSafeState = async (itemId) => {
|
|
||||||
const currentState = await pool.query(
|
|
||||||
"SELECT in_safe FROM items WHERE id = ?",
|
|
||||||
[itemId]
|
|
||||||
);
|
|
||||||
if (currentState[0].length === 0) {
|
|
||||||
return { success: false };
|
|
||||||
}
|
|
||||||
|
|
||||||
if (currentState[0][0].in_safe) {
|
|
||||||
const [result] = await pool.query(
|
|
||||||
"UPDATE items SET in_safe = false WHERE id = ?",
|
|
||||||
[itemId]
|
|
||||||
);
|
|
||||||
if (result.affectedRows > 0) return { success: true };
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!currentState[0][0].in_safe) {
|
|
||||||
const [result] = await pool.query(
|
|
||||||
"UPDATE items SET in_safe = true WHERE id = ?",
|
|
||||||
[itemId]
|
|
||||||
);
|
|
||||||
if (result.affectedRows > 0) return { success: true };
|
|
||||||
}
|
|
||||||
|
|
||||||
return { success: false };
|
|
||||||
};
|
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
import mysql from "mysql2";
|
|
||||||
import dotenv from "dotenv";
|
|
||||||
dotenv.config();
|
|
||||||
|
|
||||||
const pool = mysql
|
|
||||||
.createPool({
|
|
||||||
host: process.env.DB_HOST,
|
|
||||||
user: process.env.DB_USER,
|
|
||||||
password: process.env.DB_PASSWORD,
|
|
||||||
database: process.env.DB_NAME,
|
|
||||||
})
|
|
||||||
.promise();
|
|
||||||
|
|
||||||
export const getAllLoans = async () => {
|
|
||||||
const [rows] = await pool.query("SELECT * FROM loans");
|
|
||||||
return { success: true, data: rows };
|
|
||||||
};
|
|
||||||
|
|
||||||
export const deleteLoanById = async (loanId) => {
|
|
||||||
const [result] = await pool.query("DELETE FROM loans WHERE id = ?", [loanId]);
|
|
||||||
if (result.affectedRows > 0) return { success: true };
|
|
||||||
return { success: false };
|
|
||||||
};
|
|
||||||
@@ -1,79 +0,0 @@
|
|||||||
import mysql from "mysql2";
|
|
||||||
import dotenv from "dotenv";
|
|
||||||
dotenv.config();
|
|
||||||
|
|
||||||
const pool = mysql
|
|
||||||
.createPool({
|
|
||||||
host: process.env.DB_HOST,
|
|
||||||
user: process.env.DB_USER,
|
|
||||||
password: process.env.DB_PASSWORD,
|
|
||||||
database: process.env.DB_NAME,
|
|
||||||
})
|
|
||||||
.promise();
|
|
||||||
|
|
||||||
export const createUser = async (
|
|
||||||
username,
|
|
||||||
role,
|
|
||||||
password,
|
|
||||||
isAdmin,
|
|
||||||
email,
|
|
||||||
first_name,
|
|
||||||
last_name,
|
|
||||||
) => {
|
|
||||||
const [result] = await pool.query(
|
|
||||||
"INSERT INTO users (username, role, password, is_admin, email, first_name, last_name) VALUES (?, ?, ?, ?, ?, ?, ?)",
|
|
||||||
[username, role, password, isAdmin, email, first_name, last_name],
|
|
||||||
);
|
|
||||||
if (result.affectedRows > 0) return { success: true };
|
|
||||||
return { success: false };
|
|
||||||
};
|
|
||||||
|
|
||||||
export const deleteUserById = async (userId) => {
|
|
||||||
const [result] = await pool.query("DELETE FROM users WHERE id = ?", [userId]);
|
|
||||||
if (result.affectedRows > 0) return { success: true };
|
|
||||||
return { success: false };
|
|
||||||
};
|
|
||||||
|
|
||||||
export const changePassword = async (username, newPassword) => {
|
|
||||||
const [result] = await pool.query(
|
|
||||||
"UPDATE users SET password = ?, entry_updated_at = NOW() WHERE username = ?",
|
|
||||||
[newPassword, username],
|
|
||||||
);
|
|
||||||
if (result.affectedRows > 0) return { success: true };
|
|
||||||
return { success: false };
|
|
||||||
};
|
|
||||||
|
|
||||||
export const editUserById = async (
|
|
||||||
userId,
|
|
||||||
first_name,
|
|
||||||
last_name,
|
|
||||||
role,
|
|
||||||
email,
|
|
||||||
is_admin,
|
|
||||||
) => {
|
|
||||||
const [result] = await pool.query(
|
|
||||||
"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],
|
|
||||||
);
|
|
||||||
if (result.affectedRows > 0) return { success: true };
|
|
||||||
return { success: false };
|
|
||||||
};
|
|
||||||
|
|
||||||
export const getAllUsers = async () => {
|
|
||||||
const [result] = await pool.query(
|
|
||||||
"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 };
|
|
||||||
return { success: false };
|
|
||||||
};
|
|
||||||
|
|
||||||
export const getUserById = async (userId) => {
|
|
||||||
const [rows] = await pool.query(
|
|
||||||
"SELECT id, username, first_name, last_name, role, email, is_admin FROM users WHERE id = ?",
|
|
||||||
[userId],
|
|
||||||
);
|
|
||||||
if (rows.length === 0) {
|
|
||||||
return { success: false };
|
|
||||||
}
|
|
||||||
return { success: true, data: rows[0] };
|
|
||||||
};
|
|
||||||
@@ -1,47 +0,0 @@
|
|||||||
import mysql from "mysql2";
|
|
||||||
import dotenv from "dotenv";
|
|
||||||
dotenv.config();
|
|
||||||
|
|
||||||
const pool = mysql
|
|
||||||
.createPool({
|
|
||||||
host: process.env.DB_HOST,
|
|
||||||
user: process.env.DB_USER,
|
|
||||||
password: process.env.DB_PASSWORD,
|
|
||||||
database: process.env.DB_NAME,
|
|
||||||
})
|
|
||||||
.promise();
|
|
||||||
|
|
||||||
export const loginAdmin = async (username, password) => {
|
|
||||||
const [rows] = await pool.query(
|
|
||||||
"SELECT id, username, first_name, last_name, role, is_admin FROM users WHERE username = ? AND password = ?",
|
|
||||||
[username, password]
|
|
||||||
);
|
|
||||||
|
|
||||||
if (rows.length === 0) {
|
|
||||||
return { success: false, reason: "invalid_credentials" };
|
|
||||||
}
|
|
||||||
|
|
||||||
const user = rows[0];
|
|
||||||
if (!user.is_admin) {
|
|
||||||
return { success: false, reason: "not_admin" };
|
|
||||||
}
|
|
||||||
|
|
||||||
return { success: true, data: user };
|
|
||||||
};
|
|
||||||
|
|
||||||
export const executeQuery = async (query, password, username) => {
|
|
||||||
let verified = false;
|
|
||||||
const [user] = await pool.query(
|
|
||||||
"SELECT * FROM users WHERE username = ? AND password = ?",
|
|
||||||
[username, password]
|
|
||||||
);
|
|
||||||
if (user.length > 0 && user[0].is_admin) {
|
|
||||||
verified = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!verified) {
|
|
||||||
return { success: false, message: "Unauthorized" };
|
|
||||||
}
|
|
||||||
const [result] = await pool.query(`${query}`);
|
|
||||||
return { success: true, data: result };
|
|
||||||
};
|
|
||||||
@@ -1,68 +0,0 @@
|
|||||||
import express from "express";
|
|
||||||
import { authenticateAdmin } from "../../services/authentication.js";
|
|
||||||
const router = express.Router();
|
|
||||||
import dotenv from "dotenv";
|
|
||||||
dotenv.config();
|
|
||||||
|
|
||||||
// database funcs import
|
|
||||||
import {
|
|
||||||
editItemById,
|
|
||||||
getAllItems,
|
|
||||||
deleteItemById,
|
|
||||||
createItem,
|
|
||||||
changeSafeState,
|
|
||||||
} from "./database/itemDataMgmt.database.js";
|
|
||||||
|
|
||||||
router.get("/all-items", authenticateAdmin, async (req, res) => {
|
|
||||||
const result = await getAllItems();
|
|
||||||
if (result.success) {
|
|
||||||
return res.status(200).json(result.data);
|
|
||||||
}
|
|
||||||
return res.status(500).json({ message: "Failed to retrieve items" });
|
|
||||||
});
|
|
||||||
|
|
||||||
router.delete("/delete-item/:id", authenticateAdmin, async (req, res) => {
|
|
||||||
const itemId = req.params.id;
|
|
||||||
const result = await deleteItemById(itemId);
|
|
||||||
if (result.success) {
|
|
||||||
return res.status(200).json({ message: "Item deleted successfully" });
|
|
||||||
}
|
|
||||||
return res.status(500).json({ message: "Failed to delete item" });
|
|
||||||
});
|
|
||||||
|
|
||||||
router.post("/create-item", authenticateAdmin, async (req, res) => {
|
|
||||||
const { item_name, can_borrow_role, lockerNumber } = req.body;
|
|
||||||
const result = await createItem(item_name, can_borrow_role, lockerNumber);
|
|
||||||
if (result.success) {
|
|
||||||
return res.status(201).json({ message: "Item created successfully" });
|
|
||||||
}
|
|
||||||
return res.status(500).json({ message: "Failed to create item" });
|
|
||||||
});
|
|
||||||
|
|
||||||
router.post("/edit-item/:id", authenticateAdmin, async (req, res) => {
|
|
||||||
const itemId = req.params.id;
|
|
||||||
const { item_name, can_borrow_role, safe_nr, door_key } = req.body;
|
|
||||||
|
|
||||||
const result = await editItemById(
|
|
||||||
itemId,
|
|
||||||
item_name,
|
|
||||||
can_borrow_role,
|
|
||||||
safe_nr,
|
|
||||||
door_key
|
|
||||||
);
|
|
||||||
if (result.success) {
|
|
||||||
return res.status(200).json({ message: "Item edited successfully" });
|
|
||||||
}
|
|
||||||
return res.status(500).json({ message: "Failed to edit item" });
|
|
||||||
});
|
|
||||||
|
|
||||||
router.post("/change-safe-state/:id", authenticateAdmin, async (req, res) => {
|
|
||||||
const itemId = req.params.id;
|
|
||||||
const result = await changeSafeState(itemId);
|
|
||||||
if (result.success) {
|
|
||||||
return res.status(200).json({ message: "Safe state changed successfully" });
|
|
||||||
}
|
|
||||||
return res.status(500).json({ message: "Failed to change safe state" });
|
|
||||||
});
|
|
||||||
|
|
||||||
export default router;
|
|
||||||
@@ -1,30 +0,0 @@
|
|||||||
import express from "express";
|
|
||||||
import { authenticateAdmin } from "../../services/authentication.js";
|
|
||||||
const router = express.Router();
|
|
||||||
import dotenv from "dotenv";
|
|
||||||
dotenv.config();
|
|
||||||
|
|
||||||
// database funcs import
|
|
||||||
import {
|
|
||||||
deleteLoanById,
|
|
||||||
getAllLoans,
|
|
||||||
} from "./database/loanDataMgmt.database.js";
|
|
||||||
|
|
||||||
router.get("/all-loans", authenticateAdmin, async (req, res) => {
|
|
||||||
const result = await getAllLoans();
|
|
||||||
if (result.success) {
|
|
||||||
return res.status(200).json(result.data);
|
|
||||||
}
|
|
||||||
return res.status(500).json({ message: "Failed to retrieve loans" });
|
|
||||||
});
|
|
||||||
|
|
||||||
router.delete("/delete-loan/:id", authenticateAdmin, async (req, res) => {
|
|
||||||
const loanId = req.params.id;
|
|
||||||
const result = await deleteLoanById(loanId);
|
|
||||||
if (result.success) {
|
|
||||||
return res.status(200).json({ message: "Loan deleted successfully" });
|
|
||||||
}
|
|
||||||
return res.status(500).json({ message: "Failed to delete loan" });
|
|
||||||
});
|
|
||||||
|
|
||||||
export default router;
|
|
||||||
@@ -1,123 +0,0 @@
|
|||||||
import express from "express";
|
|
||||||
import { authenticateAdmin } from "../../services/authentication.js";
|
|
||||||
const router = express.Router();
|
|
||||||
import dotenv from "dotenv";
|
|
||||||
dotenv.config();
|
|
||||||
|
|
||||||
// database funcs import
|
|
||||||
import {
|
|
||||||
createUser,
|
|
||||||
deleteUserById,
|
|
||||||
editUserById,
|
|
||||||
changePassword,
|
|
||||||
getAllUsers,
|
|
||||||
getUserById,
|
|
||||||
} from "./database/userDataMgmt.database.js";
|
|
||||||
|
|
||||||
router.post("/create-user", authenticateAdmin, async (req, res) => {
|
|
||||||
const username = req.body.username;
|
|
||||||
const role = req.body.role;
|
|
||||||
const password = req.body.password;
|
|
||||||
const isAdmin = req.body.isAdmin;
|
|
||||||
const email = req.body.email;
|
|
||||||
const first_name = req.body.first_name;
|
|
||||||
const last_name = req.body.last_name;
|
|
||||||
const result = await createUser(
|
|
||||||
username,
|
|
||||||
role,
|
|
||||||
password,
|
|
||||||
isAdmin,
|
|
||||||
email,
|
|
||||||
first_name,
|
|
||||||
last_name
|
|
||||||
);
|
|
||||||
if (result.success) {
|
|
||||||
return res.status(201).json({ message: "User created successfully" });
|
|
||||||
}
|
|
||||||
return res.status(500).json({ message: "Failed to create user" });
|
|
||||||
});
|
|
||||||
|
|
||||||
router.delete("/delete-user/:id", authenticateAdmin, async (req, res) => {
|
|
||||||
const userId = req.params.id;
|
|
||||||
const result = await deleteUserById(userId);
|
|
||||||
if (result.success) {
|
|
||||||
return res.status(200).json({ message: "User deleted successfully" });
|
|
||||||
}
|
|
||||||
return res.status(500).json({ message: "Failed to delete user" });
|
|
||||||
});
|
|
||||||
|
|
||||||
router.post("/edit-user/:id", authenticateAdmin, async (req, res) => {
|
|
||||||
const first_name = req.body.first_name;
|
|
||||||
const last_name = req.body.last_name;
|
|
||||||
const role = req.body.role;
|
|
||||||
const email = req.body.email;
|
|
||||||
const userId = req.params.id;
|
|
||||||
const is_admin = req.body.is_admin;
|
|
||||||
|
|
||||||
const result = await editUserById(
|
|
||||||
userId,
|
|
||||||
first_name,
|
|
||||||
last_name,
|
|
||||||
role,
|
|
||||||
email,
|
|
||||||
is_admin
|
|
||||||
);
|
|
||||||
|
|
||||||
if (result.success) {
|
|
||||||
return res.status(200).json({ message: "User edited successfully" });
|
|
||||||
}
|
|
||||||
return res.status(500).json({ message: "Failed to edit user" });
|
|
||||||
});
|
|
||||||
|
|
||||||
router.post("/change-password", authenticateAdmin, async (req, res) => {
|
|
||||||
const username = req.body.username;
|
|
||||||
const password = req.body.password;
|
|
||||||
|
|
||||||
const result = await changePassword(username, password);
|
|
||||||
|
|
||||||
if (result.success) {
|
|
||||||
return res.status(200).json({ message: "Password reset successfully" });
|
|
||||||
}
|
|
||||||
return res.status(500).json({ message: "Failed to reset password" });
|
|
||||||
});
|
|
||||||
|
|
||||||
router.post("/edit-user/:id", authenticateAdmin, async (req, res) => {
|
|
||||||
const userId = req.params.id;
|
|
||||||
const first_name = req.body.first_name;
|
|
||||||
const last_name = req.body.last_name;
|
|
||||||
const role = req.body.role;
|
|
||||||
const email = req.body.email;
|
|
||||||
const is_admin = req.body.is_admin;
|
|
||||||
|
|
||||||
const result = await editUserById(
|
|
||||||
userId,
|
|
||||||
first_name,
|
|
||||||
last_name,
|
|
||||||
role,
|
|
||||||
email,
|
|
||||||
is_admin
|
|
||||||
);
|
|
||||||
|
|
||||||
if (result.success) {
|
|
||||||
return res.status(200).json({ message: "User edited successfully" });
|
|
||||||
}
|
|
||||||
return res.status(500).json({ message: "Failed to edit user" });
|
|
||||||
});
|
|
||||||
|
|
||||||
router.get("/users", authenticateAdmin, async (req, res) => {
|
|
||||||
const result = await getAllUsers();
|
|
||||||
if (result.success) {
|
|
||||||
return res.status(200).json(result.data);
|
|
||||||
}
|
|
||||||
return res.status(500).json({ message: "Failed to retrieve users" });
|
|
||||||
});
|
|
||||||
|
|
||||||
router.get("/user/:id", authenticateAdmin, async (req, res) => {
|
|
||||||
const result = await getUserById(req.params.id);
|
|
||||||
if (result.success) {
|
|
||||||
return res.status(200).json({ user: result.data });
|
|
||||||
}
|
|
||||||
return res.status(500).json({ message: "Failed to retrieve user" });
|
|
||||||
});
|
|
||||||
|
|
||||||
export default router;
|
|
||||||
@@ -1,54 +0,0 @@
|
|||||||
import express from "express";
|
|
||||||
import {
|
|
||||||
generateToken,
|
|
||||||
authenticateAdmin,
|
|
||||||
} from "../../services/authentication.js";
|
|
||||||
const router = express.Router();
|
|
||||||
import dotenv from "dotenv";
|
|
||||||
dotenv.config();
|
|
||||||
|
|
||||||
// database funcs import
|
|
||||||
import { loginAdmin, executeQuery } from "./database/userMgmt.database.js";
|
|
||||||
|
|
||||||
router.post("/login", async (req, res) => {
|
|
||||||
const { username, password } = req.body || {};
|
|
||||||
if (!username || !password) {
|
|
||||||
return res.status(400).json({ message: "Missing username or password" });
|
|
||||||
}
|
|
||||||
|
|
||||||
const result = await loginAdmin(username, password);
|
|
||||||
|
|
||||||
if (result.success) {
|
|
||||||
const token = await generateToken({
|
|
||||||
username: result.data.username,
|
|
||||||
first_name: result.data.first_name,
|
|
||||||
last_name: result.data.last_name,
|
|
||||||
admin: result.data.is_admin,
|
|
||||||
});
|
|
||||||
return res.status(200).json({
|
|
||||||
message: "Login erfolgreich",
|
|
||||||
token,
|
|
||||||
first_name: result.data.first_name,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (result.reason === "not_admin") {
|
|
||||||
return res.status(403).json({ message: "Du bist kein Admin" });
|
|
||||||
}
|
|
||||||
|
|
||||||
return res.status(401).json({ message: "Ungültige Anmeldedaten" });
|
|
||||||
});
|
|
||||||
|
|
||||||
router.get("/verify-token", authenticateAdmin, async (req, res) => {
|
|
||||||
return res.status(200).json({ message: "Token is valid" });
|
|
||||||
});
|
|
||||||
|
|
||||||
router.post("/database-query", authenticateAdmin, async (req, res) => {
|
|
||||||
const query = req.body.query;
|
|
||||||
const password = req.body.password;
|
|
||||||
const username = req.body.username;
|
|
||||||
|
|
||||||
const result = await executeQuery(query, password, username);
|
|
||||||
});
|
|
||||||
|
|
||||||
export default router;
|
|
||||||
@@ -1,147 +0,0 @@
|
|||||||
import mysql from "mysql2";
|
|
||||||
import dotenv from "dotenv";
|
|
||||||
dotenv.config();
|
|
||||||
|
|
||||||
const pool = mysql
|
|
||||||
.createPool({
|
|
||||||
host: process.env.DB_HOST,
|
|
||||||
user: process.env.DB_USER,
|
|
||||||
password: process.env.DB_PASSWORD,
|
|
||||||
database: process.env.DB_NAME,
|
|
||||||
})
|
|
||||||
.promise();
|
|
||||||
|
|
||||||
export const getItemsFromDatabaseV2 = async () => {
|
|
||||||
const [rows] = await pool.query("SELECT * FROM items;");
|
|
||||||
if (rows.length > 0) {
|
|
||||||
return { success: true, data: rows };
|
|
||||||
}
|
|
||||||
return { success: false };
|
|
||||||
};
|
|
||||||
|
|
||||||
export const getLoanByCodeV2 = async (loan_code) => {
|
|
||||||
const [result] = await pool.query(
|
|
||||||
"SELECT username, returned_date, take_date, lockers FROM loans WHERE loan_code = ?;",
|
|
||||||
[loan_code],
|
|
||||||
);
|
|
||||||
if (result.length > 0) {
|
|
||||||
return { success: true, data: result[0] };
|
|
||||||
}
|
|
||||||
return { success: false };
|
|
||||||
};
|
|
||||||
|
|
||||||
export const changeInSafeStateV2 = async (itemId) => {
|
|
||||||
const [result] = await pool.query(
|
|
||||||
"UPDATE items SET in_safe = NOT in_safe WHERE id = ?",
|
|
||||||
[itemId],
|
|
||||||
);
|
|
||||||
if (result.affectedRows > 0) {
|
|
||||||
return { success: true };
|
|
||||||
}
|
|
||||||
return { success: false };
|
|
||||||
};
|
|
||||||
|
|
||||||
export const setReturnDateV2 = async (loanCode) => {
|
|
||||||
try {
|
|
||||||
const [items] = await pool.query(
|
|
||||||
"SELECT loaned_items_id, username FROM loans WHERE loan_code = ?",
|
|
||||||
[loanCode],
|
|
||||||
);
|
|
||||||
|
|
||||||
if (items.length === 0)
|
|
||||||
return { success: false, message: "No items found for loan" };
|
|
||||||
|
|
||||||
const itemIds = Array.isArray(items[0].loaned_items_id)
|
|
||||||
? items[0].loaned_items_id
|
|
||||||
: JSON.parse(items[0].loaned_items_id || "[]");
|
|
||||||
|
|
||||||
const [result] = await pool.query(
|
|
||||||
"UPDATE loans SET returned_date = NOW() WHERE loan_code = ? AND returned_date IS NULL",
|
|
||||||
[loanCode],
|
|
||||||
);
|
|
||||||
|
|
||||||
if (result.affectedRows === 0) return { success: false };
|
|
||||||
|
|
||||||
if (itemIds.length > 0) {
|
|
||||||
await pool.query(
|
|
||||||
"UPDATE items SET in_safe = 1, currently_borrowing = NULL, last_borrowed_person = ? WHERE id IN (?)",
|
|
||||||
[items[0].username, itemIds],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return { success: true, data: { returned: true } };
|
|
||||||
} catch (error) {
|
|
||||||
console.error("setReturnDateV2 error:", error);
|
|
||||||
return { success: false, message: "Failed to set return date" };
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
export const setTakeDateV2 = async (loanCode) => {
|
|
||||||
const [isTaken] = await pool.query(
|
|
||||||
"SELECT take_date FROM loans WHERE loan_code = ?",
|
|
||||||
[loanCode],
|
|
||||||
);
|
|
||||||
|
|
||||||
if (isTaken.length === 0 || isTaken[0].take_date !== null) {
|
|
||||||
return { success: false, message: "Loan not found or already taken" };
|
|
||||||
}
|
|
||||||
|
|
||||||
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, message: "No items found for loan" };
|
|
||||||
|
|
||||||
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 = ? AND take_date IS NULL",
|
|
||||||
[loanCode],
|
|
||||||
);
|
|
||||||
|
|
||||||
if (result.affectedRows > 0 && setItemStates.affectedRows > 0) {
|
|
||||||
return { success: true };
|
|
||||||
}
|
|
||||||
return { message: "Failed to set take date", success: false };
|
|
||||||
};
|
|
||||||
|
|
||||||
export const getAllLoansV2 = async () => {
|
|
||||||
const [result] = await pool.query("SELECT * FROM loans;");
|
|
||||||
if (result.length > 0) {
|
|
||||||
return { success: true, data: result };
|
|
||||||
}
|
|
||||||
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 };
|
|
||||||
};
|
|
||||||
@@ -1,95 +0,0 @@
|
|||||||
import express from "express";
|
|
||||||
import { authenticate } from "../../services/authentication.js";
|
|
||||||
const router = express.Router();
|
|
||||||
import dotenv from "dotenv";
|
|
||||||
dotenv.config();
|
|
||||||
|
|
||||||
import {
|
|
||||||
getItemsFromDatabaseV2,
|
|
||||||
changeInSafeStateV2,
|
|
||||||
setTakeDateV2,
|
|
||||||
setReturnDateV2,
|
|
||||||
getLoanByCodeV2,
|
|
||||||
openDoor,
|
|
||||||
} from "./api.database.js";
|
|
||||||
|
|
||||||
// Route for API to get all items from the database
|
|
||||||
router.get("/items/:key", authenticate, async (req, res) => {
|
|
||||||
const result = await getItemsFromDatabaseV2();
|
|
||||||
if (result.success) {
|
|
||||||
res.status(200).json({ data: result.data });
|
|
||||||
} else {
|
|
||||||
res.status(500).json({ message: "Failed to fetch items" });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Route for API to control the safe state of an item
|
|
||||||
router.post("/change-state/:key/:itemId", authenticate, async (req, res) => {
|
|
||||||
const itemId = req.params.itemId;
|
|
||||||
|
|
||||||
const result = await changeInSafeStateV2(itemId);
|
|
||||||
if (result.success) {
|
|
||||||
res.status(200).json({ data: result.data });
|
|
||||||
} else {
|
|
||||||
res.status(500).json({ message: "Failed to update item state" });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Route for API to get a loan by its code
|
|
||||||
router.get(
|
|
||||||
"/get-loan-by-code/:key/:loan_code",
|
|
||||||
authenticate,
|
|
||||||
async (req, res) => {
|
|
||||||
const loan_code = req.params.loan_code;
|
|
||||||
const result = await getLoanByCodeV2(loan_code);
|
|
||||||
if (result.success) {
|
|
||||||
res.status(200).json({ data: result.data });
|
|
||||||
} else {
|
|
||||||
res.status(404).json({ message: "Loan not found" });
|
|
||||||
}
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
// Route for API to set the return date by the loan code
|
|
||||||
router.post(
|
|
||||||
"/set-return-date/:key/:loan_code",
|
|
||||||
authenticate,
|
|
||||||
async (req, res) => {
|
|
||||||
const loanCode = req.params.loan_code;
|
|
||||||
const result = await setReturnDateV2(loanCode);
|
|
||||||
if (result.success) {
|
|
||||||
res.status(200).json({});
|
|
||||||
} else {
|
|
||||||
res.status(500).json({ message: "Failed to set return date" });
|
|
||||||
}
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
// Route for API to set the take away date by the loan code
|
|
||||||
router.post(
|
|
||||||
"/set-take-date/:key/:loan_code",
|
|
||||||
authenticate,
|
|
||||||
async (req, res) => {
|
|
||||||
const loanCode = req.params.loan_code;
|
|
||||||
const result = await setTakeDateV2(loanCode);
|
|
||||||
if (result.success) {
|
|
||||||
res.status(200).json({});
|
|
||||||
} else {
|
|
||||||
res.status(500).json({ message: result.message });
|
|
||||||
}
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
// 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;
|
|
||||||
@@ -1,328 +0,0 @@
|
|||||||
import mysql from "mysql2";
|
|
||||||
import dotenv from "dotenv";
|
|
||||||
dotenv.config();
|
|
||||||
|
|
||||||
const pool = mysql
|
|
||||||
.createPool({
|
|
||||||
host: process.env.DB_HOST,
|
|
||||||
user: process.env.DB_USER,
|
|
||||||
password: process.env.DB_PASSWORD,
|
|
||||||
database: process.env.DB_NAME,
|
|
||||||
})
|
|
||||||
.promise();
|
|
||||||
|
|
||||||
export const createLoanInDatabase = async (
|
|
||||||
username,
|
|
||||||
startDate,
|
|
||||||
endDate,
|
|
||||||
note,
|
|
||||||
itemIds,
|
|
||||||
) => {
|
|
||||||
if (!username)
|
|
||||||
return { success: false, code: "BAD_REQUEST", message: "Missing username" };
|
|
||||||
if (!Array.isArray(itemIds) || itemIds.length === 0)
|
|
||||||
return {
|
|
||||||
success: false,
|
|
||||||
code: "BAD_REQUEST",
|
|
||||||
message: "No items provided",
|
|
||||||
};
|
|
||||||
if (!startDate || !endDate)
|
|
||||||
return { success: false, code: "BAD_REQUEST", message: "Missing dates" };
|
|
||||||
|
|
||||||
const start = new Date(startDate);
|
|
||||||
const end = new Date(endDate);
|
|
||||||
if (
|
|
||||||
!(start instanceof Date) ||
|
|
||||||
isNaN(start.getTime()) ||
|
|
||||||
!(end instanceof Date) ||
|
|
||||||
isNaN(end.getTime()) ||
|
|
||||||
start >= end
|
|
||||||
) {
|
|
||||||
return {
|
|
||||||
success: false,
|
|
||||||
code: "BAD_REQUEST",
|
|
||||||
message: "Invalid date range",
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
const conn = await pool.getConnection();
|
|
||||||
try {
|
|
||||||
await conn.beginTransaction();
|
|
||||||
|
|
||||||
// Ensure all items exist and collect names + lockers
|
|
||||||
const [itemsRows] = await conn.query(
|
|
||||||
"SELECT id, item_name, safe_nr FROM items WHERE id IN (?)",
|
|
||||||
[itemIds],
|
|
||||||
);
|
|
||||||
if (!itemsRows || itemsRows.length !== itemIds.length) {
|
|
||||||
await conn.rollback();
|
|
||||||
return {
|
|
||||||
success: false,
|
|
||||||
code: "BAD_REQUEST",
|
|
||||||
message: "One or more items not found",
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
const itemNames = itemIds
|
|
||||||
.map(
|
|
||||||
(id) => itemsRows.find((r) => Number(r.id) === Number(id))?.item_name,
|
|
||||||
)
|
|
||||||
.filter(Boolean);
|
|
||||||
|
|
||||||
// Build lockers array (unique, only 2-digit numbers from safe_nr)
|
|
||||||
const lockers = [
|
|
||||||
...new Set(
|
|
||||||
itemsRows
|
|
||||||
.map((r) => r.safe_nr)
|
|
||||||
.filter(
|
|
||||||
(sn) =>
|
|
||||||
sn !== null &&
|
|
||||||
sn !== undefined &&
|
|
||||||
Number.isInteger(Number(sn)) &&
|
|
||||||
Number(sn) >= 0 &&
|
|
||||||
Number(sn) <= 99,
|
|
||||||
)
|
|
||||||
.map((sn) => Number(sn)),
|
|
||||||
),
|
|
||||||
];
|
|
||||||
|
|
||||||
// Check availability (no overlap with existing loans)
|
|
||||||
const [confRows] = await conn.query(
|
|
||||||
`
|
|
||||||
SELECT COUNT(*) AS conflicts
|
|
||||||
FROM loans l
|
|
||||||
JOIN JSON_TABLE(l.loaned_items_id, '$[*]' COLUMNS (item_id INT PATH '$')) jt
|
|
||||||
ON TRUE
|
|
||||||
WHERE jt.item_id IN (?)
|
|
||||||
AND l.deleted = 0
|
|
||||||
AND l.start_date < ?
|
|
||||||
AND COALESCE(l.returned_date, l.end_date) > ?
|
|
||||||
`,
|
|
||||||
[itemIds, end, start],
|
|
||||||
);
|
|
||||||
if (confRows?.[0]?.conflicts > 0) {
|
|
||||||
await conn.rollback();
|
|
||||||
return {
|
|
||||||
success: false,
|
|
||||||
code: "CONFLICT",
|
|
||||||
message: "One or more items are not available in the selected period",
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// Generate unique loan_code (retry a few times)
|
|
||||||
let loanCode = null;
|
|
||||||
for (let i = 0; i < 6; i++) {
|
|
||||||
const candidate = Math.floor(100000 + Math.random() * 899999); // 6 digits
|
|
||||||
const [exists] = await conn.query(
|
|
||||||
"SELECT 1 FROM loans WHERE loan_code = ? LIMIT 1",
|
|
||||||
[candidate],
|
|
||||||
);
|
|
||||||
if (exists.length === 0) {
|
|
||||||
loanCode = candidate;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!loanCode) {
|
|
||||||
await conn.rollback();
|
|
||||||
return {
|
|
||||||
success: false,
|
|
||||||
code: "SERVER_ERROR",
|
|
||||||
message: "Failed to generate unique loan code",
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// Insert loan (now includes lockers)
|
|
||||||
const [insertRes] = await conn.query(
|
|
||||||
`
|
|
||||||
INSERT INTO loans (username, loan_code, start_date, end_date, lockers, loaned_items_id, loaned_items_name, note)
|
|
||||||
VALUES (?, ?, ?, ?, CAST(? AS JSON), CAST(? AS JSON), CAST(? AS JSON), ?)
|
|
||||||
`,
|
|
||||||
[
|
|
||||||
username,
|
|
||||||
loanCode,
|
|
||||||
new Date(start).toISOString().slice(0, 19).replace("T", " "),
|
|
||||||
new Date(end).toISOString().slice(0, 19).replace("T", " "),
|
|
||||||
JSON.stringify(lockers),
|
|
||||||
JSON.stringify(itemIds.map((n) => Number(n))),
|
|
||||||
JSON.stringify(itemNames),
|
|
||||||
note,
|
|
||||||
],
|
|
||||||
);
|
|
||||||
|
|
||||||
await conn.commit();
|
|
||||||
return {
|
|
||||||
success: true,
|
|
||||||
data: {
|
|
||||||
id: insertRes.insertId,
|
|
||||||
loan_code: loanCode,
|
|
||||||
username,
|
|
||||||
start_date: start,
|
|
||||||
end_date: end,
|
|
||||||
items: itemIds,
|
|
||||||
item_names: itemNames,
|
|
||||||
lockers,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
} catch (err) {
|
|
||||||
await conn.rollback();
|
|
||||||
console.error("createLoanInDatabase error:", err);
|
|
||||||
return {
|
|
||||||
success: false,
|
|
||||||
code: "SERVER_ERROR",
|
|
||||||
message: "Failed to create loan",
|
|
||||||
};
|
|
||||||
} finally {
|
|
||||||
conn.release();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
export const getLoanInfoWithID = async (loanId) => {
|
|
||||||
const [rows] = await pool.query("SELECT * FROM loans WHERE id = ?;", [
|
|
||||||
loanId,
|
|
||||||
]);
|
|
||||||
if (rows.length > 0) {
|
|
||||||
return { success: true, data: rows[0] };
|
|
||||||
}
|
|
||||||
return { success: false };
|
|
||||||
};
|
|
||||||
|
|
||||||
export const getLoansFromDatabase = async (username) => {
|
|
||||||
const [result] = await pool.query(
|
|
||||||
"SELECT * FROM loans WHERE username = ? AND deleted = 0;",
|
|
||||||
[username],
|
|
||||||
);
|
|
||||||
if (result.length > 0) {
|
|
||||||
return { success: true, status: true, data: result };
|
|
||||||
} else if (result.length === 0) {
|
|
||||||
return { success: true, status: true, data: [] };
|
|
||||||
}
|
|
||||||
return { success: false };
|
|
||||||
};
|
|
||||||
|
|
||||||
export const getBorrowableItemsFromDatabase = async (
|
|
||||||
startDate,
|
|
||||||
endDate,
|
|
||||||
role = 0,
|
|
||||||
) => {
|
|
||||||
// Overlap if: loan.start < end AND effective_end > start
|
|
||||||
// effective_end is returned_date if set, otherwise end_date
|
|
||||||
const hasRoleFilter = Number(role) > 0;
|
|
||||||
|
|
||||||
const sql = `
|
|
||||||
SELECT i.*
|
|
||||||
FROM items i
|
|
||||||
WHERE ${hasRoleFilter ? "i.can_borrow_role >= ? AND " : ""}NOT EXISTS (
|
|
||||||
SELECT 1
|
|
||||||
FROM loans l
|
|
||||||
JOIN JSON_TABLE(l.loaned_items_id, '$[*]' COLUMNS (item_id INT PATH '$')) jt
|
|
||||||
WHERE jt.item_id = i.id
|
|
||||||
AND l.deleted = 0
|
|
||||||
AND l.start_date < ?
|
|
||||||
AND COALESCE(l.returned_date, l.end_date) > ?
|
|
||||||
);
|
|
||||||
`;
|
|
||||||
|
|
||||||
const params = hasRoleFilter
|
|
||||||
? [role, endDate, startDate]
|
|
||||||
: [endDate, startDate];
|
|
||||||
|
|
||||||
const [rows] = await pool.query(sql, params);
|
|
||||||
if (rows.length > 0) {
|
|
||||||
return { success: true, data: rows };
|
|
||||||
}
|
|
||||||
return { success: false };
|
|
||||||
};
|
|
||||||
|
|
||||||
export const SETdeleteLoanFromDatabase = async (loanId) => {
|
|
||||||
const [result] = await pool.query(
|
|
||||||
"UPDATE loans SET deleted = 1 WHERE id = ?;",
|
|
||||||
[loanId],
|
|
||||||
);
|
|
||||||
if (result.affectedRows > 0) {
|
|
||||||
return { success: true };
|
|
||||||
} else {
|
|
||||||
return { success: false };
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
export const getALLLoans = async () => {
|
|
||||||
const [result] = await pool.query("SELECT * FROM loans WHERE deleted = 0;");
|
|
||||||
if (result.length > 0) {
|
|
||||||
return { success: true, data: result };
|
|
||||||
}
|
|
||||||
return { success: false };
|
|
||||||
};
|
|
||||||
|
|
||||||
export const getItems = async () => {
|
|
||||||
const [result] = await pool.query("SELECT * FROM items;");
|
|
||||||
if (result.length > 0) {
|
|
||||||
return { success: true, data: result };
|
|
||||||
}
|
|
||||||
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 };
|
|
||||||
};
|
|
||||||
@@ -1,55 +0,0 @@
|
|||||||
import mysql from "mysql2";
|
|
||||||
import dotenv from "dotenv";
|
|
||||||
dotenv.config();
|
|
||||||
|
|
||||||
const pool = mysql
|
|
||||||
.createPool({
|
|
||||||
host: process.env.DB_HOST,
|
|
||||||
user: process.env.DB_USER,
|
|
||||||
password: process.env.DB_PASSWORD,
|
|
||||||
database: process.env.DB_NAME,
|
|
||||||
})
|
|
||||||
.promise();
|
|
||||||
|
|
||||||
export const loginFunc = async (username, password) => {
|
|
||||||
const [result] = await pool.query(
|
|
||||||
"SELECT * FROM users WHERE username = ? AND password = ?",
|
|
||||||
[username, password]
|
|
||||||
);
|
|
||||||
if (result.length > 0) return { success: true, data: result[0] };
|
|
||||||
return { success: false };
|
|
||||||
};
|
|
||||||
|
|
||||||
export const getItems = async () => {
|
|
||||||
const [rows] = await pool.query("SELECT * FROM items;");
|
|
||||||
if (rows.length > 0) {
|
|
||||||
return { success: true, data: rows };
|
|
||||||
}
|
|
||||||
return { success: false };
|
|
||||||
};
|
|
||||||
|
|
||||||
export const getALLLoans = async () => {
|
|
||||||
const [rows] = await pool.query("SELECT * FROM loans;");
|
|
||||||
if (rows.length > 0) {
|
|
||||||
return { success: true, data: rows };
|
|
||||||
}
|
|
||||||
return { success: false };
|
|
||||||
};
|
|
||||||
|
|
||||||
export const changePassword = async (username, oldPassword, newPassword) => {
|
|
||||||
// get user current password
|
|
||||||
const [user] = await pool.query(
|
|
||||||
"SELECT * FROM users WHERE username = ? AND password = ?",
|
|
||||||
[username, oldPassword]
|
|
||||||
);
|
|
||||||
if (user.length === 0) return { success: false };
|
|
||||||
|
|
||||||
// update password
|
|
||||||
|
|
||||||
const [result] = await pool.query(
|
|
||||||
"UPDATE users SET password = ? WHERE username = ?",
|
|
||||||
[newPassword, username]
|
|
||||||
);
|
|
||||||
if (result.affectedRows > 0) return { success: true };
|
|
||||||
return { success: false };
|
|
||||||
};
|
|
||||||
@@ -1,173 +0,0 @@
|
|||||||
import express from "express";
|
|
||||||
import { authenticate, generateToken } from "../../services/authentication.js";
|
|
||||||
const router = express.Router();
|
|
||||||
import dotenv from "dotenv";
|
|
||||||
dotenv.config();
|
|
||||||
|
|
||||||
// database funcs import
|
|
||||||
import {
|
|
||||||
createLoanInDatabase,
|
|
||||||
getLoanInfoWithID,
|
|
||||||
getLoansFromDatabase,
|
|
||||||
getBorrowableItemsFromDatabase,
|
|
||||||
getALLLoans,
|
|
||||||
getItems,
|
|
||||||
SETdeleteLoanFromDatabase,
|
|
||||||
setReturnDate,
|
|
||||||
setTakeDate,
|
|
||||||
} from "./database/loansMgmt.database.js";
|
|
||||||
import { sendMailLoan } from "./services/mailer.js";
|
|
||||||
|
|
||||||
router.post("/createLoan", authenticate, async (req, res) => {
|
|
||||||
try {
|
|
||||||
const { items, startDate, endDate, note } = req.body || {};
|
|
||||||
|
|
||||||
if (!Array.isArray(items) || items.length === 0) {
|
|
||||||
return res.status(400).json({ message: "Items array is required" });
|
|
||||||
}
|
|
||||||
|
|
||||||
// If dates are not provided, default to now .. +7 days
|
|
||||||
const start =
|
|
||||||
startDate ?? new Date().toISOString().slice(0, 19).replace("T", " ");
|
|
||||||
const end =
|
|
||||||
endDate ??
|
|
||||||
new Date(Date.now() + 7 * 24 * 60 * 60 * 1000)
|
|
||||||
.toISOString()
|
|
||||||
.slice(0, 19)
|
|
||||||
.replace("T", " ");
|
|
||||||
|
|
||||||
// Coerce item IDs to numbers and filter invalids
|
|
||||||
const itemIds = items
|
|
||||||
.map((v) => Number(v))
|
|
||||||
.filter((n) => Number.isFinite(n));
|
|
||||||
|
|
||||||
if (itemIds.length === 0) {
|
|
||||||
return res.status(400).json({ message: "No valid item IDs provided" });
|
|
||||||
}
|
|
||||||
|
|
||||||
const result = await createLoanInDatabase(
|
|
||||||
req.user.username,
|
|
||||||
start,
|
|
||||||
end,
|
|
||||||
note,
|
|
||||||
itemIds,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (result.success) {
|
|
||||||
const mailInfo = await getLoanInfoWithID(result.data.id);
|
|
||||||
console.log(mailInfo);
|
|
||||||
sendMailLoan(
|
|
||||||
mailInfo.data.username,
|
|
||||||
mailInfo.data.loaned_items_name,
|
|
||||||
mailInfo.data.start_date,
|
|
||||||
mailInfo.data.end_date,
|
|
||||||
mailInfo.data.created_at,
|
|
||||||
mailInfo.data.note,
|
|
||||||
);
|
|
||||||
return res.status(201).json({
|
|
||||||
message: "Loan created successfully",
|
|
||||||
loanId: result.data.id,
|
|
||||||
loanCode: result.data.loan_code,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (result.code === "CONFLICT") {
|
|
||||||
return res
|
|
||||||
.status(409)
|
|
||||||
.json({ message: "Items not available in the selected period" });
|
|
||||||
}
|
|
||||||
|
|
||||||
if (result.code === "BAD_REQUEST") {
|
|
||||||
return res.status(400).json({ message: result.message });
|
|
||||||
}
|
|
||||||
|
|
||||||
return res.status(500).json({ message: "Failed to create loan" });
|
|
||||||
} catch (err) {
|
|
||||||
console.error("createLoan error:", err);
|
|
||||||
return res.status(500).json({ message: "Failed to create loan" });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
router.get("/loans", authenticate, async (req, res) => {
|
|
||||||
const result = await getLoansFromDatabase(req.user.username);
|
|
||||||
if (result.success) {
|
|
||||||
res.status(200).json(result.data);
|
|
||||||
} else if (result.status) {
|
|
||||||
res.status(200).json([]);
|
|
||||||
} else {
|
|
||||||
res.status(500).json({ message: "Failed to fetch loans" });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
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) => {
|
|
||||||
const result = await getItems();
|
|
||||||
if (result.success) {
|
|
||||||
res.status(200).json(result.data);
|
|
||||||
} else {
|
|
||||||
res.status(500).json({ message: "Failed to fetch items" });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
router.delete("/delete-loan/:id", authenticate, async (req, res) => {
|
|
||||||
const loanId = req.params.id;
|
|
||||||
const result = await SETdeleteLoanFromDatabase(loanId);
|
|
||||||
if (result.success) {
|
|
||||||
res.status(200).json({ message: "Loan deleted successfully" });
|
|
||||||
} else {
|
|
||||||
res.status(500).json({ message: "Failed to delete loan" });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
router.get("/all-loans", authenticate, async (req, res) => {
|
|
||||||
const result = await getALLLoans();
|
|
||||||
if (result.success) {
|
|
||||||
res.status(200).json(result.data);
|
|
||||||
} else {
|
|
||||||
res.status(500).json({ message: "Failed to fetch loans" });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
router.post("/borrowable-items", authenticate, async (req, res) => {
|
|
||||||
const { startDate, endDate } = req.body || {};
|
|
||||||
if (!startDate || !endDate) {
|
|
||||||
return res
|
|
||||||
.status(400)
|
|
||||||
.json({ message: "startDate and endDate are required" });
|
|
||||||
}
|
|
||||||
|
|
||||||
const result = await getBorrowableItemsFromDatabase(
|
|
||||||
startDate,
|
|
||||||
endDate,
|
|
||||||
req.user.role,
|
|
||||||
);
|
|
||||||
if (result.success) {
|
|
||||||
// return the array directly for consistency with /items
|
|
||||||
return res.status(200).json(result.data);
|
|
||||||
} else {
|
|
||||||
return res
|
|
||||||
.status(500)
|
|
||||||
.json({ message: "Failed to fetch borrowable items" });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
export default router;
|
|
||||||
@@ -1,215 +0,0 @@
|
|||||||
import nodemailer from "nodemailer";
|
|
||||||
import dotenv from "dotenv";
|
|
||||||
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,
|
|
||||||
note,
|
|
||||||
}) {
|
|
||||||
const brand = process.env.MAIL_BRAND_COLOR || "#0ea5e9";
|
|
||||||
const itemsList =
|
|
||||||
Array.isArray(items) && items.length
|
|
||||||
? `<ul style="margin:4px 0 0 18px; padding:0;">${items
|
|
||||||
.map(
|
|
||||||
(i) =>
|
|
||||||
`<li style="margin:2px 0; color:#111827; line-height:1.3;">${i}</li>`,
|
|
||||||
)
|
|
||||||
.join("")}</ul>`
|
|
||||||
: "<span style='color:#111827;'>N/A</span>";
|
|
||||||
|
|
||||||
return `<!doctype html>
|
|
||||||
<html lang="de">
|
|
||||||
<head>
|
|
||||||
<meta charset="utf-8">
|
|
||||||
<meta name="color-scheme" content="light">
|
|
||||||
<meta name="supported-color-schemes" content="light">
|
|
||||||
<meta name="x-apple-disable-message-reformatting">
|
|
||||||
<meta name="viewport" content="width=device-width,initial-scale=1">
|
|
||||||
<style>
|
|
||||||
:root { color-scheme: light; supported-color-schemes: light; }
|
|
||||||
body { margin:0; padding:0; }
|
|
||||||
/* Mobile stacking */
|
|
||||||
@media (max-width:480px) {
|
|
||||||
.outer { width:100% !important; }
|
|
||||||
.pad-sm { padding:16px !important; }
|
|
||||||
.w-label { width:120px !important; }
|
|
||||||
}
|
|
||||||
/* Dark-mode override safety */
|
|
||||||
@media (prefers-color-scheme: dark) {
|
|
||||||
body, table, td, p, a, h1, h2, h3 { background:#ffffff !important; color:#111827 !important; }
|
|
||||||
.brand-header { background:${brand} !important; color:#ffffff !important; }
|
|
||||||
a { color:${brand} !important; }
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
</head>
|
|
||||||
<body bgcolor="#ffffff" style="background:#ffffff; font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,Arial,sans-serif; color:#111827; -webkit-text-size-adjust:100%;">
|
|
||||||
<!-- Preheader (hidden) -->
|
|
||||||
<div style="display:none; max-height:0; overflow:hidden; opacity:0; mso-hide:all;">
|
|
||||||
Neue Ausleihe erstellt – Übersicht der Buchung.
|
|
||||||
</div>
|
|
||||||
<div role="article" aria-roledescription="email" lang="de" style="padding:24px; background:#f2f4f7;">
|
|
||||||
<table role="presentation" cellpadding="0" cellspacing="0" width="100%" class="outer" style="max-width:600px; margin:0 auto; background:#ffffff; border:1px solid #e5e7eb; border-radius:14px; overflow:hidden;">
|
|
||||||
<tr>
|
|
||||||
<td class="brand-header" style="padding:22px 26px; background:${brand}; color:#ffffff;">
|
|
||||||
<h1 style="margin:0; font-size:18px; line-height:1.35; font-weight:600;">Neue Ausleihe erstellt</h1>
|
|
||||||
</td>
|
|
||||||
</tr>
|
|
||||||
<tr>
|
|
||||||
<td class="pad-sm" style="padding:24px 26px; color:#111827;">
|
|
||||||
<p style="margin:0 0 14px 0; line-height:1.4;">Es wurde eine neue Ausleihe angelegt. Hier sind die Details:</p>
|
|
||||||
<table role="presentation" cellpadding="0" cellspacing="0" width="100%" style="border-collapse:collapse; font-size:14px; line-height:1.3; background:#fcfcfd; border:1px solid #e5e7eb; border-radius:10px; overflow:hidden;">
|
|
||||||
<tbody>
|
|
||||||
<tr>
|
|
||||||
<td class="w-label" style="padding:10px 14px; color:#6b7280; width:170px; border-bottom:1px solid #ececec;">Benutzer</td>
|
|
||||||
<td style="padding:10px 14px; font-weight:600; border-bottom:1px solid #ececec; color:#111827;">${
|
|
||||||
user || "N/A"
|
|
||||||
}</td>
|
|
||||||
</tr>
|
|
||||||
<tr>
|
|
||||||
<td style="padding:10px 14px; color:#6b7280; vertical-align:top; border-bottom:1px solid #ececec;">Ausgeliehene Gegenstände</td>
|
|
||||||
<td style="padding:10px 14px; font-weight:600; border-bottom:1px solid #ececec; color:#111827;">${itemsList}</td>
|
|
||||||
</tr>
|
|
||||||
<tr>
|
|
||||||
<td style="padding:10px 14px; color:#6b7280; border-bottom:1px solid #ececec;">Startdatum</td>
|
|
||||||
<td style="padding:10px 14px; font-weight:600; border-bottom:1px solid #ececec; color:#111827;">${formatDateTime(
|
|
||||||
startDate,
|
|
||||||
)}</td>
|
|
||||||
</tr>
|
|
||||||
<tr>
|
|
||||||
<td style="padding:10px 14px; color:#6b7280; border-bottom:1px solid #ececec;">Enddatum</td>
|
|
||||||
<td style="padding:10px 14px; font-weight:600; border-bottom:1px solid #ececec; color:#111827;">${formatDateTime(
|
|
||||||
endDate,
|
|
||||||
)}</td>
|
|
||||||
</tr>
|
|
||||||
<tr>
|
|
||||||
<td style="padding:10px 14px; color:#6b7280;">Erstellt am</td>
|
|
||||||
<td style="padding:10px 14px; font-weight:600; color:#111827;">${formatDateTime(
|
|
||||||
createdDate,
|
|
||||||
)}</td>
|
|
||||||
</tr>
|
|
||||||
<tr>
|
|
||||||
<td style="padding:10px 14px; color:#6b7280; vertical-align:top;">Notiz</td>
|
|
||||||
<td style="padding:10px 14px; font-weight:600; color:#111827;">${
|
|
||||||
note || "Keine Notiz"
|
|
||||||
}</td>
|
|
||||||
</tr>
|
|
||||||
</tbody>
|
|
||||||
</table>
|
|
||||||
<p style="margin:22px 0 0 0; font-size:14px;">
|
|
||||||
<a href="https://admin.insta.the1s.de/api" style="display:inline-block; background:${brand}; color:#ffffff; text-decoration:none; padding:10px 16px; border-radius:6px; font-weight:600; font-size:14px;" target="_blank" rel="noopener noreferrer">
|
|
||||||
Übersicht öffnen
|
|
||||||
</a>
|
|
||||||
</p>
|
|
||||||
<p style="margin:18px 0 0 0; font-size:12px; color:#6b7280; line-height:1.4;">
|
|
||||||
Diese E-Mail wurde automatisch vom Ausleihsystem gesendet. Bitte nicht antworten.
|
|
||||||
</p>
|
|
||||||
</td>
|
|
||||||
</tr>
|
|
||||||
</table>
|
|
||||||
</div>
|
|
||||||
</body>
|
|
||||||
</html>`;
|
|
||||||
}
|
|
||||||
|
|
||||||
function buildLoanEmailText({
|
|
||||||
user,
|
|
||||||
items,
|
|
||||||
startDate,
|
|
||||||
endDate,
|
|
||||||
createdDate,
|
|
||||||
note,
|
|
||||||
}) {
|
|
||||||
const itemsText =
|
|
||||||
Array.isArray(items) && items.length ? items.join(", ") : "N/A";
|
|
||||||
return [
|
|
||||||
"Neue Ausleihe erstellt",
|
|
||||||
"",
|
|
||||||
`Benutzer: ${user || "N/A"}`,
|
|
||||||
`Gegenstände: ${itemsText}`,
|
|
||||||
`Start: ${formatDateTime(startDate)}`,
|
|
||||||
`Ende: ${formatDateTime(endDate)}`,
|
|
||||||
`Erstellt am: ${formatDateTime(createdDate)}`,
|
|
||||||
`Notiz: ${note || "Keine Notiz"}`,
|
|
||||||
].join("\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
export function sendMailLoan(
|
|
||||||
user,
|
|
||||||
items,
|
|
||||||
startDate,
|
|
||||||
endDate,
|
|
||||||
createdDate,
|
|
||||||
note,
|
|
||||||
) {
|
|
||||||
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 info = await transporter.sendMail({
|
|
||||||
from: '"Ausleihsystem" <noreply@mcs-medien.de>',
|
|
||||||
to: process.env.MAIL_SENDEES,
|
|
||||||
subject: "Eine neue Ausleihe wurde erstellt!",
|
|
||||||
text: buildLoanEmailText({
|
|
||||||
user,
|
|
||||||
items,
|
|
||||||
startDate,
|
|
||||||
endDate,
|
|
||||||
createdDate,
|
|
||||||
note,
|
|
||||||
}),
|
|
||||||
html: buildLoanEmail({
|
|
||||||
user,
|
|
||||||
items,
|
|
||||||
startDate,
|
|
||||||
endDate,
|
|
||||||
createdDate,
|
|
||||||
note,
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
|
|
||||||
console.log("Loan message sent:", info.messageId);
|
|
||||||
})();
|
|
||||||
}
|
|
||||||
@@ -1,43 +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,
|
|
||||||
});
|
|
||||||
|
|
||||||
console.log("Contact message sent: %s", info.messageId);
|
|
||||||
})();
|
|
||||||
}
|
|
||||||
@@ -1,48 +0,0 @@
|
|||||||
import express from "express";
|
|
||||||
import { authenticate, generateToken } from "../../services/authentication.js";
|
|
||||||
const router = express.Router();
|
|
||||||
import dotenv from "dotenv";
|
|
||||||
dotenv.config();
|
|
||||||
|
|
||||||
// database funcs import
|
|
||||||
import { loginFunc, changePassword } from "./database/userMgmt.database.js";
|
|
||||||
import { sendMail } from "./services/mailer_v2.js";
|
|
||||||
|
|
||||||
router.post("/login", async (req, res) => {
|
|
||||||
const result = await loginFunc(req.body.username, req.body.password);
|
|
||||||
if (result.success) {
|
|
||||||
const token = await generateToken({
|
|
||||||
username: result.data.username,
|
|
||||||
is_admin: result.data.is_admin,
|
|
||||||
first_name: result.data.first_name,
|
|
||||||
last_name: result.data.last_name,
|
|
||||||
role: result.data.role,
|
|
||||||
});
|
|
||||||
res.status(200).json({ message: "Login successful", token });
|
|
||||||
} else {
|
|
||||||
res.status(401).json({ message: "Invalid credentials" });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
router.post("/change-password", authenticate, async (req, res) => {
|
|
||||||
const oldPassword = req.body.oldPassword;
|
|
||||||
const newPassword = req.body.newPassword;
|
|
||||||
const username = req.user.username;
|
|
||||||
const result = await changePassword(username, oldPassword, newPassword);
|
|
||||||
if (result.success) {
|
|
||||||
res.status(200).json({ message: "Password changed successfully" });
|
|
||||||
} else {
|
|
||||||
res.status(500).json({ message: "Failed to change password" });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
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;
|
|
||||||
@@ -1,57 +0,0 @@
|
|||||||
use borrow_system_new;
|
|
||||||
|
|
||||||
CREATE TABLE users (
|
|
||||||
id int NOT NULL AUTO_INCREMENT,
|
|
||||||
username varchar(100) NOT NULL UNIQUE,
|
|
||||||
password varchar(255) NOT NULL,
|
|
||||||
email varchar(255) NOT NULL,
|
|
||||||
first_name varchar(255) NOT NULL,
|
|
||||||
last_name varchar(255) NOT NULL,
|
|
||||||
role int NOT NULL,
|
|
||||||
is_admin bool NOT NULL DEFAULT false,
|
|
||||||
entry_created_at timestamp NULL DEFAULT CURRENT_TIMESTAMP,
|
|
||||||
entry_updated_at timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
|
||||||
PRIMARY KEY (id)
|
|
||||||
) ENGINE=InnoDB;
|
|
||||||
|
|
||||||
CREATE TABLE loans (
|
|
||||||
id int NOT NULL AUTO_INCREMENT,
|
|
||||||
username varchar(100) NOT NULL,
|
|
||||||
lockers json NOT NULL DEFAULT ('[]'),
|
|
||||||
loan_code Char(6) NOT NULL UNIQUE,
|
|
||||||
start_date timestamp NOT NULL,
|
|
||||||
end_date timestamp NOT NULL,
|
|
||||||
take_date timestamp NULL DEFAULT NULL,
|
|
||||||
returned_date timestamp NULL DEFAULT NULL,
|
|
||||||
created_at timestamp NULL DEFAULT CURRENT_TIMESTAMP,
|
|
||||||
loaned_items_id json NOT NULL DEFAULT ('[]'),
|
|
||||||
loaned_items_name json NOT NULL DEFAULT ('[]'),
|
|
||||||
deleted bool NOT NULL DEFAULT false,
|
|
||||||
note varchar(500) DEFAULT NULL,
|
|
||||||
PRIMARY KEY (id),
|
|
||||||
CHECK (loan_code REGEXP '^[0-9]{6}$')
|
|
||||||
) ENGINE=InnoDB;
|
|
||||||
|
|
||||||
CREATE TABLE items (
|
|
||||||
id int NOT NULL AUTO_INCREMENT,
|
|
||||||
item_name varchar(255) NOT NULL UNIQUE,
|
|
||||||
can_borrow_role INT NOT NULL,
|
|
||||||
in_safe bool NOT NULL DEFAULT true,
|
|
||||||
safe_nr INT DEFAULT NULL UNIQUE,
|
|
||||||
door_key INT DEFAULT NULL UNIQUE,
|
|
||||||
entry_created_at timestamp NULL DEFAULT CURRENT_TIMESTAMP,
|
|
||||||
entry_updated_at timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
|
||||||
last_borrowed_person varchar(255) DEFAULT NULL,
|
|
||||||
currently_borrowing varchar(255) DEFAULT NULL,
|
|
||||||
PRIMARY KEY (id)
|
|
||||||
) ENGINE=InnoDB;
|
|
||||||
|
|
||||||
CREATE TABLE apiKeys (
|
|
||||||
id INT NOT NULL AUTO_INCREMENT,
|
|
||||||
api_key CHAR(8) NOT NULL UNIQUE,
|
|
||||||
entry_name VARCHAR(100) NOT NULL,
|
|
||||||
last_used_at TIMESTAMP NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
|
|
||||||
entry_created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
|
|
||||||
PRIMARY KEY (id),
|
|
||||||
CHECK (api_key REGEXP '^[0-9]{8}$')
|
|
||||||
) ENGINE=InnoDB;
|
|
||||||
@@ -1,62 +0,0 @@
|
|||||||
import express from "express";
|
|
||||||
import cors from "cors";
|
|
||||||
import env from "dotenv";
|
|
||||||
import info from "./info.json" assert { type: "json" };
|
|
||||||
import { authenticate } from "./services/authentication.js";
|
|
||||||
|
|
||||||
// frontend routes
|
|
||||||
import loansMgmtRouter from "./routes/app/loanMgmt.route.js";
|
|
||||||
import userMgmtRouterAPP from "./routes/app/userMgmt.route.js";
|
|
||||||
|
|
||||||
// admin routes
|
|
||||||
import userDataMgmtRouter from "./routes/admin/userDataMgmt.route.js";
|
|
||||||
import loanDataMgmtRouter from "./routes/admin/loanDataMgmt.route.js";
|
|
||||||
import itemDataMgmtRouter from "./routes/admin/itemDataMgmt.route.js";
|
|
||||||
import apiDataMgmtRouter from "./routes/admin/apiDataMgmt.route.js";
|
|
||||||
import userMgmtRouterADMIN from "./routes/admin/userMgmt.route.js";
|
|
||||||
|
|
||||||
// API routes
|
|
||||||
import apiRouter from "./routes/api/api.route.js";
|
|
||||||
|
|
||||||
env.config();
|
|
||||||
const app = express();
|
|
||||||
const port = 8004;
|
|
||||||
|
|
||||||
app.use(cors());
|
|
||||||
// Body-Parser VOR den Routen registrieren
|
|
||||||
app.use(express.json({ limit: "10mb" }));
|
|
||||||
app.use(express.urlencoded({ extended: true, limit: "10mb" }));
|
|
||||||
|
|
||||||
// frontend routes
|
|
||||||
app.use("/api/loans", loansMgmtRouter);
|
|
||||||
app.use("/api/users", userMgmtRouterAPP);
|
|
||||||
|
|
||||||
// admin routes
|
|
||||||
app.use("/api/admin/loan-data", loanDataMgmtRouter);
|
|
||||||
app.use("/api/admin/user-data", userDataMgmtRouter);
|
|
||||||
app.use("/api/admin/item-data", itemDataMgmtRouter);
|
|
||||||
app.use("/api/admin/api-data", apiDataMgmtRouter);
|
|
||||||
app.use("/api/admin/user-mgmt", userMgmtRouterADMIN);
|
|
||||||
|
|
||||||
// API routes
|
|
||||||
app.use("/api", apiRouter);
|
|
||||||
|
|
||||||
app.set("view engine", "ejs");
|
|
||||||
|
|
||||||
app.listen(port, () => {
|
|
||||||
console.log(`Server is running on port: ${port}`);
|
|
||||||
});
|
|
||||||
|
|
||||||
app.get("/verify", authenticate, async (req, res) => {
|
|
||||||
res.status(200).json({ message: "Token is valid", user: req.user });
|
|
||||||
});
|
|
||||||
|
|
||||||
app.get("/", (req, res) => {
|
|
||||||
res.send(info);
|
|
||||||
});
|
|
||||||
|
|
||||||
// error handling code
|
|
||||||
app.use((err, req, res, next) => {
|
|
||||||
console.error(err.stack);
|
|
||||||
res.status(500).send("Something broke!");
|
|
||||||
});
|
|
||||||
@@ -1,90 +0,0 @@
|
|||||||
import { SignJWT, jwtVerify } from "jose";
|
|
||||||
import env from "dotenv";
|
|
||||||
import { verifyAPIKeyDB } from "./database.js";
|
|
||||||
env.config();
|
|
||||||
|
|
||||||
const secretKey = process.env.SECRET_KEY;
|
|
||||||
if (!secretKey) {
|
|
||||||
throw new Error("Missing SECRET_KEY environment variable");
|
|
||||||
}
|
|
||||||
const secret = new TextEncoder().encode(secretKey);
|
|
||||||
|
|
||||||
export async function generateToken(payload) {
|
|
||||||
return await new SignJWT(payload)
|
|
||||||
.setProtectedHeader({ alg: "HS256" })
|
|
||||||
.setIssuedAt()
|
|
||||||
.setExpirationTime("2h")
|
|
||||||
.sign(secret);
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function authenticateAdmin(req, res, next) {
|
|
||||||
const authHeader = req.headers["authorization"];
|
|
||||||
if (!authHeader) {
|
|
||||||
return res.status(401).json({ message: "Unauthorized" });
|
|
||||||
}
|
|
||||||
|
|
||||||
const [scheme, token] = authHeader.split(" ");
|
|
||||||
if (!/^Bearer$/i.test(scheme) || !token) {
|
|
||||||
return res.status(401).json({ message: "Unauthorized" });
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
const payload = await verifyToken(token);
|
|
||||||
if (!payload?.admin) {
|
|
||||||
return res.status(403).json({ message: "Forbidden: admin only" });
|
|
||||||
}
|
|
||||||
req.user = payload;
|
|
||||||
return next();
|
|
||||||
} catch {
|
|
||||||
return res.status(403).json({ message: "Forbidden 403" });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function authenticate(req, res, next) {
|
|
||||||
const authHeader = req.headers["authorization"];
|
|
||||||
const apiKey = req.params.key;
|
|
||||||
|
|
||||||
if (authHeader) {
|
|
||||||
const parts = authHeader.split(" ");
|
|
||||||
const scheme = parts[0];
|
|
||||||
const token = parts[1];
|
|
||||||
|
|
||||||
if (!/^Bearer$/i.test(scheme) || !token) {
|
|
||||||
return res.status(401).json({ message: "Unauthorized" });
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
const payload = await verifyToken(token);
|
|
||||||
req.user = payload;
|
|
||||||
return next();
|
|
||||||
} catch {
|
|
||||||
return res.status(403).json({ message: "Present token invalid" }); // present token invalid
|
|
||||||
}
|
|
||||||
} else if (apiKey) {
|
|
||||||
try {
|
|
||||||
await verifyAPIKey(apiKey);
|
|
||||||
return next();
|
|
||||||
} catch {
|
|
||||||
return res.status(403).json({ message: "API Key invalid" }); // fix: don't chain after sendStatus
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return res.status(401).json({ message: "Unauthorized" }); // no credentials
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function verifyAPIKey(apiKey) {
|
|
||||||
const result = await verifyAPIKeyDB(apiKey);
|
|
||||||
|
|
||||||
if (result.valid) {
|
|
||||||
return;
|
|
||||||
} else {
|
|
||||||
throw new Error("Invalid API Key");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function verifyToken(token) {
|
|
||||||
const { payload } = await jwtVerify(token, secret, {
|
|
||||||
algorithms: ["HS256"],
|
|
||||||
});
|
|
||||||
return payload;
|
|
||||||
}
|
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
import mysql from "mysql2";
|
|
||||||
import dotenv from "dotenv";
|
|
||||||
dotenv.config();
|
|
||||||
|
|
||||||
const pool = mysql
|
|
||||||
.createPool({
|
|
||||||
host: process.env.DB_HOST,
|
|
||||||
user: process.env.DB_USER,
|
|
||||||
password: process.env.DB_PASSWORD,
|
|
||||||
database: process.env.DB_NAME,
|
|
||||||
})
|
|
||||||
.promise();
|
|
||||||
|
|
||||||
export const verifyAPIKeyDB = async (apiKey) => {
|
|
||||||
const [result] = await pool.query(
|
|
||||||
"SELECT * FROM apiKeys WHERE api_key = ?;",
|
|
||||||
[apiKey]
|
|
||||||
);
|
|
||||||
if (result.length > 0) {
|
|
||||||
const [lastUsed] = await pool.query(
|
|
||||||
"UPDATE apiKeys SET last_used_at = NOW() WHERE api_key = ?;",
|
|
||||||
[apiKey]
|
|
||||||
);
|
|
||||||
if (lastUsed.affectedRows > 0) {
|
|
||||||
return { valid: true };
|
|
||||||
} else {
|
|
||||||
return { valid: false };
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return { valid: false };
|
|
||||||
}
|
|
||||||
};
|
|
||||||
@@ -1,62 +1,57 @@
|
|||||||
services:
|
services:
|
||||||
# usr-frontend_v2:
|
# borrow_system-frontend:
|
||||||
# container_name: borrow_system-usr-frontend
|
# container_name: borrow_system-frontend
|
||||||
# build: ./FrontendV2
|
# build: ./FrontendV2
|
||||||
# ports:
|
# ports:
|
||||||
# - "8001:80"
|
# - "8001:8001"
|
||||||
|
# environment:
|
||||||
|
# - CHOKIDAR_USEPOLLING=true
|
||||||
|
# volumes:
|
||||||
|
# - ./FrontendV2:/app
|
||||||
|
# - /app/node_modules
|
||||||
# restart: unless-stopped
|
# restart: unless-stopped
|
||||||
|
|
||||||
# admin-frontend:
|
# admin-frontend:
|
||||||
# container_name: borrow_system-admin-frontend
|
# container_name: admin-frontend
|
||||||
# build: ./admin
|
# build: ./admin
|
||||||
# ports:
|
# ports:
|
||||||
# - "8003:80"
|
# - "8003:8003"
|
||||||
|
# environment:
|
||||||
|
# - CHOKIDAR_USEPOLLING=true
|
||||||
|
# volumes:
|
||||||
|
# - ./admin:/app
|
||||||
|
# - /app/node_modules
|
||||||
# restart: unless-stopped
|
# restart: unless-stopped
|
||||||
|
|
||||||
backend_v2:
|
borrow_system-backend:
|
||||||
container_name: borrow_system-backend_v2
|
container_name: borrow_system-backend
|
||||||
build: ./backendV2
|
build: ./backend
|
||||||
ports:
|
ports:
|
||||||
- "8004:8004"
|
- "8002:8002"
|
||||||
environment:
|
environment:
|
||||||
NODE_ENV: production
|
DB_HOST: mysql
|
||||||
DB_HOST: mysql_v2
|
|
||||||
DB_USER: root
|
DB_USER: root
|
||||||
DB_PASSWORD: ${DB_PASSWORD_V2}
|
DB_PASSWORD: ${DB_PASSWORD}
|
||||||
DB_NAME: borrow_system_new
|
DB_NAME: borrow_system
|
||||||
depends_on:
|
depends_on:
|
||||||
- mysql_v2
|
- mysql
|
||||||
|
volumes:
|
||||||
|
- ./backend:/borrow_system-backend
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
|
|
||||||
mysql_v2:
|
mysql:
|
||||||
container_name: borrow_system-mysql-v2
|
container_name: borrow_system-mysql
|
||||||
image: mysql:8.0
|
image: mysql:8.0
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
environment:
|
environment:
|
||||||
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD_V2}
|
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
|
||||||
MYSQL_DATABASE: borrow_system_new
|
MYSQL_DATABASE: borrow_system
|
||||||
TZ: Europe/Berlin
|
TZ: Europe/Berlin
|
||||||
volumes:
|
volumes:
|
||||||
- mysql-v2-data:/var/lib/mysql
|
- mysql-data:/var/lib/mysql
|
||||||
- ./mysql-timezone.cnf:/etc/mysql/conf.d/timezone.cnf:ro
|
- ./mysql-timezone.cnf:/etc/mysql/conf.d/timezone.cnf:ro
|
||||||
ports:
|
ports:
|
||||||
- "3310:3306"
|
- "3309:3306"
|
||||||
|
|
||||||
postgresql:
|
|
||||||
image: postgres
|
|
||||||
container_name: borrow_system-postgresql
|
|
||||||
shm_size: 128mb
|
|
||||||
environment:
|
|
||||||
POSTGRES_USER: root
|
|
||||||
POSTGRES_PASSWORD: ${POSTGRES_ROOT_PASSWORD}
|
|
||||||
|
|
||||||
adminer:
|
|
||||||
container_name: borrow_system-adminer
|
|
||||||
image: adminer
|
|
||||||
restart: unless-stopped
|
|
||||||
ports:
|
|
||||||
- "8080:8080"
|
|
||||||
|
|
||||||
volumes:
|
volumes:
|
||||||
mysql-data:
|
mysql-data:
|
||||||
mysql-v2-data:
|
|
||||||
|
|||||||
24
frontend/.gitignore
vendored
Normal file
24
frontend/.gitignore
vendored
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
# Logs
|
||||||
|
logs
|
||||||
|
*.log
|
||||||
|
npm-debug.log*
|
||||||
|
yarn-debug.log*
|
||||||
|
yarn-error.log*
|
||||||
|
pnpm-debug.log*
|
||||||
|
lerna-debug.log*
|
||||||
|
|
||||||
|
node_modules
|
||||||
|
dist
|
||||||
|
dist-ssr
|
||||||
|
*.local
|
||||||
|
|
||||||
|
# Editor directories and files
|
||||||
|
.vscode/*
|
||||||
|
!.vscode/extensions.json
|
||||||
|
.idea
|
||||||
|
.DS_Store
|
||||||
|
*.suo
|
||||||
|
*.ntvs*
|
||||||
|
*.njsproj
|
||||||
|
*.sln
|
||||||
|
*.sw?
|
||||||
12
frontend/Dockerfile
Normal file
12
frontend/Dockerfile
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
FROM node:20-alpine
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
COPY package*.json ./
|
||||||
|
RUN npm install
|
||||||
|
|
||||||
|
COPY . .
|
||||||
|
|
||||||
|
EXPOSE 8001
|
||||||
|
|
||||||
|
CMD ["npm", "run", "dev"]
|
||||||
69
frontend/README.md
Normal file
69
frontend/README.md
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
# React + TypeScript + Vite
|
||||||
|
|
||||||
|
This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.
|
||||||
|
|
||||||
|
Currently, two official plugins are available:
|
||||||
|
|
||||||
|
- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react) uses [Babel](https://babeljs.io/) for Fast Refresh
|
||||||
|
- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh
|
||||||
|
|
||||||
|
## Expanding the ESLint configuration
|
||||||
|
|
||||||
|
If you are developing a production application, we recommend updating the configuration to enable type-aware lint rules:
|
||||||
|
|
||||||
|
```js
|
||||||
|
export default tseslint.config([
|
||||||
|
globalIgnores(['dist']),
|
||||||
|
{
|
||||||
|
files: ['**/*.{ts,tsx}'],
|
||||||
|
extends: [
|
||||||
|
// Other configs...
|
||||||
|
|
||||||
|
// Remove tseslint.configs.recommended and replace with this
|
||||||
|
...tseslint.configs.recommendedTypeChecked,
|
||||||
|
// Alternatively, use this for stricter rules
|
||||||
|
...tseslint.configs.strictTypeChecked,
|
||||||
|
// Optionally, add this for stylistic rules
|
||||||
|
...tseslint.configs.stylisticTypeChecked,
|
||||||
|
|
||||||
|
// Other configs...
|
||||||
|
],
|
||||||
|
languageOptions: {
|
||||||
|
parserOptions: {
|
||||||
|
project: ['./tsconfig.node.json', './tsconfig.app.json'],
|
||||||
|
tsconfigRootDir: import.meta.dirname,
|
||||||
|
},
|
||||||
|
// other options...
|
||||||
|
},
|
||||||
|
},
|
||||||
|
])
|
||||||
|
```
|
||||||
|
|
||||||
|
You can also install [eslint-plugin-react-x](https://github.com/Rel1cx/eslint-react/tree/main/packages/plugins/eslint-plugin-react-x) and [eslint-plugin-react-dom](https://github.com/Rel1cx/eslint-react/tree/main/packages/plugins/eslint-plugin-react-dom) for React-specific lint rules:
|
||||||
|
|
||||||
|
```js
|
||||||
|
// eslint.config.js
|
||||||
|
import reactX from 'eslint-plugin-react-x'
|
||||||
|
import reactDom from 'eslint-plugin-react-dom'
|
||||||
|
|
||||||
|
export default tseslint.config([
|
||||||
|
globalIgnores(['dist']),
|
||||||
|
{
|
||||||
|
files: ['**/*.{ts,tsx}'],
|
||||||
|
extends: [
|
||||||
|
// Other configs...
|
||||||
|
// Enable lint rules for React
|
||||||
|
reactX.configs['recommended-typescript'],
|
||||||
|
// Enable lint rules for React DOM
|
||||||
|
reactDom.configs.recommended,
|
||||||
|
],
|
||||||
|
languageOptions: {
|
||||||
|
parserOptions: {
|
||||||
|
project: ['./tsconfig.node.json', './tsconfig.app.json'],
|
||||||
|
tsconfigRootDir: import.meta.dirname,
|
||||||
|
},
|
||||||
|
// other options...
|
||||||
|
},
|
||||||
|
},
|
||||||
|
])
|
||||||
|
```
|
||||||
23
frontend/eslint.config.js
Normal file
23
frontend/eslint.config.js
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
import js from '@eslint/js'
|
||||||
|
import globals from 'globals'
|
||||||
|
import reactHooks from 'eslint-plugin-react-hooks'
|
||||||
|
import reactRefresh from 'eslint-plugin-react-refresh'
|
||||||
|
import tseslint from 'typescript-eslint'
|
||||||
|
import { globalIgnores } from 'eslint/config'
|
||||||
|
|
||||||
|
export default tseslint.config([
|
||||||
|
globalIgnores(['dist']),
|
||||||
|
{
|
||||||
|
files: ['**/*.{ts,tsx}'],
|
||||||
|
extends: [
|
||||||
|
js.configs.recommended,
|
||||||
|
tseslint.configs.recommended,
|
||||||
|
reactHooks.configs['recommended-latest'],
|
||||||
|
reactRefresh.configs.vite,
|
||||||
|
],
|
||||||
|
languageOptions: {
|
||||||
|
ecmaVersion: 2020,
|
||||||
|
globals: globals.browser,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
])
|
||||||
13
frontend/index.html
Normal file
13
frontend/index.html
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8" />
|
||||||
|
<link rel="icon" type="image/svg+xml" href="/shapes.svg" />
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
|
<title>Ausleihsystem</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div id="root"></div>
|
||||||
|
<script type="module" src="/src/main.tsx"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
4664
frontend/package-lock.json
generated
Normal file
4664
frontend/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
45
frontend/package.json
Normal file
45
frontend/package.json
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
{
|
||||||
|
"name": "frontend",
|
||||||
|
"private": true,
|
||||||
|
"version": "0.0.0",
|
||||||
|
"type": "module",
|
||||||
|
"scripts": {
|
||||||
|
"dev": "vite",
|
||||||
|
"build": "tsc -b && vite build",
|
||||||
|
"lint": "eslint .",
|
||||||
|
"preview": "vite preview"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"@tailwindcss/vite": "^4.1.11",
|
||||||
|
"@tanstack/react-query": "^5.85.5",
|
||||||
|
"jotai": "^2.15.0",
|
||||||
|
"js-cookie": "^3.0.5",
|
||||||
|
"lucide-react": "^0.539.0",
|
||||||
|
"primeicons": "^7.0.0",
|
||||||
|
"primereact": "^10.9.6",
|
||||||
|
"react": "^19.1.1",
|
||||||
|
"react-dom": "^19.1.1",
|
||||||
|
"react-router-dom": "^7.8.0",
|
||||||
|
"react-toastify": "^11.0.5",
|
||||||
|
"split-lines": "^3.0.0",
|
||||||
|
"tailwind-merge": "^3.3.1",
|
||||||
|
"tailwindcss": "^4.1.11",
|
||||||
|
"tailwindcss-animate": "^1.0.7",
|
||||||
|
"tw-animate-css": "^1.3.5",
|
||||||
|
"vite-plugin-svgr": "^4.3.0"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@eslint/js": "^9.32.0",
|
||||||
|
"@types/js-cookie": "^3.0.6",
|
||||||
|
"@types/react": "^19.1.9",
|
||||||
|
"@types/react-dom": "^19.1.7",
|
||||||
|
"@vitejs/plugin-react": "^4.7.0",
|
||||||
|
"eslint": "^9.32.0",
|
||||||
|
"eslint-plugin-react-hooks": "^5.2.0",
|
||||||
|
"eslint-plugin-react-refresh": "^0.4.20",
|
||||||
|
"globals": "^16.3.0",
|
||||||
|
"typescript": "~5.8.3",
|
||||||
|
"typescript-eslint": "^8.39.0",
|
||||||
|
"vite": "^7.1.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
1
frontend/public/shapes.svg
Normal file
1
frontend/public/shapes.svg
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-shapes-icon lucide-shapes"><path d="M8.3 10a.7.7 0 0 1-.626-1.079L11.4 3a.7.7 0 0 1 1.198-.043L16.3 8.9a.7.7 0 0 1-.572 1.1Z"/><rect x="3" y="14" width="7" height="7" rx="1"/><circle cx="17.5" cy="17.5" r="3.5"/></svg>
|
||||||
|
After Width: | Height: | Size: 420 B |
1
frontend/src/App.css
Normal file
1
frontend/src/App.css
Normal file
@@ -0,0 +1 @@
|
|||||||
|
@import "tailwindcss";
|
||||||
54
frontend/src/App.tsx
Normal file
54
frontend/src/App.tsx
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
import "./App.css";
|
||||||
|
import Layout from "./layout/Layout";
|
||||||
|
import { useEffect, useState } from "react";
|
||||||
|
import { AddLoan } from "./components/AddLoan";
|
||||||
|
import LoginForm from "./components/LoginForm";
|
||||||
|
import Cookies from "js-cookie";
|
||||||
|
import {
|
||||||
|
fetchAllData,
|
||||||
|
ALL_ITEMS_UPDATED_EVENT,
|
||||||
|
AUTH_LOGOUT_EVENT,
|
||||||
|
} from "./utils/fetchData";
|
||||||
|
import { myToast } from "./utils/toastify";
|
||||||
|
|
||||||
|
function App() {
|
||||||
|
const [isLoggedIn, setIsLoggedIn] = useState(false);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const token = Cookies.get("token");
|
||||||
|
if (token) {
|
||||||
|
setIsLoggedIn(true);
|
||||||
|
fetchAllData(token);
|
||||||
|
}
|
||||||
|
localStorage.setItem("borrowableItems", JSON.stringify([]));
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const onAuthLogout = () => {
|
||||||
|
setIsLoggedIn(false);
|
||||||
|
};
|
||||||
|
window.addEventListener(AUTH_LOGOUT_EVENT, onAuthLogout);
|
||||||
|
return () => window.removeEventListener(AUTH_LOGOUT_EVENT, onAuthLogout);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const handleLogout = () => {
|
||||||
|
Cookies.remove("token");
|
||||||
|
localStorage.removeItem("allItems");
|
||||||
|
localStorage.removeItem("allLoans");
|
||||||
|
localStorage.removeItem("userLoans");
|
||||||
|
localStorage.removeItem("borrowableItems");
|
||||||
|
window.dispatchEvent(new Event(ALL_ITEMS_UPDATED_EVENT));
|
||||||
|
myToast("Logged out successfully!", "success");
|
||||||
|
setIsLoggedIn(false);
|
||||||
|
};
|
||||||
|
|
||||||
|
return isLoggedIn ? (
|
||||||
|
<Layout onLogout={handleLogout}>
|
||||||
|
<AddLoan />
|
||||||
|
</Layout>
|
||||||
|
) : (
|
||||||
|
<LoginForm onLogin={() => setIsLoggedIn(true)} />
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export default App;
|
||||||
6
frontend/src/States/Atoms.tsx
Normal file
6
frontend/src/States/Atoms.tsx
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
import { atom } from "jotai";
|
||||||
|
|
||||||
|
// Atoms to store the start and end dates for loans
|
||||||
|
export const startDate = atom<string | null>(null);
|
||||||
|
export const endDate = atom<string | null>(null);
|
||||||
|
export const getBorrowableItemsAtom = atom<string[] | boolean>(false);
|
||||||
36
frontend/src/States/README.md
Normal file
36
frontend/src/States/README.md
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
# How to use Atoms
|
||||||
|
Atoms are the fundamental building blocks of state management in this system. They represent individual pieces of state that can be shared and manipulated across different components.
|
||||||
|
|
||||||
|
You can also name it global state.
|
||||||
|
|
||||||
|
## Creating an Atom
|
||||||
|
to create an atom you have to declare an atom like this:
|
||||||
|
|
||||||
|
```ts
|
||||||
|
import { atom } from 'jotai';
|
||||||
|
|
||||||
|
export const NAME_OF_YOUR_ATOM = atom<type_of_your_atom>(initial_value);
|
||||||
|
```
|
||||||
|
|
||||||
|
In this project we declare all atoms in the `States/Atoms.tsx`file. Which you can find above this README file.
|
||||||
|
|
||||||
|
## Using an Atom
|
||||||
|
To use an atom in your component, you can use the `useAtom` hook provided by Jotai. Here's an example of how to use an atom in a React component:
|
||||||
|
|
||||||
|
```tsx
|
||||||
|
import { useAtom } from 'jotai';
|
||||||
|
import { NAME_OF_YOUR_ATOM } from '@/States/Atoms';
|
||||||
|
|
||||||
|
const MyComponent = () => {
|
||||||
|
const [value, setValue] = useAtom(NAME_OF_YOUR_ATOM);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<p>Current value: {value}</p>
|
||||||
|
<button onClick={() => setValue(newValue)}>Update Value</button>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
As you can see, you can use `useAtom` like `useState` but the state is global. In this example `value` is the current state of the atom, and `setValue` is a function to update the state, which is also known as the setter function.
|
||||||
1
frontend/src/assets/react.svg
Normal file
1
frontend/src/assets/react.svg
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="35.93" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 228"><path fill="#00D8FF" d="M210.483 73.824a171.49 171.49 0 0 0-8.24-2.597c.465-1.9.893-3.777 1.273-5.621c6.238-30.281 2.16-54.676-11.769-62.708c-13.355-7.7-35.196.329-57.254 19.526a171.23 171.23 0 0 0-6.375 5.848a155.866 155.866 0 0 0-4.241-3.917C100.759 3.829 77.587-4.822 63.673 3.233C50.33 10.957 46.379 33.89 51.995 62.588a170.974 170.974 0 0 0 1.892 8.48c-3.28.932-6.445 1.924-9.474 2.98C17.309 83.498 0 98.307 0 113.668c0 15.865 18.582 31.778 46.812 41.427a145.52 145.52 0 0 0 6.921 2.165a167.467 167.467 0 0 0-2.01 9.138c-5.354 28.2-1.173 50.591 12.134 58.266c13.744 7.926 36.812-.22 59.273-19.855a145.567 145.567 0 0 0 5.342-4.923a168.064 168.064 0 0 0 6.92 6.314c21.758 18.722 43.246 26.282 56.54 18.586c13.731-7.949 18.194-32.003 12.4-61.268a145.016 145.016 0 0 0-1.535-6.842c1.62-.48 3.21-.974 4.76-1.488c29.348-9.723 48.443-25.443 48.443-41.52c0-15.417-17.868-30.326-45.517-39.844Zm-6.365 70.984c-1.4.463-2.836.91-4.3 1.345c-3.24-10.257-7.612-21.163-12.963-32.432c5.106-11 9.31-21.767 12.459-31.957c2.619.758 5.16 1.557 7.61 2.4c23.69 8.156 38.14 20.213 38.14 29.504c0 9.896-15.606 22.743-40.946 31.14Zm-10.514 20.834c2.562 12.94 2.927 24.64 1.23 33.787c-1.524 8.219-4.59 13.698-8.382 15.893c-8.067 4.67-25.32-1.4-43.927-17.412a156.726 156.726 0 0 1-6.437-5.87c7.214-7.889 14.423-17.06 21.459-27.246c12.376-1.098 24.068-2.894 34.671-5.345a134.17 134.17 0 0 1 1.386 6.193ZM87.276 214.515c-7.882 2.783-14.16 2.863-17.955.675c-8.075-4.657-11.432-22.636-6.853-46.752a156.923 156.923 0 0 1 1.869-8.499c10.486 2.32 22.093 3.988 34.498 4.994c7.084 9.967 14.501 19.128 21.976 27.15a134.668 134.668 0 0 1-4.877 4.492c-9.933 8.682-19.886 14.842-28.658 17.94ZM50.35 144.747c-12.483-4.267-22.792-9.812-29.858-15.863c-6.35-5.437-9.555-10.836-9.555-15.216c0-9.322 13.897-21.212 37.076-29.293c2.813-.98 5.757-1.905 8.812-2.773c3.204 10.42 7.406 21.315 12.477 32.332c-5.137 11.18-9.399 22.249-12.634 32.792a134.718 134.718 0 0 1-6.318-1.979Zm12.378-84.26c-4.811-24.587-1.616-43.134 6.425-47.789c8.564-4.958 27.502 2.111 47.463 19.835a144.318 144.318 0 0 1 3.841 3.545c-7.438 7.987-14.787 17.08-21.808 26.988c-12.04 1.116-23.565 2.908-34.161 5.309a160.342 160.342 0 0 1-1.76-7.887Zm110.427 27.268a347.8 347.8 0 0 0-7.785-12.803c8.168 1.033 15.994 2.404 23.343 4.08c-2.206 7.072-4.956 14.465-8.193 22.045a381.151 381.151 0 0 0-7.365-13.322Zm-45.032-43.861c5.044 5.465 10.096 11.566 15.065 18.186a322.04 322.04 0 0 0-30.257-.006c4.974-6.559 10.069-12.652 15.192-18.18ZM82.802 87.83a323.167 323.167 0 0 0-7.227 13.238c-3.184-7.553-5.909-14.98-8.134-22.152c7.304-1.634 15.093-2.97 23.209-3.984a321.524 321.524 0 0 0-7.848 12.897Zm8.081 65.352c-8.385-.936-16.291-2.203-23.593-3.793c2.26-7.3 5.045-14.885 8.298-22.6a321.187 321.187 0 0 0 7.257 13.246c2.594 4.48 5.28 8.868 8.038 13.147Zm37.542 31.03c-5.184-5.592-10.354-11.779-15.403-18.433c4.902.192 9.899.29 14.978.29c5.218 0 10.376-.117 15.453-.343c-4.985 6.774-10.018 12.97-15.028 18.486Zm52.198-57.817c3.422 7.8 6.306 15.345 8.596 22.52c-7.422 1.694-15.436 3.058-23.88 4.071a382.417 382.417 0 0 0 7.859-13.026a347.403 347.403 0 0 0 7.425-13.565Zm-16.898 8.101a358.557 358.557 0 0 1-12.281 19.815a329.4 329.4 0 0 1-23.444.823c-7.967 0-15.716-.248-23.178-.732a310.202 310.202 0 0 1-12.513-19.846h.001a307.41 307.41 0 0 1-10.923-20.627a310.278 310.278 0 0 1 10.89-20.637l-.001.001a307.318 307.318 0 0 1 12.413-19.761c7.613-.576 15.42-.876 23.31-.876H128c7.926 0 15.743.303 23.354.883a329.357 329.357 0 0 1 12.335 19.695a358.489 358.489 0 0 1 11.036 20.54a329.472 329.472 0 0 1-11 20.722Zm22.56-122.124c8.572 4.944 11.906 24.881 6.52 51.026c-.344 1.668-.73 3.367-1.15 5.09c-10.622-2.452-22.155-4.275-34.23-5.408c-7.034-10.017-14.323-19.124-21.64-27.008a160.789 160.789 0 0 1 5.888-5.4c18.9-16.447 36.564-22.941 44.612-18.3ZM128 90.808c12.625 0 22.86 10.235 22.86 22.86s-10.235 22.86-22.86 22.86s-22.86-10.235-22.86-22.86s10.235-22.86 22.86-22.86Z"></path></svg>
|
||||||
|
After Width: | Height: | Size: 4.0 KiB |
69
frontend/src/components/AddLoan.tsx
Normal file
69
frontend/src/components/AddLoan.tsx
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
import { getBorrowableItems } from "../utils/fetchData";
|
||||||
|
import { useAtom } from "jotai";
|
||||||
|
import { startDate, endDate } from "../States/Atoms";
|
||||||
|
import Cookies from "js-cookie";
|
||||||
|
|
||||||
|
export const AddLoan = () => {
|
||||||
|
const [start, setStart] = useAtom(startDate);
|
||||||
|
const [end, setEnd] = useAtom(endDate);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="space-y-4">
|
||||||
|
<h2 className="text-lg sm:text-xl font-bold text-slate-900">
|
||||||
|
1. Zeitraum wählen
|
||||||
|
</h2>
|
||||||
|
<form
|
||||||
|
className="space-y-3"
|
||||||
|
onSubmit={(e) => {
|
||||||
|
e.preventDefault();
|
||||||
|
const form = e.currentTarget as HTMLFormElement;
|
||||||
|
const fd = new FormData(form);
|
||||||
|
const start = (fd.get("startDate") as string) || "";
|
||||||
|
const end = (fd.get("endDate") as string) || "";
|
||||||
|
setStart(start);
|
||||||
|
setEnd(end);
|
||||||
|
Cookies.set("startDate", start);
|
||||||
|
Cookies.set("endDate", end);
|
||||||
|
getBorrowableItems();
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<div className="grid grid-cols-1 sm:grid-cols-2 gap-3">
|
||||||
|
<div>
|
||||||
|
<label
|
||||||
|
htmlFor="startDate"
|
||||||
|
className="block text-sm font-medium text-slate-700 mb-1"
|
||||||
|
>
|
||||||
|
Start
|
||||||
|
</label>
|
||||||
|
<input
|
||||||
|
type="datetime-local"
|
||||||
|
id="startDate"
|
||||||
|
name="startDate"
|
||||||
|
className="w-full border border-slate-300 rounded-lg px-3 py-2.5 focus:ring-2 focus:ring-indigo-500 focus:outline-none bg-white"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<label
|
||||||
|
htmlFor="endDate"
|
||||||
|
className="block text-sm font-medium text-slate-700 mb-1"
|
||||||
|
>
|
||||||
|
Ende
|
||||||
|
</label>
|
||||||
|
<input
|
||||||
|
type="datetime-local"
|
||||||
|
id="endDate"
|
||||||
|
name="endDate"
|
||||||
|
className="w-full border border-slate-300 rounded-lg px-3 py-2.5 focus:ring-2 focus:ring-indigo-500 focus:outline-none bg-white"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<button
|
||||||
|
type="submit"
|
||||||
|
className="w-full bg-indigo-600 text-white font-bold py-2.5 px-4 rounded-lg shadow hover:bg-indigo-700 transition"
|
||||||
|
>
|
||||||
|
Verfügbare Gegenstände anzeigen
|
||||||
|
</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
12
frontend/src/components/Footer.tsx
Normal file
12
frontend/src/components/Footer.tsx
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
import React from "react";
|
||||||
|
|
||||||
|
const Footer: React.FC = () => {
|
||||||
|
return (
|
||||||
|
<footer className="fixed bottom-0 left-0 text-sm w-full bg-slate-100 text-center py-2 border-t border-slate-200 z-50">
|
||||||
|
<p>Made with ❤️ by Theis Gaedigk - Jahrgang 2019</p>
|
||||||
|
<p>v1.1</p>
|
||||||
|
</footer>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default Footer;
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user