Files
Checkmate/server/src/service/business/inviteService.ts
T

116 lines
3.5 KiB
TypeScript
Raw Normal View History

2026-02-04 17:49:45 +00:00
import type { Invite, UserRole } from "@/types/index.js";
import { canManageRole } from "@/types/user.js";
2026-01-18 05:38:50 +00:00
import type { IInvitesRepository } from "@/repositories/index.js";
2026-01-22 21:49:49 +00:00
import { AppError } from "@/utils/AppError.js";
2026-03-20 11:01:52 -07:00
import { ISettingsService } from "../system/settingsService.js";
import { IEmailService } from "../infrastructure/emailService.js";
2026-01-18 05:38:50 +00:00
const SERVICE_NAME = "inviteService";
2026-03-05 14:46:28 -08:00
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 {
2026-01-18 05:38:50 +00:00
static SERVICE_NAME = SERVICE_NAME;
2026-03-20 11:01:52 -07:00
private settingsService: ISettingsService;
private emailService: IEmailService;
2026-01-18 05:38:50 +00:00
private invitesRepository: IInvitesRepository;
constructor({
invitesRepository,
settingsService,
emailService,
}: {
invitesRepository: IInvitesRepository;
2026-03-20 11:01:52 -07:00
settingsService: ISettingsService;
emailService: IEmailService;
2026-01-18 05:38:50 +00:00
}) {
this.invitesRepository = invitesRepository;
this.settingsService = settingsService;
this.emailService = emailService;
}
get serviceName() {
return InviteService.SERVICE_NAME;
}
2026-02-04 17:49:45 +00:00
getInviteToken = async ({ invite, teamId, userRoles }: { invite: Partial<Invite>; teamId: string; userRoles: UserRole[] }) => {
2026-01-18 05:38:50 +00:00
invite.teamId = teamId;
2026-02-04 17:49:45 +00:00
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,
});
}
}
2026-01-18 05:38:50 +00:00
const inviteToken = await this.invitesRepository.create(invite);
return inviteToken;
};
2026-03-20 11:01:52 -07:00
sendInviteEmail = async ({ invite, firstName, userRoles }: { invite: Partial<Invite>; firstName: string; userRoles: UserRole[] }) => {
2026-02-04 17:57:00 +00:00
const inviteRoles = invite.role ?? [];
2026-03-20 11:01:52 -07:00
if (!invite.email) {
throw new AppError({
message: "Invite email is required to send an invite",
service: SERVICE_NAME,
method: "sendInviteEmail",
status: 400,
});
}
2026-02-04 17:57:00 +00:00
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,
2026-03-20 11:01:52 -07:00
method: "sendInviteEmail",
2026-02-04 17:57:00 +00:00
status: 403,
});
}
}
2026-01-18 05:38:50 +00:00
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}`,
});
2026-03-20 11:01:52 -07:00
if (!html) {
throw new AppError({
message: "Failed to build invite e-mail... Please verify your settings.",
service: SERVICE_NAME,
method: "sendInviteEmail",
status: 500,
});
}
2026-01-18 05:38:50 +00:00
const result = await this.emailService.sendEmail(invite.email, "Welcome to Uptime Monitor", html);
if (!result) {
2026-01-22 21:49:49 +00:00
throw new AppError({
message: "Failed to send invite e-mail... Please verify your settings.",
service: SERVICE_NAME,
method: "sendInviteEmail",
status: 500,
});
2026-01-18 05:38:50 +00:00
}
};
verifyInviteToken = async ({ inviteToken }: { inviteToken: string }) => {
return await this.invitesRepository.findByToken(inviteToken);
};
}