ln/app/server/graphql/resolvers/userUpdateResolver.ts
2025-07-08 16:36:00 +03:00

36 lines
1.1 KiB
TypeScript

import { call } from '@app/common';
import { ensureIdentity } from '@app/server/helpers';
import { IdentityContext } from '@app/server/identity';
import { prisma } from '@app/server/prisma/client';
import { MutationUserUpdateArgs, User } from '@generated/types';
import { toUserSchema } from './mappers/userMapper';
export async function UserUpdateResolver(
_: any,
{ input }: MutationUserUpdateArgs,
{ identity }: IdentityContext
): Promise<User> {
ensureIdentity(identity);
if (input.id !== identity.id) throw new Error('Operation is not permitted.');
if (input.description != null && input.description.length > 700)
throw new Error('Description is too long.');
const description = call((): string | null | undefined => {
if (input.description != null && input.description.length !== 0) return input.description;
if (input.description != null && input.description.length === 0) return null;
return undefined;
});
const updated = await prisma.user.update({
where: {
id: input.id
},
data: {
description
}
});
return toUserSchema(updated);
}