mirror of
https://github.com/movie-web/backend.git
synced 2025-09-13 16:33:26 +00:00
Update progress import endpoint to be more efficient on memory
This commit is contained in:
@@ -77,33 +77,42 @@ export const userProgressRouter = makeRouter((app) => {
|
|||||||
body: z.array(progressItemSchema),
|
body: z.array(progressItemSchema),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
handle(async ({ auth, params, body: newItems, em, req, limiter }) => {
|
handle(async ({ auth, params, body, em, req, limiter }) => {
|
||||||
await auth.assert();
|
await auth.assert();
|
||||||
|
|
||||||
if (auth.user.id !== params.uid)
|
if (auth.user.id !== params.uid)
|
||||||
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 itemsUpserted: ProgressItem[] = [];
|
||||||
const itemsToUpsert: ProgressItem[] = [];
|
|
||||||
|
|
||||||
for (const newItem of newItems) {
|
const newItems = [...body];
|
||||||
const existingItem = existingItems.find(
|
|
||||||
|
for (const existingItem of await em.find(ProgressItem, {
|
||||||
|
userId: params.uid,
|
||||||
|
})) {
|
||||||
|
const newItemIndex = newItems.findIndex(
|
||||||
(item) =>
|
(item) =>
|
||||||
item.tmdbId == newItem.tmdbId &&
|
item.tmdbId == existingItem.tmdbId &&
|
||||||
item.seasonId == newItem.seasonId &&
|
item.seasonId == existingItem.seasonId &&
|
||||||
item.episodeId == newItem.episodeId,
|
item.episodeId == existingItem.episodeId,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (existingItem) {
|
if (newItemIndex > -1) {
|
||||||
|
const newItem = newItems[newItemIndex];
|
||||||
if (existingItem.watched < newItem.watched) {
|
if (existingItem.watched < newItem.watched) {
|
||||||
existingItem.updatedAt = new Date();
|
existingItem.updatedAt = new Date();
|
||||||
existingItem.watched = newItem.watched;
|
existingItem.watched = newItem.watched;
|
||||||
}
|
}
|
||||||
itemsToUpsert.push(existingItem);
|
itemsUpserted.push(existingItem);
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
itemsToUpsert.push({
|
// Remove the item from the array, we have processed it
|
||||||
|
newItems.splice(newItemIndex, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// All unprocessed items, aka all items that don't already exist
|
||||||
|
for (const newItem of newItems) {
|
||||||
|
itemsUpserted.push({
|
||||||
id: randomUUID(),
|
id: randomUUID(),
|
||||||
duration: newItem.duration,
|
duration: newItem.duration,
|
||||||
episodeId: newItem.episodeId,
|
episodeId: newItem.episodeId,
|
||||||
@@ -118,7 +127,7 @@ export const userProgressRouter = makeRouter((app) => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const progressItems = await em.upsertMany(ProgressItem, itemsToUpsert);
|
const progressItems = await em.upsertMany(ProgressItem, itemsUpserted);
|
||||||
|
|
||||||
await em.flush();
|
await em.flush();
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user