refactor to use repository

This commit is contained in:
Alex Holliday committed 2026-01-08 17:47:26 +00:00
1 parent c42932a7b3
commit 58a77d6aa2
11 files changed
+120 -44

No files matched your search

+4 -2
View File
@@ -46,7 +46,9 @@ class RecoveryController implements IRecoveryController {
if (user) {
try {
const recoveryToken = await this.recoveryService.create(user._id);
const recoveryToken = await this.recoveryService.create(
user._id.toString()
);
// Don't wait for email to be sent
this.emailService
.sendGeneric(user.email, "Password Recovery", {
@@ -77,7 +79,7 @@ class RecoveryController implements IRecoveryController {
recoveryToken.userId.toString(),
password
);
await this.recoveryService.delete(recoveryToken._id);
await this.recoveryService.delete(recoveryToken.id);
const user = await User.findById(recoveryToken.userId);
if (!user) {
+6 -3
View File
@@ -47,6 +47,7 @@ import {
MongoOrgMembershipRepository,
MongoMaintenanceRepository,
MongoNotificationChannelRepository,
MongoRecoveryTokenRepository,
} from "@/repositories/index.js";
export const initServices = async () => {
@@ -62,7 +63,9 @@ export const initServices = async () => {
const orgRepository = new MongoOrgRepository();
const orgMembershipRepository = new MongoOrgMembershipRepository();
const maintenanceRepository = new MongoMaintenanceRepository();
const notificationChannelRepository = new MongoNotificationChannelRepository();
const notificationChannelRepository =
new MongoNotificationChannelRepository();
const recoveryTokenRepository = new MongoRecoveryTokenRepository();
const checkService = new CheckService(checksRepository, monitorRepository);
const inviteService = new InviteService(
@@ -147,11 +150,11 @@ export const initServices = async () => {
);
const queueService = new QueueService(jobQueue);
const teamService = new TeamService(jobQueue, monitorRepository);
const roleService = new RoleService();
const roleService = new RoleService(roleRepository);
const teamMemberService = new TeamMemberService();
const statusPageService = new StatusPageService();
const diagnosticService = new DiagnosticService(jobQueue);
const recoveryService = new RecoveryService();
const recoveryService = new RecoveryService(recoveryTokenRepository);
const services = {
checkService,
inviteService,
@@ -1 +1,12 @@
export interface IRecoveryTokenRepistory {}
import type { RecoveryToken } from "@/types/domain/index.js";
export interface IRecoveryTokenRepistory {
// create
create(userId: string, tokenHash: string): Promise<boolean>;
// single fetch
findByTokenHash(tokenHash: string): Promise<RecoveryToken | null>;
// collection fetch
// update
// delete
deleteByid(tokenId: string): Promise<boolean>;
}
@@ -1,5 +1,38 @@
import { type IRecoveryToken, RecoveryToken } from "@/db/models/index.js";
import type { RecoveryToken as RecoveryTokenEntity } from "@/types/domain/index.js";
import { IRecoveryTokenRepistory } from "@/repositories/index.js";
class MongoRecoveryTokenRepository implements IRecoveryTokenRepistory {}
class MongoRecoveryTokenRepository implements IRecoveryTokenRepistory {
private toEntity = (doc: IRecoveryToken): RecoveryTokenEntity => {
return {
id: doc._id.toString(),
userId: doc.userId.toString(),
tokenHash: doc.tokenHash,
expiry: doc.expiry,
createdAt: doc.createdAt,
updatedAt: doc.updatedAt,
};
};
create = async (userId: string, tokenHash: string) => {
const recoveryToken = await RecoveryToken.create({
userId,
tokenHash,
});
if (!recoveryToken) return false;
return true;
};
findByTokenHash = async (tokenHash: string) => {
const recoveryToken = await RecoveryToken.findOne({ tokenHash });
if (!recoveryToken) return null;
return this.toEntity(recoveryToken);
};
deleteByid = async (tokenId: string) => {
const result = await RecoveryToken.deleteOne({ _id: tokenId });
return result.deletedCount === 1;
};
}
export default MongoRecoveryTokenRepository;
+22 -13
View File
@@ -1,36 +1,45 @@
import crypto from "node:crypto";
import { RecoveryToken, IRecoveryToken } from "@/db/models/index.js";
import ApiError from "@/utils/ApiError.js";
import { Types } from "mongoose";
import { IRecoveryTokenRepistory } from "@/repositories/index.js";
import type { RecoveryToken } from "@/types/domain/index.js";
const SERVICE_NAME = "RecoveryService";
export interface IRecoveryService {
create: (userId: Types.ObjectId) => Promise<string>;
get: (token: string) => Promise<IRecoveryToken>;
create: (userId: string) => Promise<string>;
get: (token: string) => Promise<RecoveryToken>;
delete: (id: string) => Promise<boolean>;
}
class RecoveryService implements IRecoveryService {
public SERVICE_NAME: string;
constructor() {
private recoveryTokenRepository: IRecoveryTokenRepistory;
constructor(recoveryTokenRepository: IRecoveryTokenRepistory) {
this.SERVICE_NAME = SERVICE_NAME;
this.recoveryTokenRepository = recoveryTokenRepository;
}
create = async (userId: Types.ObjectId) => {
create = async (userId: string) => {
const token = crypto.randomBytes(32).toString("hex");
const tokenHash = crypto.createHash("sha256").update(token).digest("hex");
await RecoveryToken.create({
const didCreate = await this.recoveryTokenRepository.create(
userId,
tokenHash,
});
tokenHash
);
if (!didCreate) {
throw new ApiError("Failed to create recovery token", 500);
}
return token;
};
get = async (token: string) => {
const tokenHash = crypto.createHash("sha256").update(token).digest("hex");
const recoveryToken = await RecoveryToken.findOne({ tokenHash });
const recoveryToken = await this.recoveryTokenRepository.findByTokenHash(
tokenHash
);
if (!recoveryToken) {
throw new ApiError("Recovery token not found", 404);
}
@@ -43,11 +52,11 @@ class RecoveryService implements IRecoveryService {
};
delete = async (id: string) => {
const result = await RecoveryToken.deleteOne({ _id: id });
if (!result.deletedCount) {
const didDelete = await this.recoveryTokenRepository.deleteByid(id);
if (!didDelete) {
throw new ApiError("Recovery token not found", 404);
}
return result.deletedCount === 1;
return didDelete;
};
}
+4 -2
View File
@@ -1,5 +1,5 @@
import { Role, IRole } from "@/db/models/index.js";
import type { IJobQueue } from "../infrastructure/JobQueue.js";
import type { IRoleRepository } from "@/repositories/index.js";
import ApiError from "@/utils/ApiError.js";
const SERVICE_NAME = "RoleService";
@@ -11,9 +11,11 @@ export interface IRoleService {
class RoleService implements IRoleService {
public SERVICE_NAME: string;
private roleRepository: IRoleRepository;
constructor() {
constructor(roleRepository: IRoleRepository) {
this.SERVICE_NAME = SERVICE_NAME;
this.roleRepository = roleRepository;
}
getAll = async (orgId: string, type: string) => {