34 lines
1.2 KiB
TypeScript
34 lines
1.2 KiB
TypeScript
import { ensureIdentity } from '@app/server/helpers';
|
|
import { IdentityContext } from '@app/server/identity';
|
|
import { prisma } from '@app/server/prisma/client';
|
|
import { Book, MutationBookUpdateArgs } from '@generated/types';
|
|
import { toBookSchema } from './mappers/bookMapper';
|
|
|
|
export async function BookUpdateResolver(
|
|
_: any,
|
|
{ input }: MutationBookUpdateArgs,
|
|
{ req, res, identity }: IdentityContext
|
|
): Promise<Book> {
|
|
ensureIdentity(identity);
|
|
|
|
const model = await prisma.book.findUniqueOrThrow({
|
|
where: { id: input.id }
|
|
});
|
|
|
|
if (identity.id !== model.authorId) throw new Error('Operation is not permitted.');
|
|
|
|
if (input.title != null && input.title.length > 60) throw new Error('Title is too long.');
|
|
if (input.description != null && input.description.length > 900)
|
|
throw new Error('Description is too long.');
|
|
|
|
const modelUpdated = await prisma.book.update({
|
|
where: { id: input.id },
|
|
data: {
|
|
title: input.title != null ? input.title : undefined,
|
|
description: input.description != null ? input.description : undefined,
|
|
progressStatus: input.progressStatus != null ? input.progressStatus : undefined
|
|
}
|
|
});
|
|
|
|
return toBookSchema(modelUpdated, { req, res, identity });
|
|
}
|