feat: add total duration and progress to series

This commit is contained in:
Varun Bajaj 2026-02-16 00:52:01 -05:00
parent b01facc034
commit 00f9940712
3 changed files with 48 additions and 1 deletions

View file

@ -65,6 +65,37 @@ class Series extends Model {
series.books = await series.getBooksExpandedWithLibraryItem()
return series
}
/**
*
* @param {string} seriesId
* @returns {Promise<number>} total duration (same unit as libraryItem.duration)
*/
static async getTotalDurationById(seriesId) {
const { book: bookModel, libraryItem: libraryItemModel, bookSeries: bookSeriesModel } = this.sequelize.models
// Sum durations on libraryItems for books that belong to the series.
// This relies on Sequelize associations existing between:
// libraryItem -> book, book -> bookSeries (or bookSeries as a through model).
const total = await libraryItemModel.sum('duration', {
where: { mediaType: 'book' },
include: [
{
model: bookModel,
attributes: [],
include: [
{
model: bookSeriesModel,
attributes: [],
where: { seriesId }
}
]
}
]
})
return Number(total) || 0
}
/**
*