refactor notifications service

This commit is contained in:
Alex Holliday committed 2026-01-21 13:51:04 -08:00
1 parent 5b43d9d173
commit d627a25b40
13 files changed
+132 -47

No files matched your search

@@ -2,6 +2,8 @@ import mongoose from "mongoose";
import { NotificationModel, type NotificationDocument } from "@/db/models/index.js";
import { INotificationsRepository } from "@/repositories/index.js";
import type { Notification } from "@/types/index.js";
import { AppError } from "@/utils/AppError.js";
import { not } from "joi";
class MongoNotificationsRepository implements INotificationsRepository {
private mapDocuments = (documents: NotificationDocument[]): Notification[] => {
@@ -36,11 +38,61 @@ class MongoNotificationsRepository implements INotificationsRepository {
};
};
create = async (notificationData: Partial<Notification>) => {
const notification = await NotificationModel.create({ ...notificationData });
if (!notification) {
throw new AppError({ message: "Failed to create notification", status: 500 });
}
return this.toEntity(notification);
};
findById = async (id: string, teamId: string): Promise<Notification> => {
const notification = await NotificationModel.findOne({
_id: new mongoose.Types.ObjectId(id),
teamId: new mongoose.Types.ObjectId(teamId),
});
if (!notification) {
throw new AppError({ message: "Notification not found", status: 404 });
}
return this.toEntity(notification);
};
findNotificationsByIds = async (ids: string[]) => {
const mongoIds = ids.map((id) => new mongoose.Types.ObjectId(id));
const documents = await NotificationModel.find({ _id: { $in: mongoIds } });
return this.mapDocuments(documents);
};
findByTeamId = async (teamId: string): Promise<Notification[]> => {
const documents = await NotificationModel.find({ teamId });
return this.mapDocuments(documents);
};
updateById = async (id: string, teamId: string, patch: Partial<Notification>): Promise<Notification> => {
const notification = await NotificationModel.findOneAndUpdate(
{
_id: new mongoose.Types.ObjectId(id),
teamId: new mongoose.Types.ObjectId(teamId),
},
{ $set: patch },
{ new: true, runValidators: true }
);
if (!notification) {
throw new AppError({ message: "Notification not found or could not be updated", status: 404 });
}
return this.toEntity(notification);
};
deleteById = async (id: string, teamId: string): Promise<Notification> => {
const deleted = await NotificationModel.findOneAndDelete({
_id: new mongoose.Types.ObjectId(id),
teamId: new mongoose.Types.ObjectId(teamId),
});
if (!deleted) {
throw new AppError({ message: "Notification not found or could not be deleted", status: 404 });
}
return this.toEntity(deleted);
};
}
export default MongoNotificationsRepository;