101 lines
2.2 KiB
TypeScript
101 lines
2.2 KiB
TypeScript
import { ensureIdentity } from '@app/server/helpers';
|
|
import { IdentityContext } from '@app/server/identity';
|
|
import { prisma, uuid } from '@app/server/prisma/client';
|
|
import { MutationRatingSetArgs, RatingType } from '@generated/types';
|
|
|
|
export async function RatingSetResolver(
|
|
_: any,
|
|
{ input }: MutationRatingSetArgs,
|
|
{ identity }: IdentityContext
|
|
): Promise<void> {
|
|
ensureIdentity(identity);
|
|
if (input.bookId != null) {
|
|
const rating = await prisma.rating.findUnique({
|
|
where: {
|
|
userId_bookId: {
|
|
userId: identity.id,
|
|
bookId: input.bookId
|
|
}
|
|
}
|
|
});
|
|
|
|
if (rating != null && input.ratingType !== RatingType.None) {
|
|
await prisma.rating.update({
|
|
where: { id: rating.id },
|
|
data: {
|
|
type: input.ratingType
|
|
}
|
|
});
|
|
return;
|
|
}
|
|
|
|
if (input.ratingType === RatingType.None) {
|
|
await prisma.rating.delete({
|
|
where: {
|
|
userId_bookId: {
|
|
userId: identity.id,
|
|
bookId: input.bookId
|
|
}
|
|
}
|
|
});
|
|
return;
|
|
}
|
|
|
|
await prisma.rating.create({
|
|
data: {
|
|
id: uuid(),
|
|
userId: identity.id,
|
|
bookId: input.bookId,
|
|
type: input.ratingType
|
|
}
|
|
});
|
|
|
|
return;
|
|
}
|
|
|
|
if (input.bookChapterId != null) {
|
|
const rating = await prisma.rating.findUnique({
|
|
where: {
|
|
userId_bookChapterId: {
|
|
userId: identity.id,
|
|
bookChapterId: input.bookChapterId
|
|
}
|
|
}
|
|
});
|
|
|
|
if (rating != null && input.ratingType !== RatingType.None) {
|
|
await prisma.rating.update({
|
|
where: { id: rating.id },
|
|
data: {
|
|
type: input.ratingType
|
|
}
|
|
});
|
|
return;
|
|
}
|
|
|
|
if (input.ratingType === RatingType.None) {
|
|
await prisma.rating.delete({
|
|
where: {
|
|
userId_bookChapterId: {
|
|
userId: identity.id,
|
|
bookChapterId: input.bookChapterId
|
|
}
|
|
}
|
|
});
|
|
return;
|
|
}
|
|
|
|
await prisma.rating.create({
|
|
data: {
|
|
id: uuid(),
|
|
userId: identity.id,
|
|
bookChapterId: input.bookChapterId,
|
|
type: input.ratingType
|
|
}
|
|
});
|
|
|
|
return;
|
|
}
|
|
|
|
throw new Error('Invalid Rating id input.');
|
|
}
|