Update progress import endpoint to be more efficient on memory

This commit is contained in:
William Oldham
2023-11-18 19:48:07 +00:00
parent 3643eea655
commit 787c8a96d6

View File

@@ -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();