Skip to content

Commit

Permalink
Fix/prevent already member to be invited again (#189)
Browse files Browse the repository at this point in the history
* FEAT/isPgError utils added

* REFACTOR/transaction added

* CHORE/updated

* FIX/prevent already member to be invited again
  • Loading branch information
AugustinSorel authored Jun 30, 2024
1 parent 452486f commit 2ea75b6
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
2 changes: 0 additions & 2 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -462,8 +462,6 @@ const FeaturesGridBackground = () => {
);
};

//TODO: leaving team
//TODO: leaving team
//TODO: renaming team
//TODO: deleting team
//TODO: add random facts to team page eg heavier lifter or most active in team
Expand Down
5 changes: 1 addition & 4 deletions src/app/teams/[id]/_inviteUserForm/inviteUserForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,7 @@ const useFormSchema = () => {
const team = utils.team.get.getData({ id: pageParams.id });

const userAcceptedInvite = team?.usersToTeams.find((userToTeam) => {
return (
userToTeam.team?.teamInvite?.accepted &&
userToTeam.user.email === vals.email
);
return userToTeam.user.email === vals.email;
});

return !userAcceptedInvite;
Expand Down
18 changes: 10 additions & 8 deletions src/server/api/routers/team.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,17 @@ export const teamRouter = createTRPCRouter({
.merge(teamInviteSchema.pick({ token: true })),
)
.mutation(async ({ ctx, input }) => {
await ctx.db.insert(usersToTeams).values({
memberId: ctx.session.user.id,
teamId: input.id,
});
await ctx.db.transaction(async (tx) => {
await tx.insert(usersToTeams).values({
memberId: ctx.session.user.id,
teamId: input.id,
});

await ctx.db
.update(teamInvites)
.set({ accepted: true })
.where(eq(teamInvites.token, input.token));
await tx
.update(teamInvites)
.set({ accepted: true })
.where(eq(teamInvites.token, input.token));
});
}),

getInvite: protectedProcedure
Expand Down
9 changes: 9 additions & 0 deletions src/server/db/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { PostgresError } from "postgres";

export const isPgError = (error: unknown): error is PostgresError => {
if (!(error instanceof Error)) {
return false;
}

return error.constructor.name === "PostgresError";
};

0 comments on commit 2ea75b6

Please sign in to comment.