mirror of
https://github.com/movie-web/backend.git
synced 2025-09-13 18:13:26 +00:00
Update progress import endpoint
This commit is contained in:
@@ -84,6 +84,7 @@ export const userProgressRouter = makeRouter((app) => {
|
|||||||
throw new StatusError('Cannot modify user other than yourself', 403);
|
throw new StatusError('Cannot modify user other than yourself', 403);
|
||||||
|
|
||||||
const existingItems = await em.find(ProgressItem, { userId: params.uid });
|
const existingItems = await em.find(ProgressItem, { userId: params.uid });
|
||||||
|
const itemsToUpsert: ProgressItem[] = [];
|
||||||
|
|
||||||
for (const newItem of newItems) {
|
for (const newItem of newItems) {
|
||||||
const existingItem = existingItems.find(
|
const existingItem = existingItems.find(
|
||||||
@@ -98,10 +99,11 @@ export const userProgressRouter = makeRouter((app) => {
|
|||||||
existingItem.updatedAt = new Date();
|
existingItem.updatedAt = new Date();
|
||||||
existingItem.watched = newItem.watched;
|
existingItem.watched = newItem.watched;
|
||||||
}
|
}
|
||||||
|
itemsToUpsert.push(existingItem);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
existingItems.push({
|
itemsToUpsert.push({
|
||||||
id: randomUUID(),
|
id: randomUUID(),
|
||||||
duration: newItem.duration,
|
duration: newItem.duration,
|
||||||
episodeId: newItem.episodeId,
|
episodeId: newItem.episodeId,
|
||||||
@@ -116,7 +118,7 @@ export const userProgressRouter = makeRouter((app) => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const progressItems = await em.upsertMany(ProgressItem, existingItems);
|
const progressItems = await em.upsertMany(ProgressItem, itemsToUpsert);
|
||||||
|
|
||||||
await em.flush();
|
await em.flush();
|
||||||
|
|
||||||
@@ -126,21 +128,7 @@ export const userProgressRouter = makeRouter((app) => {
|
|||||||
window: '10m',
|
window: '10m',
|
||||||
});
|
});
|
||||||
|
|
||||||
// Construct a response that only has the items that were requested to be updated in the same order
|
return progressItems.map(formatProgressItem);
|
||||||
// ! is used on find as the item *should* always exist if the code above works correctly
|
|
||||||
const newItemResponses = newItems
|
|
||||||
.map(
|
|
||||||
(newItem) =>
|
|
||||||
progressItems.find(
|
|
||||||
(item) =>
|
|
||||||
item.tmdbId == newItem.tmdbId &&
|
|
||||||
item.seasonId == newItem.seasonId &&
|
|
||||||
item.episodeId == newItem.episodeId,
|
|
||||||
)!,
|
|
||||||
)
|
|
||||||
.map(formatProgressItem);
|
|
||||||
|
|
||||||
return newItemResponses;
|
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user