feat: Add ability to edit avatar (#11399)
Co-authored-by: Udit Takkar <[email protected]> Co-authored-by: Udit Takkar <[email protected]>
This commit is contained in:
8 files changed
+51
-12
No files matched your search
@@ -1,5 +1,7 @@
|
||||
import { isOrganisationAdmin } from "@calcom/lib/server/queries/organisations";
|
||||
import { isOrganisationAdmin, isOrganisationOwner } from "@calcom/lib/server/queries/organisations";
|
||||
import { resizeBase64Image } from "@calcom/lib/server/resizeBase64Image";
|
||||
import { prisma } from "@calcom/prisma";
|
||||
import { MembershipRole } from "@calcom/prisma/enums";
|
||||
import type { TrpcSessionUser } from "@calcom/trpc/server/trpc";
|
||||
|
||||
import { TRPCError } from "@trpc/server";
|
||||
@@ -21,6 +23,11 @@ export const updateUserHandler = async ({ ctx, input }: UpdateUserOptions) => {
|
||||
|
||||
if (!(await isOrganisationAdmin(userId, organizationId))) throw new TRPCError({ code: "UNAUTHORIZED" });
|
||||
|
||||
// only OWNER can update the role to OWNER
|
||||
if (input.role === MembershipRole.OWNER && !(await isOrganisationOwner(userId, organizationId))) {
|
||||
throw new TRPCError({ code: "UNAUTHORIZED" });
|
||||
}
|
||||
|
||||
// Is requested user a member of the organization?
|
||||
const requestedMember = await prisma.membership.findFirst({
|
||||
where: {
|
||||
@@ -33,6 +40,11 @@ export const updateUserHandler = async ({ ctx, input }: UpdateUserOptions) => {
|
||||
if (!requestedMember)
|
||||
throw new TRPCError({ code: "UNAUTHORIZED", message: "User does not belong to your organization" });
|
||||
|
||||
let avatar = input.avatar;
|
||||
if (input.avatar) {
|
||||
avatar = await resizeBase64Image(input.avatar);
|
||||
}
|
||||
|
||||
// Update user
|
||||
await prisma.$transaction([
|
||||
prisma.user.update({
|
||||
@@ -44,6 +56,7 @@ export const updateUserHandler = async ({ ctx, input }: UpdateUserOptions) => {
|
||||
email: input.email,
|
||||
name: input.name,
|
||||
timeZone: input.timeZone,
|
||||
avatar,
|
||||
},
|
||||
}),
|
||||
prisma.membership.update({
|
||||
|
||||
@@ -5,7 +5,8 @@ export const ZUpdateUserInputSchema = z.object({
|
||||
bio: z.string().optional(),
|
||||
name: z.string().optional(),
|
||||
email: z.string().optional(),
|
||||
role: z.enum(["ADMIN", "MEMBER"]),
|
||||
avatar: z.string().optional(),
|
||||
role: z.enum(["ADMIN", "MEMBER", "OWNER"]),
|
||||
timeZone: z.string(),
|
||||
});
|
||||
|
||||
|
||||
Reference in new issue
Block a user