Add updatedAt saving into progress imports

This commit is contained in:
William Oldham 2023-12-06 19:43:37 +00:00
parent 8cffd0e7e8
commit 53d5ca1461
1 changed files with 17 additions and 3 deletions

View File

@ -18,6 +18,7 @@ const progressItemSchema = z.object({
episodeId: z.string().optional(),
seasonNumber: z.number().optional(),
episodeNumber: z.number().optional(),
updatedAt: z.string().datetime({ offset: true }).optional(),
});
export const userProgressRouter = makeRouter((app) => {
@ -59,7 +60,7 @@ export const userProgressRouter = makeRouter((app) => {
duration: body.duration,
watched: body.watched,
meta: body.meta,
updatedAt: new Date(),
updatedAt: defaultAndCoerceDateTime(body.updatedAt),
});
await em.persistAndFlush(progressItem);
@ -100,7 +101,9 @@ export const userProgressRouter = makeRouter((app) => {
if (newItemIndex > -1) {
const newItem = newItems[newItemIndex];
if (existingItem.watched < newItem.watched) {
existingItem.updatedAt = new Date();
existingItem.updatedAt = defaultAndCoerceDateTime(
newItem.updatedAt,
);
existingItem.watched = newItem.watched;
}
itemsUpserted.push(existingItem);
@ -123,7 +126,7 @@ export const userProgressRouter = makeRouter((app) => {
tmdbId: newItem.tmdbId,
userId: params.uid,
watched: newItem.watched,
updatedAt: new Date(),
updatedAt: defaultAndCoerceDateTime(newItem.updatedAt),
});
}
@ -207,3 +210,14 @@ export const userProgressRouter = makeRouter((app) => {
}),
);
});
// 1st August 2021 - movie-web epoch
const minEpoch = 1627776000000;
function defaultAndCoerceDateTime(dateTime: string | undefined) {
const epoch = dateTime ? new Date(dateTime).getTime() : Date.now();
const clampedEpoch = Math.max(minEpoch, Math.min(epoch, Date.now()));
return new Date(clampedEpoch);
}