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 { canManageRole } from "@/types/user.js";
|
||||||
import type { IInvitesRepository } from "@/repositories/index.js";
|
import type { IInvitesRepository } from "@/repositories/index.js";
|
||||||
import { AppError } from "@/utils/AppError.js";
|
import { AppError } from "@/utils/AppError.js";
|
||||||
|
import { ISettingsService } from "../system/settingsService.js";
|
||||||
|
import { IEmailService } from "../infrastructure/emailService.js";
|
||||||
|
|
||||||
const SERVICE_NAME = "inviteService";
|
const SERVICE_NAME = "inviteService";
|
||||||
|
|
||||||
@@ -14,8 +16,8 @@ export interface IInviteService {
|
|||||||
export class InviteService implements IInviteService {
|
export class InviteService implements IInviteService {
|
||||||
static SERVICE_NAME = SERVICE_NAME;
|
static SERVICE_NAME = SERVICE_NAME;
|
||||||
|
|
||||||
private settingsService: any;
|
private settingsService: ISettingsService;
|
||||||
private emailService: any;
|
private emailService: IEmailService;
|
||||||
private invitesRepository: IInvitesRepository;
|
private invitesRepository: IInvitesRepository;
|
||||||
|
|
||||||
constructor({
|
constructor({
|
||||||
@@ -24,8 +26,8 @@ export class InviteService implements IInviteService {
|
|||||||
emailService,
|
emailService,
|
||||||
}: {
|
}: {
|
||||||
invitesRepository: IInvitesRepository;
|
invitesRepository: IInvitesRepository;
|
||||||
settingsService: any;
|
settingsService: ISettingsService;
|
||||||
emailService: any;
|
emailService: IEmailService;
|
||||||
}) {
|
}) {
|
||||||
this.invitesRepository = invitesRepository;
|
this.invitesRepository = invitesRepository;
|
||||||
this.settingsService = settingsService;
|
this.settingsService = settingsService;
|
||||||
@@ -57,8 +59,16 @@ export class InviteService implements IInviteService {
|
|||||||
return inviteToken;
|
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 ?? [];
|
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) {
|
for (const targetRole of inviteRoles) {
|
||||||
const canManage = userRoles.some((actorRole) => canManageRole(actorRole, targetRole));
|
const canManage = userRoles.some((actorRole) => canManageRole(actorRole, targetRole));
|
||||||
@@ -66,7 +76,7 @@ export class InviteService implements IInviteService {
|
|||||||
throw new AppError({
|
throw new AppError({
|
||||||
message: "You do not have permission to create this invite",
|
message: "You do not have permission to create this invite",
|
||||||
service: SERVICE_NAME,
|
service: SERVICE_NAME,
|
||||||
method: "getInviteToken",
|
method: "sendInviteEmail",
|
||||||
status: 403,
|
status: 403,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -79,6 +89,16 @@ export class InviteService implements IInviteService {
|
|||||||
name: firstName,
|
name: firstName,
|
||||||
link: `${clientHost}/register/${inviteToken.token}`,
|
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);
|
const result = await this.emailService.sendEmail(invite.email, "Welcome to Uptime Monitor", html);
|
||||||
if (!result) {
|
if (!result) {
|
||||||
throw new AppError({
|
throw new AppError({
|
||||||
|
|||||||
Reference in new issue
Block a user