ln/app/server/graphql/resolvers/bookChapterDelete.ts

25 lines
1,014 B
TypeScript

import { ensureIdentity } from '@app/server/helpers';
import { IdentityContext } from '@app/server/identity';
import { prisma } from '@app/server/prisma/client';
import { updateBookStats } from '@app/server/service/book';
import { Book, MutationBookChapterDeleteArgs } from '@generated/types';
import { toBookSchema } from './mappers/bookMapper';
export async function BookChapterDeleteResolver(
_: any,
{ id }: MutationBookChapterDeleteArgs,
{ req, res, identity }: IdentityContext
): Promise<Book> {
ensureIdentity(identity);
const model = await prisma.bookChapter.findUniqueOrThrow({
where: { id },
select: { book: { select: { id: true, authorId: true } } }
});
if (identity.id !== model.book.authorId) throw new Error('Operation is not permitted.');
await prisma.bookChapter.delete({ where: { id } });
await updateBookStats(model.book.id);
const book = await prisma.book.findUniqueOrThrow({ where: { id: model.book.id } });
return toBookSchema(book, { req, res, identity });
}