Skip to content

Commit

Permalink
fix invitions and add better error handling (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
cmintey authored Nov 7, 2024
1 parent 86e8501 commit c81a523
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 28 deletions.
3 changes: 3 additions & 0 deletions src/lib/components/admin/Groups.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
valueAttr: { type: "text", minlength: 3, maxlength: 32, required: true },
// Returns the updated response value
response: async (name: string) => {
if (!name) {
return;
}
const groupsAPI = new GroupsAPI();
const group = await groupsAPI.create(name);
if (group) {
Expand Down
30 changes: 18 additions & 12 deletions src/lib/components/admin/InviteUser.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,24 @@
$: form = $page.form;
const triggerToast = () => {
const toastConfig: ToastSettings = {
message: "Invite sent!",
background: "variant-filled-success",
autohide: true,
timeout: 3000
};
$: if (form?.success && form?.sent !== null && config.smtp.enable) {
let toastConfig: ToastSettings;
if (form?.sent) {
toastConfig = {
message: "Invite sent!",
background: "variant-filled-success",
autohide: true,
timeout: 3000
};
} else {
toastConfig = {
message: `Invite failed to send: ${form?.message} `,
background: "variant-filled-error",
autohide: true,
timeout: 3000
};
}
toastStore.trigger(toastConfig);
};
if (form?.success && config.smtp.enable) {
triggerToast();
}
let groupId = "";
Expand All @@ -35,7 +41,7 @@
let showUrl = true;
const triggerInviteModal = () => {
if (groups.length === 0 && defaultGroup) {
if (!config.smtp.enable && groups.length === 0 && defaultGroup) {
groupId = defaultGroup.id;
setTimeout(() => submitButton.click(), 200);
showUrl = true;
Expand Down
33 changes: 23 additions & 10 deletions src/lib/server/email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ const sendEmail = async (options: Mail.Options) => {
!config.smtp.fromName)
) {
console.log("SMTP not set up properly, check your settings");
return false;
return {
success: false,
message: "SMTP not set up properly, check your settings"
};
}

const transport = nodemailer.createTransport({
Expand All @@ -54,16 +57,26 @@ const sendEmail = async (options: Mail.Options) => {
pass: config.smtp.pass
}
});
const msgInfo = await transport.sendMail({
from: {
name: config.smtp.fromName,
address: config.smtp.from
},
...options
});

console.log(msgInfo);
return msgInfo.accepted.length > 0;
return await transport
.sendMail({
from: {
name: config.smtp.fromName,
address: config.smtp.from
},
...options
})
.then((msgInfo) => ({
success: msgInfo.accepted.length === 1,
message: null
}))
.catch((e) => {
console.error(e);
return {
success: false,
message: e?.response
};
});
};

export const sendSignupLink = async (to: string, url: string) => {
Expand Down
6 changes: 3 additions & 3 deletions src/lib/server/invite-user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const inviteUser = async ({ url, request }: RequestEvent) => {
}
});

return { action: "invite-email", success: true, url: tokenUrl.href };
return { action: "invite-email", success: true, sent: null, message: null, url: tokenUrl.href };
}

await client.signupToken.create({
Expand All @@ -54,6 +54,6 @@ export const inviteUser = async ({ url, request }: RequestEvent) => {
}
});

await sendSignupLink(data.data["invite-email"]!, tokenUrl.href);
return { action: "invite-email", success: true, url: null };
const msgInfo = await sendSignupLink(data.data["invite-email"]!, tokenUrl.href);
return { action: "invite-email", success: true, sent: msgInfo.success, message: msgInfo.message, url: null };
};
6 changes: 4 additions & 2 deletions src/routes/admin/groups/[groupId]/members/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
type: "component",
component: "addUser",
response: async (userId: string) => {
await groupAPI.addMember(userId);
invalidateAll();
if (userId) {
await groupAPI.addMember(userId);
invalidateAll();
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/routes/setup-wizard/step/[step]/InviteUsersStep.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import InviteUser from "$lib/components/admin/InviteUser.svelte";
import type { Group } from "@prisma/client";
const config: Config = $page.data.config;
$: config = $page.data.config satisfies Config;
const groups: Group[] = $page.data.groups;
</script>

Expand Down

0 comments on commit c81a523

Please sign in to comment.