adding that deletion of album removes its items

This commit is contained in:
Marek Maslowski
2026-04-04 19:52:49 +02:00
parent 21063e6230
commit 2baf407809
5 changed files with 21 additions and 15 deletions

View File

@@ -136,7 +136,8 @@ router.get('/albums', authenticate, async (req: Request, res: Response) => {
router.post('/trips/:tripId/album-links/:linkId/sync', authenticate, async (req: Request, res: Response) => {
const authReq = req as AuthRequest;
const { tripId, linkId } = req.params;
const result = await syncAlbumAssets(tripId, linkId, authReq.user.id);
const sid = req.headers['x-socket-id'] as string;
const result = await syncAlbumAssets(tripId, linkId, authReq.user.id, sid);
if (result.error) return res.status(result.status!).json({ error: result.error });
res.json({ success: true, added: result.added, total: result.total });
if (result.added! > 0) {

View File

@@ -72,8 +72,9 @@ router.get('/albums', authenticate, async (req: Request, res: Response) => {
router.post('/trips/:tripId/album-links/:linkId/sync', authenticate, async (req: Request, res: Response) => {
const authReq = req as AuthRequest;
const { tripId, linkId } = req.params;
const sid = req.headers['x-socket-id'] as string;
handleServiceResult(res, await syncSynologyAlbumLink(authReq.user.id, tripId, linkId));
handleServiceResult(res, await syncSynologyAlbumLink(authReq.user.id, tripId, sid, linkId));
});
router.post('/search', authenticate, async (req: Request, res: Response) => {

View File

@@ -343,7 +343,8 @@ export function deleteAlbumLink(linkId: string, tripId: string, userId: number)
export async function syncAlbumAssets(
tripId: string,
linkId: string,
userId: number
userId: number,
sid: string,
): Promise<{ success?: boolean; added?: number; total?: number; error?: string; status?: number }> {
const response = getAlbumIdFromLink(tripId, linkId, userId);
if (!response.success) return { error: 'Album link not found', status: 404 };
@@ -365,7 +366,7 @@ export async function syncAlbumAssets(
asset_ids: assets.map((a: any) => a.id),
};
const result = await addTripPhotos(tripId, userId, true, [selection]);
const result = await addTripPhotos(tripId, userId, true, [selection], sid, linkId);
if ('error' in result) return { error: result.error.message, status: result.error.status };
updateSyncTimeForAlbumLink(linkId);

View File

@@ -348,7 +348,7 @@ export async function listSynologyAlbums(userId: number): Promise<ServiceResult<
}
export async function syncSynologyAlbumLink(userId: number, tripId: string, linkId: string): Promise<ServiceResult<SyncAlbumResult>> {
export async function syncSynologyAlbumLink(userId: number, tripId: string, linkId: string, sid: string): Promise<ServiceResult<SyncAlbumResult>> {
const response = getAlbumIdFromLink(tripId, linkId, userId);
if (!response.success) return response as ServiceResult<SyncAlbumResult>;
@@ -380,11 +380,12 @@ export async function syncSynologyAlbumLink(userId: number, tripId: string, link
asset_ids: allItems.map(item => String(item.additional?.thumbnail?.cache_key || '')).filter(id => id),
};
updateSyncTimeForAlbumLink(linkId);
const result = await addTripPhotos(tripId, userId, true, [selection]);
const result = await addTripPhotos(tripId, userId, true, [selection], sid, linkId);
if (!result.success) return result as ServiceResult<SyncAlbumResult>;
updateSyncTimeForAlbumLink(linkId);
return success({ added: result.data.added, total: allItems.length });
}

View File

@@ -67,10 +67,10 @@ export function listTripAlbumLinks(tripId: string, userId: number): ServiceResul
//-----------------------------------------------
// managing photos in trip
function _addTripPhoto(tripId: string, userId: number, provider: string, assetId: string, shared: boolean): boolean {
function _addTripPhoto(tripId: string, userId: number, provider: string, assetId: string, shared: boolean, albumLinkId?: string): boolean {
const result = db.prepare(
'INSERT OR IGNORE INTO trip_photos (trip_id, user_id, asset_id, provider, shared) VALUES (?, ?, ?, ?, ?)'
).run(tripId, userId, assetId, provider, shared ? 1 : 0);
'INSERT OR IGNORE INTO trip_photos (trip_id, user_id, asset_id, provider, shared, album_link_id) VALUES (?, ?, ?, ?, ?, ?)'
).run(tripId, userId, assetId, provider, shared ? 1 : 0, albumLinkId || null);
return result.changes > 0;
}
@@ -79,7 +79,8 @@ export async function addTripPhotos(
userId: number,
shared: boolean,
selections: Selection[],
sid?: string,
sid: string,
albumLinkId?: string,
): Promise<ServiceResult<{ added: number; shared: boolean }>> {
const access = canAccessTrip(tripId, userId);
if (!access) {
@@ -96,7 +97,7 @@ export async function addTripPhotos(
for (const raw of selection.asset_ids) {
const assetId = String(raw || '').trim();
if (!assetId) continue;
if (_addTripPhoto(tripId, userId, selection.provider, assetId, shared)) {
if (_addTripPhoto(tripId, userId, selection.provider, assetId, shared, albumLinkId)) {
added++;
}
}
@@ -213,9 +214,10 @@ export function removeAlbumLink(tripId: string, linkId: string, userId: number):
}
try {
db.prepare('DELETE FROM trip_photos WHERE trip_id = ? AND album_link_id = ?')
.run(tripId, linkId);
db.prepare('DELETE FROM trip_album_links WHERE id = ? AND trip_id = ? AND user_id = ?')
.run(linkId, tripId, userId);
return success(true);
} catch (error) {
return mapDbError(error, 'Failed to remove album link');