convert to ts
This commit is contained in:
1 parent
288ac08cd6
commit
3e8d4cb49b
4 files changed
+81
-2
No files matched your search
@@ -260,10 +260,9 @@ export const initializeServices = async ({
|
|||||||
|
|
||||||
const diagnosticService = new DiagnosticService();
|
const diagnosticService = new DiagnosticService();
|
||||||
const inviteService = new InviteService({
|
const inviteService = new InviteService({
|
||||||
db,
|
invitesRepository,
|
||||||
settingsService,
|
settingsService,
|
||||||
emailService,
|
emailService,
|
||||||
stringService,
|
|
||||||
errorService,
|
errorService,
|
||||||
});
|
});
|
||||||
const maintenanceWindowService = new MaintenanceWindowService({
|
const maintenanceWindowService = new MaintenanceWindowService({
|
||||||
|
|||||||
@@ -2,7 +2,9 @@ import type { Invite } from "@/types/index.js";
|
|||||||
|
|
||||||
export interface IInvitesRepository {
|
export interface IInvitesRepository {
|
||||||
// create
|
// create
|
||||||
|
create(invite: Partial<Invite>): Promise<Invite>;
|
||||||
// fetch
|
// fetch
|
||||||
|
findByToken(token: string): Promise<Invite>;
|
||||||
findByTokenAndDelete(token: string): Promise<Invite>;
|
findByTokenAndDelete(token: string): Promise<Invite>;
|
||||||
// update
|
// update
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import type { Invite } from "@/types/index.js";
|
|||||||
import { type InviteDocument, InviteModel } from "@/db/models/index.js";
|
import { type InviteDocument, InviteModel } from "@/db/models/index.js";
|
||||||
import { AppError } from "@/utils/AppError.js";
|
import { AppError } from "@/utils/AppError.js";
|
||||||
import mongoose from "mongoose";
|
import mongoose from "mongoose";
|
||||||
|
import crypto from "crypto";
|
||||||
|
|
||||||
class MongoInvitesRepository implements IInvitesRepository {
|
class MongoInvitesRepository implements IInvitesRepository {
|
||||||
private toStringId = (value?: mongoose.Types.ObjectId | string | null): string => {
|
private toStringId = (value?: mongoose.Types.ObjectId | string | null): string => {
|
||||||
@@ -32,6 +33,23 @@ class MongoInvitesRepository implements IInvitesRepository {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
create = async (invite: Partial<Invite>) => {
|
||||||
|
await InviteModel.deleteMany({ email: invite.email });
|
||||||
|
invite.token = crypto.randomBytes(32).toString("hex");
|
||||||
|
let inviteToken = await InviteModel.create(invite);
|
||||||
|
return this.toEntity(inviteToken);
|
||||||
|
};
|
||||||
|
|
||||||
|
findByToken = async (token: string) => {
|
||||||
|
const invite = await InviteModel.findOne({
|
||||||
|
token,
|
||||||
|
});
|
||||||
|
if (invite === null) {
|
||||||
|
throw new AppError({ message: "Invite not found", status: 404 });
|
||||||
|
}
|
||||||
|
return this.toEntity(invite);
|
||||||
|
};
|
||||||
|
|
||||||
findByTokenAndDelete = async (token: string) => {
|
findByTokenAndDelete = async (token: string) => {
|
||||||
const invite = await InviteModel.findOneAndDelete({
|
const invite = await InviteModel.findOneAndDelete({
|
||||||
token,
|
token,
|
||||||
|
|||||||
@@ -0,0 +1,60 @@
|
|||||||
|
import type { Invite } from "@/types/index.js";
|
||||||
|
import type { IInvitesRepository } from "@/repositories/index.js";
|
||||||
|
|
||||||
|
const SERVICE_NAME = "inviteService";
|
||||||
|
|
||||||
|
class InviteService {
|
||||||
|
static SERVICE_NAME = SERVICE_NAME;
|
||||||
|
|
||||||
|
private settingsService: any;
|
||||||
|
private emailService: any;
|
||||||
|
private errorService: any;
|
||||||
|
private invitesRepository: IInvitesRepository;
|
||||||
|
|
||||||
|
constructor({
|
||||||
|
invitesRepository,
|
||||||
|
settingsService,
|
||||||
|
emailService,
|
||||||
|
errorService,
|
||||||
|
}: {
|
||||||
|
invitesRepository: IInvitesRepository;
|
||||||
|
settingsService: any;
|
||||||
|
emailService: any;
|
||||||
|
errorService: any;
|
||||||
|
}) {
|
||||||
|
this.invitesRepository = invitesRepository;
|
||||||
|
this.settingsService = settingsService;
|
||||||
|
this.emailService = emailService;
|
||||||
|
this.errorService = errorService;
|
||||||
|
}
|
||||||
|
|
||||||
|
get serviceName() {
|
||||||
|
return InviteService.SERVICE_NAME;
|
||||||
|
}
|
||||||
|
|
||||||
|
getInviteToken = async ({ invite, teamId }: { invite: Partial<Invite>; teamId: string }) => {
|
||||||
|
invite.teamId = teamId;
|
||||||
|
const inviteToken = await this.invitesRepository.create(invite);
|
||||||
|
return inviteToken;
|
||||||
|
};
|
||||||
|
|
||||||
|
sendInviteEmail = async ({ invite, firstName }: { invite: Partial<Invite>; firstName: any }) => {
|
||||||
|
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}`,
|
||||||
|
});
|
||||||
|
const result = await this.emailService.sendEmail(invite.email, "Welcome to Uptime Monitor", html);
|
||||||
|
if (!result) {
|
||||||
|
throw this.errorService.createServerError("Failed to send invite e-mail... Please verify your settings.");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
verifyInviteToken = async ({ inviteToken }: { inviteToken: string }) => {
|
||||||
|
return await this.invitesRepository.findByToken(inviteToken);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export default InviteService;
|
||||||
Reference in new issue
Block a user