sendEmail caught transport failures internally and resolved to false or undefined, so every caller's .catch() was dead code and send failures were silently suppressed. Password recovery in particular reported "email sent successfully" while the user received nothing. sendEmail now throws an AppError carrying the underlying cause, and returns Promise<string> instead of Promise<string | false | undefined>. Call sites updated for the new contract: - UserService.registerUser: the existing .catch() now actually fires - UserService.requestRecovery: the failure reaches the client instead of a misleading success response - InviteService and the test-email endpoint: keep their user-facing messages and attach the SMTP cause - EmailProvider: catches and returns false, because NotificationsService fans providers out through Promise.all and a rejection there would abort the other channels' alerts Closes #3757
116 lines
3.7 KiB
TypeScript
116 lines
3.7 KiB
TypeScript
import type { Invite } from "@/domain/invites/invite.type.js";
|
|
import type { UserRole } from "@/domain/users/user.type.js";
|
|
import { canManageRole } from "@/domain/users/user.type.js";
|
|
import type { IInvitesRepository } from "@/domain/invites/invite.repository.interface.js";
|
|
import { AppError } from "@/utils/AppError.js";
|
|
import { ISettingsService } from "../app-settings/app-settings.service.js";
|
|
import { IEmailService } from "@/service/emailService.js";
|
|
|
|
const SERVICE_NAME = "inviteService";
|
|
|
|
export interface IInviteService {
|
|
getInviteToken(params: { invite: Partial<Invite>; teamId: string; userRoles: UserRole[] }): Promise<Invite>;
|
|
sendInviteEmail(params: { invite: Partial<Invite>; firstName: string; userRoles: UserRole[] }): Promise<void>;
|
|
verifyInviteToken(params: { inviteToken: string }): Promise<Invite>;
|
|
}
|
|
|
|
export class InviteService implements IInviteService {
|
|
static SERVICE_NAME = SERVICE_NAME;
|
|
|
|
private settingsService: ISettingsService;
|
|
private emailService: IEmailService;
|
|
private invitesRepository: IInvitesRepository;
|
|
|
|
constructor({
|
|
invitesRepository,
|
|
settingsService,
|
|
emailService,
|
|
}: {
|
|
invitesRepository: IInvitesRepository;
|
|
settingsService: ISettingsService;
|
|
emailService: IEmailService;
|
|
}) {
|
|
this.invitesRepository = invitesRepository;
|
|
this.settingsService = settingsService;
|
|
this.emailService = emailService;
|
|
}
|
|
|
|
getInviteToken = async ({ invite, teamId, userRoles }: { invite: Partial<Invite>; teamId: string; userRoles: UserRole[] }) => {
|
|
invite.teamId = teamId;
|
|
|
|
const inviteRoles = invite.role ?? [];
|
|
|
|
for (const targetRole of inviteRoles) {
|
|
const canManage = userRoles.some((actorRole) => canManageRole(actorRole, targetRole));
|
|
if (!canManage) {
|
|
throw new AppError({
|
|
message: "You do not have permission to create this invite",
|
|
service: SERVICE_NAME,
|
|
method: "getInviteToken",
|
|
status: 403,
|
|
});
|
|
}
|
|
}
|
|
|
|
const inviteToken = await this.invitesRepository.create(invite);
|
|
return inviteToken;
|
|
};
|
|
|
|
sendInviteEmail = async ({ invite, firstName, userRoles }: { invite: Partial<Invite>; firstName: string; userRoles: UserRole[] }) => {
|
|
const inviteRoles = invite.role ?? [];
|
|
if (!invite.email) {
|
|
throw new AppError({
|
|
message: "Invite email is required to send an invite",
|
|
service: SERVICE_NAME,
|
|
method: "sendInviteEmail",
|
|
status: 400,
|
|
});
|
|
}
|
|
|
|
for (const targetRole of inviteRoles) {
|
|
const canManage = userRoles.some((actorRole) => canManageRole(actorRole, targetRole));
|
|
if (!canManage) {
|
|
throw new AppError({
|
|
message: "You do not have permission to create this invite",
|
|
service: SERVICE_NAME,
|
|
method: "sendInviteEmail",
|
|
status: 403,
|
|
});
|
|
}
|
|
}
|
|
|
|
const inviteToken = await this.invitesRepository.create(invite);
|
|
const { clientHost } = this.settingsService.getSettings();
|
|
|
|
const html = await this.emailService.buildEmail("employeeActivationTemplate", {
|
|
name: firstName,
|
|
link: `${clientHost}/register/${inviteToken.token}`,
|
|
});
|
|
|
|
if (!html) {
|
|
throw new AppError({
|
|
message: "Failed to build invite e-mail... Please verify your settings.",
|
|
service: SERVICE_NAME,
|
|
method: "sendInviteEmail",
|
|
status: 500,
|
|
});
|
|
}
|
|
|
|
try {
|
|
await this.emailService.sendEmail(invite.email, "Welcome to Uptime Monitor", html);
|
|
} catch (error: unknown) {
|
|
throw new AppError({
|
|
message: "Failed to send invite e-mail... Please verify your settings.",
|
|
service: SERVICE_NAME,
|
|
method: "sendInviteEmail",
|
|
status: 500,
|
|
details: { cause: error instanceof Error ? error.message : "Unknown error" },
|
|
});
|
|
}
|
|
};
|
|
|
|
verifyInviteToken = async ({ inviteToken }: { inviteToken: string }) => {
|
|
return await this.invitesRepository.findByToken(inviteToken);
|
|
};
|
|
}
|