API: asyncHandler zamiast try/catch+next(e) w routes (mniejsza ilosc kodu, odrzucone Promise trafiaja do errorHandler)
This commit is contained in:
39
AGENTS.md
39
AGENTS.md
@@ -3,6 +3,28 @@
|
||||
## Struktura
|
||||
- `app` (React+Vite, 5173), `admin` (React+Vite, 5174), `api` (Express+Prisma+PostgreSQL, 9002).
|
||||
|
||||
## Frontend (app + admin — ten sam stack)
|
||||
- React 18 + Vite + TypeScript; routing: React Router v6; stylowanie: Tailwind v4 + cva (`cn`/twMerge), ikony lucide-react, dialogi Radix.
|
||||
- Stan klienta: Zustand (`src/store/` — hooki `useAuth` itp.); dane serwerowe: TanStack Query v5, hooki centralnie w `src/api/queries.ts` — komponenty NIE robią fetcha bezpośrednio.
|
||||
- Klient API: `src/api/client.ts` (tokeny, 401→refresh→retry), typy: `src/api/types.ts`.
|
||||
|
||||
## Eksploracja kodu
|
||||
- Do otwartych pytań o zachowanie/semantykę kodu (np. "jak działa X") używaj najpierw `semantic_search` (indeks wektorowy OpenAI+LanceDB, działa po polsku), potem docelaj `grep`/`read`. `path` zawęża do podkatalogu (np. `api/src`).
|
||||
- `grep`/`glob` — gdy znasz dokładne symbole/terminy; `semantic_search` — gdy znasz tylko intencję.
|
||||
|
||||
## API — architektura i konwencje
|
||||
- Warstwy: `routes/` (cienkie, walidacja zod) → `services/` (logika biznesowa) → Prisma. Nowy endpoint = route + service + swagger.
|
||||
- `api/src/swagger.ts` prowadzony ręcznie — każdy nowy/zmieniony endpoint wymaga wpisu tam.
|
||||
- Błędy: `AppError` + helpery z `api/src/lib/errors.ts` (`badRequest`, `unauthorized`, `forbidden`, `notFound`, `conflict`); ciało `{ error: { code, message } }`.
|
||||
- Handler w `routes/`: owinięty w `asyncHandler(async (req, res) => { ... })` z `api/src/lib/asyncHandler.ts` (Express 4 nie przekazuje odrzuconych Promise do `middleware/errorHandler.ts` — wrapper to robi). `throw` = błąd; wewnętrzne `try/catch` tylko gdy catch robi coś więcej niż `next(e)` (np. sprzątanie plików, mapowanie błędu) — wtedy rethrow.
|
||||
- `prisma/schema.prisma` = single source of truth; zmiana schematu → `npm run prisma:migrate` (kontener API sam robi `migrate deploy` + seed przy starcie).
|
||||
- Adres API w app: `app/public/config.js` (`window.__FITOS_CONFIG__.apiUrl`) — runtime, bez rebuildu; `VITE_API_URL` tylko fallback build-time.
|
||||
|
||||
## Inwarianty domenowe (złamanie nie wyłapią lint/testy)
|
||||
- Maszyny stanów: plan draft→active→completed/abandoned; dzień planned→completed/skipped (auto-wygasanie 23:59); sesja in_progress→completed/abandoned (auto-abandon po 24h, jedna aktywna na dzień).
|
||||
- Killstreak: kolejne skuteczne treningi (wszystkie serie = zaplanowane + dodane); reset po pominięciu lub dniu częściowym — `services/killstreak.ts`.
|
||||
- Scheduler: `services/scheduler.ts`. Pełne zasady: README „Kluczowe decyzje (MVP)".
|
||||
|
||||
## Testy — obowiązkowe po każdej zmianie
|
||||
- Konwencja: WSZYSTKIE testy projektu żyją w `<pkg>/test/` — unit/komponent w `test/*` (api: `test/unit/` + `test/integration/`), e2e w `test/e2e/`. Rezultaty i artefakty też pod `test/`: `test/results/` (trace/screenshoty Playwright) i `test/report/` (raport HTML) — oba gitignore'owane. Nie twórz katalogów testowych poza `test/`.
|
||||
- `app`/`admin`: `npm test` (vitest), `npm run test:e2e` (Playwright, API mockowane, dev server sam). `api`: `npm test` (unit+integration, supertest).
|
||||
@@ -12,20 +34,27 @@
|
||||
- Selektory: `getByRole`/`getByText`/`input[type=...]` — `getByLabel` NIE działa (`Field` bez `<label>`).
|
||||
|
||||
## Zmiana kontraktu API — aktualizuj WSZYSTKO w jednej zmiance
|
||||
1. `api/src/routes/**` + walidacja zod + `api/test/**`
|
||||
1. `api/src/routes/**` + walidacja zod + `api/src/swagger.ts` + `api/test/**`
|
||||
2. typy: `app/src/api/types.ts` i/lub `admin/src/api/types.ts`
|
||||
3. front: `api/queries.ts`, `pages/**`, `store/auth.ts`
|
||||
4. mocki `app|admin/test/e2e/mock-api.ts` + testy e2e
|
||||
3. front: `app/src/api/queries.ts` / `admin/src/api/queries.ts`, `app/src/pages/**` / `admin/src/pages/**`, `app/src/store/auth.ts` / `admin/src/store/auth.ts`
|
||||
4. mocki `app/test/e2e/mock-api.ts` / `admin/test/e2e/mock-api.ts` + testy e2e
|
||||
5. `npm test` (api) + `npm test`/`test:e2e` (dotknięte fronty)
|
||||
|
||||
`prisma/schema.prisma` → `npm run prisma:migrate` (kontener API sam robi `migrate deploy` + seed przy starcie).
|
||||
|
||||
## Kontenery — rebuilduj tylko dotknięte
|
||||
- `api/`: `fitos-api` (9002) + `fitos-db` (pg16, wolumen `fitos_pgdata`). `admin/`: 9000 (nginx+dist). `app/`: 9001 (nginx+dist).
|
||||
- Zmiana `<pkg>/src/**` (albo `api/prisma/**`, `index.html`) → w katalogu `<pkg>/`: `docker compose build <pkg> && docker compose up -d`.
|
||||
- `.env` api → `up -d --force-recreate api`. Nigdy nie ruszaj wolumenu `fitos_pgdata`.
|
||||
- Weryfikacja po zmianie api: `docker compose ps` + `curl localhost:9002/health`.
|
||||
|
||||
## Git / commity
|
||||
- Po zakończonej zmianie (testy zielone) commituj sam — nie czekaj na polecenie.
|
||||
- Styl wiadomości: PL **bez polskich znaków**, wzór `Obszar: co zmieniono (efekt)` — patrz `git log`.
|
||||
- Jeden commit = jedna logiczna zmiana; nie mieszaj refaktora z featurem. Nie pushuj, jeśli nie poproszono.
|
||||
|
||||
## Język
|
||||
- Kod, nazwy zmiennych/funkcji/typów: EN. Komentarze w kodzie: PL.
|
||||
- Opisy testów (unit + e2e) i wiadomości commitów: PL (commity bez polskich znaków).
|
||||
|
||||
## Komendy
|
||||
| | komenda |
|
||||
| --- | --- |
|
||||
|
||||
10
api/src/lib/asyncHandler.ts
Normal file
10
api/src/lib/asyncHandler.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { NextFunction, Request, RequestHandler, Response } from 'express';
|
||||
|
||||
// Express 4 nie przekazuje odrzuconych Promise do errorHandler — ten wrapper to naprawia
|
||||
export function asyncHandler(
|
||||
fn: (req: Request, res: Response, next: NextFunction) => Promise<unknown>,
|
||||
): RequestHandler {
|
||||
return (req, res, next) => {
|
||||
fn(req, res, next).catch(next);
|
||||
};
|
||||
}
|
||||
@@ -42,6 +42,7 @@ import {
|
||||
listGymExceptions,
|
||||
} from '../services/gyms.js';
|
||||
import { listActivityLogs, logActivity } from '../services/audit.js';
|
||||
import { asyncHandler } from '../lib/asyncHandler.js';
|
||||
|
||||
const gymSchema = z.object({
|
||||
name: z.string().min(1).max(200),
|
||||
@@ -117,14 +118,13 @@ export function createAdminRoutes({ prisma }: AppDeps) {
|
||||
* responses:
|
||||
* 200: { description: OK }
|
||||
*/
|
||||
router.get('/gyms', async (_req, res, next) => {
|
||||
try {
|
||||
router.get(
|
||||
'/gyms',
|
||||
asyncHandler(async (_req, res) => {
|
||||
const result = await listGyms(prisma);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -135,15 +135,15 @@ export function createAdminRoutes({ prisma }: AppDeps) {
|
||||
* responses:
|
||||
* 201: { description: Created }
|
||||
*/
|
||||
router.post('/gyms', validate(createGymSchema), async (req, res, next) => {
|
||||
try {
|
||||
router.post(
|
||||
'/gyms',
|
||||
validate(createGymSchema),
|
||||
asyncHandler(async (req, res) => {
|
||||
const data = req.body as z.infer<typeof createGymSchema>;
|
||||
const result = await createGym(prisma, data);
|
||||
res.status(201).json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -154,15 +154,15 @@ export function createAdminRoutes({ prisma }: AppDeps) {
|
||||
* responses:
|
||||
* 200: { description: OK }
|
||||
*/
|
||||
router.patch('/gyms/:id', validate(updateGymSchema), async (req, res, next) => {
|
||||
try {
|
||||
router.patch(
|
||||
'/gyms/:id',
|
||||
validate(updateGymSchema),
|
||||
asyncHandler(async (req, res) => {
|
||||
const data = req.body as z.infer<typeof updateGymSchema>;
|
||||
const result = await updateGym(prisma, req.params.id, data);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -173,14 +173,13 @@ export function createAdminRoutes({ prisma }: AppDeps) {
|
||||
* responses:
|
||||
* 204: { description: Deleted }
|
||||
*/
|
||||
router.delete('/gyms/:id', async (req, res, next) => {
|
||||
try {
|
||||
await deleteGym(prisma, req.params.id);
|
||||
res.status(204).end();
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
router.delete(
|
||||
'/gyms/:id',
|
||||
asyncHandler(async (req, res) => {
|
||||
await deleteGym(prisma, req.params.id);
|
||||
res.status(204).end();
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -191,14 +190,13 @@ export function createAdminRoutes({ prisma }: AppDeps) {
|
||||
* responses:
|
||||
* 200: { description: OK }
|
||||
*/
|
||||
router.get('/gyms/:id/exceptions', async (req, res, next) => {
|
||||
try {
|
||||
router.get(
|
||||
'/gyms/:id/exceptions',
|
||||
asyncHandler(async (req, res) => {
|
||||
const result = await listGymExceptions(prisma, req.params.id);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -213,18 +211,14 @@ export function createAdminRoutes({ prisma }: AppDeps) {
|
||||
router.post(
|
||||
'/gyms/:id/exceptions',
|
||||
validate(gymExceptionSchema),
|
||||
async (req, res, next) => {
|
||||
try {
|
||||
const data = req.body as z.infer<typeof gymExceptionSchema>;
|
||||
const result = await createGymException(prisma, req.params.id, {
|
||||
date: data.date,
|
||||
availability: data.availability ?? null,
|
||||
});
|
||||
res.status(201).json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
},
|
||||
asyncHandler(async (req, res) => {
|
||||
const data = req.body as z.infer<typeof gymExceptionSchema>;
|
||||
const result = await createGymException(prisma, req.params.id, {
|
||||
date: data.date,
|
||||
availability: data.availability ?? null,
|
||||
});
|
||||
res.status(201).json(result);
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
@@ -236,14 +230,13 @@ export function createAdminRoutes({ prisma }: AppDeps) {
|
||||
* responses:
|
||||
* 204: { description: Removed }
|
||||
*/
|
||||
router.delete('/gyms/:id/exceptions/:exceptionId', async (req, res, next) => {
|
||||
try {
|
||||
router.delete(
|
||||
'/gyms/:id/exceptions/:exceptionId',
|
||||
asyncHandler(async (req, res) => {
|
||||
await deleteGymException(prisma, req.params.id, req.params.exceptionId);
|
||||
res.status(204).end();
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
const gymEquipmentSchema = z.object({
|
||||
equipmentId: z.string().min(1),
|
||||
@@ -268,20 +261,16 @@ export function createAdminRoutes({ prisma }: AppDeps) {
|
||||
router.post(
|
||||
'/gyms/:id/equipment',
|
||||
validate(gymEquipmentSchema),
|
||||
async (req, res, next) => {
|
||||
try {
|
||||
const { equipmentId, quantity } = req.body as z.infer<typeof gymEquipmentSchema>;
|
||||
const result = await addGymEquipment(
|
||||
prisma,
|
||||
req.params.id,
|
||||
equipmentId,
|
||||
quantity ?? 1,
|
||||
);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
},
|
||||
asyncHandler(async (req, res) => {
|
||||
const { equipmentId, quantity } = req.body as z.infer<typeof gymEquipmentSchema>;
|
||||
const result = await addGymEquipment(
|
||||
prisma,
|
||||
req.params.id,
|
||||
equipmentId,
|
||||
quantity ?? 1,
|
||||
);
|
||||
res.json(result);
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
@@ -298,20 +287,16 @@ export function createAdminRoutes({ prisma }: AppDeps) {
|
||||
router.patch(
|
||||
'/gyms/:id/equipment/:equipmentId',
|
||||
validate(gymEquipmentQuantitySchema),
|
||||
async (req, res, next) => {
|
||||
try {
|
||||
const { quantity } = req.body as z.infer<typeof gymEquipmentQuantitySchema>;
|
||||
const result = await updateGymEquipmentQuantity(
|
||||
prisma,
|
||||
req.params.id,
|
||||
req.params.equipmentId,
|
||||
quantity,
|
||||
);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
},
|
||||
asyncHandler(async (req, res) => {
|
||||
const { quantity } = req.body as z.infer<typeof gymEquipmentQuantitySchema>;
|
||||
const result = await updateGymEquipmentQuantity(
|
||||
prisma,
|
||||
req.params.id,
|
||||
req.params.equipmentId,
|
||||
quantity,
|
||||
);
|
||||
res.json(result);
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
@@ -325,14 +310,13 @@ export function createAdminRoutes({ prisma }: AppDeps) {
|
||||
* 400: { description: Equipment not in gym }
|
||||
* 404: { description: Gym not found }
|
||||
*/
|
||||
router.delete('/gyms/:id/equipment/:equipmentId', async (req, res, next) => {
|
||||
try {
|
||||
router.delete(
|
||||
'/gyms/:id/equipment/:equipmentId',
|
||||
asyncHandler(async (req, res) => {
|
||||
await removeGymEquipment(prisma, req.params.id, req.params.equipmentId);
|
||||
res.status(204).end();
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
const claimsQuerySchema = z.object({
|
||||
status: z.enum(['PENDING', 'APPROVED', 'REJECTED']).optional(),
|
||||
@@ -347,15 +331,15 @@ export function createAdminRoutes({ prisma }: AppDeps) {
|
||||
* responses:
|
||||
* 200: { description: OK }
|
||||
*/
|
||||
router.get('/gyms/claims', validate(claimsQuerySchema, 'query'), async (req, res, next) => {
|
||||
try {
|
||||
router.get(
|
||||
'/gyms/claims',
|
||||
validate(claimsQuerySchema, 'query'),
|
||||
asyncHandler(async (req, res) => {
|
||||
const q = req.query as z.infer<typeof claimsQuerySchema>;
|
||||
const result = await listGymClaims(prisma, q.status);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -367,14 +351,13 @@ export function createAdminRoutes({ prisma }: AppDeps) {
|
||||
* 200: { description: OK }
|
||||
* 404: { description: Claim not found }
|
||||
*/
|
||||
router.get('/gyms/claims/:id', async (req, res, next) => {
|
||||
try {
|
||||
router.get(
|
||||
'/gyms/claims/:id',
|
||||
asyncHandler(async (req, res) => {
|
||||
const result = await getGymClaim(prisma, req.params.id);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -386,8 +369,9 @@ export function createAdminRoutes({ prisma }: AppDeps) {
|
||||
* 200: { description: File (attachment) }
|
||||
* 404: { description: Claim or document not found }
|
||||
*/
|
||||
router.get('/gyms/claims/:id/documents/:docId', async (req, res, next) => {
|
||||
try {
|
||||
router.get(
|
||||
'/gyms/claims/:id/documents/:docId',
|
||||
asyncHandler(async (req, res) => {
|
||||
const claim = await prisma.gymClaim.findUnique({
|
||||
where: { id: req.params.id },
|
||||
include: {
|
||||
@@ -401,10 +385,8 @@ export function createAdminRoutes({ prisma }: AppDeps) {
|
||||
if (!fs.existsSync(filePath)) throw notFound('Document file missing');
|
||||
const safeName = doc.originalName.replace(/[\\/]/g, '_');
|
||||
res.download(filePath, safeName);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -417,8 +399,9 @@ export function createAdminRoutes({ prisma }: AppDeps) {
|
||||
* 404: { description: Claim not found }
|
||||
* 409: { description: Claim is not pending }
|
||||
*/
|
||||
router.post('/gyms/claims/:id/approve', async (req, res, next) => {
|
||||
try {
|
||||
router.post(
|
||||
'/gyms/claims/:id/approve',
|
||||
asyncHandler(async (req, res) => {
|
||||
await approveGymClaim(prisma, req.user!.id, req.params.id);
|
||||
await logActivity(prisma, {
|
||||
actorId: req.user!.id,
|
||||
@@ -427,10 +410,8 @@ export function createAdminRoutes({ prisma }: AppDeps) {
|
||||
entityId: req.params.id,
|
||||
});
|
||||
res.json({ ok: true });
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
const rejectClaimSchema = z.object({
|
||||
reason: z.string().max(1000).optional(),
|
||||
@@ -450,22 +431,18 @@ export function createAdminRoutes({ prisma }: AppDeps) {
|
||||
router.post(
|
||||
'/gyms/claims/:id/reject',
|
||||
validate(rejectClaimSchema),
|
||||
async (req, res, next) => {
|
||||
try {
|
||||
const { reason } = req.body as z.infer<typeof rejectClaimSchema>;
|
||||
await rejectGymClaim(prisma, req.user!.id, req.params.id, reason);
|
||||
await logActivity(prisma, {
|
||||
actorId: req.user!.id,
|
||||
action: 'gym.claim.rejected',
|
||||
entityType: 'gym-claim',
|
||||
entityId: req.params.id,
|
||||
details: { reason: reason ?? null },
|
||||
});
|
||||
res.json({ ok: true });
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
},
|
||||
asyncHandler(async (req, res) => {
|
||||
const { reason } = req.body as z.infer<typeof rejectClaimSchema>;
|
||||
await rejectGymClaim(prisma, req.user!.id, req.params.id, reason);
|
||||
await logActivity(prisma, {
|
||||
actorId: req.user!.id,
|
||||
action: 'gym.claim.rejected',
|
||||
entityType: 'gym-claim',
|
||||
entityId: req.params.id,
|
||||
details: { reason: reason ?? null },
|
||||
});
|
||||
res.json({ ok: true });
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
@@ -478,14 +455,13 @@ export function createAdminRoutes({ prisma }: AppDeps) {
|
||||
* 200: { description: OK }
|
||||
* 404: { description: Gym not found }
|
||||
*/
|
||||
router.get('/gyms/:id', async (req, res, next) => {
|
||||
try {
|
||||
router.get(
|
||||
'/gyms/:id',
|
||||
asyncHandler(async (req, res) => {
|
||||
const gym = await getGym(prisma, req.params.id);
|
||||
res.json(gym);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -496,15 +472,14 @@ export function createAdminRoutes({ prisma }: AppDeps) {
|
||||
* responses:
|
||||
* 200: { description: OK }
|
||||
*/
|
||||
router.get('/users', async (req, res, next) => {
|
||||
try {
|
||||
router.get(
|
||||
'/users',
|
||||
asyncHandler(async (req, res) => {
|
||||
const search = req.query.search as string | undefined;
|
||||
const result = await listUsers(prisma, search);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -516,15 +491,15 @@ export function createAdminRoutes({ prisma }: AppDeps) {
|
||||
* 201: { description: Created }
|
||||
* 409: { description: Email already registered }
|
||||
*/
|
||||
router.post('/users', validate(createUserSchema), async (req, res, next) => {
|
||||
try {
|
||||
router.post(
|
||||
'/users',
|
||||
validate(createUserSchema),
|
||||
asyncHandler(async (req, res) => {
|
||||
const data = req.body as z.infer<typeof createUserSchema>;
|
||||
const result = await createUser(prisma, data);
|
||||
res.status(201).json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -537,15 +512,15 @@ export function createAdminRoutes({ prisma }: AppDeps) {
|
||||
* 404: { description: User not found }
|
||||
* 409: { description: Email already registered }
|
||||
*/
|
||||
router.patch('/users/:id', validate(updateUserSchema), async (req, res, next) => {
|
||||
try {
|
||||
router.patch(
|
||||
'/users/:id',
|
||||
validate(updateUserSchema),
|
||||
asyncHandler(async (req, res) => {
|
||||
const data = req.body as z.infer<typeof updateUserSchema>;
|
||||
const result = await updateUser(prisma, req.params.id, data);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -556,14 +531,13 @@ export function createAdminRoutes({ prisma }: AppDeps) {
|
||||
* responses:
|
||||
* 204: { description: Deleted }
|
||||
*/
|
||||
router.delete('/users/:id', async (req, res, next) => {
|
||||
try {
|
||||
router.delete(
|
||||
'/users/:id',
|
||||
asyncHandler(async (req, res) => {
|
||||
await deleteUser(prisma, req.params.id);
|
||||
res.status(204).end();
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -578,15 +552,11 @@ export function createAdminRoutes({ prisma }: AppDeps) {
|
||||
router.post(
|
||||
'/users/:id/memberships',
|
||||
validate(membershipSchema),
|
||||
async (req, res, next) => {
|
||||
try {
|
||||
const { gymId, roles } = req.body as z.infer<typeof membershipSchema>;
|
||||
const result = await addMembership(prisma, req.params.id, gymId, roles);
|
||||
res.status(201).json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
},
|
||||
asyncHandler(async (req, res) => {
|
||||
const { gymId, roles } = req.body as z.infer<typeof membershipSchema>;
|
||||
const result = await addMembership(prisma, req.params.id, gymId, roles);
|
||||
res.status(201).json(result);
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
@@ -602,15 +572,11 @@ export function createAdminRoutes({ prisma }: AppDeps) {
|
||||
router.patch(
|
||||
'/users/:id/memberships/:gymId',
|
||||
validate(updateMembershipSchema),
|
||||
async (req, res, next) => {
|
||||
try {
|
||||
const { roles } = req.body as z.infer<typeof updateMembershipSchema>;
|
||||
const result = await updateMembership(prisma, req.params.id, req.params.gymId, roles);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
},
|
||||
asyncHandler(async (req, res) => {
|
||||
const { roles } = req.body as z.infer<typeof updateMembershipSchema>;
|
||||
const result = await updateMembership(prisma, req.params.id, req.params.gymId, roles);
|
||||
res.json(result);
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
@@ -622,14 +588,13 @@ export function createAdminRoutes({ prisma }: AppDeps) {
|
||||
* responses:
|
||||
* 204: { description: Deleted }
|
||||
*/
|
||||
router.delete('/users/:id/memberships/:gymId', async (req, res, next) => {
|
||||
try {
|
||||
router.delete(
|
||||
'/users/:id/memberships/:gymId',
|
||||
asyncHandler(async (req, res) => {
|
||||
await removeMembership(prisma, req.params.id, req.params.gymId);
|
||||
res.status(204).end();
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -641,14 +606,13 @@ export function createAdminRoutes({ prisma }: AppDeps) {
|
||||
* 200: { description: OK }
|
||||
* 404: { description: User not found }
|
||||
*/
|
||||
router.get('/users/:id/focus-chain', async (req, res, next) => {
|
||||
try {
|
||||
router.get(
|
||||
'/users/:id/focus-chain',
|
||||
asyncHandler(async (req, res) => {
|
||||
const result = await getFocusChain(prisma, req.params.id);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
const logsQuerySchema = z.object({
|
||||
action: z.string().min(1).max(100).optional(),
|
||||
@@ -669,8 +633,10 @@ export function createAdminRoutes({ prisma }: AppDeps) {
|
||||
* responses:
|
||||
* 200: { description: OK, returns { logs, total } }
|
||||
*/
|
||||
router.get('/logs', validate(logsQuerySchema, 'query'), async (req, res, next) => {
|
||||
try {
|
||||
router.get(
|
||||
'/logs',
|
||||
validate(logsQuerySchema, 'query'),
|
||||
asyncHandler(async (req, res) => {
|
||||
const q = req.query as z.infer<typeof logsQuerySchema>;
|
||||
const result = await listActivityLogs(prisma, {
|
||||
action: q.action,
|
||||
@@ -681,10 +647,8 @@ export function createAdminRoutes({ prisma }: AppDeps) {
|
||||
offset: q.offset ?? 0,
|
||||
});
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
const focusAreaSchema = z.object({
|
||||
name: z.string().min(1).max(100),
|
||||
@@ -710,14 +674,13 @@ export function createAdminRoutes({ prisma }: AppDeps) {
|
||||
* responses:
|
||||
* 200: { description: OK }
|
||||
*/
|
||||
router.get('/focus-areas', async (_req, res, next) => {
|
||||
try {
|
||||
router.get(
|
||||
'/focus-areas',
|
||||
asyncHandler(async (_req, res) => {
|
||||
const result = await listFocusAreas(prisma);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -732,15 +695,11 @@ export function createAdminRoutes({ prisma }: AppDeps) {
|
||||
router.post(
|
||||
'/focus-areas',
|
||||
validate(focusAreaSchema),
|
||||
async (req, res, next) => {
|
||||
try {
|
||||
const data = req.body as z.infer<typeof focusAreaSchema>;
|
||||
const result = await createFocusArea(prisma, data);
|
||||
res.status(201).json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
},
|
||||
asyncHandler(async (req, res) => {
|
||||
const data = req.body as z.infer<typeof focusAreaSchema>;
|
||||
const result = await createFocusArea(prisma, data);
|
||||
res.status(201).json(result);
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
@@ -756,15 +715,11 @@ export function createAdminRoutes({ prisma }: AppDeps) {
|
||||
router.patch(
|
||||
'/focus-areas/:id',
|
||||
validate(focusAreaUpdateSchema),
|
||||
async (req, res, next) => {
|
||||
try {
|
||||
const data = req.body as z.infer<typeof focusAreaUpdateSchema>;
|
||||
const result = await updateFocusArea(prisma, req.params.id, data);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
},
|
||||
asyncHandler(async (req, res) => {
|
||||
const data = req.body as z.infer<typeof focusAreaUpdateSchema>;
|
||||
const result = await updateFocusArea(prisma, req.params.id, data);
|
||||
res.json(result);
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
@@ -777,14 +732,13 @@ export function createAdminRoutes({ prisma }: AppDeps) {
|
||||
* 204: { description: Deleted }
|
||||
* 404: { description: Not found }
|
||||
*/
|
||||
router.delete('/focus-areas/:id', async (req, res, next) => {
|
||||
try {
|
||||
router.delete(
|
||||
'/focus-areas/:id',
|
||||
asyncHandler(async (req, res) => {
|
||||
await deleteFocusArea(prisma, req.params.id);
|
||||
res.status(204).end();
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
return router;
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import { REFRESH_COOKIE } from '../lib/jwt.js';
|
||||
import { validate } from '../middleware/validate.js';
|
||||
import { authLimiter } from '../middleware/rateLimit.js';
|
||||
import { AppError, unauthorized } from '../lib/errors.js';
|
||||
import { asyncHandler } from '../lib/asyncHandler.js';
|
||||
import { login, register, refresh } from '../services/auth.js';
|
||||
import { logActivity } from '../services/audit.js';
|
||||
|
||||
@@ -57,8 +58,11 @@ export function createAuthRoutes({ prisma }: AppDeps) {
|
||||
* 201: { description: Created, returns user + access token }
|
||||
* 409: { description: Email already registered }
|
||||
*/
|
||||
router.post('/register', authLimiter, validate(registerSchema), async (req, res, next) => {
|
||||
try {
|
||||
router.post(
|
||||
'/register',
|
||||
authLimiter,
|
||||
validate(registerSchema),
|
||||
asyncHandler(async (req, res) => {
|
||||
const data = req.body as z.infer<typeof registerSchema>;
|
||||
const result = await register(prisma, data);
|
||||
await logActivity(prisma, {
|
||||
@@ -70,10 +74,8 @@ export function createAuthRoutes({ prisma }: AppDeps) {
|
||||
});
|
||||
setRefreshCookie(res, result.refreshToken);
|
||||
res.status(201).json({ user: result.user, accessToken: result.accessToken });
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -85,16 +87,17 @@ export function createAuthRoutes({ prisma }: AppDeps) {
|
||||
* 200: { description: OK, returns user + access token }
|
||||
* 401: { description: Invalid credentials }
|
||||
*/
|
||||
router.post('/login', authLimiter, validate(loginSchema), async (req, res, next) => {
|
||||
try {
|
||||
router.post(
|
||||
'/login',
|
||||
authLimiter,
|
||||
validate(loginSchema),
|
||||
asyncHandler(async (req, res) => {
|
||||
const data = req.body as z.infer<typeof loginSchema>;
|
||||
const result = await login(prisma, data);
|
||||
setRefreshCookie(res, result.refreshToken);
|
||||
res.json({ user: result.user, accessToken: result.accessToken });
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -106,17 +109,20 @@ export function createAuthRoutes({ prisma }: AppDeps) {
|
||||
* 200: { description: OK, returns new access token }
|
||||
* 401: { description: Missing/invalid/revoked refresh token }
|
||||
*/
|
||||
router.post('/refresh', async (req, res, next) => {
|
||||
try {
|
||||
router.post(
|
||||
'/refresh',
|
||||
asyncHandler(async (req, res) => {
|
||||
const token = (req.cookies as Record<string, string | undefined>)[REFRESH_COOKIE];
|
||||
if (!token) throw unauthorized('Missing refresh token');
|
||||
const result = await refresh(prisma, token);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
res.clearCookie(REFRESH_COOKIE, { path: '/' });
|
||||
next(e instanceof AppError ? e : unauthorized('Invalid refresh token'));
|
||||
}
|
||||
});
|
||||
try {
|
||||
if (!token) throw unauthorized('Missing refresh token');
|
||||
const result = await refresh(prisma, token);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
res.clearCookie(REFRESH_COOKIE, { path: '/' });
|
||||
throw e instanceof AppError ? e : unauthorized('Invalid refresh token');
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
|
||||
@@ -4,6 +4,7 @@ import type { AppDeps } from '../deps.js';
|
||||
import { authenticate } from '../middleware/auth.js';
|
||||
import { validate } from '../middleware/validate.js';
|
||||
import { getCalendar } from '../services/calendar.js';
|
||||
import { asyncHandler } from '../lib/asyncHandler.js';
|
||||
|
||||
const querySchema = z.object({
|
||||
month: z
|
||||
@@ -25,15 +26,15 @@ export function createCalendarRoutes({ prisma }: AppDeps) {
|
||||
* responses:
|
||||
* 200: { description: OK }
|
||||
*/
|
||||
router.get('/', validate(querySchema, 'query'), async (req, res, next) => {
|
||||
try {
|
||||
router.get(
|
||||
'/',
|
||||
validate(querySchema, 'query'),
|
||||
asyncHandler(async (req, res) => {
|
||||
const { month } = req.query as z.infer<typeof querySchema>;
|
||||
const result = await getCalendar(prisma, req.user!.id, month);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
return router;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ import { Router } from 'express';
|
||||
import type { AppDeps } from '../deps.js';
|
||||
import { authenticate } from '../middleware/auth.js';
|
||||
import { getDashboard } from '../services/dashboard.js';
|
||||
import { asyncHandler } from '../lib/asyncHandler.js';
|
||||
|
||||
export function createDashboardRoutes({ prisma }: AppDeps) {
|
||||
const router = Router();
|
||||
@@ -16,14 +17,13 @@ export function createDashboardRoutes({ prisma }: AppDeps) {
|
||||
* responses:
|
||||
* 200: { description: OK }
|
||||
*/
|
||||
router.get('/', async (req, res, next) => {
|
||||
try {
|
||||
router.get(
|
||||
'/',
|
||||
asyncHandler(async (req, res) => {
|
||||
const result = await getDashboard(prisma, req.user!.id);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
return router;
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import {
|
||||
updateEquipment,
|
||||
} from '../services/equipment.js';
|
||||
import { equipmentKindSchema, validateEquipmentSpec } from '../lib/equipmentSpec.js';
|
||||
import { asyncHandler } from '../lib/asyncHandler.js';
|
||||
|
||||
/**
|
||||
* Equipment catalog entry. `kind` determines the shape of the type-specific `spec` payload:
|
||||
@@ -51,14 +52,13 @@ export function createEquipmentRoutes({ prisma }: AppDeps) {
|
||||
* responses:
|
||||
* 200: { description: OK }
|
||||
*/
|
||||
router.get('/', async (_req, res, next) => {
|
||||
try {
|
||||
router.get(
|
||||
'/',
|
||||
asyncHandler(async (_req, res) => {
|
||||
const result = await listEquipment(prisma);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -70,15 +70,15 @@ export function createEquipmentRoutes({ prisma }: AppDeps) {
|
||||
* 201: { description: Created }
|
||||
* 409: { description: Name already exists }
|
||||
*/
|
||||
router.post('/', validate(equipmentSchema), async (req, res, next) => {
|
||||
try {
|
||||
router.post(
|
||||
'/',
|
||||
validate(equipmentSchema),
|
||||
asyncHandler(async (req, res) => {
|
||||
const data = req.body as z.infer<typeof equipmentSchema>;
|
||||
const result = await createEquipment(prisma, data);
|
||||
res.status(201).json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -90,15 +90,15 @@ export function createEquipmentRoutes({ prisma }: AppDeps) {
|
||||
* 200: { description: OK }
|
||||
* 404: { description: Not found }
|
||||
*/
|
||||
router.patch('/:id', validate(equipmentUpdateSchema), async (req, res, next) => {
|
||||
try {
|
||||
router.patch(
|
||||
'/:id',
|
||||
validate(equipmentUpdateSchema),
|
||||
asyncHandler(async (req, res) => {
|
||||
const data = req.body as z.infer<typeof equipmentUpdateSchema>;
|
||||
const result = await updateEquipment(prisma, req.params.id, data);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -110,14 +110,13 @@ export function createEquipmentRoutes({ prisma }: AppDeps) {
|
||||
* 204: { description: Deleted }
|
||||
* 404: { description: Not found }
|
||||
*/
|
||||
router.delete('/:id', async (req, res, next) => {
|
||||
try {
|
||||
router.delete(
|
||||
'/:id',
|
||||
asyncHandler(async (req, res) => {
|
||||
await deleteEquipment(prisma, req.params.id);
|
||||
res.status(204).end();
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
return router;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ import { Router } from 'express';
|
||||
import type { AppDeps } from '../deps.js';
|
||||
import { authenticate } from '../middleware/auth.js';
|
||||
import { listEquipment } from '../services/equipment.js';
|
||||
import { asyncHandler } from '../lib/asyncHandler.js';
|
||||
|
||||
export function createEquipmentPublicRoutes({ prisma }: AppDeps) {
|
||||
const router = Router();
|
||||
@@ -16,14 +17,13 @@ export function createEquipmentPublicRoutes({ prisma }: AppDeps) {
|
||||
* responses:
|
||||
* 200: { description: OK }
|
||||
*/
|
||||
router.get('/', async (_req, res, next) => {
|
||||
try {
|
||||
router.get(
|
||||
'/',
|
||||
asyncHandler(async (_req, res) => {
|
||||
const result = await listEquipment(prisma);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
return router;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ import { Router } from 'express';
|
||||
import type { AppDeps } from '../deps.js';
|
||||
import { authenticate } from '../middleware/auth.js';
|
||||
import { getExercisePool, listExercisePool } from '../services/exercisePool.js';
|
||||
import { asyncHandler } from '../lib/asyncHandler.js';
|
||||
|
||||
export function createExercisePoolRoutes({ prisma }: AppDeps) {
|
||||
const router = Router();
|
||||
@@ -16,14 +17,13 @@ export function createExercisePoolRoutes({ prisma }: AppDeps) {
|
||||
* responses:
|
||||
* 200: { description: OK }
|
||||
*/
|
||||
router.get('/', async (_req, res, next) => {
|
||||
try {
|
||||
router.get(
|
||||
'/',
|
||||
asyncHandler(async (_req, res) => {
|
||||
const result = await listExercisePool(prisma);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -35,14 +35,13 @@ export function createExercisePoolRoutes({ prisma }: AppDeps) {
|
||||
* 200: { description: OK }
|
||||
* 404: { description: Not found }
|
||||
*/
|
||||
router.get('/:id', async (req, res, next) => {
|
||||
try {
|
||||
router.get(
|
||||
'/:id',
|
||||
asyncHandler(async (req, res) => {
|
||||
const result = await getExercisePool(prisma, req.params.id);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
return router;
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import {
|
||||
listExercises,
|
||||
updateExercise,
|
||||
} from '../services/exercises.js';
|
||||
import { asyncHandler } from '../lib/asyncHandler.js';
|
||||
|
||||
const muscleArraySchema = z.array(z.string().min(1).max(50)).max(20);
|
||||
|
||||
@@ -48,14 +49,13 @@ export function createExerciseRoutes({ prisma }: AppDeps) {
|
||||
* responses:
|
||||
* 200: { description: OK }
|
||||
*/
|
||||
router.get('/', async (_req, res, next) => {
|
||||
try {
|
||||
router.get(
|
||||
'/',
|
||||
asyncHandler(async (_req, res) => {
|
||||
const result = await listExercises(prisma);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -66,15 +66,15 @@ export function createExerciseRoutes({ prisma }: AppDeps) {
|
||||
* responses:
|
||||
* 201: { description: Created }
|
||||
*/
|
||||
router.post('/', validate(exerciseSchema), async (req, res, next) => {
|
||||
try {
|
||||
router.post(
|
||||
'/',
|
||||
validate(exerciseSchema),
|
||||
asyncHandler(async (req, res) => {
|
||||
const data = req.body as z.infer<typeof exerciseSchema>;
|
||||
const result = await createExercise(prisma, data);
|
||||
res.status(201).json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -86,14 +86,13 @@ export function createExerciseRoutes({ prisma }: AppDeps) {
|
||||
* 200: { description: OK }
|
||||
* 404: { description: Not found }
|
||||
*/
|
||||
router.get('/:id', async (req, res, next) => {
|
||||
try {
|
||||
router.get(
|
||||
'/:id',
|
||||
asyncHandler(async (req, res) => {
|
||||
const result = await getExercise(prisma, req.params.id);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -105,15 +104,15 @@ export function createExerciseRoutes({ prisma }: AppDeps) {
|
||||
* 200: { description: OK }
|
||||
* 404: { description: Not found }
|
||||
*/
|
||||
router.patch('/:id', validate(exerciseUpdateSchema), async (req, res, next) => {
|
||||
try {
|
||||
router.patch(
|
||||
'/:id',
|
||||
validate(exerciseUpdateSchema),
|
||||
asyncHandler(async (req, res) => {
|
||||
const data = req.body as z.infer<typeof exerciseUpdateSchema>;
|
||||
const result = await updateExercise(prisma, req.params.id, data);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -126,14 +125,13 @@ export function createExerciseRoutes({ prisma }: AppDeps) {
|
||||
* 404: { description: Not found }
|
||||
* 409: { description: Used in plans }
|
||||
*/
|
||||
router.delete('/:id', async (req, res, next) => {
|
||||
try {
|
||||
router.delete(
|
||||
'/:id',
|
||||
asyncHandler(async (req, res) => {
|
||||
await deleteExercise(prisma, req.params.id);
|
||||
res.status(204).end();
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
return router;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ import { Router } from 'express';
|
||||
import type { AppDeps } from '../deps.js';
|
||||
import { authenticate } from '../middleware/auth.js';
|
||||
import { listFocusAreas } from '../services/focusAreas.js';
|
||||
import { asyncHandler } from '../lib/asyncHandler.js';
|
||||
|
||||
export function createFocusAreaRoutes({ prisma }: AppDeps) {
|
||||
const router = Router();
|
||||
@@ -16,14 +17,13 @@ export function createFocusAreaRoutes({ prisma }: AppDeps) {
|
||||
* responses:
|
||||
* 200: { description: OK }
|
||||
*/
|
||||
router.get('/', async (_req, res, next) => {
|
||||
try {
|
||||
router.get(
|
||||
'/',
|
||||
asyncHandler(async (_req, res) => {
|
||||
const result = await listFocusAreas(prisma);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
return router;
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ import {
|
||||
updateOwnedGym,
|
||||
} from '../services/gyms.js';
|
||||
import { logActivity } from '../services/audit.js';
|
||||
import { asyncHandler } from '../lib/asyncHandler.js';
|
||||
import { createGymSchema, rateGymSchema, updateGymSchema } from './gyms/schemas.js';
|
||||
import { createGymClaimRoutes } from './gyms/claims.js';
|
||||
import { createGymInvitationRoutes } from './gyms/invitations.js';
|
||||
@@ -39,14 +40,13 @@ export function createGymRoutes(deps: AppDeps) {
|
||||
* responses:
|
||||
* 200: { description: OK }
|
||||
*/
|
||||
router.get('/', async (req, res, next) => {
|
||||
try {
|
||||
router.get(
|
||||
'/',
|
||||
asyncHandler(async (req, res) => {
|
||||
const result = await listUserGyms(prisma, req.user!.id);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -57,14 +57,13 @@ export function createGymRoutes(deps: AppDeps) {
|
||||
* responses:
|
||||
* 200: { description: OK }
|
||||
*/
|
||||
router.get('/all', async (_req, res, next) => {
|
||||
try {
|
||||
router.get(
|
||||
'/all',
|
||||
asyncHandler(async (_req, res) => {
|
||||
const result = await listAllGyms(prisma);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -77,8 +76,10 @@ export function createGymRoutes(deps: AppDeps) {
|
||||
* 400: { description: Validation failed (e.g. missing location for VIRTUAL) }
|
||||
* 403: { description: Non-admin requested kind=REAL }
|
||||
*/
|
||||
router.post('/', validate(createGymSchema), async (req, res, next) => {
|
||||
try {
|
||||
router.post(
|
||||
'/',
|
||||
validate(createGymSchema),
|
||||
asyncHandler(async (req, res) => {
|
||||
const data = req.body as z.infer<typeof createGymSchema>;
|
||||
const result = await createGym(prisma, req.user!.id, data, req.user!.isAdmin);
|
||||
await logActivity(prisma, {
|
||||
@@ -89,10 +90,8 @@ export function createGymRoutes(deps: AppDeps) {
|
||||
details: { name: result.gym.name, kind: result.gym.kind },
|
||||
});
|
||||
res.status(201).json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -103,14 +102,13 @@ export function createGymRoutes(deps: AppDeps) {
|
||||
* responses:
|
||||
* 200: { description: OK }
|
||||
*/
|
||||
router.get('/virtual', async (req, res, next) => {
|
||||
try {
|
||||
router.get(
|
||||
'/virtual',
|
||||
asyncHandler(async (req, res) => {
|
||||
const result = await listVirtualGyms(prisma, req.user!.id);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
// Per-gym sub-routes: gallery photos, then equipment/exceptions.
|
||||
router.use('/:gymId/photos', createGymPhotoRoutes(deps));
|
||||
@@ -127,14 +125,13 @@ export function createGymRoutes(deps: AppDeps) {
|
||||
* 403: { description: VIRTUAL gym and the caller is not a manager }
|
||||
* 404: { description: Gym not found }
|
||||
*/
|
||||
router.get('/:gymId', async (req, res, next) => {
|
||||
try {
|
||||
router.get(
|
||||
'/:gymId',
|
||||
asyncHandler(async (req, res) => {
|
||||
const result = await getGymPublic(prisma, req.user!.id, req.user!.isAdmin, req.params.gymId);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -146,8 +143,9 @@ export function createGymRoutes(deps: AppDeps) {
|
||||
* 201: { description: Joined }
|
||||
* 409: { description: Already a member }
|
||||
*/
|
||||
router.post('/:gymId/join', async (req, res, next) => {
|
||||
try {
|
||||
router.post(
|
||||
'/:gymId/join',
|
||||
asyncHandler(async (req, res) => {
|
||||
const result = await joinGym(prisma, req.user!.id, req.params.gymId);
|
||||
await logActivity(prisma, {
|
||||
actorId: req.user!.id,
|
||||
@@ -157,10 +155,8 @@ export function createGymRoutes(deps: AppDeps) {
|
||||
details: { name: result.membership.name },
|
||||
});
|
||||
res.status(201).json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -174,15 +170,15 @@ export function createGymRoutes(deps: AppDeps) {
|
||||
* 404: { description: Gym not found }
|
||||
* 409: { description: Gym is not a public virtual gym }
|
||||
*/
|
||||
router.post('/:gymId/rate', validate(rateGymSchema), async (req, res, next) => {
|
||||
try {
|
||||
router.post(
|
||||
'/:gymId/rate',
|
||||
validate(rateGymSchema),
|
||||
asyncHandler(async (req, res) => {
|
||||
const { vote } = req.body as z.infer<typeof rateGymSchema>;
|
||||
const result = await rateGym(prisma, req.user!.id, req.params.gymId, vote);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -195,8 +191,10 @@ export function createGymRoutes(deps: AppDeps) {
|
||||
* 403: { description: Not the gym owner }
|
||||
* 404: { description: Gym not found }
|
||||
*/
|
||||
router.patch('/:gymId', validate(updateGymSchema), async (req, res, next) => {
|
||||
try {
|
||||
router.patch(
|
||||
'/:gymId',
|
||||
validate(updateGymSchema),
|
||||
asyncHandler(async (req, res) => {
|
||||
const data = req.body as z.infer<typeof updateGymSchema>;
|
||||
const result = await updateOwnedGym(prisma, req.params.gymId, req.user!.id, data);
|
||||
await logActivity(prisma, {
|
||||
@@ -207,10 +205,8 @@ export function createGymRoutes(deps: AppDeps) {
|
||||
details: { name: result.gym.name },
|
||||
});
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -223,8 +219,9 @@ export function createGymRoutes(deps: AppDeps) {
|
||||
* 403: { description: Not the gym owner }
|
||||
* 404: { description: Gym not found }
|
||||
*/
|
||||
router.delete('/:gymId', async (req, res, next) => {
|
||||
try {
|
||||
router.delete(
|
||||
'/:gymId',
|
||||
asyncHandler(async (req, res) => {
|
||||
const result = await deleteOwnedGym(prisma, req.params.gymId, req.user!.id);
|
||||
await logActivity(prisma, {
|
||||
actorId: req.user!.id,
|
||||
@@ -234,10 +231,8 @@ export function createGymRoutes(deps: AppDeps) {
|
||||
details: { name: result.name },
|
||||
});
|
||||
res.status(204).end();
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
return router;
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import type { Availability } from '../../services/me.js';
|
||||
import { logActivity } from '../../services/audit.js';
|
||||
import { MAX_CLAIM_FILES, claimUpload, removeTmpFiles } from '../../lib/uploads.js';
|
||||
import { badRequest } from '../../lib/errors.js';
|
||||
import { asyncHandler } from '../../lib/asyncHandler.js';
|
||||
import { gymClaimSchema } from './schemas.js';
|
||||
|
||||
/** Mounted at /gyms/claims. */
|
||||
@@ -42,7 +43,7 @@ export function createGymClaimRoutes({ prisma }: AppDeps) {
|
||||
next();
|
||||
}),
|
||||
validate(gymClaimSchema),
|
||||
async (req, res, next) => {
|
||||
asyncHandler(async (req, res) => {
|
||||
try {
|
||||
const body = req.body as z.infer<typeof gymClaimSchema>;
|
||||
let gymAvailability: Availability | null | undefined;
|
||||
@@ -87,9 +88,9 @@ export function createGymClaimRoutes({ prisma }: AppDeps) {
|
||||
res.status(201).json(result);
|
||||
} catch (e) {
|
||||
removeTmpFiles(((req.files as Express.Multer.File[] | undefined) ?? []).map((f) => ({ path: f.path })));
|
||||
next(e);
|
||||
throw e;
|
||||
}
|
||||
},
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
@@ -101,14 +102,13 @@ export function createGymClaimRoutes({ prisma }: AppDeps) {
|
||||
* responses:
|
||||
* 200: { description: OK }
|
||||
*/
|
||||
router.get('/', async (req, res, next) => {
|
||||
try {
|
||||
router.get(
|
||||
'/',
|
||||
asyncHandler(async (req, res) => {
|
||||
const result = await listMyGymClaims(prisma, req.user!.id);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -121,8 +121,9 @@ export function createGymClaimRoutes({ prisma }: AppDeps) {
|
||||
* 403: { description: Not the claim author }
|
||||
* 409: { description: Claim is not pending }
|
||||
*/
|
||||
router.delete('/:claimId', async (req, res, next) => {
|
||||
try {
|
||||
router.delete(
|
||||
'/:claimId',
|
||||
asyncHandler(async (req, res) => {
|
||||
await withdrawGymClaim(prisma, req.user!.id, req.params.claimId);
|
||||
await logActivity(prisma, {
|
||||
actorId: req.user!.id,
|
||||
@@ -131,10 +132,8 @@ export function createGymClaimRoutes({ prisma }: AppDeps) {
|
||||
entityId: req.params.claimId,
|
||||
});
|
||||
res.status(204).end();
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
return router;
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import {
|
||||
withdrawGymInvitation,
|
||||
} from '../../services/gyms.js';
|
||||
import { logActivity } from '../../services/audit.js';
|
||||
import { asyncHandler } from '../../lib/asyncHandler.js';
|
||||
import { gymInvitationSchema, gymInviteeSearchSchema } from './schemas.js';
|
||||
|
||||
/** Mounted at /gyms/invitations. */
|
||||
@@ -30,8 +31,10 @@ export function createGymInvitationRoutes({ prisma }: AppDeps) {
|
||||
* 404: { description: Gym or user not found }
|
||||
* 409: { description: Gym not private virtual / already a member / pending invitation exists }
|
||||
*/
|
||||
router.post('/', validate(gymInvitationSchema), async (req, res, next) => {
|
||||
try {
|
||||
router.post(
|
||||
'/',
|
||||
validate(gymInvitationSchema),
|
||||
asyncHandler(async (req, res) => {
|
||||
const { gymId, userId } = req.body as z.infer<typeof gymInvitationSchema>;
|
||||
const result = await sendGymInvitation(prisma, req.user!.id, gymId, userId);
|
||||
await logActivity(prisma, {
|
||||
@@ -42,10 +45,8 @@ export function createGymInvitationRoutes({ prisma }: AppDeps) {
|
||||
details: { gymId, userId },
|
||||
});
|
||||
res.status(201).json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -56,14 +57,13 @@ export function createGymInvitationRoutes({ prisma }: AppDeps) {
|
||||
* responses:
|
||||
* 200: { description: OK }
|
||||
*/
|
||||
router.get('/', async (req, res, next) => {
|
||||
try {
|
||||
router.get(
|
||||
'/',
|
||||
asyncHandler(async (req, res) => {
|
||||
const result = await listGymInvitations(prisma, req.user!.id);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -79,21 +79,17 @@ export function createGymInvitationRoutes({ prisma }: AppDeps) {
|
||||
router.get(
|
||||
'/search',
|
||||
validate(gymInviteeSearchSchema, 'query'),
|
||||
async (req, res, next) => {
|
||||
try {
|
||||
const q = req.query as z.infer<typeof gymInviteeSearchSchema>;
|
||||
const result = await searchGymInvitees(
|
||||
prisma,
|
||||
req.user!.id,
|
||||
q.gymId,
|
||||
q.firstName,
|
||||
q.lastName,
|
||||
);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
},
|
||||
asyncHandler(async (req, res) => {
|
||||
const q = req.query as z.infer<typeof gymInviteeSearchSchema>;
|
||||
const result = await searchGymInvitees(
|
||||
prisma,
|
||||
req.user!.id,
|
||||
q.gymId,
|
||||
q.firstName,
|
||||
q.lastName,
|
||||
);
|
||||
res.json(result);
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
@@ -107,8 +103,9 @@ export function createGymInvitationRoutes({ prisma }: AppDeps) {
|
||||
* 403: { description: Not the invited user }
|
||||
* 410: { description: Invitation expired }
|
||||
*/
|
||||
router.post('/:invitationId/accept', async (req, res, next) => {
|
||||
try {
|
||||
router.post(
|
||||
'/:invitationId/accept',
|
||||
asyncHandler(async (req, res) => {
|
||||
const result = await acceptGymInvitation(prisma, req.user!.id, req.params.invitationId);
|
||||
await logActivity(prisma, {
|
||||
actorId: req.user!.id,
|
||||
@@ -117,10 +114,8 @@ export function createGymInvitationRoutes({ prisma }: AppDeps) {
|
||||
entityId: req.params.invitationId,
|
||||
});
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -133,8 +128,9 @@ export function createGymInvitationRoutes({ prisma }: AppDeps) {
|
||||
* 403: { description: Not the invited user }
|
||||
* 410: { description: Invitation expired }
|
||||
*/
|
||||
router.post('/:invitationId/decline', async (req, res, next) => {
|
||||
try {
|
||||
router.post(
|
||||
'/:invitationId/decline',
|
||||
asyncHandler(async (req, res) => {
|
||||
const result = await declineGymInvitation(prisma, req.user!.id, req.params.invitationId);
|
||||
await logActivity(prisma, {
|
||||
actorId: req.user!.id,
|
||||
@@ -143,10 +139,8 @@ export function createGymInvitationRoutes({ prisma }: AppDeps) {
|
||||
entityId: req.params.invitationId,
|
||||
});
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -159,8 +153,9 @@ export function createGymInvitationRoutes({ prisma }: AppDeps) {
|
||||
* 403: { description: Not the sender }
|
||||
* 409: { description: Invitation is not pending }
|
||||
*/
|
||||
router.delete('/:invitationId', async (req, res, next) => {
|
||||
try {
|
||||
router.delete(
|
||||
'/:invitationId',
|
||||
asyncHandler(async (req, res) => {
|
||||
await withdrawGymInvitation(prisma, req.user!.id, req.params.invitationId);
|
||||
await logActivity(prisma, {
|
||||
actorId: req.user!.id,
|
||||
@@ -169,10 +164,8 @@ export function createGymInvitationRoutes({ prisma }: AppDeps) {
|
||||
entityId: req.params.invitationId,
|
||||
});
|
||||
res.status(204).end();
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
return router;
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import {
|
||||
updateEquipmentQuantity,
|
||||
} from '../../services/gyms.js';
|
||||
import { equipmentQuantitySchema, equipmentSchema, gymExceptionSchema } from './schemas.js';
|
||||
import { asyncHandler } from '../../lib/asyncHandler.js';
|
||||
|
||||
type GymParams = { gymId: string };
|
||||
|
||||
@@ -33,22 +34,18 @@ export function createGymManagementRoutes({ prisma }: AppDeps) {
|
||||
router.post(
|
||||
'/equipment',
|
||||
validate(equipmentSchema),
|
||||
async (req, res, next) => {
|
||||
try {
|
||||
const { equipmentId, quantity } = req.body as z.infer<typeof equipmentSchema>;
|
||||
const { gymId } = req.params as GymParams;
|
||||
const result = await addEquipment(
|
||||
prisma,
|
||||
gymId,
|
||||
req.user!.id,
|
||||
equipmentId,
|
||||
quantity ?? 1,
|
||||
);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
},
|
||||
asyncHandler(async (req, res) => {
|
||||
const { equipmentId, quantity } = req.body as z.infer<typeof equipmentSchema>;
|
||||
const { gymId } = req.params as GymParams;
|
||||
const result = await addEquipment(
|
||||
prisma,
|
||||
gymId,
|
||||
req.user!.id,
|
||||
equipmentId,
|
||||
quantity ?? 1,
|
||||
);
|
||||
res.json(result);
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
@@ -65,22 +62,18 @@ export function createGymManagementRoutes({ prisma }: AppDeps) {
|
||||
router.patch(
|
||||
'/equipment/:equipmentId',
|
||||
validate(equipmentQuantitySchema),
|
||||
async (req, res, next) => {
|
||||
try {
|
||||
const { quantity } = req.body as z.infer<typeof equipmentQuantitySchema>;
|
||||
const { gymId } = req.params as GymParams;
|
||||
const result = await updateEquipmentQuantity(
|
||||
prisma,
|
||||
gymId,
|
||||
req.user!.id,
|
||||
req.params.equipmentId,
|
||||
quantity,
|
||||
);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
},
|
||||
asyncHandler(async (req, res) => {
|
||||
const { quantity } = req.body as z.infer<typeof equipmentQuantitySchema>;
|
||||
const { gymId } = req.params as GymParams;
|
||||
const result = await updateEquipmentQuantity(
|
||||
prisma,
|
||||
gymId,
|
||||
req.user!.id,
|
||||
req.params.equipmentId,
|
||||
quantity,
|
||||
);
|
||||
res.json(result);
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
@@ -93,15 +86,14 @@ export function createGymManagementRoutes({ prisma }: AppDeps) {
|
||||
* 204: { description: Removed }
|
||||
* 403: { description: Not the gym owner }
|
||||
*/
|
||||
router.delete('/equipment/:equipmentId', async (req, res, next) => {
|
||||
try {
|
||||
router.delete(
|
||||
'/equipment/:equipmentId',
|
||||
asyncHandler(async (req, res) => {
|
||||
const { gymId, equipmentId } = req.params as GymParams & { equipmentId: string };
|
||||
await removeEquipment(prisma, gymId, req.user!.id, equipmentId);
|
||||
res.status(204).end();
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -113,15 +105,14 @@ export function createGymManagementRoutes({ prisma }: AppDeps) {
|
||||
* 200: { description: OK }
|
||||
* 403: { description: Not the gym owner }
|
||||
*/
|
||||
router.get('/exceptions', async (req, res, next) => {
|
||||
try {
|
||||
router.get(
|
||||
'/exceptions',
|
||||
asyncHandler(async (req, res) => {
|
||||
const { gymId } = req.params as GymParams;
|
||||
const result = await listOwnedGymExceptions(prisma, gymId, req.user!.id);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -137,19 +128,15 @@ export function createGymManagementRoutes({ prisma }: AppDeps) {
|
||||
router.post(
|
||||
'/exceptions',
|
||||
validate(gymExceptionSchema),
|
||||
async (req, res, next) => {
|
||||
try {
|
||||
const data = req.body as z.infer<typeof gymExceptionSchema>;
|
||||
const { gymId } = req.params as GymParams;
|
||||
const result = await createOwnedGymException(prisma, gymId, req.user!.id, {
|
||||
date: data.date,
|
||||
availability: data.availability ?? null,
|
||||
});
|
||||
res.status(201).json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
},
|
||||
asyncHandler(async (req, res) => {
|
||||
const data = req.body as z.infer<typeof gymExceptionSchema>;
|
||||
const { gymId } = req.params as GymParams;
|
||||
const result = await createOwnedGymException(prisma, gymId, req.user!.id, {
|
||||
date: data.date,
|
||||
availability: data.availability ?? null,
|
||||
});
|
||||
res.status(201).json(result);
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
@@ -163,8 +150,9 @@ export function createGymManagementRoutes({ prisma }: AppDeps) {
|
||||
* 403: { description: Not the gym owner }
|
||||
* 404: { description: Exception not found }
|
||||
*/
|
||||
router.delete('/exceptions/:exceptionId', async (req, res, next) => {
|
||||
try {
|
||||
router.delete(
|
||||
'/exceptions/:exceptionId',
|
||||
asyncHandler(async (req, res) => {
|
||||
const { gymId, exceptionId } = req.params as GymParams & { exceptionId: string };
|
||||
await deleteOwnedGymException(
|
||||
prisma,
|
||||
@@ -173,10 +161,8 @@ export function createGymManagementRoutes({ prisma }: AppDeps) {
|
||||
exceptionId,
|
||||
);
|
||||
res.status(204).end();
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
return router;
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import {
|
||||
import { logActivity } from '../../services/audit.js';
|
||||
import { MAX_PHOTO_FILES, gymPhotoUpload, photoUploadDir, removeTmpFiles } from '../../lib/uploads.js';
|
||||
import { badRequest, forbidden, notFound } from '../../lib/errors.js';
|
||||
import { asyncHandler } from '../../lib/asyncHandler.js';
|
||||
|
||||
type GymParams = { gymId: string };
|
||||
|
||||
@@ -46,7 +47,7 @@ export function createGymPhotoRoutes({ prisma }: AppDeps) {
|
||||
}
|
||||
next();
|
||||
}),
|
||||
async (req, res, next) => {
|
||||
asyncHandler(async (req, res) => {
|
||||
const { gymId } = req.params as GymParams;
|
||||
try {
|
||||
const files = ((req.files as Express.Multer.File[] | undefined) ?? []).map((f) => ({
|
||||
@@ -71,9 +72,9 @@ export function createGymPhotoRoutes({ prisma }: AppDeps) {
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
removeTmpFiles(((req.files as Express.Multer.File[] | undefined) ?? []).map((f) => ({ path: f.path })));
|
||||
next(e);
|
||||
throw e;
|
||||
}
|
||||
},
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
@@ -87,9 +88,10 @@ export function createGymPhotoRoutes({ prisma }: AppDeps) {
|
||||
* 403: { description: Not the gym owner and not an admin }
|
||||
* 404: { description: Photo not found }
|
||||
*/
|
||||
router.delete('/:photoId', async (req, res, next) => {
|
||||
const { gymId, photoId } = req.params as GymParams & { photoId: string };
|
||||
try {
|
||||
router.delete(
|
||||
'/:photoId',
|
||||
asyncHandler(async (req, res) => {
|
||||
const { gymId, photoId } = req.params as GymParams & { photoId: string };
|
||||
const result = await deleteGymPhoto(
|
||||
prisma,
|
||||
req.user!.id,
|
||||
@@ -105,10 +107,8 @@ export function createGymPhotoRoutes({ prisma }: AppDeps) {
|
||||
details: { photoId },
|
||||
});
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -121,9 +121,10 @@ export function createGymPhotoRoutes({ prisma }: AppDeps) {
|
||||
* 403: { description: Not the gym owner and not an admin }
|
||||
* 404: { description: Photo not found }
|
||||
*/
|
||||
router.patch('/:photoId/cover', async (req, res, next) => {
|
||||
const { gymId, photoId } = req.params as GymParams & { photoId: string };
|
||||
try {
|
||||
router.patch(
|
||||
'/:photoId/cover',
|
||||
asyncHandler(async (req, res) => {
|
||||
const { gymId, photoId } = req.params as GymParams & { photoId: string };
|
||||
const result = await setGymPhotoCover(
|
||||
prisma,
|
||||
req.user!.id,
|
||||
@@ -139,10 +140,8 @@ export function createGymPhotoRoutes({ prisma }: AppDeps) {
|
||||
details: { photoId },
|
||||
});
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -155,8 +154,9 @@ export function createGymPhotoRoutes({ prisma }: AppDeps) {
|
||||
* 403: { description: VIRTUAL gym and the caller is not a manager }
|
||||
* 404: { description: Photo not found or file missing }
|
||||
*/
|
||||
router.get('/:photoId', async (req, res, next) => {
|
||||
try {
|
||||
router.get(
|
||||
'/:photoId',
|
||||
asyncHandler(async (req, res) => {
|
||||
const { gymId, photoId } = req.params as GymParams & { photoId: string };
|
||||
const photo = await prisma.gymPhoto.findUnique({ where: { id: photoId } });
|
||||
if (!photo || photo.gymId !== gymId) throw notFound('Photo not found');
|
||||
@@ -170,10 +170,8 @@ export function createGymPhotoRoutes({ prisma }: AppDeps) {
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
res.sendFile(filePath, (err) => (err ? reject(err) : resolve()));
|
||||
});
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
return router;
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ import { getActiveSession } from '../services/sessions.js';
|
||||
import { addMyComment, deleteMyComment } from '../services/comments.js';
|
||||
import { getExerciseProgress } from '../services/exerciseProgress.js';
|
||||
import { logActivity } from '../services/audit.js';
|
||||
import { asyncHandler } from '../lib/asyncHandler.js';
|
||||
|
||||
const muscleArraySchema = z.array(z.string().min(1).max(50)).max(20);
|
||||
|
||||
@@ -81,14 +82,13 @@ export function createMeRoutes({ prisma }: AppDeps) {
|
||||
* responses:
|
||||
* 200: { description: OK }
|
||||
*/
|
||||
router.get('/', async (req, res, next) => {
|
||||
try {
|
||||
router.get(
|
||||
'/',
|
||||
asyncHandler(async (req, res) => {
|
||||
const result = await getProfile(prisma, req.user!.id);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -99,14 +99,13 @@ export function createMeRoutes({ prisma }: AppDeps) {
|
||||
* responses:
|
||||
* 200: { description: OK }
|
||||
*/
|
||||
router.get('/active-session', async (req, res, next) => {
|
||||
try {
|
||||
router.get(
|
||||
'/active-session',
|
||||
asyncHandler(async (req, res) => {
|
||||
const result = await getActiveSession(prisma, req.user!.id);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -117,14 +116,13 @@ export function createMeRoutes({ prisma }: AppDeps) {
|
||||
* responses:
|
||||
* 200: { description: OK }
|
||||
*/
|
||||
router.get('/focus-chain', async (req, res, next) => {
|
||||
try {
|
||||
router.get(
|
||||
'/focus-chain',
|
||||
asyncHandler(async (req, res) => {
|
||||
const result = await getFocusChain(prisma, req.user!.id);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -135,8 +133,10 @@ export function createMeRoutes({ prisma }: AppDeps) {
|
||||
* responses:
|
||||
* 200: { description: OK }
|
||||
*/
|
||||
router.patch('/', validate(profileSchema), async (req, res, next) => {
|
||||
try {
|
||||
router.patch(
|
||||
'/',
|
||||
validate(profileSchema),
|
||||
asyncHandler(async (req, res) => {
|
||||
const data = req.body as z.infer<typeof profileSchema>;
|
||||
const result = await updateProfile(prisma, req.user!.id, data);
|
||||
await logActivity(prisma, {
|
||||
@@ -147,10 +147,8 @@ export function createMeRoutes({ prisma }: AppDeps) {
|
||||
details: { fields: Object.keys(data) },
|
||||
});
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -161,14 +159,13 @@ export function createMeRoutes({ prisma }: AppDeps) {
|
||||
* responses:
|
||||
* 200: { description: OK }
|
||||
*/
|
||||
router.get('/body-parameters', async (req, res, next) => {
|
||||
try {
|
||||
router.get(
|
||||
'/body-parameters',
|
||||
asyncHandler(async (req, res) => {
|
||||
const result = await getBodyParameters(prisma, req.user!.id);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -179,14 +176,13 @@ export function createMeRoutes({ prisma }: AppDeps) {
|
||||
* responses:
|
||||
* 200: { description: OK }
|
||||
*/
|
||||
router.get('/history', async (req, res, next) => {
|
||||
try {
|
||||
router.get(
|
||||
'/history',
|
||||
asyncHandler(async (req, res) => {
|
||||
const result = await getHistory(prisma, req.user!.id);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -197,14 +193,13 @@ export function createMeRoutes({ prisma }: AppDeps) {
|
||||
* responses:
|
||||
* 200: { description: OK }
|
||||
*/
|
||||
router.get('/exercise-progress', async (req, res, next) => {
|
||||
try {
|
||||
router.get(
|
||||
'/exercise-progress',
|
||||
asyncHandler(async (req, res) => {
|
||||
const result = await getExerciseProgress(prisma, req.user!.id);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -216,15 +211,15 @@ export function createMeRoutes({ prisma }: AppDeps) {
|
||||
* 201: { description: Created }
|
||||
* 400: { description: Invalid session anchor }
|
||||
*/
|
||||
router.post('/comments', validate(myCommentSchema), async (req, res, next) => {
|
||||
try {
|
||||
router.post(
|
||||
'/comments',
|
||||
validate(myCommentSchema),
|
||||
asyncHandler(async (req, res) => {
|
||||
const data = req.body as z.infer<typeof myCommentSchema>;
|
||||
const result = await addMyComment(prisma, req.user!.id, data);
|
||||
res.status(201).json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -236,14 +231,13 @@ export function createMeRoutes({ prisma }: AppDeps) {
|
||||
* 204: { description: Deleted }
|
||||
* 404: { description: Comment not found }
|
||||
*/
|
||||
router.delete('/comments/:commentId', async (req, res, next) => {
|
||||
try {
|
||||
router.delete(
|
||||
'/comments/:commentId',
|
||||
asyncHandler(async (req, res) => {
|
||||
await deleteMyComment(prisma, req.user!.id, req.params.commentId);
|
||||
res.status(204).end();
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -254,8 +248,10 @@ export function createMeRoutes({ prisma }: AppDeps) {
|
||||
* responses:
|
||||
* 201: { description: Created }
|
||||
*/
|
||||
router.post('/body-parameters/weight', validate(weightSchema), async (req, res, next) => {
|
||||
try {
|
||||
router.post(
|
||||
'/body-parameters/weight',
|
||||
validate(weightSchema),
|
||||
asyncHandler(async (req, res) => {
|
||||
const data = req.body as z.infer<typeof weightSchema>;
|
||||
const result = await addWeight(prisma, req.user!.id, data);
|
||||
await logActivity(prisma, {
|
||||
@@ -266,10 +262,8 @@ export function createMeRoutes({ prisma }: AppDeps) {
|
||||
details: { value: data.value },
|
||||
});
|
||||
res.status(201).json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -283,22 +277,18 @@ export function createMeRoutes({ prisma }: AppDeps) {
|
||||
router.post(
|
||||
'/body-parameters/circumference',
|
||||
validate(circumferenceSchema),
|
||||
async (req, res, next) => {
|
||||
try {
|
||||
const data = req.body as z.infer<typeof circumferenceSchema>;
|
||||
const result = await addCircumference(prisma, req.user!.id, data);
|
||||
await logActivity(prisma, {
|
||||
actorId: req.user!.id,
|
||||
action: 'circumference.added',
|
||||
entityType: 'user',
|
||||
entityId: req.user!.id,
|
||||
details: { name: data.name, value: data.value },
|
||||
});
|
||||
res.status(201).json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
},
|
||||
asyncHandler(async (req, res) => {
|
||||
const data = req.body as z.infer<typeof circumferenceSchema>;
|
||||
const result = await addCircumference(prisma, req.user!.id, data);
|
||||
await logActivity(prisma, {
|
||||
actorId: req.user!.id,
|
||||
action: 'circumference.added',
|
||||
entityType: 'user',
|
||||
entityId: req.user!.id,
|
||||
details: { name: data.name, value: data.value },
|
||||
});
|
||||
res.status(201).json(result);
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
@@ -311,8 +301,9 @@ export function createMeRoutes({ prisma }: AppDeps) {
|
||||
* 204: { description: Deleted }
|
||||
* 404: { description: Not found }
|
||||
*/
|
||||
router.delete('/body-parameters/weight/:id', async (req, res, next) => {
|
||||
try {
|
||||
router.delete(
|
||||
'/body-parameters/weight/:id',
|
||||
asyncHandler(async (req, res) => {
|
||||
const result = await deleteWeight(prisma, req.user!.id, req.params.id);
|
||||
await logActivity(prisma, {
|
||||
actorId: req.user!.id,
|
||||
@@ -322,10 +313,8 @@ export function createMeRoutes({ prisma }: AppDeps) {
|
||||
details: { value: result.value },
|
||||
});
|
||||
res.status(204).end();
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -337,8 +326,9 @@ export function createMeRoutes({ prisma }: AppDeps) {
|
||||
* 204: { description: Deleted }
|
||||
* 404: { description: Not found }
|
||||
*/
|
||||
router.delete('/body-parameters/circumference/:id', async (req, res, next) => {
|
||||
try {
|
||||
router.delete(
|
||||
'/body-parameters/circumference/:id',
|
||||
asyncHandler(async (req, res) => {
|
||||
const result = await deleteCircumference(prisma, req.user!.id, req.params.id);
|
||||
await logActivity(prisma, {
|
||||
actorId: req.user!.id,
|
||||
@@ -348,10 +338,8 @@ export function createMeRoutes({ prisma }: AppDeps) {
|
||||
details: { name: result.name, value: result.value },
|
||||
});
|
||||
res.status(204).end();
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
return router;
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import {
|
||||
listConversations,
|
||||
sendMessage,
|
||||
} from '../services/messages.js';
|
||||
import { asyncHandler } from '../lib/asyncHandler.js';
|
||||
|
||||
const sendSchema = z.object({
|
||||
body: z.string().min(1).max(1000),
|
||||
@@ -29,14 +30,13 @@ export function createMessageRoutes({ prisma }: AppDeps) {
|
||||
* responses:
|
||||
* 200: { description: OK }
|
||||
*/
|
||||
router.get('/conversations', async (req, res, next) => {
|
||||
try {
|
||||
router.get(
|
||||
'/conversations',
|
||||
asyncHandler(async (req, res) => {
|
||||
const result = await listConversations(prisma, req.user!.id);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -48,14 +48,13 @@ export function createMessageRoutes({ prisma }: AppDeps) {
|
||||
* 200: { description: OK }
|
||||
* 404: { description: Not a party of the relation or not ACTIVE }
|
||||
*/
|
||||
router.get('/conversations/:coachingId', async (req, res, next) => {
|
||||
try {
|
||||
router.get(
|
||||
'/conversations/:coachingId',
|
||||
asyncHandler(async (req, res) => {
|
||||
const result = await getConversation(prisma, req.user!.id, req.params.coachingId);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -68,15 +67,15 @@ export function createMessageRoutes({ prisma }: AppDeps) {
|
||||
* 400: { description: Invalid attachment }
|
||||
* 404: { description: Not a party of the relation or not ACTIVE }
|
||||
*/
|
||||
router.post('/conversations/:coachingId', validate(sendSchema), async (req, res, next) => {
|
||||
try {
|
||||
router.post(
|
||||
'/conversations/:coachingId',
|
||||
validate(sendSchema),
|
||||
asyncHandler(async (req, res) => {
|
||||
const data = req.body as z.infer<typeof sendSchema>;
|
||||
const result = await sendMessage(prisma, req.user!.id, req.params.coachingId, data);
|
||||
res.status(201).json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -88,14 +87,13 @@ export function createMessageRoutes({ prisma }: AppDeps) {
|
||||
* 204: { description: Deleted }
|
||||
* 404: { description: Message not found or not the author }
|
||||
*/
|
||||
router.delete('/:id', async (req, res, next) => {
|
||||
try {
|
||||
router.delete(
|
||||
'/:id',
|
||||
asyncHandler(async (req, res) => {
|
||||
await deleteMessage(prisma, req.user!.id, req.params.id);
|
||||
res.status(204).end();
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
return router;
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ import {
|
||||
updatePlan,
|
||||
} from '../services/plans.js';
|
||||
import { logActivity } from '../services/audit.js';
|
||||
import { asyncHandler } from '../lib/asyncHandler.js';
|
||||
import { activateSchema, createPlanSchema, updatePlanSchema } from './plans-schemas.js';
|
||||
|
||||
export function createPlanRoutes({ prisma }: AppDeps) {
|
||||
@@ -29,14 +30,13 @@ export function createPlanRoutes({ prisma }: AppDeps) {
|
||||
* responses:
|
||||
* 200: { description: OK }
|
||||
*/
|
||||
router.get('/', async (req, res, next) => {
|
||||
try {
|
||||
router.get(
|
||||
'/',
|
||||
asyncHandler(async (req, res) => {
|
||||
const result = await listPlans(prisma, req.user!.id);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -50,8 +50,10 @@ export function createPlanRoutes({ prisma }: AppDeps) {
|
||||
* 400: { description: Validation error / start date not a Monday / start week in the past }
|
||||
* 409: { description: User already has an active plan }
|
||||
*/
|
||||
router.post('/', validate(createPlanSchema), async (req, res, next) => {
|
||||
try {
|
||||
router.post(
|
||||
'/',
|
||||
validate(createPlanSchema),
|
||||
asyncHandler(async (req, res) => {
|
||||
const data = req.body as z.infer<typeof createPlanSchema>;
|
||||
const result = await createPlan(prisma, req.user!.id, data);
|
||||
await logActivity(prisma, {
|
||||
@@ -62,10 +64,8 @@ export function createPlanRoutes({ prisma }: AppDeps) {
|
||||
details: { title: result.plan.title },
|
||||
});
|
||||
res.status(201).json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -77,14 +77,13 @@ export function createPlanRoutes({ prisma }: AppDeps) {
|
||||
* 200: { description: OK }
|
||||
* 404: { description: Not found }
|
||||
*/
|
||||
router.get('/:id', async (req, res, next) => {
|
||||
try {
|
||||
router.get(
|
||||
'/:id',
|
||||
asyncHandler(async (req, res) => {
|
||||
const result = await getPlan(prisma, req.params.id, req.user!.id);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -95,8 +94,10 @@ export function createPlanRoutes({ prisma }: AppDeps) {
|
||||
* responses:
|
||||
* 200: { description: OK }
|
||||
*/
|
||||
router.patch('/:id', validate(updatePlanSchema), async (req, res, next) => {
|
||||
try {
|
||||
router.patch(
|
||||
'/:id',
|
||||
validate(updatePlanSchema),
|
||||
asyncHandler(async (req, res) => {
|
||||
const data = req.body as z.infer<typeof updatePlanSchema>;
|
||||
const result = await updatePlan(prisma, req.params.id, req.user!.id, data);
|
||||
await logActivity(prisma, {
|
||||
@@ -107,10 +108,8 @@ export function createPlanRoutes({ prisma }: AppDeps) {
|
||||
details: { title: data.title },
|
||||
});
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -121,8 +120,9 @@ export function createPlanRoutes({ prisma }: AppDeps) {
|
||||
* responses:
|
||||
* 204: { description: Deleted }
|
||||
*/
|
||||
router.delete('/:id', async (req, res, next) => {
|
||||
try {
|
||||
router.delete(
|
||||
'/:id',
|
||||
asyncHandler(async (req, res) => {
|
||||
const result = await deletePlan(prisma, req.params.id, req.user!.id);
|
||||
await logActivity(prisma, {
|
||||
actorId: req.user!.id,
|
||||
@@ -132,10 +132,8 @@ export function createPlanRoutes({ prisma }: AppDeps) {
|
||||
details: { title: result.title },
|
||||
});
|
||||
res.status(204).end();
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -148,8 +146,10 @@ export function createPlanRoutes({ prisma }: AppDeps) {
|
||||
* 200: { description: OK }
|
||||
* 409: { description: User already has an active plan }
|
||||
*/
|
||||
router.post('/:id/activate', validate(activateSchema), async (req, res, next) => {
|
||||
try {
|
||||
router.post(
|
||||
'/:id/activate',
|
||||
validate(activateSchema),
|
||||
asyncHandler(async (req, res) => {
|
||||
const { startDate } = req.body as z.infer<typeof activateSchema>;
|
||||
const result = await activatePlan(prisma, req.params.id, req.user!.id, startDate);
|
||||
await logActivity(prisma, {
|
||||
@@ -160,10 +160,8 @@ export function createPlanRoutes({ prisma }: AppDeps) {
|
||||
details: { title: result.title, startDate },
|
||||
});
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -174,8 +172,9 @@ export function createPlanRoutes({ prisma }: AppDeps) {
|
||||
* responses:
|
||||
* 200: { description: OK }
|
||||
*/
|
||||
router.post('/:id/abandon', async (req, res, next) => {
|
||||
try {
|
||||
router.post(
|
||||
'/:id/abandon',
|
||||
asyncHandler(async (req, res) => {
|
||||
const result = await abandonPlan(prisma, req.params.id, req.user!.id);
|
||||
await logActivity(prisma, {
|
||||
actorId: req.user!.id,
|
||||
@@ -185,10 +184,8 @@ export function createPlanRoutes({ prisma }: AppDeps) {
|
||||
details: { title: result.title },
|
||||
});
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -199,8 +196,9 @@ export function createPlanRoutes({ prisma }: AppDeps) {
|
||||
* responses:
|
||||
* 200: { description: OK }
|
||||
*/
|
||||
router.post('/:id/complete', async (req, res, next) => {
|
||||
try {
|
||||
router.post(
|
||||
'/:id/complete',
|
||||
asyncHandler(async (req, res) => {
|
||||
const result = await completePlan(prisma, req.params.id, req.user!.id);
|
||||
await logActivity(prisma, {
|
||||
actorId: req.user!.id,
|
||||
@@ -210,10 +208,8 @@ export function createPlanRoutes({ prisma }: AppDeps) {
|
||||
details: { title: result.title },
|
||||
});
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
return router;
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ import {
|
||||
updateSet,
|
||||
} from '../services/sessions.js';
|
||||
import { logActivity } from '../services/audit.js';
|
||||
import { asyncHandler } from '../lib/asyncHandler.js';
|
||||
|
||||
const addExerciseSchema = z.object({ exerciseId: z.string().min(1) });
|
||||
|
||||
@@ -46,8 +47,9 @@ export function createSessionRoutes({ prisma }: AppDeps) {
|
||||
* 201: { description: Created }
|
||||
* 409: { description: User already has an active session }
|
||||
*/
|
||||
router.post('/days/:dayId/sessions', async (req, res, next) => {
|
||||
try {
|
||||
router.post(
|
||||
'/days/:dayId/sessions',
|
||||
asyncHandler(async (req, res) => {
|
||||
const result = await startSession(prisma, req.params.dayId, req.user!.id);
|
||||
await logActivity(prisma, {
|
||||
actorId: req.user!.id,
|
||||
@@ -56,10 +58,8 @@ export function createSessionRoutes({ prisma }: AppDeps) {
|
||||
entityId: result.sessionId,
|
||||
});
|
||||
res.status(201).json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -71,14 +71,13 @@ export function createSessionRoutes({ prisma }: AppDeps) {
|
||||
* 200: { description: OK }
|
||||
* 404: { description: Not found }
|
||||
*/
|
||||
router.get('/days/:dayId', async (req, res, next) => {
|
||||
try {
|
||||
router.get(
|
||||
'/days/:dayId',
|
||||
asyncHandler(async (req, res) => {
|
||||
const result = await getDay(prisma, req.params.dayId, req.user!.id);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -89,14 +88,13 @@ export function createSessionRoutes({ prisma }: AppDeps) {
|
||||
* responses:
|
||||
* 200: { description: OK }
|
||||
*/
|
||||
router.get('/sessions/:id', async (req, res, next) => {
|
||||
try {
|
||||
router.get(
|
||||
'/sessions/:id',
|
||||
asyncHandler(async (req, res) => {
|
||||
const result = await getSession(prisma, req.params.id, req.user!.id);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -110,15 +108,11 @@ export function createSessionRoutes({ prisma }: AppDeps) {
|
||||
router.post(
|
||||
'/sessions/:id/exercises',
|
||||
validate(addExerciseSchema),
|
||||
async (req, res, next) => {
|
||||
try {
|
||||
const { exerciseId } = req.body as z.infer<typeof addExerciseSchema>;
|
||||
const result = await addExercise(prisma, req.params.id, req.user!.id, exerciseId);
|
||||
res.status(201).json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
},
|
||||
asyncHandler(async (req, res) => {
|
||||
const { exerciseId } = req.body as z.infer<typeof addExerciseSchema>;
|
||||
const result = await addExercise(prisma, req.params.id, req.user!.id, exerciseId);
|
||||
res.status(201).json(result);
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
@@ -133,15 +127,11 @@ export function createSessionRoutes({ prisma }: AppDeps) {
|
||||
router.patch(
|
||||
'/sessions/exercises/:id',
|
||||
validate(updateExerciseSchema),
|
||||
async (req, res, next) => {
|
||||
try {
|
||||
const data = req.body as z.infer<typeof updateExerciseSchema>;
|
||||
const result = await updateExercise(prisma, req.params.id, req.user!.id, data);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
},
|
||||
asyncHandler(async (req, res) => {
|
||||
const data = req.body as z.infer<typeof updateExerciseSchema>;
|
||||
const result = await updateExercise(prisma, req.params.id, req.user!.id, data);
|
||||
res.json(result);
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
@@ -155,14 +145,13 @@ export function createSessionRoutes({ prisma }: AppDeps) {
|
||||
* 201: { description: Created }
|
||||
* 400: { description: Norm not done or an extra set is already in progress }
|
||||
*/
|
||||
router.post('/sessions/exercises/:id/sets', async (req, res, next) => {
|
||||
try {
|
||||
router.post(
|
||||
'/sessions/exercises/:id/sets',
|
||||
asyncHandler(async (req, res) => {
|
||||
const result = await addSet(prisma, req.params.id, req.user!.id);
|
||||
res.status(201).json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -176,15 +165,11 @@ export function createSessionRoutes({ prisma }: AppDeps) {
|
||||
router.patch(
|
||||
'/sessions/sets/:id',
|
||||
validate(updateSetSchema),
|
||||
async (req, res, next) => {
|
||||
try {
|
||||
const data = req.body as z.infer<typeof updateSetSchema>;
|
||||
const result = await updateSet(prisma, req.params.id, req.user!.id, data);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
},
|
||||
asyncHandler(async (req, res) => {
|
||||
const data = req.body as z.infer<typeof updateSetSchema>;
|
||||
const result = await updateSet(prisma, req.params.id, req.user!.id, data);
|
||||
res.json(result);
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
@@ -198,14 +183,13 @@ export function createSessionRoutes({ prisma }: AppDeps) {
|
||||
* 400: { description: Planned set or session not in progress }
|
||||
* 404: { description: Not found }
|
||||
*/
|
||||
router.delete('/sessions/sets/:id', async (req, res, next) => {
|
||||
try {
|
||||
router.delete(
|
||||
'/sessions/sets/:id',
|
||||
asyncHandler(async (req, res) => {
|
||||
const result = await deleteSet(prisma, req.params.id, req.user!.id);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -218,8 +202,9 @@ export function createSessionRoutes({ prisma }: AppDeps) {
|
||||
* 200: { description: OK }
|
||||
* 400: { description: Session not in progress or planned sets still pending }
|
||||
*/
|
||||
router.post('/sessions/:id/complete', async (req, res, next) => {
|
||||
try {
|
||||
router.post(
|
||||
'/sessions/:id/complete',
|
||||
asyncHandler(async (req, res) => {
|
||||
const result = await completeSession(prisma, req.params.id, req.user!.id);
|
||||
await logActivity(prisma, {
|
||||
actorId: req.user!.id,
|
||||
@@ -228,10 +213,8 @@ export function createSessionRoutes({ prisma }: AppDeps) {
|
||||
entityId: req.params.id,
|
||||
});
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -243,8 +226,9 @@ export function createSessionRoutes({ prisma }: AppDeps) {
|
||||
* 200: { description: OK }
|
||||
* 400: { description: Session not in progress }
|
||||
*/
|
||||
router.post('/sessions/:id/abandon', async (req, res, next) => {
|
||||
try {
|
||||
router.post(
|
||||
'/sessions/:id/abandon',
|
||||
asyncHandler(async (req, res) => {
|
||||
const result = await abandonSession(prisma, req.params.id, req.user!.id);
|
||||
await logActivity(prisma, {
|
||||
actorId: req.user!.id,
|
||||
@@ -253,10 +237,8 @@ export function createSessionRoutes({ prisma }: AppDeps) {
|
||||
entityId: req.params.id,
|
||||
});
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
return router;
|
||||
}
|
||||
|
||||
@@ -34,6 +34,7 @@ import {
|
||||
} from '../services/plans.js';
|
||||
import { activateSchema, createPlanSchema, updatePlanSchema } from './plans-schemas.js';
|
||||
import { logActivity } from '../services/audit.js';
|
||||
import { asyncHandler } from '../lib/asyncHandler.js';
|
||||
|
||||
const invitationSchema = z.object({
|
||||
userId: z.string().min(1),
|
||||
@@ -71,14 +72,13 @@ export function createTrainerRoutes({ prisma }: AppDeps) {
|
||||
* responses:
|
||||
* 200: { description: OK }
|
||||
*/
|
||||
router.get('/invitations', async (req, res, next) => {
|
||||
try {
|
||||
router.get(
|
||||
'/invitations',
|
||||
asyncHandler(async (req, res) => {
|
||||
const result = await listInvitations(prisma, req.user!.id);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -92,8 +92,10 @@ export function createTrainerRoutes({ prisma }: AppDeps) {
|
||||
* 404: { description: User not found }
|
||||
* 409: { description: Invitation exists or pending limit reached }
|
||||
*/
|
||||
router.post('/invitations', validate(invitationSchema), async (req, res, next) => {
|
||||
try {
|
||||
router.post(
|
||||
'/invitations',
|
||||
validate(invitationSchema),
|
||||
asyncHandler(async (req, res) => {
|
||||
const { userId } = req.body as z.infer<typeof invitationSchema>;
|
||||
const result = await sendInvitation(prisma, req.user!.id, userId);
|
||||
await logActivity(prisma, {
|
||||
@@ -104,10 +106,8 @@ export function createTrainerRoutes({ prisma }: AppDeps) {
|
||||
details: { targetEmail: result.targetEmail },
|
||||
});
|
||||
res.status(201).json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -122,8 +122,9 @@ export function createTrainerRoutes({ prisma }: AppDeps) {
|
||||
* 409: { description: Invitation is not pending }
|
||||
* 410: { description: Invitation expired }
|
||||
*/
|
||||
router.post('/invitations/:id/accept', async (req, res, next) => {
|
||||
try {
|
||||
router.post(
|
||||
'/invitations/:id/accept',
|
||||
asyncHandler(async (req, res) => {
|
||||
const result = await acceptInvitation(prisma, req.user!.id, req.params.id);
|
||||
await logActivity(prisma, {
|
||||
actorId: req.user!.id,
|
||||
@@ -132,10 +133,8 @@ export function createTrainerRoutes({ prisma }: AppDeps) {
|
||||
entityId: result.coaching.id,
|
||||
});
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -150,8 +149,9 @@ export function createTrainerRoutes({ prisma }: AppDeps) {
|
||||
* 409: { description: Invitation is not pending }
|
||||
* 410: { description: Invitation expired }
|
||||
*/
|
||||
router.post('/invitations/:id/decline', async (req, res, next) => {
|
||||
try {
|
||||
router.post(
|
||||
'/invitations/:id/decline',
|
||||
asyncHandler(async (req, res) => {
|
||||
const result = await declineInvitation(prisma, req.user!.id, req.params.id);
|
||||
await logActivity(prisma, {
|
||||
actorId: req.user!.id,
|
||||
@@ -160,10 +160,8 @@ export function createTrainerRoutes({ prisma }: AppDeps) {
|
||||
entityId: result.coaching.id,
|
||||
});
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -177,8 +175,9 @@ export function createTrainerRoutes({ prisma }: AppDeps) {
|
||||
* 404: { description: Invitation not found }
|
||||
* 409: { description: Invitation is not pending }
|
||||
*/
|
||||
router.delete('/invitations/:id', async (req, res, next) => {
|
||||
try {
|
||||
router.delete(
|
||||
'/invitations/:id',
|
||||
asyncHandler(async (req, res) => {
|
||||
await withdrawInvitation(prisma, req.user!.id, req.params.id);
|
||||
await logActivity(prisma, {
|
||||
actorId: req.user!.id,
|
||||
@@ -187,10 +186,8 @@ export function createTrainerRoutes({ prisma }: AppDeps) {
|
||||
entityId: req.params.id,
|
||||
});
|
||||
res.status(204).end();
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -203,16 +200,16 @@ export function createTrainerRoutes({ prisma }: AppDeps) {
|
||||
* 400: { description: Missing query parameters }
|
||||
* 403: { description: Trainer role required }
|
||||
*/
|
||||
router.get('/search', trainerOnly, async (req, res, next) => {
|
||||
try {
|
||||
router.get(
|
||||
'/search',
|
||||
trainerOnly,
|
||||
asyncHandler(async (req, res) => {
|
||||
const parsed = searchQuerySchema.safeParse(req.query);
|
||||
if (!parsed.success) throw badRequest('firstName and lastName query parameters are required');
|
||||
const result = await searchUsers(prisma, req.user!.id, parsed.data.firstName, parsed.data.lastName);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -224,15 +221,15 @@ export function createTrainerRoutes({ prisma }: AppDeps) {
|
||||
* 200: { description: OK }
|
||||
* 403: { description: Trainer role required }
|
||||
*/
|
||||
router.get('/trainees', trainerOnly, async (req, res, next) => {
|
||||
try {
|
||||
router.get(
|
||||
'/trainees',
|
||||
trainerOnly,
|
||||
asyncHandler(async (req, res) => {
|
||||
const gymId = typeof req.query.gymId === 'string' ? req.query.gymId : undefined;
|
||||
const result = await listTrainees(prisma, req.user!.id, gymId);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -244,8 +241,10 @@ export function createTrainerRoutes({ prisma }: AppDeps) {
|
||||
* 204: { description: Archived }
|
||||
* 404: { description: Trainee not found }
|
||||
*/
|
||||
router.delete('/trainees/:userId', trainerOnly, async (req, res, next) => {
|
||||
try {
|
||||
router.delete(
|
||||
'/trainees/:userId',
|
||||
trainerOnly,
|
||||
asyncHandler(async (req, res) => {
|
||||
await archiveTrainee(prisma, req.user!.id, req.params.userId);
|
||||
await logActivity(prisma, {
|
||||
actorId: req.user!.id,
|
||||
@@ -254,10 +253,8 @@ export function createTrainerRoutes({ prisma }: AppDeps) {
|
||||
details: { traineeId: req.params.userId },
|
||||
});
|
||||
res.status(204).end();
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -269,14 +266,14 @@ export function createTrainerRoutes({ prisma }: AppDeps) {
|
||||
* 200: { description: OK }
|
||||
* 404: { description: Trainee not found }
|
||||
*/
|
||||
router.get('/trainees/:userId', trainerOnly, async (req, res, next) => {
|
||||
try {
|
||||
router.get(
|
||||
'/trainees/:userId',
|
||||
trainerOnly,
|
||||
asyncHandler(async (req, res) => {
|
||||
const result = await getTraineeDetail(prisma, req.user!.id, req.params.userId);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -288,14 +285,14 @@ export function createTrainerRoutes({ prisma }: AppDeps) {
|
||||
* 200: { description: OK }
|
||||
* 404: { description: Trainee not found }
|
||||
*/
|
||||
router.get('/trainees/:userId/history', trainerOnly, async (req, res, next) => {
|
||||
try {
|
||||
router.get(
|
||||
'/trainees/:userId/history',
|
||||
trainerOnly,
|
||||
asyncHandler(async (req, res) => {
|
||||
const result = await getTraineeHistory(prisma, req.user!.id, req.params.userId);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -307,14 +304,14 @@ export function createTrainerRoutes({ prisma }: AppDeps) {
|
||||
* 200: { description: OK }
|
||||
* 404: { description: Trainee not found }
|
||||
*/
|
||||
router.get('/trainees/:userId/report', trainerOnly, async (req, res, next) => {
|
||||
try {
|
||||
router.get(
|
||||
'/trainees/:userId/report',
|
||||
trainerOnly,
|
||||
asyncHandler(async (req, res) => {
|
||||
const result = await getTraineeReport(prisma, req.user!.id, req.params.userId);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -326,14 +323,14 @@ export function createTrainerRoutes({ prisma }: AppDeps) {
|
||||
* 200: { description: OK }
|
||||
* 404: { description: Trainee not found }
|
||||
*/
|
||||
router.get('/trainees/:userId/moves', trainerOnly, async (req, res, next) => {
|
||||
try {
|
||||
router.get(
|
||||
'/trainees/:userId/moves',
|
||||
trainerOnly,
|
||||
asyncHandler(async (req, res) => {
|
||||
const result = await getTraineeMoves(prisma, req.user!.id, req.params.userId);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -348,16 +345,16 @@ export function createTrainerRoutes({ prisma }: AppDeps) {
|
||||
* 403: { description: Trainer role required }
|
||||
* 404: { description: Trainee not found }
|
||||
*/
|
||||
router.get('/trainees/:userId/calendar', trainerOnly, async (req, res, next) => {
|
||||
try {
|
||||
router.get(
|
||||
'/trainees/:userId/calendar',
|
||||
trainerOnly,
|
||||
asyncHandler(async (req, res) => {
|
||||
const parsed = calendarQuerySchema.safeParse(req.query);
|
||||
if (!parsed.success) throw badRequest('month must be in YYYY-MM format');
|
||||
const result = await getTraineeCalendar(prisma, req.user!.id, req.params.userId, parsed.data.month);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -370,14 +367,14 @@ export function createTrainerRoutes({ prisma }: AppDeps) {
|
||||
* 403: { description: Trainer role required }
|
||||
* 404: { description: Trainee not found }
|
||||
*/
|
||||
router.get('/trainees/:userId/exercise-progress', trainerOnly, async (req, res, next) => {
|
||||
try {
|
||||
router.get(
|
||||
'/trainees/:userId/exercise-progress',
|
||||
trainerOnly,
|
||||
asyncHandler(async (req, res) => {
|
||||
const result = await getTraineeExerciseProgress(prisma, req.user!.id, req.params.userId);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -390,14 +387,14 @@ export function createTrainerRoutes({ prisma }: AppDeps) {
|
||||
* 403: { description: Trainer role required }
|
||||
* 404: { description: Trainee not found }
|
||||
*/
|
||||
router.get('/trainees/:userId/exercise-latest', trainerOnly, async (req, res, next) => {
|
||||
try {
|
||||
router.get(
|
||||
'/trainees/:userId/exercise-latest',
|
||||
trainerOnly,
|
||||
asyncHandler(async (req, res) => {
|
||||
const result = await getTraineeLatestResults(prisma, req.user!.id, req.params.userId);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -410,15 +407,15 @@ export function createTrainerRoutes({ prisma }: AppDeps) {
|
||||
* 403: { description: Trainer role required }
|
||||
* 404: { description: Trainee not found }
|
||||
*/
|
||||
router.get('/trainees/:userId/plans', trainerOnly, async (req, res, next) => {
|
||||
try {
|
||||
router.get(
|
||||
'/trainees/:userId/plans',
|
||||
trainerOnly,
|
||||
asyncHandler(async (req, res) => {
|
||||
await assertActiveTrainee(prisma, req.user!.id, req.params.userId);
|
||||
const result = await listPlans(prisma, req.params.userId);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -433,8 +430,11 @@ export function createTrainerRoutes({ prisma }: AppDeps) {
|
||||
* 404: { description: Trainee not found }
|
||||
* 409: { description: Trainee already has an active plan }
|
||||
*/
|
||||
router.post('/trainees/:userId/plans', trainerOnly, validate(createPlanSchema), async (req, res, next) => {
|
||||
try {
|
||||
router.post(
|
||||
'/trainees/:userId/plans',
|
||||
trainerOnly,
|
||||
validate(createPlanSchema),
|
||||
asyncHandler(async (req, res) => {
|
||||
await assertActiveTrainee(prisma, req.user!.id, req.params.userId);
|
||||
const data = req.body as z.infer<typeof createPlanSchema>;
|
||||
const result = await createPlan(prisma, req.params.userId, data, req.user!.id);
|
||||
@@ -446,10 +446,8 @@ export function createTrainerRoutes({ prisma }: AppDeps) {
|
||||
details: { title: result.plan.title, traineeId: req.params.userId },
|
||||
});
|
||||
res.status(201).json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -462,15 +460,15 @@ export function createTrainerRoutes({ prisma }: AppDeps) {
|
||||
* 403: { description: Trainer role required }
|
||||
* 404: { description: Trainee or plan not found }
|
||||
*/
|
||||
router.get('/trainees/:userId/plans/:planId', trainerOnly, async (req, res, next) => {
|
||||
try {
|
||||
router.get(
|
||||
'/trainees/:userId/plans/:planId',
|
||||
trainerOnly,
|
||||
asyncHandler(async (req, res) => {
|
||||
await assertActiveTrainee(prisma, req.user!.id, req.params.userId);
|
||||
const result = await getPlan(prisma, req.params.planId, req.user!.id);
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -488,23 +486,19 @@ export function createTrainerRoutes({ prisma }: AppDeps) {
|
||||
'/trainees/:userId/plans/:planId',
|
||||
trainerOnly,
|
||||
validate(updatePlanSchema),
|
||||
async (req, res, next) => {
|
||||
try {
|
||||
await assertActiveTrainee(prisma, req.user!.id, req.params.userId);
|
||||
const data = req.body as z.infer<typeof updatePlanSchema>;
|
||||
const result = await updatePlan(prisma, req.params.planId, req.user!.id, data);
|
||||
await logActivity(prisma, {
|
||||
actorId: req.user!.id,
|
||||
action: 'plan.updated',
|
||||
entityType: 'plan',
|
||||
entityId: req.params.planId,
|
||||
details: { title: data.title, traineeId: req.params.userId },
|
||||
});
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
},
|
||||
asyncHandler(async (req, res) => {
|
||||
await assertActiveTrainee(prisma, req.user!.id, req.params.userId);
|
||||
const data = req.body as z.infer<typeof updatePlanSchema>;
|
||||
const result = await updatePlan(prisma, req.params.planId, req.user!.id, data);
|
||||
await logActivity(prisma, {
|
||||
actorId: req.user!.id,
|
||||
action: 'plan.updated',
|
||||
entityType: 'plan',
|
||||
entityId: req.params.planId,
|
||||
details: { title: data.title, traineeId: req.params.userId },
|
||||
});
|
||||
res.json(result);
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
@@ -519,8 +513,10 @@ export function createTrainerRoutes({ prisma }: AppDeps) {
|
||||
* 403: { description: Trainer role required }
|
||||
* 404: { description: Trainee or plan not found }
|
||||
*/
|
||||
router.delete('/trainees/:userId/plans/:planId', trainerOnly, async (req, res, next) => {
|
||||
try {
|
||||
router.delete(
|
||||
'/trainees/:userId/plans/:planId',
|
||||
trainerOnly,
|
||||
asyncHandler(async (req, res) => {
|
||||
await assertActiveTrainee(prisma, req.user!.id, req.params.userId);
|
||||
const result = await deletePlan(prisma, req.params.planId, req.user!.id);
|
||||
await logActivity(prisma, {
|
||||
@@ -531,10 +527,8 @@ export function createTrainerRoutes({ prisma }: AppDeps) {
|
||||
details: { title: result.title, traineeId: req.params.userId },
|
||||
});
|
||||
res.status(204).end();
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -553,23 +547,19 @@ export function createTrainerRoutes({ prisma }: AppDeps) {
|
||||
'/trainees/:userId/plans/:planId/activate',
|
||||
trainerOnly,
|
||||
validate(activateSchema),
|
||||
async (req, res, next) => {
|
||||
try {
|
||||
await assertActiveTrainee(prisma, req.user!.id, req.params.userId);
|
||||
const { startDate } = req.body as z.infer<typeof activateSchema>;
|
||||
const result = await activatePlan(prisma, req.params.planId, req.user!.id, startDate);
|
||||
await logActivity(prisma, {
|
||||
actorId: req.user!.id,
|
||||
action: 'plan.activated',
|
||||
entityType: 'plan',
|
||||
entityId: req.params.planId,
|
||||
details: { title: result.title, startDate, traineeId: req.params.userId },
|
||||
});
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
},
|
||||
asyncHandler(async (req, res) => {
|
||||
await assertActiveTrainee(prisma, req.user!.id, req.params.userId);
|
||||
const { startDate } = req.body as z.infer<typeof activateSchema>;
|
||||
const result = await activatePlan(prisma, req.params.planId, req.user!.id, startDate);
|
||||
await logActivity(prisma, {
|
||||
actorId: req.user!.id,
|
||||
action: 'plan.activated',
|
||||
entityType: 'plan',
|
||||
entityId: req.params.planId,
|
||||
details: { title: result.title, startDate, traineeId: req.params.userId },
|
||||
});
|
||||
res.json(result);
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
@@ -584,8 +574,10 @@ export function createTrainerRoutes({ prisma }: AppDeps) {
|
||||
* 403: { description: Trainer role required }
|
||||
* 404: { description: Trainee or plan not found }
|
||||
*/
|
||||
router.post('/trainees/:userId/plans/:planId/abandon', trainerOnly, async (req, res, next) => {
|
||||
try {
|
||||
router.post(
|
||||
'/trainees/:userId/plans/:planId/abandon',
|
||||
trainerOnly,
|
||||
asyncHandler(async (req, res) => {
|
||||
await assertActiveTrainee(prisma, req.user!.id, req.params.userId);
|
||||
const result = await abandonPlan(prisma, req.params.planId, req.user!.id);
|
||||
await logActivity(prisma, {
|
||||
@@ -596,10 +588,8 @@ export function createTrainerRoutes({ prisma }: AppDeps) {
|
||||
details: { title: result.title, traineeId: req.params.userId },
|
||||
});
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -613,8 +603,10 @@ export function createTrainerRoutes({ prisma }: AppDeps) {
|
||||
* 403: { description: Trainer role required }
|
||||
* 404: { description: Trainee or plan not found }
|
||||
*/
|
||||
router.post('/trainees/:userId/plans/:planId/complete', trainerOnly, async (req, res, next) => {
|
||||
try {
|
||||
router.post(
|
||||
'/trainees/:userId/plans/:planId/complete',
|
||||
trainerOnly,
|
||||
asyncHandler(async (req, res) => {
|
||||
await assertActiveTrainee(prisma, req.user!.id, req.params.userId);
|
||||
const result = await completePlan(prisma, req.params.planId, req.user!.id);
|
||||
await logActivity(prisma, {
|
||||
@@ -625,10 +617,8 @@ export function createTrainerRoutes({ prisma }: AppDeps) {
|
||||
details: { title: result.title, traineeId: req.params.userId },
|
||||
});
|
||||
res.json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
@@ -646,15 +636,11 @@ export function createTrainerRoutes({ prisma }: AppDeps) {
|
||||
'/trainees/:userId/comments',
|
||||
trainerOnly,
|
||||
validate(commentSchema),
|
||||
async (req, res, next) => {
|
||||
try {
|
||||
const data = req.body as z.infer<typeof commentSchema>;
|
||||
const result = await addTraineeComment(prisma, req.user!.id, req.params.userId, data);
|
||||
res.status(201).json(result);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
},
|
||||
asyncHandler(async (req, res) => {
|
||||
const data = req.body as z.infer<typeof commentSchema>;
|
||||
const result = await addTraineeComment(prisma, req.user!.id, req.params.userId, data);
|
||||
res.status(201).json(result);
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
@@ -668,14 +654,14 @@ export function createTrainerRoutes({ prisma }: AppDeps) {
|
||||
* 403: { description: Trainer role required }
|
||||
* 404: { description: Comment not found or not the author }
|
||||
*/
|
||||
router.delete('/trainees/:userId/comments/:commentId', trainerOnly, async (req, res, next) => {
|
||||
try {
|
||||
router.delete(
|
||||
'/trainees/:userId/comments/:commentId',
|
||||
trainerOnly,
|
||||
asyncHandler(async (req, res) => {
|
||||
await deleteTraineeComment(prisma, req.user!.id, req.params.userId, req.params.commentId);
|
||||
res.status(204).end();
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
return router;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user