26 lines
542 B
TypeScript
26 lines
542 B
TypeScript
import { prisma } from '@app/server/prisma/client';
|
|
|
|
export async function updateBookStats(id: string) {
|
|
const bookWordsTotal = await prisma.bookChapter.aggregate({
|
|
where: {
|
|
bookId: id
|
|
},
|
|
_sum: {
|
|
wordsTotal: true
|
|
}
|
|
});
|
|
|
|
const chaptersTotal = await prisma.bookChapter.count({
|
|
where: {
|
|
bookId: id
|
|
}
|
|
});
|
|
|
|
return prisma.book.update({
|
|
where: { id },
|
|
data: {
|
|
wordsTotal: bookWordsTotal._sum.wordsTotal != null ? bookWordsTotal._sum.wordsTotal : 0,
|
|
chaptersTotal
|
|
}
|
|
});
|
|
}
|