change in hadnling return values from unified service

This commit is contained in:
Marek Maslowski
2026-04-04 13:36:12 +02:00
parent 68f0d399ca
commit 504713d920
4 changed files with 300 additions and 274 deletions
+43 -53
View File
@@ -1,6 +1,5 @@
import express, { Request, Response } from 'express';
import { authenticate } from '../middleware/auth';
import { broadcast } from '../websocket';
import { AuthRequest } from '../types';
import {
listTripPhotos,
@@ -10,94 +9,85 @@ import {
addTripPhotos,
removeTripPhoto,
setTripPhotoSharing,
notifySharedTripPhotos,
normalizeSelections,
} from '../services/memoriesService';
const router = express.Router();
//------------------------------------------------
// routes for managing photos linked to trip
router.get('/trips/:tripId/photos', authenticate, (req: Request, res: Response) => {
const authReq = req as AuthRequest;
const { tripId } = req.params;
const result = listTripPhotos(tripId, authReq.user.id);
if ('error' in result) return res.status(result.status).json({ error: result.error });
res.json({ photos: result.photos });
if ('error' in result) return res.status(result.error.status).json({ error: result.error.message });
res.json({ photos: result.data });
});
router.get('/trips/:tripId/album-links', authenticate, (req: Request, res: Response) => {
const authReq = req as AuthRequest;
const { tripId } = req.params;
const result = listTripAlbumLinks(tripId, authReq.user.id);
if ('error' in result) return res.status(result.status).json({ error: result.error });
res.json({ links: result.links });
});
router.delete('/trips/:tripId/album-links/:linkId', authenticate, (req: Request, res: Response) => {
const authReq = req as AuthRequest;
const { tripId, linkId } = req.params;
const result = removeAlbumLink(tripId, linkId, authReq.user.id);
if ('error' in result) return res.status(result.status).json({ error: result.error });
res.json({ success: true });
broadcast(tripId, 'memories:updated', { userId: authReq.user.id }, req.headers['x-socket-id'] as string);
});
router.post('/trips/:tripId/photos', authenticate, (req: Request, res: Response) => {
router.post('/trips/:tripId/photos', authenticate, async (req: Request, res: Response) => {
const authReq = req as AuthRequest;
const { tripId } = req.params;
const sid = req.headers['x-socket-id'] as string;
const selections = normalizeSelections(req.body?.selections, req.body?.provider, req.body?.asset_ids);
const shared = req.body?.shared === undefined ? true : !!req.body?.shared;
const result = addTripPhotos(
const result = await addTripPhotos(
tripId,
authReq.user.id,
shared,
selections,
req.body?.selections || [],
sid,
);
if ('error' in result) return res.status(result.status).json({ error: result.error });
if ('error' in result) return res.status(result.error.status).json({ error: result.error.message });
res.json({ success: true, added: result.added });
broadcast(tripId, 'memories:updated', { userId: authReq.user.id }, req.headers['x-socket-id'] as string);
if (result.shared && result.added > 0) {
void notifySharedTripPhotos(
tripId,
authReq.user.id,
authReq.user.username || authReq.user.email,
result.added,
).catch(() => {});
}
res.json({ success: true, added: result.data.added });
});
router.delete('/trips/:tripId/photos', authenticate, (req: Request, res: Response) => {
router.put('/trips/:tripId/photos/sharing', authenticate, async (req: Request, res: Response) => {
const authReq = req as AuthRequest;
const { tripId } = req.params;
const result = removeTripPhoto(tripId, authReq.user.id, req.body?.provider, req.body?.asset_id);
if ('error' in result) return res.status(result.status).json({ error: result.error });
res.json({ success: true });
broadcast(tripId, 'memories:updated', { userId: authReq.user.id }, req.headers['x-socket-id'] as string);
});
router.put('/trips/:tripId/photos/sharing', authenticate, (req: Request, res: Response) => {
const authReq = req as AuthRequest;
const { tripId } = req.params;
const result = setTripPhotoSharing(
const result = await setTripPhotoSharing(
tripId,
authReq.user.id,
req.body?.provider,
req.body?.asset_id,
req.body?.shared,
);
if ('error' in result) return res.status(result.status).json({ error: result.error });
if ('error' in result) return res.status(result.error.status).json({ error: result.error.message });
res.json({ success: true });
broadcast(tripId, 'memories:updated', { userId: authReq.user.id }, req.headers['x-socket-id'] as string);
});
router.post('/trips/:tripId/album-links', authenticate, (req: Request, res: Response) => {
router.delete('/trips/:tripId/photos', authenticate, async (req: Request, res: Response) => {
const authReq = req as AuthRequest;
const { tripId } = req.params;
const result = await removeTripPhoto(tripId, authReq.user.id, req.body?.provider, req.body?.asset_id);
if ('error' in result) return res.status(result.error.status).json({ error: result.error.message });
res.json({ success: true });
});
//------------------------------
// routes for managing album links
router.get('/trips/:tripId/album-links', authenticate, (req: Request, res: Response) => {
const authReq = req as AuthRequest;
const { tripId } = req.params;
const result = listTripAlbumLinks(tripId, authReq.user.id);
if ('error' in result) return res.status(result.error.status).json({ error: result.error.message });
res.json({ links: result.data });
});
router.post('/trips/:tripId/album-links', authenticate, async (req: Request, res: Response) => {
const authReq = req as AuthRequest;
const { tripId } = req.params;
const result = createTripAlbumLink(tripId, authReq.user.id, req.body?.provider, req.body?.album_id, req.body?.album_name);
if ('error' in result) return res.status(result.status).json({ error: result.error });
if ('error' in result) return res.status(result.error.status).json({ error: result.error.message });
res.json({ success: true });
});
router.delete('/trips/:tripId/album-links/:linkId', authenticate, async (req: Request, res: Response) => {
const authReq = req as AuthRequest;
const { tripId, linkId } = req.params;
const result = removeAlbumLink(tripId, linkId, authReq.user.id);
if ('error' in result) return res.status(result.error.status).json({ error: result.error.message });
res.json({ success: true });
});