documenso/packages/lib/server-only/user/update-profile.ts

48 lines
979 B
TypeScript
Raw Normal View History

2025-01-02 04:33:37 +00:00
import { UserSecurityAuditLogType } from '@prisma/client';
2023-06-09 08:21:18 +00:00
import { prisma } from '@documenso/prisma';
2024-01-30 06:31:27 +00:00
import type { RequestMetadata } from '../../universal/extract-request-metadata';
2023-06-09 08:21:18 +00:00
export type UpdateProfileOptions = {
userId: number;
name: string;
signature: string;
2024-01-30 06:31:27 +00:00
requestMetadata?: RequestMetadata;
2023-06-09 08:21:18 +00:00
};
2024-01-30 06:31:27 +00:00
export const updateProfile = async ({
userId,
name,
signature,
requestMetadata,
}: UpdateProfileOptions) => {
2023-06-09 08:21:18 +00:00
// Existence check
await prisma.user.findFirstOrThrow({
where: {
id: userId,
},
});
2025-08-25 11:00:35 +00:00
await prisma.$transaction(async (tx) => {
2024-01-30 06:31:27 +00:00
await tx.userSecurityAuditLog.create({
data: {
userId,
type: UserSecurityAuditLogType.ACCOUNT_PROFILE_UPDATE,
userAgent: requestMetadata?.userAgent,
ipAddress: requestMetadata?.ipAddress,
},
});
2023-06-09 08:21:18 +00:00
2025-08-25 11:00:35 +00:00
await tx.user.update({
2024-01-30 06:31:27 +00:00
where: {
id: userId,
},
data: {
name,
signature,
},
});
});
2023-06-09 08:21:18 +00:00
};