reorg to domain and api

This commit is contained in:
Alex Holliday committed 2026-06-09 12:34:15 -07:00
1 parent 0fb210e9e1
commit d022f7d849
190 files changed
+643 -830

No files matched your search

+30
View File
@@ -0,0 +1,30 @@
import { Schema, model, type Types } from "mongoose";
import type { Invite } from "@/domain/invites/invite.type.js";
type InviteDocumentBase = Omit<Invite, "id" | "teamId" | "createdAt" | "updatedAt" | "expiry"> & {
teamId: Types.ObjectId;
expiry: Date;
};
interface InviteDocument extends InviteDocumentBase {
_id: Types.ObjectId;
createdAt: Date;
updatedAt: Date;
}
const InviteSchema = new Schema<InviteDocument>(
{
email: { type: String, required: true, unique: true },
teamId: { type: Schema.Types.ObjectId, ref: "Team", immutable: true, required: true },
role: { type: [String], required: true, default: ["user"] },
token: { type: String, required: true },
expiry: { type: Date, default: Date.now, expires: 3600 },
},
{ timestamps: true }
);
const InviteModel = model<InviteDocument>("Invite", InviteSchema);
export type { InviteDocument };
export { InviteModel };
export default InviteModel;
@@ -0,0 +1,13 @@
import type { Invite } from "@/domain/invites/invite.type.js";
export interface IInvitesRepository {
// create
create(invite: Partial<Invite>): Promise<Invite>;
// fetch
findByToken(token: string): Promise<Invite>;
findByTokenAndDelete(token: string): Promise<Invite>;
// update
// delete
// other
}
@@ -0,0 +1,63 @@
import { IInvitesRepository } from "@/domain/invites/invite.repository.interface.js";
import type { Invite } from "@/domain/invites/invite.type.js";
import { type InviteDocument, InviteModel } from "@/domain/invites/invite.model.js";
import { AppError } from "@/utils/AppError.js";
import mongoose from "mongoose";
import crypto from "crypto";
class MongoInvitesRepository implements IInvitesRepository {
private toStringId = (value?: mongoose.Types.ObjectId | string | null): string => {
if (!value) {
return "";
}
return value instanceof mongoose.Types.ObjectId ? value.toString() : String(value);
};
private toDateString = (value?: Date | string | null): string => {
if (!value) {
return new Date(0).toISOString();
}
return value instanceof Date ? value.toISOString() : new Date(value).toISOString();
};
protected toEntity = (doc: InviteDocument): Invite => {
return {
id: this.toStringId(doc._id),
email: doc.email,
teamId: this.toStringId(doc.teamId),
role: doc.role ?? [],
token: doc.token,
expiry: this.toDateString(doc.expiry),
createdAt: this.toDateString(doc.createdAt),
updatedAt: this.toDateString(doc.updatedAt),
};
};
create = async (invite: Partial<Invite>) => {
await InviteModel.deleteMany({ email: invite.email });
invite.token = crypto.randomBytes(32).toString("hex");
const 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) => {
const invite = await InviteModel.findOneAndDelete({
token,
});
if (invite === null) {
throw new AppError({ message: "Invite not found", status: 404 });
}
return this.toEntity(invite);
};
}
export default MongoInvitesRepository;
+117
View File
@@ -0,0 +1,117 @@
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;
}
get serviceName() {
return InviteService.SERVICE_NAME;
}
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,
});
}
const result = await this.emailService.sendEmail(invite.email, "Welcome to Uptime Monitor", html);
if (!result) {
throw new AppError({
message: "Failed to send invite e-mail... Please verify your settings.",
service: SERVICE_NAME,
method: "sendInviteEmail",
status: 500,
});
}
};
verifyInviteToken = async ({ inviteToken }: { inviteToken: string }) => {
return await this.invitesRepository.findByToken(inviteToken);
};
}
+12
View File
@@ -0,0 +1,12 @@
import type { UserRole } from "@/domain/users/user.type.js";
export interface Invite {
id: string;
email: string;
teamId: string;
role: UserRole[];
token: string;
expiry: string;
createdAt: string;
updatedAt: string;
}