chore: enhance error handling in sendInvite mutation and invalidate email cache on success
This commit is contained in:
parent
6890ed64c7
commit
f1a323f478
2 changed files with 24 additions and 10 deletions
|
|
@ -31,12 +31,16 @@ export const UserViewHeader = () => {
|
|||
router.reload();
|
||||
},
|
||||
});
|
||||
const utils = api.useUtils();
|
||||
const { mutateAsync: sendInvite } = api.invite.sendInvite.useMutation({
|
||||
onError: (error) => {
|
||||
toast.error(error.message);
|
||||
},
|
||||
onSuccess: async () => {
|
||||
toast.success("Email inviata con successo");
|
||||
await utils.comunicazioni.getEmails.invalidate({
|
||||
userId: data?.id || "",
|
||||
});
|
||||
},
|
||||
});
|
||||
if (!data) return <div>Errore</div>;
|
||||
|
|
|
|||
|
|
@ -27,19 +27,29 @@ export const inviteRouter = createTRPCRouter({
|
|||
}),
|
||||
)
|
||||
.mutation(async ({ input }) => {
|
||||
const user = await getUser({ userId: input.id, db });
|
||||
const token = await createInviteToken(user.email);
|
||||
try {
|
||||
const user = await getUser({ userId: input.id, db });
|
||||
const token = await createInviteToken(user.email);
|
||||
|
||||
const inviteUrl = `${env.BASE_URL}/auth/accetta-invito/${token}`;
|
||||
const inviteUrl = `${env.BASE_URL}/auth/accetta-invito/${token}`;
|
||||
|
||||
await sendInvitoEmail({
|
||||
userId: input.id,
|
||||
email: user.email,
|
||||
nome: user.nome,
|
||||
inviteUrl,
|
||||
});
|
||||
await sendInvitoEmail({
|
||||
userId: input.id,
|
||||
email: user.email,
|
||||
nome: user.nome,
|
||||
inviteUrl,
|
||||
});
|
||||
|
||||
return { success: true };
|
||||
return { success: true };
|
||||
} catch (e) {
|
||||
console.error("Error in sendInvite:", e);
|
||||
throw new TRPCError({
|
||||
code: "INTERNAL_SERVER_ERROR",
|
||||
message:
|
||||
"Errore durante l'invio dell'invito: " +
|
||||
(e instanceof Error ? e.message : "Unknown error"),
|
||||
});
|
||||
}
|
||||
}),
|
||||
|
||||
// Verify invite token (public - user clicking the link)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue