fix errors in inviteService
This commit is contained in:
1 parent
2fb7516ca1
commit
533a8911cd
1 file changed
+26
-6
@@ -2,6 +2,8 @@ import type { Invite, UserRole } from "@/types/index.js";
|
||||
import { canManageRole } from "@/types/user.js";
|
||||
import type { IInvitesRepository } from "@/repositories/index.js";
|
||||
import { AppError } from "@/utils/AppError.js";
|
||||
import { ISettingsService } from "../system/settingsService.js";
|
||||
import { IEmailService } from "../infrastructure/emailService.js";
|
||||
|
||||
const SERVICE_NAME = "inviteService";
|
||||
|
||||
@@ -14,8 +16,8 @@ export interface IInviteService {
|
||||
export class InviteService implements IInviteService {
|
||||
static SERVICE_NAME = SERVICE_NAME;
|
||||
|
||||
private settingsService: any;
|
||||
private emailService: any;
|
||||
private settingsService: ISettingsService;
|
||||
private emailService: IEmailService;
|
||||
private invitesRepository: IInvitesRepository;
|
||||
|
||||
constructor({
|
||||
@@ -24,8 +26,8 @@ export class InviteService implements IInviteService {
|
||||
emailService,
|
||||
}: {
|
||||
invitesRepository: IInvitesRepository;
|
||||
settingsService: any;
|
||||
emailService: any;
|
||||
settingsService: ISettingsService;
|
||||
emailService: IEmailService;
|
||||
}) {
|
||||
this.invitesRepository = invitesRepository;
|
||||
this.settingsService = settingsService;
|
||||
@@ -57,8 +59,16 @@ export class InviteService implements IInviteService {
|
||||
return inviteToken;
|
||||
};
|
||||
|
||||
sendInviteEmail = async ({ invite, firstName, userRoles }: { invite: Partial<Invite>; firstName: any; userRoles: UserRole[] }) => {
|
||||
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));
|
||||
@@ -66,7 +76,7 @@ export class InviteService implements IInviteService {
|
||||
throw new AppError({
|
||||
message: "You do not have permission to create this invite",
|
||||
service: SERVICE_NAME,
|
||||
method: "getInviteToken",
|
||||
method: "sendInviteEmail",
|
||||
status: 403,
|
||||
});
|
||||
}
|
||||
@@ -79,6 +89,16 @@ export class InviteService implements IInviteService {
|
||||
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,
|
||||
});
|
||||
}
|
||||
|
||||
const result = await this.emailService.sendEmail(invite.email, "Welcome to Uptime Monitor", html);
|
||||
if (!result) {
|
||||
throw new AppError({
|
||||
|
||||
Reference in new issue
Block a user