Merge pull request #3784 from bluewave-labs/chore/refactor-utils
chore: refactor utils
This commit is contained in:
16 files changed
+89
-268
No files matched your search
@@ -1,26 +1,13 @@
|
|||||||
import mongoose, { type UpdateQuery } from "mongoose";
|
import { type UpdateQuery } from "mongoose";
|
||||||
import { ISettingsRepository } from "@/domain/app-settings/app-settings-repository.interface.js";
|
import { ISettingsRepository } from "@/domain/app-settings/app-settings-repository.interface.js";
|
||||||
import type { Settings, SettingsUpdate } from "@/domain/app-settings/app-settings.type.js";
|
import type { Settings, SettingsUpdate } from "@/domain/app-settings/app-settings.type.js";
|
||||||
import { AppSettingsModel, type AppSettingsDocument } from "@/domain/app-settings/app-settings.model.js";
|
import { AppSettingsModel, type AppSettingsDocument } from "@/domain/app-settings/app-settings.model.js";
|
||||||
|
import { toStringId, toDateString } from "@/utils/mongoMappers.js";
|
||||||
|
|
||||||
class MongoSettingsRepository implements ISettingsRepository {
|
class MongoSettingsRepository implements ISettingsRepository {
|
||||||
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: AppSettingsDocument): Settings => {
|
protected toEntity = (doc: AppSettingsDocument): Settings => {
|
||||||
return {
|
return {
|
||||||
id: this.toStringId(doc._id),
|
id: toStringId(doc._id),
|
||||||
checkTTL: doc.checkTTL,
|
checkTTL: doc.checkTTL,
|
||||||
language: doc.language,
|
language: doc.language,
|
||||||
jwtSecret: doc.jwtSecret ?? undefined,
|
jwtSecret: doc.jwtSecret ?? undefined,
|
||||||
@@ -41,8 +28,8 @@ class MongoSettingsRepository implements ISettingsRepository {
|
|||||||
singleton: doc.singleton,
|
singleton: doc.singleton,
|
||||||
version: doc.version ?? 1,
|
version: doc.version ?? 1,
|
||||||
globalThresholds: doc.globalThresholds ?? undefined,
|
globalThresholds: doc.globalThresholds ?? undefined,
|
||||||
createdAt: this.toDateString(doc.createdAt),
|
createdAt: toDateString(doc.createdAt),
|
||||||
updatedAt: this.toDateString(doc.updatedAt),
|
updatedAt: toDateString(doc.updatedAt),
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ import { CheckModel, type CheckDocument } from "@/domain/checks/check.model.js";
|
|||||||
import mongoose from "mongoose";
|
import mongoose from "mongoose";
|
||||||
import { getDateForRange } from "@/utils/dataUtils.js";
|
import { getDateForRange } from "@/utils/dataUtils.js";
|
||||||
import { ILogger } from "@/utils/logger.js";
|
import { ILogger } from "@/utils/logger.js";
|
||||||
|
import { toStringId, toDateString } from "@/utils/mongoMappers.js";
|
||||||
|
|
||||||
import { getHardwareUpChecks, getHardwareStats, getHardwareTotalChecks } from "@/domain/checks/check.hardware.aggregations.js";
|
import { getHardwareUpChecks, getHardwareStats, getHardwareTotalChecks } from "@/domain/checks/check.hardware.aggregations.js";
|
||||||
|
|
||||||
@@ -33,20 +34,6 @@ class MongoChecksRepository implements IChecksRepository {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private toEntity = (doc: CheckDocument): Check => {
|
private toEntity = (doc: CheckDocument): Check => {
|
||||||
const toStringId = (value: mongoose.Types.ObjectId | string | undefined | null): string => {
|
|
||||||
if (!value) {
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
return value instanceof mongoose.Types.ObjectId ? value.toString() : String(value);
|
|
||||||
};
|
|
||||||
|
|
||||||
const toDateString = (value?: Date | string | null): string => {
|
|
||||||
if (!value) {
|
|
||||||
return new Date(0).toISOString();
|
|
||||||
}
|
|
||||||
return value instanceof Date ? value.toISOString() : new Date(value).toISOString();
|
|
||||||
};
|
|
||||||
|
|
||||||
const mapTimings = (timings?: GotTimings): GotTimings => {
|
const mapTimings = (timings?: GotTimings): GotTimings => {
|
||||||
const phases = timings?.phases ?? {
|
const phases = timings?.phases ?? {
|
||||||
wait: 0,
|
wait: 0,
|
||||||
|
|||||||
@@ -4,36 +4,22 @@ import type { DLQItem } from "@/domain/dlq/dlq.type.js";
|
|||||||
import type { IDLQRepository, DLQQueryFilters, DLQStatusCount } from "@/domain/dlq/dlq.repository.interface.js";
|
import type { IDLQRepository, DLQQueryFilters, DLQStatusCount } from "@/domain/dlq/dlq.repository.interface.js";
|
||||||
import mongoose from "mongoose";
|
import mongoose from "mongoose";
|
||||||
import { AppError } from "@/utils/AppError.js";
|
import { AppError } from "@/utils/AppError.js";
|
||||||
|
import { toStringId, toDateString } from "@/utils/mongoMappers.js";
|
||||||
class MongoDLQRepository implements IDLQRepository {
|
class MongoDLQRepository implements IDLQRepository {
|
||||||
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: DLQItemDocument): DLQItem => {
|
protected toEntity = (doc: DLQItemDocument): DLQItem => {
|
||||||
return {
|
return {
|
||||||
id: this.toStringId(doc._id),
|
id: toStringId(doc._id),
|
||||||
type: doc.type,
|
type: doc.type,
|
||||||
status: doc.status,
|
status: doc.status,
|
||||||
payload: doc.payload,
|
payload: doc.payload,
|
||||||
monitorId: this.toStringId(doc.monitorId),
|
monitorId: toStringId(doc.monitorId),
|
||||||
teamId: this.toStringId(doc.teamId),
|
teamId: toStringId(doc.teamId),
|
||||||
retryCount: doc.retryCount,
|
retryCount: doc.retryCount,
|
||||||
maxRetries: doc.maxRetries,
|
maxRetries: doc.maxRetries,
|
||||||
lastError: doc.lastError,
|
lastError: doc.lastError,
|
||||||
nextRetryAt: this.toDateString(doc.nextRetryAt),
|
nextRetryAt: toDateString(doc.nextRetryAt),
|
||||||
createdAt: this.toDateString(doc.createdAt),
|
createdAt: toDateString(doc.createdAt),
|
||||||
updatedAt: this.toDateString(doc.updatedAt),
|
updatedAt: toDateString(doc.updatedAt),
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import { GeoCheckMetadataDocument, GeoCheckModel, type GeoCheckDocument } from "
|
|||||||
import mongoose, { PipelineStage } from "mongoose";
|
import mongoose, { PipelineStage } from "mongoose";
|
||||||
import { getDateForRange } from "@/utils/dataUtils.js";
|
import { getDateForRange } from "@/utils/dataUtils.js";
|
||||||
import { ILogger } from "@/utils/logger.js";
|
import { ILogger } from "@/utils/logger.js";
|
||||||
|
import { toStringId, toDateString } from "@/utils/mongoMappers.js";
|
||||||
|
|
||||||
const SERVICE_NAME = "GeoChecksRepository";
|
const SERVICE_NAME = "GeoChecksRepository";
|
||||||
|
|
||||||
@@ -17,20 +18,6 @@ class MongoGeoChecksRepository implements IGeoChecksRepository {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private toEntity = (doc: GeoCheckDocument): GeoCheck => {
|
private toEntity = (doc: GeoCheckDocument): GeoCheck => {
|
||||||
const toStringId = (value: mongoose.Types.ObjectId | string | undefined | null): string => {
|
|
||||||
if (!value) {
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
return value instanceof mongoose.Types.ObjectId ? value.toString() : String(value);
|
|
||||||
};
|
|
||||||
|
|
||||||
const toDateString = (value?: Date | string | null): string => {
|
|
||||||
if (!value) {
|
|
||||||
return new Date(0).toISOString();
|
|
||||||
}
|
|
||||||
return value instanceof Date ? value.toISOString() : new Date(value).toISOString();
|
|
||||||
};
|
|
||||||
|
|
||||||
const mapMetadata = (metadata: GeoCheckMetadataDocument): GeoCheckMetadata => ({
|
const mapMetadata = (metadata: GeoCheckMetadataDocument): GeoCheckMetadata => ({
|
||||||
monitorId: toStringId(metadata.monitorId),
|
monitorId: toStringId(metadata.monitorId),
|
||||||
teamId: toStringId(metadata.teamId),
|
teamId: toStringId(metadata.teamId),
|
||||||
|
|||||||
@@ -4,22 +4,9 @@ import type { Incident, IncidentSummary } from "@/domain/incidents/incident.type
|
|||||||
import type { IIncidentsRepository } from "@/domain/incidents/incident.repository.interface.js";
|
import type { IIncidentsRepository } from "@/domain/incidents/incident.repository.interface.js";
|
||||||
import mongoose from "mongoose";
|
import mongoose from "mongoose";
|
||||||
import { AppError } from "@/utils/AppError.js";
|
import { AppError } from "@/utils/AppError.js";
|
||||||
|
import { toStringId, toDateString } from "@/utils/mongoMappers.js";
|
||||||
|
|
||||||
class MongoIncidentsRepository implements IIncidentsRepository {
|
class MongoIncidentsRepository implements IIncidentsRepository {
|
||||||
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();
|
|
||||||
};
|
|
||||||
|
|
||||||
private buildMatchStage({
|
private buildMatchStage({
|
||||||
teamId,
|
teamId,
|
||||||
startDate,
|
startDate,
|
||||||
@@ -48,20 +35,20 @@ class MongoIncidentsRepository implements IIncidentsRepository {
|
|||||||
|
|
||||||
protected toEntity = (doc: IncidentDocument): Incident => {
|
protected toEntity = (doc: IncidentDocument): Incident => {
|
||||||
return {
|
return {
|
||||||
id: this.toStringId(doc._id),
|
id: toStringId(doc._id),
|
||||||
monitorId: this.toStringId(doc.monitorId),
|
monitorId: toStringId(doc.monitorId),
|
||||||
teamId: this.toStringId(doc.teamId),
|
teamId: toStringId(doc.teamId),
|
||||||
startTime: this.toDateString(doc.startTime),
|
startTime: toDateString(doc.startTime),
|
||||||
endTime: doc.endTime ? this.toDateString(doc.endTime) : null,
|
endTime: doc.endTime ? toDateString(doc.endTime) : null,
|
||||||
status: doc.status,
|
status: doc.status,
|
||||||
message: doc.message ?? null,
|
message: doc.message ?? null,
|
||||||
statusCode: doc.statusCode ?? null,
|
statusCode: doc.statusCode ?? null,
|
||||||
resolutionType: doc.resolutionType ?? null,
|
resolutionType: doc.resolutionType ?? null,
|
||||||
resolvedBy: doc.resolvedBy ? this.toStringId(doc.resolvedBy) : null,
|
resolvedBy: doc.resolvedBy ? toStringId(doc.resolvedBy) : null,
|
||||||
resolvedByEmail: doc.resolvedByEmail ?? null,
|
resolvedByEmail: doc.resolvedByEmail ?? null,
|
||||||
comment: doc.comment ?? null,
|
comment: doc.comment ?? null,
|
||||||
createdAt: this.toDateString(doc.createdAt),
|
createdAt: toDateString(doc.createdAt),
|
||||||
updatedAt: this.toDateString(doc.updatedAt),
|
updatedAt: toDateString(doc.updatedAt),
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -254,22 +241,22 @@ class MongoIncidentsRepository implements IIncidentsRepository {
|
|||||||
avgResolutionTimeHours,
|
avgResolutionTimeHours,
|
||||||
topMonitor: monitorResult[0]
|
topMonitor: monitorResult[0]
|
||||||
? {
|
? {
|
||||||
monitorId: this.toStringId(monitorResult[0].monitorId),
|
monitorId: toStringId(monitorResult[0].monitorId),
|
||||||
monitorName: monitorResult[0].monitorName ?? null,
|
monitorName: monitorResult[0].monitorName ?? null,
|
||||||
incidentCount: monitorResult[0].count,
|
incidentCount: monitorResult[0].count,
|
||||||
}
|
}
|
||||||
: null,
|
: null,
|
||||||
latestIncidents: latestIncidents.map((incident) => ({
|
latestIncidents: latestIncidents.map((incident) => ({
|
||||||
id: this.toStringId(incident._id),
|
id: toStringId(incident._id),
|
||||||
monitorId: this.toStringId(incident.monitorId),
|
monitorId: toStringId(incident.monitorId),
|
||||||
monitorName: incident.monitorName ?? null,
|
monitorName: incident.monitorName ?? null,
|
||||||
status: incident.status,
|
status: incident.status,
|
||||||
startTime: this.toDateString(incident.startTime),
|
startTime: toDateString(incident.startTime),
|
||||||
endTime: incident.endTime ? this.toDateString(incident.endTime) : null,
|
endTime: incident.endTime ? toDateString(incident.endTime) : null,
|
||||||
resolutionType: incident.resolutionType ?? null,
|
resolutionType: incident.resolutionType ?? null,
|
||||||
message: incident.message ?? null,
|
message: incident.message ?? null,
|
||||||
statusCode: incident.statusCode ?? null,
|
statusCode: incident.statusCode ?? null,
|
||||||
createdAt: this.toDateString(incident.createdAt),
|
createdAt: toDateString(incident.createdAt),
|
||||||
})),
|
})),
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -2,34 +2,20 @@ import { IInvitesRepository } from "@/domain/invites/invite.repository.interface
|
|||||||
import type { Invite } from "@/domain/invites/invite.type.js";
|
import type { Invite } from "@/domain/invites/invite.type.js";
|
||||||
import { type InviteDocument, InviteModel } from "@/domain/invites/invite.model.js";
|
import { type InviteDocument, InviteModel } from "@/domain/invites/invite.model.js";
|
||||||
import { AppError } from "@/utils/AppError.js";
|
import { AppError } from "@/utils/AppError.js";
|
||||||
import mongoose from "mongoose";
|
|
||||||
import crypto from "crypto";
|
import crypto from "crypto";
|
||||||
|
import { toStringId, toDateString } from "@/utils/mongoMappers.js";
|
||||||
|
|
||||||
class MongoInvitesRepository implements IInvitesRepository {
|
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 => {
|
protected toEntity = (doc: InviteDocument): Invite => {
|
||||||
return {
|
return {
|
||||||
id: this.toStringId(doc._id),
|
id: toStringId(doc._id),
|
||||||
email: doc.email,
|
email: doc.email,
|
||||||
teamId: this.toStringId(doc.teamId),
|
teamId: toStringId(doc.teamId),
|
||||||
role: doc.role ?? [],
|
role: doc.role ?? [],
|
||||||
token: doc.token,
|
token: doc.token,
|
||||||
expiry: this.toDateString(doc.expiry),
|
expiry: toDateString(doc.expiry),
|
||||||
createdAt: this.toDateString(doc.createdAt),
|
createdAt: toDateString(doc.createdAt),
|
||||||
updatedAt: this.toDateString(doc.updatedAt),
|
updatedAt: toDateString(doc.updatedAt),
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -3,22 +3,8 @@ import { type MaintenanceWindowDocument, MaintenanceWindowModel } from "@/domain
|
|||||||
import { IMaintenanceWindowsRepository } from "./maintenance-window.repository.interface.js";
|
import { IMaintenanceWindowsRepository } from "./maintenance-window.repository.interface.js";
|
||||||
import mongoose, { SortOrder } from "mongoose";
|
import mongoose, { SortOrder } from "mongoose";
|
||||||
import { AppError } from "@/utils/AppError.js";
|
import { AppError } from "@/utils/AppError.js";
|
||||||
|
import { toStringId, toDateString } from "@/utils/mongoMappers.js";
|
||||||
class MongoMaintenanceWindowsRepository implements IMaintenanceWindowsRepository {
|
class MongoMaintenanceWindowsRepository implements IMaintenanceWindowsRepository {
|
||||||
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();
|
|
||||||
};
|
|
||||||
|
|
||||||
private mapDocuments = (documents: MaintenanceWindowDocument[]): MaintenanceWindow[] => {
|
private mapDocuments = (documents: MaintenanceWindowDocument[]): MaintenanceWindow[] => {
|
||||||
if (!documents?.length) {
|
if (!documents?.length) {
|
||||||
return [];
|
return [];
|
||||||
@@ -28,18 +14,18 @@ class MongoMaintenanceWindowsRepository implements IMaintenanceWindowsRepository
|
|||||||
|
|
||||||
private toEntity = (doc: MaintenanceWindowDocument): MaintenanceWindow => {
|
private toEntity = (doc: MaintenanceWindowDocument): MaintenanceWindow => {
|
||||||
return {
|
return {
|
||||||
id: this.toStringId(doc._id),
|
id: toStringId(doc._id),
|
||||||
monitorIds: doc.monitorIds.map(this.toStringId),
|
monitorIds: doc.monitorIds.map(toStringId),
|
||||||
teamId: this.toStringId(doc.teamId),
|
teamId: toStringId(doc.teamId),
|
||||||
active: doc.active,
|
active: doc.active,
|
||||||
name: doc.name,
|
name: doc.name,
|
||||||
duration: doc.duration,
|
duration: doc.duration,
|
||||||
durationUnit: doc.durationUnit,
|
durationUnit: doc.durationUnit,
|
||||||
repeat: doc.repeat,
|
repeat: doc.repeat,
|
||||||
start: this.toDateString(doc.start),
|
start: toDateString(doc.start),
|
||||||
end: this.toDateString(doc.end),
|
end: toDateString(doc.end),
|
||||||
createdAt: this.toDateString(doc.createdAt),
|
createdAt: toDateString(doc.createdAt),
|
||||||
updatedAt: this.toDateString(doc.updatedAt),
|
updatedAt: toDateString(doc.updatedAt),
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -3,19 +3,9 @@ import type { CheckResultInput, MonitorStats } from "@/domain/monitor-stats/moni
|
|||||||
import { IMonitorStatsRepository } from "@/domain/monitor-stats/monitor-stats.repository.interface.js";
|
import { IMonitorStatsRepository } from "@/domain/monitor-stats/monitor-stats.repository.interface.js";
|
||||||
import mongoose from "mongoose";
|
import mongoose from "mongoose";
|
||||||
import { AppError } from "@/utils/AppError.js";
|
import { AppError } from "@/utils/AppError.js";
|
||||||
|
import { toStringId, toDateString } from "@/utils/mongoMappers.js";
|
||||||
class MongoMonitorStatsRepository implements IMonitorStatsRepository {
|
class MongoMonitorStatsRepository implements IMonitorStatsRepository {
|
||||||
private toEntity = (doc: MonitorStatsDocument): MonitorStats => {
|
private toEntity = (doc: MonitorStatsDocument): MonitorStats => {
|
||||||
const toStringId = (value: unknown): string => {
|
|
||||||
if (value instanceof mongoose.Types.ObjectId) {
|
|
||||||
return value.toString();
|
|
||||||
}
|
|
||||||
return value?.toString() ?? "";
|
|
||||||
};
|
|
||||||
|
|
||||||
const toDateString = (value: Date | string): string => {
|
|
||||||
return value instanceof Date ? value.toISOString() : value;
|
|
||||||
};
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: toStringId(doc._id),
|
id: toStringId(doc._id),
|
||||||
monitorId: toStringId(doc.monitorId),
|
monitorId: toStringId(doc.monitorId),
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import mongoose, { type FilterQuery, type PipelineStage } from "mongoose";
|
|||||||
import { MongoBulkWriteError } from "mongodb";
|
import { MongoBulkWriteError } from "mongodb";
|
||||||
import { AppError } from "@/utils/AppError.js";
|
import { AppError } from "@/utils/AppError.js";
|
||||||
import { IMonitorsRepository, TeamQueryConfig, SummaryConfig } from "@/domain/monitors/monitor.repository.interface.js";
|
import { IMonitorsRepository, TeamQueryConfig, SummaryConfig } from "@/domain/monitors/monitor.repository.interface.js";
|
||||||
|
import { toStringId, toDateString } from "@/utils/mongoMappers.js";
|
||||||
class MongoMonitorsRepository implements IMonitorsRepository {
|
class MongoMonitorsRepository implements IMonitorsRepository {
|
||||||
create = async (monitor: Monitor, teamId: string, userId: string) => {
|
create = async (monitor: Monitor, teamId: string, userId: string) => {
|
||||||
const monitorModel = new MonitorModel({ ...monitor, teamId, userId });
|
const monitorModel = new MonitorModel({ ...monitor, teamId, userId });
|
||||||
@@ -440,18 +441,6 @@ class MongoMonitorsRepository implements IMonitorsRepository {
|
|||||||
};
|
};
|
||||||
|
|
||||||
private toEntity = (doc: MonitorDocument): Monitor => {
|
private toEntity = (doc: MonitorDocument): Monitor => {
|
||||||
const toStringId = (value: unknown): string => {
|
|
||||||
if (value instanceof mongoose.Types.ObjectId) {
|
|
||||||
return value.toString();
|
|
||||||
}
|
|
||||||
return value?.toString() ?? "";
|
|
||||||
};
|
|
||||||
|
|
||||||
const toDateString = (value: Date | string): string => {
|
|
||||||
if (!value) return "";
|
|
||||||
return value instanceof Date ? value.toISOString() : value;
|
|
||||||
};
|
|
||||||
|
|
||||||
const notificationIds = (doc.notifications ?? []).map((notification) => toStringId(notification));
|
const notificationIds = (doc.notifications ?? []).map((notification) => toStringId(notification));
|
||||||
const tagIds = (doc.tags ?? []).map((tag) => toStringId(tag));
|
const tagIds = (doc.tags ?? []).map((tag) => toStringId(tag));
|
||||||
return {
|
return {
|
||||||
@@ -506,10 +495,6 @@ class MongoMonitorsRepository implements IMonitorsRepository {
|
|||||||
};
|
};
|
||||||
|
|
||||||
private toCheckSnapshot = (doc: CheckSnapshotDocument): CheckSnapshot => {
|
private toCheckSnapshot = (doc: CheckSnapshotDocument): CheckSnapshot => {
|
||||||
const toDateString = (value: Date | string): string => {
|
|
||||||
return value instanceof Date ? value.toISOString() : value;
|
|
||||||
};
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: doc.id,
|
id: doc.id,
|
||||||
status: doc.status,
|
status: doc.status,
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import { NotificationModel, type NotificationDocument } from "@/domain/notificat
|
|||||||
import { INotificationsRepository } from "@/domain/notifications/notification.repository.interface.js";
|
import { INotificationsRepository } from "@/domain/notifications/notification.repository.interface.js";
|
||||||
import type { Notification } from "@/domain/notifications/notification.type.js";
|
import type { Notification } from "@/domain/notifications/notification.type.js";
|
||||||
import { AppError } from "@/utils/AppError.js";
|
import { AppError } from "@/utils/AppError.js";
|
||||||
|
import { toStringId, toDateString } from "@/utils/mongoMappers.js";
|
||||||
|
|
||||||
class MongoNotificationsRepository implements INotificationsRepository {
|
class MongoNotificationsRepository implements INotificationsRepository {
|
||||||
private mapDocuments = (documents: NotificationDocument[]): Notification[] => {
|
private mapDocuments = (documents: NotificationDocument[]): Notification[] => {
|
||||||
@@ -13,14 +14,6 @@ class MongoNotificationsRepository implements INotificationsRepository {
|
|||||||
};
|
};
|
||||||
|
|
||||||
private toEntity = (doc: NotificationDocument): Notification => {
|
private toEntity = (doc: NotificationDocument): Notification => {
|
||||||
const toStringId = (value: mongoose.Types.ObjectId | string): string => {
|
|
||||||
return value instanceof mongoose.Types.ObjectId ? value.toString() : value;
|
|
||||||
};
|
|
||||||
|
|
||||||
const toDateString = (value: Date | string): string => {
|
|
||||||
return value instanceof Date ? value.toISOString() : value;
|
|
||||||
};
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: toStringId(doc._id),
|
id: toStringId(doc._id),
|
||||||
userId: toStringId(doc.userId),
|
userId: toStringId(doc.userId),
|
||||||
|
|||||||
@@ -2,35 +2,22 @@ import type { RecoveryToken } from "@/domain/recovery-tokens/recovery-token.type
|
|||||||
import type { IRecoveryTokensRepository } from "./recovery-token.repository.interface.js";
|
import type { IRecoveryTokensRepository } from "./recovery-token.repository.interface.js";
|
||||||
import type { RecoveryTokenDocument } from "@/domain/recovery-tokens/recovery-token.model.js";
|
import type { RecoveryTokenDocument } from "@/domain/recovery-tokens/recovery-token.model.js";
|
||||||
import { RecoveryTokenModel } from "@/domain/recovery-tokens/recovery-token.model.js";
|
import { RecoveryTokenModel } from "@/domain/recovery-tokens/recovery-token.model.js";
|
||||||
import mongoose from "mongoose";
|
|
||||||
import crypto from "crypto";
|
import crypto from "crypto";
|
||||||
import { AppError } from "@/utils/AppError.js";
|
import { AppError } from "@/utils/AppError.js";
|
||||||
|
import { toStringId, toDateString } from "@/utils/mongoMappers.js";
|
||||||
const SERVICE_NAME = "MongoRecoveryTokensRepository";
|
const SERVICE_NAME = "MongoRecoveryTokensRepository";
|
||||||
|
|
||||||
class MongoRecoveryTokensRepository implements IRecoveryTokensRepository {
|
class MongoRecoveryTokensRepository implements IRecoveryTokensRepository {
|
||||||
static SERVICE_NAME = SERVICE_NAME;
|
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: RecoveryTokenDocument): RecoveryToken => {
|
protected toEntity = (doc: RecoveryTokenDocument): RecoveryToken => {
|
||||||
return {
|
return {
|
||||||
id: this.toStringId(doc._id),
|
id: toStringId(doc._id),
|
||||||
email: doc.email,
|
email: doc.email,
|
||||||
token: doc.token,
|
token: doc.token,
|
||||||
expiry: this.toDateString(doc.expiry),
|
expiry: toDateString(doc.expiry),
|
||||||
createdAt: this.toDateString(doc.createdAt),
|
createdAt: toDateString(doc.createdAt),
|
||||||
updatedAt: this.toDateString(doc.updatedAt),
|
updatedAt: toDateString(doc.updatedAt),
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -4,28 +4,15 @@ import type { StatusPage, StatusPageLogo, StatusPageLogoDocument } from "@/domai
|
|||||||
import mongoose from "mongoose";
|
import mongoose from "mongoose";
|
||||||
import { AppError } from "@/utils/AppError.js";
|
import { AppError } from "@/utils/AppError.js";
|
||||||
import { normalizeStatusPageDomain } from "@/utils/statusPageDomain.js";
|
import { normalizeStatusPageDomain } from "@/utils/statusPageDomain.js";
|
||||||
|
import { toStringId, toDateString } from "@/utils/mongoMappers.js";
|
||||||
// Type for update data that can include document-level fields (Buffer for logo)
|
// Type for update data that can include document-level fields (Buffer for logo)
|
||||||
type StatusPageUpdateData = Partial<Omit<StatusPage, "id" | "userId" | "teamId" | "logo" | "createdAt" | "updatedAt">> & {
|
type StatusPageUpdateData = Partial<Omit<StatusPage, "id" | "userId" | "teamId" | "logo" | "createdAt" | "updatedAt">> & {
|
||||||
logo?: StatusPageLogoDocument | null;
|
logo?: StatusPageLogoDocument | null;
|
||||||
};
|
};
|
||||||
|
|
||||||
class MongoStatusPagesRepository implements IStatusPagesRepository {
|
class MongoStatusPagesRepository implements IStatusPagesRepository {
|
||||||
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();
|
|
||||||
};
|
|
||||||
|
|
||||||
private mapIdArray = (values?: Array<mongoose.Types.ObjectId | string>): string[] => {
|
private mapIdArray = (values?: Array<mongoose.Types.ObjectId | string>): string[] => {
|
||||||
return values?.map((value) => this.toStringId(value)) ?? [];
|
return values?.map((value) => toStringId(value)) ?? [];
|
||||||
};
|
};
|
||||||
|
|
||||||
private mapLogo = (logo?: StatusPageLogoDocument | null): StatusPageLogo | undefined => {
|
private mapLogo = (logo?: StatusPageLogoDocument | null): StatusPageLogo | undefined => {
|
||||||
@@ -42,9 +29,9 @@ class MongoStatusPagesRepository implements IStatusPagesRepository {
|
|||||||
|
|
||||||
private toEntity = (doc: StatusPageDocument): StatusPage => {
|
private toEntity = (doc: StatusPageDocument): StatusPage => {
|
||||||
return {
|
return {
|
||||||
id: this.toStringId(doc._id),
|
id: toStringId(doc._id),
|
||||||
userId: this.toStringId(doc.userId),
|
userId: toStringId(doc.userId),
|
||||||
teamId: this.toStringId(doc.teamId),
|
teamId: toStringId(doc.teamId),
|
||||||
type: doc.type,
|
type: doc.type,
|
||||||
companyName: doc.companyName,
|
companyName: doc.companyName,
|
||||||
url: doc.url,
|
url: doc.url,
|
||||||
@@ -63,8 +50,8 @@ class MongoStatusPagesRepository implements IStatusPagesRepository {
|
|||||||
customCSS: doc.customCSS,
|
customCSS: doc.customCSS,
|
||||||
theme: doc.theme,
|
theme: doc.theme,
|
||||||
themeMode: doc.themeMode,
|
themeMode: doc.themeMode,
|
||||||
createdAt: this.toDateString(doc.createdAt),
|
createdAt: toDateString(doc.createdAt),
|
||||||
updatedAt: this.toDateString(doc.updatedAt),
|
updatedAt: toDateString(doc.updatedAt),
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,36 +1,22 @@
|
|||||||
import { ITagsRepository } from "@/domain/tags/tag.repository.interface.js";
|
import { ITagsRepository } from "@/domain/tags/tag.repository.interface.js";
|
||||||
import { Tag } from "@/domain/tags/tag.type.js";
|
import { Tag } from "@/domain/tags/tag.type.js";
|
||||||
import { TagDocument, TagModel } from "@/domain/tags/tag.model.js";
|
import { TagDocument, TagModel } from "@/domain/tags/tag.model.js";
|
||||||
import mongoose from "mongoose";
|
|
||||||
import { AppError } from "@/utils/AppError.js";
|
import { AppError } from "@/utils/AppError.js";
|
||||||
|
import { toStringId, toDateString } from "@/utils/mongoMappers.js";
|
||||||
|
|
||||||
const SERVICE_NAME = "TagsRepository";
|
const SERVICE_NAME = "TagsRepository";
|
||||||
|
|
||||||
class MongoTagsRepository implements ITagsRepository {
|
class MongoTagsRepository implements ITagsRepository {
|
||||||
static SERVICE_NAME = SERVICE_NAME;
|
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 => {
|
protected toEntity = (doc: TagDocument): Tag => {
|
||||||
return {
|
return {
|
||||||
id: this.toStringId(doc._id),
|
id: toStringId(doc._id),
|
||||||
teamId: this.toStringId(doc.teamId),
|
teamId: toStringId(doc.teamId),
|
||||||
name: doc.name,
|
name: doc.name,
|
||||||
color: doc.color,
|
color: doc.color,
|
||||||
createdAt: this.toDateString(doc.createdAt),
|
createdAt: toDateString(doc.createdAt),
|
||||||
updatedAt: this.toDateString(doc.updatedAt),
|
updatedAt: toDateString(doc.updatedAt),
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,29 +1,14 @@
|
|||||||
import { Team } from "@/domain/teams/team.type.js";
|
import { Team } from "@/domain/teams/team.type.js";
|
||||||
import { TeamDocument, TeamModel } from "@/domain/teams/team.model.js";
|
import { TeamDocument, TeamModel } from "@/domain/teams/team.model.js";
|
||||||
import { ITeamsRepository } from "@/domain/teams/team.repository.interface.js";
|
import { ITeamsRepository } from "@/domain/teams/team.repository.interface.js";
|
||||||
import mongoose from "mongoose";
|
import { toStringId, toDateString } from "@/utils/mongoMappers.js";
|
||||||
|
|
||||||
class MongoTeamsRepository implements ITeamsRepository {
|
class MongoTeamsRepository implements ITeamsRepository {
|
||||||
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();
|
|
||||||
};
|
|
||||||
|
|
||||||
private toEntity = (doc: TeamDocument): Team => {
|
private toEntity = (doc: TeamDocument): Team => {
|
||||||
return {
|
return {
|
||||||
id: this.toStringId(doc._id),
|
id: toStringId(doc._id),
|
||||||
email: doc.email,
|
email: doc.email,
|
||||||
createdAt: this.toDateString(doc.createdAt),
|
createdAt: toDateString(doc.createdAt),
|
||||||
updatedAt: this.toDateString(doc.updatedAt),
|
updatedAt: toDateString(doc.updatedAt),
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -34,7 +19,7 @@ class MongoTeamsRepository implements ITeamsRepository {
|
|||||||
|
|
||||||
findAllTeamIds = async (): Promise<string[]> => {
|
findAllTeamIds = async (): Promise<string[]> => {
|
||||||
const teams = await TeamModel.find({}, { _id: 1 }).lean();
|
const teams = await TeamModel.find({}, { _id: 1 }).lean();
|
||||||
return teams.map((team) => this.toStringId(team._id));
|
return teams.map((team) => toStringId(team._id));
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,27 +1,14 @@
|
|||||||
import mongoose from "mongoose";
|
|
||||||
import { IUsersRepository } from "@/domain/users/user.repository.interface.js";
|
import { IUsersRepository } from "@/domain/users/user.repository.interface.js";
|
||||||
import { UserModel, type UserDocument } from "@/domain/users/user.model.js";
|
import { UserModel, type UserDocument } from "@/domain/users/user.model.js";
|
||||||
import type { User, UserProfileImage } from "@/domain/users/user.type.js";
|
import type { User, UserProfileImage } from "@/domain/users/user.type.js";
|
||||||
import { GenerateAvatarImage } from "@/utils/imageProcessing.js";
|
import { GenerateAvatarImage } from "@/utils/imageProcessing.js";
|
||||||
import { ParseBoolean } from "@/utils/utils.js";
|
import { ParseBoolean } from "@/utils/utils.js";
|
||||||
import { AppError } from "@/utils/AppError.js";
|
import { AppError } from "@/utils/AppError.js";
|
||||||
|
import { toStringId, toDateString } from "@/utils/mongoMappers.js";
|
||||||
const SERVICE_NAME = "MongoUsersRepository";
|
const SERVICE_NAME = "MongoUsersRepository";
|
||||||
|
|
||||||
class MongoUsersRepository implements IUsersRepository {
|
class MongoUsersRepository implements IUsersRepository {
|
||||||
static SERVICE_NAME = SERVICE_NAME;
|
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();
|
|
||||||
};
|
|
||||||
|
|
||||||
private mapProfileImage = (image?: (UserProfileImage & { data?: Buffer }) | null) => {
|
private mapProfileImage = (image?: (UserProfileImage & { data?: Buffer }) | null) => {
|
||||||
if (!image) {
|
if (!image) {
|
||||||
@@ -35,7 +22,7 @@ class MongoUsersRepository implements IUsersRepository {
|
|||||||
|
|
||||||
protected toEntity = (doc: UserDocument): User => {
|
protected toEntity = (doc: UserDocument): User => {
|
||||||
return {
|
return {
|
||||||
id: this.toStringId(doc._id),
|
id: toStringId(doc._id),
|
||||||
firstName: doc.firstName,
|
firstName: doc.firstName,
|
||||||
lastName: doc.lastName,
|
lastName: doc.lastName,
|
||||||
email: doc.email,
|
email: doc.email,
|
||||||
@@ -45,10 +32,10 @@ class MongoUsersRepository implements IUsersRepository {
|
|||||||
isActive: doc.isActive ?? false,
|
isActive: doc.isActive ?? false,
|
||||||
isVerified: doc.isVerified ?? false,
|
isVerified: doc.isVerified ?? false,
|
||||||
role: doc.role ?? [],
|
role: doc.role ?? [],
|
||||||
teamId: this.toStringId(doc.teamId),
|
teamId: toStringId(doc.teamId),
|
||||||
checkTTL: doc.checkTTL ?? undefined,
|
checkTTL: doc.checkTTL ?? undefined,
|
||||||
createdAt: this.toDateString(doc.createdAt),
|
createdAt: toDateString(doc.createdAt),
|
||||||
updatedAt: this.toDateString(doc.updatedAt),
|
updatedAt: toDateString(doc.updatedAt),
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
import mongoose from "mongoose";
|
||||||
|
|
||||||
|
export const toStringId = (value?: mongoose.Types.ObjectId | string | null): string => {
|
||||||
|
if (!value) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
return value instanceof mongoose.Types.ObjectId ? value.toString() : String(value);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const toDateString = (value?: Date | string | null): string => {
|
||||||
|
if (!value) {
|
||||||
|
return new Date(0).toISOString();
|
||||||
|
}
|
||||||
|
return value instanceof Date ? value.toISOString() : new Date(value).toISOString();
|
||||||
|
};
|
||||||
Reference in new issue
Block a user