create tags repository, update related repositories
This commit is contained in:
1 parent
a740a9e005
commit
b1871b31a5
5 files changed
+112
-1
No files matched your search
@@ -25,6 +25,9 @@ export { default as MongoSettingsRepository } from "@/repositories/settings/Mong
|
|||||||
export * from "@/repositories/notifications/INotificationsRepository.js";
|
export * from "@/repositories/notifications/INotificationsRepository.js";
|
||||||
export { default as MongoNotificationsRepository } from "@/repositories/notifications/MongoNotificationsRepository.js";
|
export { default as MongoNotificationsRepository } from "@/repositories/notifications/MongoNotificationsRepository.js";
|
||||||
|
|
||||||
|
export * from "@/repositories/tags/ITagsRepository.js";
|
||||||
|
export { default as MongoTagsRepository } from "@/repositories/tags/MongoTagsRepository.js";
|
||||||
|
|
||||||
export * from "@/repositories/incidents/IIncidentsRepository.js";
|
export * from "@/repositories/incidents/IIncidentsRepository.js";
|
||||||
export { default as MongoIncidentsRepository } from "@/repositories/incidents/MongoIncidentsRepository.js";
|
export { default as MongoIncidentsRepository } from "@/repositories/incidents/MongoIncidentsRepository.js";
|
||||||
|
|
||||||
|
|||||||
@@ -51,6 +51,7 @@ export interface IMonitorsRepository {
|
|||||||
findMonitorsSummaryByTeamId(teamId: string, config?: SummaryConfig): Promise<MonitorsSummary>;
|
findMonitorsSummaryByTeamId(teamId: string, config?: SummaryConfig): Promise<MonitorsSummary>;
|
||||||
findGroupsByTeamId(teamId: string): Promise<string[]>;
|
findGroupsByTeamId(teamId: string): Promise<string[]>;
|
||||||
removeNotificationFromMonitors(notificationId: string): Promise<void>;
|
removeNotificationFromMonitors(notificationId: string): Promise<void>;
|
||||||
|
removeTagFromMonitors(tagId: string): Promise<void>;
|
||||||
updateNotifications(teamId: string, monitorIds: string[], notificationIds: string[], action: "add" | "remove" | "set"): Promise<number>;
|
updateNotifications(teamId: string, monitorIds: string[], notificationIds: string[], action: "add" | "remove" | "set"): Promise<number>;
|
||||||
deleteByTeamIdsNotIn(teamIds: string[]): Promise<number>;
|
deleteByTeamIdsNotIn(teamIds: string[]): Promise<number>;
|
||||||
findAllMonitorIds(): Promise<string[]>;
|
findAllMonitorIds(): Promise<string[]>;
|
||||||
|
|||||||
@@ -351,6 +351,10 @@ class MongoMonitorsRepository implements IMonitorsRepository {
|
|||||||
await MonitorModel.updateMany({ notifications: notificationId }, { $pull: { notifications: notificationId } });
|
await MonitorModel.updateMany({ notifications: notificationId }, { $pull: { notifications: notificationId } });
|
||||||
};
|
};
|
||||||
|
|
||||||
|
removeTagFromMonitors = async (tagId: string): Promise<void> => {
|
||||||
|
await MonitorModel.updateMany({ tags: tagId }, { $pull: { tags: tagId } });
|
||||||
|
};
|
||||||
|
|
||||||
updateNotifications = async (
|
updateNotifications = async (
|
||||||
teamId: string,
|
teamId: string,
|
||||||
monitorIds: string[],
|
monitorIds: string[],
|
||||||
@@ -406,7 +410,7 @@ class MongoMonitorsRepository implements IMonitorsRepository {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const notificationIds = (doc.notifications ?? []).map((notification) => toStringId(notification));
|
const notificationIds = (doc.notifications ?? []).map((notification) => toStringId(notification));
|
||||||
|
const tagIds = (doc.tags ?? []).map((tag) => toStringId(tag));
|
||||||
return {
|
return {
|
||||||
id: toStringId(doc._id),
|
id: toStringId(doc._id),
|
||||||
userId: toStringId(doc.userId),
|
userId: toStringId(doc.userId),
|
||||||
@@ -429,6 +433,7 @@ class MongoMonitorsRepository implements IMonitorsRepository {
|
|||||||
interval: doc.interval,
|
interval: doc.interval,
|
||||||
uptimePercentage: doc.uptimePercentage ?? undefined,
|
uptimePercentage: doc.uptimePercentage ?? undefined,
|
||||||
notifications: notificationIds,
|
notifications: notificationIds,
|
||||||
|
tags: tagIds,
|
||||||
secret: doc.secret ?? undefined,
|
secret: doc.secret ?? undefined,
|
||||||
cpuAlertThreshold: doc.cpuAlertThreshold,
|
cpuAlertThreshold: doc.cpuAlertThreshold,
|
||||||
cpuAlertCounter: doc.cpuAlertCounter,
|
cpuAlertCounter: doc.cpuAlertCounter,
|
||||||
@@ -468,6 +473,7 @@ class MongoMonitorsRepository implements IMonitorsRepository {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const notificationIds = (doc.notifications ?? []).map((notification: unknown) => toStringId(notification));
|
const notificationIds = (doc.notifications ?? []).map((notification: unknown) => toStringId(notification));
|
||||||
|
const tagIds = (doc.tags ?? []).map((tag: unknown) => toStringId(tag));
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: toStringId(doc._id),
|
id: toStringId(doc._id),
|
||||||
@@ -491,6 +497,7 @@ class MongoMonitorsRepository implements IMonitorsRepository {
|
|||||||
interval: doc.interval,
|
interval: doc.interval,
|
||||||
uptimePercentage: doc.uptimePercentage ?? undefined,
|
uptimePercentage: doc.uptimePercentage ?? undefined,
|
||||||
notifications: notificationIds,
|
notifications: notificationIds,
|
||||||
|
tags: tagIds,
|
||||||
secret: doc.secret ?? undefined,
|
secret: doc.secret ?? undefined,
|
||||||
cpuAlertThreshold: doc.cpuAlertThreshold,
|
cpuAlertThreshold: doc.cpuAlertThreshold,
|
||||||
cpuAlertCounter: doc.cpuAlertCounter,
|
cpuAlertCounter: doc.cpuAlertCounter,
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
import { Tag } from "@/types/index.js";
|
||||||
|
|
||||||
|
export interface ITagsRepository {
|
||||||
|
// create
|
||||||
|
create(tagData: Partial<Tag>): Promise<Tag>;
|
||||||
|
// read
|
||||||
|
findById(tagId: string, teamId: string): Promise<Tag>;
|
||||||
|
findByTeamId(teamId: string): Promise<Tag[]>;
|
||||||
|
// update
|
||||||
|
updateById(tagId: string, teamId: string, patch: Partial<Tag>): Promise<Tag>;
|
||||||
|
// delete
|
||||||
|
deleteById(tagId: string, teamId: string): Promise<Tag>;
|
||||||
|
}
|
||||||
@@ -0,0 +1,87 @@
|
|||||||
|
import { ITagsRepository } from "@/repositories/tags/ITagsRepository.js";
|
||||||
|
import { Tag } from "@/types/tag.js";
|
||||||
|
import { TagDocument, TagModel } from "@/db/models/index.js";
|
||||||
|
import mongoose from "mongoose";
|
||||||
|
import { AppError } from "@/utils/AppError.js";
|
||||||
|
|
||||||
|
const SERVICE_NAME = "TagsRepository";
|
||||||
|
|
||||||
|
class MongoTagsRepository implements ITagsRepository {
|
||||||
|
static SERVICE_NAME = SERVICE_NAME;
|
||||||
|
|
||||||
|
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: TagDocument): Tag => {
|
||||||
|
return {
|
||||||
|
id: this.toStringId(doc._id),
|
||||||
|
teamId: this.toStringId(doc.teamId),
|
||||||
|
name: doc.name,
|
||||||
|
color: doc.color,
|
||||||
|
createdAt: this.toDateString(doc.createdAt),
|
||||||
|
updatedAt: this.toDateString(doc.updatedAt),
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
async create(tagData: Partial<Tag>): Promise<Tag> {
|
||||||
|
try {
|
||||||
|
const tag = await TagModel.create({ ...tagData });
|
||||||
|
return this.toEntity(tag);
|
||||||
|
} catch (error) {
|
||||||
|
if (error && typeof error === "object" && (error as { code?: number }).code === 11000) {
|
||||||
|
throw new AppError({ message: `A tag named "${tagData.name}" already exists`, service: SERVICE_NAME, status: 409 });
|
||||||
|
}
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async findById(tagId: string, teamId: string): Promise<Tag> {
|
||||||
|
const tag = await TagModel.findOne({ _id: tagId, teamId });
|
||||||
|
if (!tag) {
|
||||||
|
throw new AppError({ message: "Tag not found", service: SERVICE_NAME, status: 404 });
|
||||||
|
}
|
||||||
|
return this.toEntity(tag);
|
||||||
|
}
|
||||||
|
|
||||||
|
async findByTeamId(teamId: string): Promise<Tag[]> {
|
||||||
|
const tags = await TagModel.find({ teamId });
|
||||||
|
return tags.map(this.toEntity);
|
||||||
|
}
|
||||||
|
|
||||||
|
async updateById(tagId: string, teamId: string, patch: Partial<Tag>): Promise<Tag> {
|
||||||
|
const updatedTag = await TagModel.findOneAndUpdate(
|
||||||
|
{ _id: tagId, teamId },
|
||||||
|
{
|
||||||
|
$set: {
|
||||||
|
...patch,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{ new: true, runValidators: true }
|
||||||
|
);
|
||||||
|
if (!updatedTag) {
|
||||||
|
throw new AppError({ message: "Tag not found or could not be updated", service: SERVICE_NAME, status: 404 });
|
||||||
|
}
|
||||||
|
return this.toEntity(updatedTag);
|
||||||
|
}
|
||||||
|
|
||||||
|
async deleteById(tagId: string, teamId: string): Promise<Tag> {
|
||||||
|
const deletedTag = await TagModel.findOneAndDelete({ _id: tagId, teamId });
|
||||||
|
if (!deletedTag) {
|
||||||
|
throw new AppError({ message: "Tag not found or could not be deleted", service: SERVICE_NAME, status: 404 });
|
||||||
|
}
|
||||||
|
return this.toEntity(deletedTag);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default MongoTagsRepository;
|
||||||
Reference in new issue
Block a user