2022-12-08 23:20:24 +00:00
|
|
|
import { MembershipRole, Prisma } from "@prisma/client";
|
2021-12-09 23:51:30 +00:00
|
|
|
import { randomBytes } from "crypto";
|
|
|
|
import { z } from "zod";
|
|
|
|
|
2022-12-07 19:55:47 +00:00
|
|
|
import { getRequestedSlugError } from "@calcom/app-store/stripepayment/lib/team-billing";
|
2022-07-28 19:58:26 +00:00
|
|
|
import { getUserAvailability } from "@calcom/core/getUserAvailability";
|
|
|
|
import { sendTeamInviteEmail } from "@calcom/emails";
|
2022-11-10 20:23:56 +00:00
|
|
|
import {
|
|
|
|
cancelTeamSubscriptionFromStripe,
|
|
|
|
purchaseTeamSubscription,
|
2022-12-13 12:43:37 +00:00
|
|
|
updateQuantitySubscriptionFromStripe,
|
2022-11-10 20:23:56 +00:00
|
|
|
} from "@calcom/features/ee/teams/lib/payments";
|
2022-12-13 12:43:37 +00:00
|
|
|
import { IS_TEAM_BILLING_ENABLED, WEBAPP_URL } from "@calcom/lib/constants";
|
2022-07-28 19:58:26 +00:00
|
|
|
import { getTranslation } from "@calcom/lib/server/i18n";
|
2022-11-10 20:23:56 +00:00
|
|
|
import { getTeamWithMembers, isTeamAdmin, isTeamMember, isTeamOwner } from "@calcom/lib/server/queries/teams";
|
2022-07-28 19:58:26 +00:00
|
|
|
import slugify from "@calcom/lib/slugify";
|
2022-08-26 21:10:12 +00:00
|
|
|
import {
|
|
|
|
closeComDeleteTeam,
|
|
|
|
closeComDeleteTeamMembership,
|
|
|
|
closeComUpdateTeam,
|
|
|
|
closeComUpsertTeamUser,
|
|
|
|
} from "@calcom/lib/sync/SyncServiceManager";
|
2022-07-28 19:58:26 +00:00
|
|
|
import { availabilityUserSelect } from "@calcom/prisma";
|
2022-11-10 20:23:56 +00:00
|
|
|
import { teamMetadataSchema } from "@calcom/prisma/zod-utils";
|
2022-02-07 23:35:26 +00:00
|
|
|
|
2021-12-09 23:51:30 +00:00
|
|
|
import { TRPCError } from "@trpc/server";
|
|
|
|
|
2022-11-10 23:40:01 +00:00
|
|
|
import { authedProcedure, router } from "../../trpc";
|
2022-07-22 17:27:06 +00:00
|
|
|
|
2023-01-25 08:56:20 +00:00
|
|
|
const isEmail = (str: string) => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(str);
|
2022-11-10 23:40:01 +00:00
|
|
|
export const viewerTeamsRouter = router({
|
2021-12-09 23:51:30 +00:00
|
|
|
// Retrieves team by id
|
2022-11-10 23:40:01 +00:00
|
|
|
get: authedProcedure
|
|
|
|
.input(
|
|
|
|
z.object({
|
|
|
|
teamId: z.number(),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.query(async ({ ctx, input }) => {
|
2022-11-10 20:23:56 +00:00
|
|
|
const team = await getTeamWithMembers(input.teamId, undefined, ctx.user.id);
|
|
|
|
if (!team) {
|
|
|
|
throw new TRPCError({ code: "NOT_FOUND", message: "Team not found." });
|
2021-12-09 23:51:30 +00:00
|
|
|
}
|
|
|
|
const membership = team?.members.find((membership) => membership.id === ctx.user.id);
|
2022-09-12 22:04:33 +00:00
|
|
|
|
2022-02-07 23:35:26 +00:00
|
|
|
return {
|
|
|
|
...team,
|
|
|
|
membership: {
|
|
|
|
role: membership?.role as MembershipRole,
|
2022-09-12 22:04:33 +00:00
|
|
|
accepted: membership?.accepted,
|
2022-02-07 23:35:26 +00:00
|
|
|
},
|
|
|
|
};
|
2021-12-09 23:51:30 +00:00
|
|
|
}),
|
2022-11-10 23:40:01 +00:00
|
|
|
// Returns teams I a member of
|
|
|
|
list: authedProcedure.query(async ({ ctx }) => {
|
|
|
|
const memberships = await ctx.prisma.membership.findMany({
|
|
|
|
where: {
|
|
|
|
userId: ctx.user.id,
|
|
|
|
},
|
|
|
|
include: {
|
|
|
|
team: true,
|
|
|
|
},
|
|
|
|
orderBy: { role: "desc" },
|
|
|
|
});
|
|
|
|
|
|
|
|
return memberships.map(({ team, ...membership }) => ({
|
|
|
|
role: membership.role,
|
|
|
|
accepted: membership.accepted,
|
|
|
|
...team,
|
|
|
|
}));
|
|
|
|
}),
|
|
|
|
create: authedProcedure
|
|
|
|
.input(
|
|
|
|
z.object({
|
|
|
|
name: z.string(),
|
|
|
|
slug: z.string().transform((val) => slugify(val.trim())),
|
|
|
|
logo: z
|
|
|
|
.string()
|
|
|
|
.optional()
|
|
|
|
.nullable()
|
|
|
|
.transform((v) => v || null),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.mutation(async ({ ctx, input }) => {
|
2022-11-10 20:23:56 +00:00
|
|
|
const { slug, name, logo } = input;
|
2021-12-09 23:51:30 +00:00
|
|
|
|
2022-11-10 20:23:56 +00:00
|
|
|
const nameCollisions = await ctx.prisma.team.findFirst({
|
2022-12-07 14:50:54 +00:00
|
|
|
where: {
|
|
|
|
slug: slug,
|
|
|
|
},
|
2021-12-09 23:51:30 +00:00
|
|
|
});
|
|
|
|
|
2022-11-10 20:23:56 +00:00
|
|
|
if (nameCollisions) throw new TRPCError({ code: "BAD_REQUEST", message: "Team name already taken." });
|
2021-12-09 23:51:30 +00:00
|
|
|
|
|
|
|
const createTeam = await ctx.prisma.team.create({
|
|
|
|
data: {
|
2022-11-10 20:23:56 +00:00
|
|
|
name,
|
|
|
|
logo,
|
|
|
|
members: {
|
|
|
|
create: {
|
|
|
|
userId: ctx.user.id,
|
|
|
|
role: MembershipRole.OWNER,
|
|
|
|
accepted: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
metadata: {
|
|
|
|
requestedSlug: slug,
|
|
|
|
},
|
2021-12-09 23:51:30 +00:00
|
|
|
},
|
|
|
|
});
|
2022-08-26 21:10:12 +00:00
|
|
|
|
|
|
|
// Sync Services: Close.com
|
|
|
|
closeComUpsertTeamUser(createTeam, ctx.user, MembershipRole.OWNER);
|
2022-09-15 09:47:59 +00:00
|
|
|
|
|
|
|
return createTeam;
|
2021-12-09 23:51:30 +00:00
|
|
|
}),
|
2022-11-10 23:40:01 +00:00
|
|
|
// Allows team owner to update team metadata
|
|
|
|
update: authedProcedure
|
|
|
|
.input(
|
|
|
|
z.object({
|
|
|
|
id: z.number(),
|
|
|
|
bio: z.string().optional(),
|
|
|
|
name: z.string().optional(),
|
|
|
|
logo: z.string().optional(),
|
|
|
|
slug: z.string().optional(),
|
|
|
|
hideBranding: z.boolean().optional(),
|
2023-01-06 10:55:57 +00:00
|
|
|
hideBookATeamMember: z.boolean().optional(),
|
2023-02-01 18:27:26 +00:00
|
|
|
brandColor: z.string().optional(),
|
|
|
|
darkBrandColor: z.string().optional(),
|
|
|
|
theme: z.string().optional().nullable(),
|
2022-11-10 23:40:01 +00:00
|
|
|
})
|
|
|
|
)
|
|
|
|
.mutation(async ({ ctx, input }) => {
|
2021-12-09 23:51:30 +00:00
|
|
|
if (!(await isTeamAdmin(ctx.user?.id, input.id))) throw new TRPCError({ code: "UNAUTHORIZED" });
|
|
|
|
|
|
|
|
if (input.slug) {
|
|
|
|
const userConflict = await ctx.prisma.team.findMany({
|
|
|
|
where: {
|
|
|
|
slug: input.slug,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
if (userConflict.some((t) => t.id !== input.id)) return;
|
|
|
|
}
|
2022-08-26 21:10:12 +00:00
|
|
|
|
|
|
|
const prevTeam = await ctx.prisma.team.findFirst({
|
|
|
|
where: {
|
|
|
|
id: input.id,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-11-10 20:23:56 +00:00
|
|
|
if (!prevTeam) throw new TRPCError({ code: "NOT_FOUND", message: "Team not found." });
|
|
|
|
|
|
|
|
const data: Prisma.TeamUpdateArgs["data"] = {
|
|
|
|
name: input.name,
|
|
|
|
logo: input.logo,
|
|
|
|
bio: input.bio,
|
|
|
|
hideBranding: input.hideBranding,
|
2023-01-06 10:55:57 +00:00
|
|
|
hideBookATeamMember: input.hideBookATeamMember,
|
2023-02-01 18:27:26 +00:00
|
|
|
brandColor: input.brandColor,
|
|
|
|
darkBrandColor: input.darkBrandColor,
|
|
|
|
theme: input.theme,
|
2022-11-10 20:23:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (
|
|
|
|
input.slug &&
|
|
|
|
IS_TEAM_BILLING_ENABLED &&
|
2022-11-11 20:38:21 +00:00
|
|
|
/** If the team doesn't have a slug we can assume that it hasn't been published yet. */
|
|
|
|
!prevTeam.slug
|
2022-11-10 20:23:56 +00:00
|
|
|
) {
|
2022-11-11 20:38:21 +00:00
|
|
|
// Save it on the metadata so we can use it later
|
|
|
|
data.metadata = {
|
|
|
|
requestedSlug: input.slug,
|
|
|
|
};
|
2022-11-10 20:23:56 +00:00
|
|
|
} else {
|
|
|
|
data.slug = input.slug;
|
2022-11-11 20:38:21 +00:00
|
|
|
|
|
|
|
// If we save slug, we don't need the requestedSlug anymore
|
|
|
|
const metadataParse = teamMetadataSchema.safeParse(prevTeam.metadata);
|
|
|
|
if (metadataParse.success) {
|
|
|
|
const { requestedSlug, ...cleanMetadata } = metadataParse.data || {};
|
|
|
|
data.metadata = {
|
|
|
|
...cleanMetadata,
|
|
|
|
};
|
|
|
|
}
|
2022-11-10 20:23:56 +00:00
|
|
|
}
|
|
|
|
|
2022-08-26 21:10:12 +00:00
|
|
|
const updatedTeam = await ctx.prisma.team.update({
|
2022-11-10 20:23:56 +00:00
|
|
|
where: { id: input.id },
|
|
|
|
data,
|
2021-12-09 23:51:30 +00:00
|
|
|
});
|
2022-08-26 21:10:12 +00:00
|
|
|
|
|
|
|
// Sync Services: Close.com
|
|
|
|
if (prevTeam) closeComUpdateTeam(prevTeam, updatedTeam);
|
2021-12-09 23:51:30 +00:00
|
|
|
}),
|
2022-11-10 23:40:01 +00:00
|
|
|
delete: authedProcedure
|
|
|
|
.input(
|
|
|
|
z.object({
|
|
|
|
teamId: z.number(),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.mutation(async ({ ctx, input }) => {
|
2021-12-09 23:51:30 +00:00
|
|
|
if (!(await isTeamOwner(ctx.user?.id, input.teamId))) throw new TRPCError({ code: "UNAUTHORIZED" });
|
|
|
|
|
2022-11-10 20:23:56 +00:00
|
|
|
if (IS_TEAM_BILLING_ENABLED) await cancelTeamSubscriptionFromStripe(input.teamId);
|
2022-02-07 23:35:26 +00:00
|
|
|
|
2021-12-09 23:51:30 +00:00
|
|
|
// delete all memberships
|
|
|
|
await ctx.prisma.membership.deleteMany({
|
|
|
|
where: {
|
|
|
|
teamId: input.teamId,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-08-26 21:10:12 +00:00
|
|
|
const deletedTeam = await ctx.prisma.team.delete({
|
2021-12-09 23:51:30 +00:00
|
|
|
where: {
|
|
|
|
id: input.teamId,
|
|
|
|
},
|
|
|
|
});
|
2022-08-26 21:10:12 +00:00
|
|
|
|
|
|
|
// Sync Services: Close.cm
|
|
|
|
closeComDeleteTeam(deletedTeam);
|
2021-12-09 23:51:30 +00:00
|
|
|
}),
|
2022-11-10 23:40:01 +00:00
|
|
|
removeMember: authedProcedure
|
|
|
|
.input(
|
|
|
|
z.object({
|
|
|
|
teamId: z.number(),
|
|
|
|
memberId: z.number(),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.mutation(async ({ ctx, input }) => {
|
2022-09-12 22:04:33 +00:00
|
|
|
const isAdmin = await isTeamAdmin(ctx.user?.id, input.teamId);
|
|
|
|
if (!isAdmin && ctx.user?.id !== input.memberId) throw new TRPCError({ code: "UNAUTHORIZED" });
|
2022-06-02 15:39:23 +00:00
|
|
|
// Only a team owner can remove another team owner.
|
|
|
|
if (
|
|
|
|
(await isTeamOwner(input.memberId, input.teamId)) &&
|
|
|
|
!(await isTeamOwner(ctx.user?.id, input.teamId))
|
|
|
|
)
|
|
|
|
throw new TRPCError({ code: "UNAUTHORIZED" });
|
2022-09-12 22:04:33 +00:00
|
|
|
if (ctx.user?.id === input.memberId && isAdmin)
|
2021-12-09 23:51:30 +00:00
|
|
|
throw new TRPCError({
|
|
|
|
code: "FORBIDDEN",
|
|
|
|
message: "You can not remove yourself from a team you own.",
|
|
|
|
});
|
2022-08-26 21:10:12 +00:00
|
|
|
|
|
|
|
const membership = await ctx.prisma.membership.delete({
|
2021-12-09 23:51:30 +00:00
|
|
|
where: {
|
2021-12-11 16:32:25 +00:00
|
|
|
userId_teamId: { userId: input.memberId, teamId: input.teamId },
|
2021-12-09 23:51:30 +00:00
|
|
|
},
|
2022-08-26 21:10:12 +00:00
|
|
|
include: {
|
|
|
|
user: true,
|
|
|
|
},
|
2021-12-09 23:51:30 +00:00
|
|
|
});
|
2022-02-07 23:35:26 +00:00
|
|
|
|
2022-08-26 21:10:12 +00:00
|
|
|
// Sync Services
|
|
|
|
closeComDeleteTeamMembership(membership.user);
|
2022-12-13 12:43:37 +00:00
|
|
|
if (IS_TEAM_BILLING_ENABLED) await updateQuantitySubscriptionFromStripe(input.teamId);
|
2021-12-09 23:51:30 +00:00
|
|
|
}),
|
2022-11-10 23:40:01 +00:00
|
|
|
inviteMember: authedProcedure
|
|
|
|
.input(
|
|
|
|
z.object({
|
|
|
|
teamId: z.number(),
|
2023-01-13 18:58:41 +00:00
|
|
|
usernameOrEmail: z.string().transform((usernameOrEmail) => usernameOrEmail.toLowerCase()),
|
2022-11-10 23:40:01 +00:00
|
|
|
role: z.nativeEnum(MembershipRole),
|
|
|
|
language: z.string(),
|
|
|
|
sendEmailInvitation: z.boolean(),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.mutation(async ({ ctx, input }) => {
|
2021-12-09 23:51:30 +00:00
|
|
|
if (!(await isTeamAdmin(ctx.user?.id, input.teamId))) throw new TRPCError({ code: "UNAUTHORIZED" });
|
2022-05-23 11:29:29 +00:00
|
|
|
if (input.role === MembershipRole.OWNER && !(await isTeamOwner(ctx.user?.id, input.teamId)))
|
|
|
|
throw new TRPCError({ code: "UNAUTHORIZED" });
|
2021-12-09 23:51:30 +00:00
|
|
|
|
|
|
|
const translation = await getTranslation(input.language ?? "en", "common");
|
|
|
|
|
|
|
|
const team = await ctx.prisma.team.findFirst({
|
|
|
|
where: {
|
|
|
|
id: input.teamId,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!team) throw new TRPCError({ code: "NOT_FOUND", message: "Team not found" });
|
|
|
|
|
|
|
|
const invitee = await ctx.prisma.user.findFirst({
|
|
|
|
where: {
|
|
|
|
OR: [{ username: input.usernameOrEmail }, { email: input.usernameOrEmail }],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!invitee) {
|
|
|
|
// liberal email match
|
|
|
|
|
|
|
|
if (!isEmail(input.usernameOrEmail))
|
|
|
|
throw new TRPCError({
|
|
|
|
code: "NOT_FOUND",
|
|
|
|
message: `Invite failed because there is no corresponding user for ${input.usernameOrEmail}`,
|
|
|
|
});
|
|
|
|
|
2022-01-13 20:05:23 +00:00
|
|
|
// valid email given, create User and add to team
|
2023-01-31 18:16:43 +00:00
|
|
|
await ctx.prisma.user.create({
|
2022-01-13 20:05:23 +00:00
|
|
|
data: {
|
|
|
|
email: input.usernameOrEmail,
|
|
|
|
invitedTo: input.teamId,
|
|
|
|
teams: {
|
|
|
|
create: {
|
|
|
|
teamId: input.teamId,
|
|
|
|
role: input.role as MembershipRole,
|
|
|
|
},
|
2021-12-09 23:51:30 +00:00
|
|
|
},
|
2022-01-13 20:05:23 +00:00
|
|
|
},
|
|
|
|
});
|
2021-12-09 23:51:30 +00:00
|
|
|
|
|
|
|
const token: string = randomBytes(32).toString("hex");
|
|
|
|
|
2022-04-21 20:32:25 +00:00
|
|
|
await ctx.prisma.verificationToken.create({
|
2021-12-09 23:51:30 +00:00
|
|
|
data: {
|
|
|
|
identifier: input.usernameOrEmail,
|
|
|
|
token,
|
|
|
|
expires: new Date(new Date().setHours(168)), // +1 week
|
|
|
|
},
|
|
|
|
});
|
|
|
|
if (ctx?.user?.name && team?.name) {
|
2022-06-06 17:49:56 +00:00
|
|
|
await sendTeamInviteEmail({
|
2021-12-09 23:51:30 +00:00
|
|
|
language: translation,
|
|
|
|
from: ctx.user.name,
|
|
|
|
to: input.usernameOrEmail,
|
|
|
|
teamName: team.name,
|
2023-01-13 18:58:41 +00:00
|
|
|
joinLink: `${WEBAPP_URL}/signup?token=${token}&callbackUrl=/teams`,
|
2023-01-31 18:16:43 +00:00
|
|
|
isCalcomMember: false,
|
2022-06-06 17:49:56 +00:00
|
|
|
});
|
2021-12-09 23:51:30 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// create provisional membership
|
|
|
|
try {
|
|
|
|
await ctx.prisma.membership.create({
|
|
|
|
data: {
|
|
|
|
teamId: input.teamId,
|
|
|
|
userId: invitee.id,
|
|
|
|
role: input.role as MembershipRole,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
if (e instanceof Prisma.PrismaClientKnownRequestError) {
|
|
|
|
if (e.code === "P2002") {
|
|
|
|
throw new TRPCError({
|
|
|
|
code: "FORBIDDEN",
|
|
|
|
message: "This user is a member of this team / has a pending invitation.",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else throw e;
|
|
|
|
}
|
|
|
|
|
2023-01-25 08:56:20 +00:00
|
|
|
let sendTo = input.usernameOrEmail;
|
|
|
|
if (!isEmail(input.usernameOrEmail)) {
|
|
|
|
sendTo = invitee.email;
|
|
|
|
}
|
2021-12-09 23:51:30 +00:00
|
|
|
// inform user of membership by email
|
|
|
|
if (input.sendEmailInvitation && ctx?.user?.name && team?.name) {
|
2022-06-06 17:49:56 +00:00
|
|
|
await sendTeamInviteEmail({
|
2021-12-09 23:51:30 +00:00
|
|
|
language: translation,
|
|
|
|
from: ctx.user.name,
|
2023-01-25 08:56:20 +00:00
|
|
|
to: sendTo,
|
2021-12-09 23:51:30 +00:00
|
|
|
teamName: team.name,
|
2022-07-22 17:27:06 +00:00
|
|
|
joinLink: WEBAPP_URL + "/settings/teams",
|
2023-01-31 18:16:43 +00:00
|
|
|
isCalcomMember: true,
|
2022-06-06 17:49:56 +00:00
|
|
|
});
|
2021-12-09 23:51:30 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-13 12:43:37 +00:00
|
|
|
if (IS_TEAM_BILLING_ENABLED) await updateQuantitySubscriptionFromStripe(input.teamId);
|
2021-12-09 23:51:30 +00:00
|
|
|
}),
|
2022-11-10 23:40:01 +00:00
|
|
|
acceptOrLeave: authedProcedure
|
|
|
|
.input(
|
|
|
|
z.object({
|
|
|
|
teamId: z.number(),
|
|
|
|
accept: z.boolean(),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.mutation(async ({ ctx, input }) => {
|
2021-12-09 23:51:30 +00:00
|
|
|
if (input.accept) {
|
2022-08-26 21:10:12 +00:00
|
|
|
const membership = await ctx.prisma.membership.update({
|
2021-12-09 23:51:30 +00:00
|
|
|
where: {
|
|
|
|
userId_teamId: { userId: ctx.user.id, teamId: input.teamId },
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
accepted: true,
|
|
|
|
},
|
2022-08-26 21:10:12 +00:00
|
|
|
include: {
|
|
|
|
team: true,
|
|
|
|
},
|
2021-12-09 23:51:30 +00:00
|
|
|
});
|
2022-08-26 21:10:12 +00:00
|
|
|
|
|
|
|
closeComUpsertTeamUser(membership.team, ctx.user, membership.role);
|
2021-12-09 23:51:30 +00:00
|
|
|
} else {
|
2022-02-07 23:35:26 +00:00
|
|
|
try {
|
|
|
|
//get team owner so we can alter their subscription seat count
|
|
|
|
const teamOwner = await ctx.prisma.membership.findFirst({
|
|
|
|
where: { teamId: input.teamId, role: MembershipRole.OWNER },
|
2022-08-26 21:10:12 +00:00
|
|
|
include: { team: true },
|
2022-02-07 23:35:26 +00:00
|
|
|
});
|
|
|
|
|
2022-08-26 21:10:12 +00:00
|
|
|
const membership = await ctx.prisma.membership.delete({
|
|
|
|
where: {
|
|
|
|
userId_teamId: { userId: ctx.user.id, teamId: input.teamId },
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
// Sync Services: Close.com
|
|
|
|
if (teamOwner) closeComUpsertTeamUser(teamOwner.team, ctx.user, membership.role);
|
2022-02-07 23:35:26 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
}
|
2021-12-09 23:51:30 +00:00
|
|
|
}
|
|
|
|
}),
|
2022-11-10 23:40:01 +00:00
|
|
|
changeMemberRole: authedProcedure
|
|
|
|
.input(
|
|
|
|
z.object({
|
|
|
|
teamId: z.number(),
|
|
|
|
memberId: z.number(),
|
|
|
|
role: z.nativeEnum(MembershipRole),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.mutation(async ({ ctx, input }) => {
|
2021-12-09 23:51:30 +00:00
|
|
|
if (!(await isTeamAdmin(ctx.user?.id, input.teamId))) throw new TRPCError({ code: "UNAUTHORIZED" });
|
2022-06-02 15:39:23 +00:00
|
|
|
// Only owners can award owner role.
|
|
|
|
if (input.role === MembershipRole.OWNER && !(await isTeamOwner(ctx.user?.id, input.teamId)))
|
|
|
|
throw new TRPCError({ code: "UNAUTHORIZED" });
|
2021-12-09 23:51:30 +00:00
|
|
|
const memberships = await ctx.prisma.membership.findMany({
|
|
|
|
where: {
|
|
|
|
teamId: input.teamId,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const targetMembership = memberships.find((m) => m.userId === input.memberId);
|
|
|
|
const myMembership = memberships.find((m) => m.userId === ctx.user.id);
|
|
|
|
const teamHasMoreThanOneOwner = memberships.some((m) => m.role === MembershipRole.OWNER);
|
|
|
|
|
|
|
|
if (myMembership?.role === MembershipRole.ADMIN && targetMembership?.role === MembershipRole.OWNER) {
|
|
|
|
throw new TRPCError({
|
|
|
|
code: "FORBIDDEN",
|
|
|
|
message: "You can not change the role of an owner if you are an admin.",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!teamHasMoreThanOneOwner) {
|
|
|
|
throw new TRPCError({
|
|
|
|
code: "FORBIDDEN",
|
|
|
|
message: "You can not change the role of the only owner of a team.",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (myMembership?.role === MembershipRole.ADMIN && input.memberId === ctx.user.id) {
|
|
|
|
throw new TRPCError({
|
|
|
|
code: "FORBIDDEN",
|
|
|
|
message: "You can not change yourself to a higher role.",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-08-26 21:10:12 +00:00
|
|
|
const membership = await ctx.prisma.membership.update({
|
2021-12-09 23:51:30 +00:00
|
|
|
where: {
|
|
|
|
userId_teamId: { userId: input.memberId, teamId: input.teamId },
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
role: input.role,
|
|
|
|
},
|
2022-08-26 21:10:12 +00:00
|
|
|
include: {
|
|
|
|
team: true,
|
|
|
|
user: true,
|
|
|
|
},
|
2021-12-09 23:51:30 +00:00
|
|
|
});
|
2022-08-26 21:10:12 +00:00
|
|
|
|
|
|
|
// Sync Services: Close.com
|
|
|
|
closeComUpsertTeamUser(membership.team, membership.user, membership.role);
|
2021-12-09 23:51:30 +00:00
|
|
|
}),
|
2022-11-10 23:40:01 +00:00
|
|
|
getMemberAvailability: authedProcedure
|
|
|
|
.input(
|
|
|
|
z.object({
|
|
|
|
teamId: z.number(),
|
|
|
|
memberId: z.number(),
|
|
|
|
timezone: z.string(),
|
|
|
|
dateFrom: z.string(),
|
|
|
|
dateTo: z.string(),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.query(async ({ ctx, input }) => {
|
2022-09-12 22:04:33 +00:00
|
|
|
const team = await isTeamMember(ctx.user?.id, input.teamId);
|
2021-12-09 23:51:30 +00:00
|
|
|
if (!team) throw new TRPCError({ code: "UNAUTHORIZED" });
|
|
|
|
|
|
|
|
// verify member is in team
|
|
|
|
const members = await ctx.prisma.membership.findMany({
|
|
|
|
where: { teamId: input.teamId },
|
2022-06-10 18:38:46 +00:00
|
|
|
include: {
|
|
|
|
user: {
|
|
|
|
select: {
|
|
|
|
...availabilityUserSelect,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-12-09 23:51:30 +00:00
|
|
|
});
|
|
|
|
const member = members?.find((m) => m.userId === input.memberId);
|
|
|
|
if (!member) throw new TRPCError({ code: "NOT_FOUND", message: "Member not found" });
|
|
|
|
if (!member.user.username)
|
|
|
|
throw new TRPCError({ code: "BAD_REQUEST", message: "Member doesn't have a username" });
|
|
|
|
|
|
|
|
// get availability for this member
|
2022-06-10 18:38:46 +00:00
|
|
|
return await getUserAvailability(
|
|
|
|
{
|
|
|
|
username: member.user.username,
|
|
|
|
dateFrom: input.dateFrom,
|
|
|
|
dateTo: input.dateTo,
|
|
|
|
},
|
|
|
|
{ user: member.user }
|
|
|
|
);
|
2022-07-21 17:02:20 +00:00
|
|
|
}),
|
2022-11-10 23:40:01 +00:00
|
|
|
getMembershipbyUser: authedProcedure
|
|
|
|
.input(
|
|
|
|
z.object({
|
|
|
|
teamId: z.number(),
|
|
|
|
memberId: z.number(),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.query(async ({ ctx, input }) => {
|
2022-07-21 17:02:20 +00:00
|
|
|
if (ctx.user.id !== input.memberId) {
|
|
|
|
throw new TRPCError({
|
|
|
|
code: "UNAUTHORIZED",
|
|
|
|
message: "You cannot view memberships that are not your own.",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return await ctx.prisma.membership.findUnique({
|
|
|
|
where: {
|
|
|
|
userId_teamId: {
|
|
|
|
userId: input.memberId,
|
|
|
|
teamId: input.teamId,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}),
|
2022-11-10 23:40:01 +00:00
|
|
|
updateMembership: authedProcedure
|
|
|
|
.input(
|
|
|
|
z.object({
|
|
|
|
teamId: z.number(),
|
|
|
|
memberId: z.number(),
|
|
|
|
disableImpersonation: z.boolean(),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.mutation(async ({ ctx, input }) => {
|
2022-07-21 17:02:20 +00:00
|
|
|
if (ctx.user.id !== input.memberId) {
|
|
|
|
throw new TRPCError({
|
|
|
|
code: "UNAUTHORIZED",
|
|
|
|
message: "You cannot edit memberships that are not your own.",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return await ctx.prisma.membership.update({
|
|
|
|
where: {
|
|
|
|
userId_teamId: {
|
|
|
|
userId: input.memberId,
|
|
|
|
teamId: input.teamId,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
disableImpersonation: input.disableImpersonation,
|
|
|
|
},
|
|
|
|
});
|
2022-11-10 20:23:56 +00:00
|
|
|
}),
|
2022-11-10 23:40:01 +00:00
|
|
|
validateTeamSlug: authedProcedure
|
|
|
|
.input(
|
|
|
|
z.object({
|
|
|
|
slug: z.string(),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.query(async ({ ctx, input }) => {
|
2022-11-10 20:23:56 +00:00
|
|
|
const team = await ctx.prisma.team.findFirst({
|
|
|
|
where: {
|
|
|
|
slug: input.slug,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return !team;
|
|
|
|
}),
|
2022-11-10 23:40:01 +00:00
|
|
|
publish: authedProcedure
|
|
|
|
.input(
|
|
|
|
z.object({
|
|
|
|
teamId: z.number(),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.mutation(async ({ ctx, input }) => {
|
2022-11-10 20:23:56 +00:00
|
|
|
if (!(await isTeamAdmin(ctx.user.id, input.teamId))) throw new TRPCError({ code: "UNAUTHORIZED" });
|
|
|
|
const { teamId: id } = input;
|
|
|
|
|
|
|
|
const prevTeam = await ctx.prisma.team.findFirst({ where: { id }, include: { members: true } });
|
|
|
|
|
|
|
|
if (!prevTeam) throw new TRPCError({ code: "NOT_FOUND", message: "Team not found." });
|
|
|
|
|
|
|
|
const metadata = teamMetadataSchema.safeParse(prevTeam.metadata);
|
|
|
|
|
2022-11-16 21:07:20 +00:00
|
|
|
if (!metadata.success) throw new TRPCError({ code: "BAD_REQUEST", message: "Invalid team metadata" });
|
2022-11-10 20:23:56 +00:00
|
|
|
|
2022-11-11 20:38:21 +00:00
|
|
|
// if payment needed, respond with checkout url
|
2022-11-10 20:23:56 +00:00
|
|
|
if (IS_TEAM_BILLING_ENABLED) {
|
|
|
|
const checkoutSession = await purchaseTeamSubscription({
|
|
|
|
teamId: prevTeam.id,
|
|
|
|
seats: prevTeam.members.length,
|
|
|
|
userId: ctx.user.id,
|
|
|
|
});
|
|
|
|
if (!checkoutSession.url)
|
|
|
|
throw new TRPCError({
|
|
|
|
code: "INTERNAL_SERVER_ERROR",
|
|
|
|
message: "Failed retrieving a checkout session URL.",
|
|
|
|
});
|
2022-11-18 23:55:34 +00:00
|
|
|
return { url: checkoutSession.url, message: "Payment required to publish team" };
|
2022-11-10 20:23:56 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 21:07:20 +00:00
|
|
|
if (!metadata.data?.requestedSlug) {
|
|
|
|
throw new TRPCError({ code: "BAD_REQUEST", message: "Can't publish team without `requestedSlug`" });
|
|
|
|
}
|
|
|
|
|
2022-11-10 20:23:56 +00:00
|
|
|
const { requestedSlug, ...newMetadata } = metadata.data;
|
|
|
|
let updatedTeam: Awaited<ReturnType<typeof ctx.prisma.team.update>>;
|
|
|
|
|
|
|
|
try {
|
|
|
|
updatedTeam = await ctx.prisma.team.update({
|
|
|
|
where: { id },
|
|
|
|
data: {
|
|
|
|
slug: requestedSlug,
|
|
|
|
metadata: { ...newMetadata },
|
|
|
|
},
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
const { message } = getRequestedSlugError(error, requestedSlug);
|
|
|
|
throw new TRPCError({ code: "INTERNAL_SERVER_ERROR", message });
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sync Services: Close.com
|
|
|
|
closeComUpdateTeam(prevTeam, updatedTeam);
|
|
|
|
|
2022-11-18 23:55:34 +00:00
|
|
|
return {
|
|
|
|
url: `${WEBAPP_URL}/settings/teams/${updatedTeam.id}/profile`,
|
2022-12-07 19:55:47 +00:00
|
|
|
message: "Team published successfully",
|
2022-11-18 23:55:34 +00:00
|
|
|
};
|
2022-11-10 23:40:01 +00:00
|
|
|
}),
|
2022-11-16 21:07:20 +00:00
|
|
|
/** This is a temporal endpoint so we can progressively upgrade teams to the new billing system. */
|
|
|
|
getUpgradeable: authedProcedure.query(async ({ ctx }) => {
|
|
|
|
if (!IS_TEAM_BILLING_ENABLED) return [];
|
|
|
|
let { teams } = await ctx.prisma.user.findUniqueOrThrow({
|
|
|
|
where: { id: ctx.user.id },
|
|
|
|
include: { teams: { where: { role: MembershipRole.OWNER }, include: { team: true } } },
|
|
|
|
});
|
|
|
|
/** We only need to return teams that don't have a `subscriptionId` on their metadata */
|
|
|
|
teams = teams.filter((m) => {
|
|
|
|
const metadata = teamMetadataSchema.safeParse(m.team.metadata);
|
|
|
|
if (metadata.success && metadata.data?.subscriptionId) return false;
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
return teams;
|
|
|
|
}),
|
2022-12-22 12:35:01 +00:00
|
|
|
listMembers: authedProcedure
|
|
|
|
.input(
|
|
|
|
z.object({
|
|
|
|
teamIds: z.number().array().optional(),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.query(async ({ ctx, input }) => {
|
|
|
|
const teams = await ctx.prisma.team.findMany({
|
|
|
|
where: {
|
|
|
|
id: {
|
|
|
|
in: input.teamIds,
|
|
|
|
},
|
|
|
|
members: {
|
|
|
|
some: {
|
|
|
|
user: {
|
|
|
|
id: ctx.user.id,
|
|
|
|
},
|
2023-01-21 17:15:59 +00:00
|
|
|
accepted: true,
|
2022-12-22 12:35:01 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
members: {
|
|
|
|
select: {
|
|
|
|
user: {
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
name: true,
|
|
|
|
avatar: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2023-02-16 22:39:57 +00:00
|
|
|
type UserMap = Record<number, (typeof teams)[number]["members"][number]["user"]>;
|
2022-12-22 12:35:01 +00:00
|
|
|
// flattern users to be unique by id
|
|
|
|
const users = teams
|
|
|
|
.flatMap((t) => t.members)
|
|
|
|
.reduce((acc, m) => (m.user.id in acc ? acc : { ...acc, [m.user.id]: m.user }), {} as UserMap);
|
|
|
|
return Object.values(users);
|
|
|
|
}),
|
2022-12-27 21:03:39 +00:00
|
|
|
hasTeamPlan: authedProcedure.query(async ({ ctx }) => {
|
|
|
|
const userId = ctx.user.id;
|
|
|
|
const hasTeamPlan = await ctx.prisma.membership.findFirst({
|
|
|
|
where: {
|
|
|
|
userId,
|
|
|
|
team: {
|
|
|
|
slug: {
|
|
|
|
not: null,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return { hasTeamPlan: !!hasTeamPlan };
|
|
|
|
}),
|
2023-01-24 15:27:05 +00:00
|
|
|
listInvites: authedProcedure.query(async ({ ctx }) => {
|
|
|
|
const userId = ctx.user.id;
|
|
|
|
return await ctx.prisma.membership.findMany({
|
|
|
|
where: {
|
|
|
|
user: {
|
|
|
|
id: userId,
|
|
|
|
},
|
|
|
|
accepted: false,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}),
|
2022-11-10 23:40:01 +00:00
|
|
|
});
|