fix: Improve continue button on org-add-team and validations [CAL-2015] (#10122)

* Improve continue button on team add and validations

* Fix server validations for orgs create team

* Remove unused prop form input

* add comment for context

* Fix duplicatedSlugs not working properly

* Fix continue button disabled state, added more examples for team names

* Fix initial state for inputValues on addTeamForm

* Don't allow whitespaces only inputs on create teams

* Use rhf instead of react useState

---------

Co-authored-by: Leo Giovanetti <[email protected]>
This commit is contained in:
alannncandLeo Giovanetti authored and GitHub committed 2023-07-17 17:17:29 -07:00
1 parent c26efcabe4
commit dad4fd17fb
4 files changed
+191 -80

No files matched your search

@@ -16,31 +16,57 @@ type CreateTeamsOptions = {
};
export const createTeamsHandler = async ({ ctx, input }: CreateTeamsOptions) => {
// Get User From context
const user = ctx.user;
if (!user) {
throw new NoUserError();
}
const { teamNames, orgId } = input;
if (orgId !== user.organizationId) {
throw new NotAuthorizedError();
}
// Validate user membership role
const userMembershipRole = await prisma.membership.findFirst({
where: {
userId: user.id,
teamId: orgId,
role: {
in: ["OWNER", "ADMIN"],
},
// @TODO: not sure if this already setup earlier
// accepted: true,
},
select: {
role: true,
},
});
if (!userMembershipRole) {
throw new NotAuthorizedError();
}
const organization = await prisma.team.findFirst({
where: { id: orgId },
select: { slug: true, metadata: true },
});
const metadata = teamMetadataSchema.parse(organization?.metadata);
if (!metadata?.requestedSlug && !organization?.slug)
throw new TRPCError({ code: "BAD_REQUEST", message: "no_organization_slug" });
if (!organization) throw new NoOrganizationError();
const userMembership = await prisma.membership.findFirst({
where: {
userId: ctx.user.id,
teamId: orgId,
},
select: {
userId: true,
role: true,
},
});
const parseTeams = teamMetadataSchema.safeParse(organization?.metadata);
// TODO test this check works
if (!userMembership || userMembership.role !== MembershipRole.OWNER)
throw new TRPCError({ code: "BAD_REQUEST", message: "not_authorized" });
if (!parseTeams.success) {
throw new InvalidMetadataError();
}
const metadata = parseTeams.success ? parseTeams.data : undefined;
if (!metadata?.requestedSlug && !organization?.slug) {
throw new NoOrganizationSlugError();
}
const [teamSlugs, userSlugs] = await prisma.$transaction([
prisma.team.findMany({ where: { parentId: orgId }, select: { slug: true } }),
@@ -51,11 +77,16 @@ export const createTeamsHandler = async ({ ctx, input }: CreateTeamsOptions) =>
.flatMap((ts) => ts.slug ?? [])
.concat(userSlugs.flatMap((us) => us.username ?? []));
const duplicatedSlugs = existingSlugs.filter((slug) => teamNames.includes(slug));
const duplicatedSlugs = existingSlugs.filter((slug) =>
teamNames.map((item) => slugify(item)).includes(slug)
);
if (duplicatedSlugs.length === teamNames.length) {
return { duplicatedSlugs };
}
await prisma.$transaction(
teamNames.flatMap((name) => {
if (!duplicatedSlugs.includes(name)) {
if (!duplicatedSlugs.includes(slugify(name))) {
return prisma.team.create({
data: {
name,
@@ -74,3 +105,33 @@ export const createTeamsHandler = async ({ ctx, input }: CreateTeamsOptions) =>
return { duplicatedSlugs };
};
class NoUserError extends TRPCError {
constructor() {
super({ code: "BAD_REQUEST", message: "no_user" });
}
}
class NotAuthorizedError extends TRPCError {
constructor() {
super({ code: "FORBIDDEN", message: "not_authorized" });
}
}
class InvalidMetadataError extends TRPCError {
constructor() {
super({ code: "BAD_REQUEST", message: "invalid_organization_metadata" });
}
}
class NoOrganizationError extends TRPCError {
constructor() {
super({ code: "BAD_REQUEST", message: "no_organization_found" });
}
}
class NoOrganizationSlugError extends TRPCError {
constructor() {
super({ code: "BAD_REQUEST", message: "no_organization_slug" });
}
}