implement missing validation
This commit is contained in:
1 parent
c156a49cc0
commit
16b267d3a5
3 files changed
+49
-56
No files matched your search
@@ -1,16 +1,21 @@
|
|||||||
import { Request, Response, NextFunction } from "express";
|
import { Request, Response, NextFunction } from "express";
|
||||||
|
|
||||||
import { Notification } from "@/types/index.js";
|
import {
|
||||||
import { createNotificationBodyValidation } from "@/validation/notificationValidation.js";
|
createNotificationBodyValidation,
|
||||||
|
deleteNotificationParamValidation,
|
||||||
|
getNotificationByIdParamValidation,
|
||||||
|
testNotificationBodyValidation,
|
||||||
|
editNotificationParamValidation,
|
||||||
|
testAllNotificationsBodyValidation,
|
||||||
|
} from "@/validation/notificationValidation.js";
|
||||||
import { AppError } from "@/utils/AppError.js";
|
import { AppError } from "@/utils/AppError.js";
|
||||||
import { IMonitorsRepository } from "@/repositories/index.js";
|
import { IMonitorsRepository } from "@/repositories/index.js";
|
||||||
import { INotificationsService } from "@/service/index.js";
|
import { INotificationsService } from "@/service/index.js";
|
||||||
import { requireTeamId } from "./controllerUtils.js";
|
import { requireTeamId, requireUserId } from "./controllerUtils.js";
|
||||||
|
|
||||||
const SERVICE_NAME = "NotificationController";
|
const SERVICE_NAME = "NotificationController";
|
||||||
|
|
||||||
class NotificationController {
|
class NotificationController {
|
||||||
static SERVICE_NAME = SERVICE_NAME;
|
|
||||||
private notificationsService: INotificationsService;
|
private notificationsService: INotificationsService;
|
||||||
private monitorsRepository: IMonitorsRepository;
|
private monitorsRepository: IMonitorsRepository;
|
||||||
constructor(notificationsService: INotificationsService, monitorsRepository: IMonitorsRepository) {
|
constructor(notificationsService: INotificationsService, monitorsRepository: IMonitorsRepository) {
|
||||||
@@ -18,13 +23,9 @@ class NotificationController {
|
|||||||
this.monitorsRepository = monitorsRepository;
|
this.monitorsRepository = monitorsRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
get serviceName() {
|
|
||||||
return NotificationController.SERVICE_NAME;
|
|
||||||
}
|
|
||||||
|
|
||||||
testNotification = async (req: Request, res: Response, next: NextFunction) => {
|
testNotification = async (req: Request, res: Response, next: NextFunction) => {
|
||||||
try {
|
try {
|
||||||
const notification: Notification = req.body;
|
const notification = testNotificationBodyValidation.parse(req.body);
|
||||||
const success = await this.notificationsService.sendTestNotification(notification);
|
const success = await this.notificationsService.sendTestNotification(notification);
|
||||||
|
|
||||||
if (!success) {
|
if (!success) {
|
||||||
@@ -42,23 +43,12 @@ class NotificationController {
|
|||||||
|
|
||||||
createNotification = async (req: Request, res: Response, next: NextFunction) => {
|
createNotification = async (req: Request, res: Response, next: NextFunction) => {
|
||||||
try {
|
try {
|
||||||
createNotificationBodyValidation.parse(req.body);
|
const validatedBody = createNotificationBodyValidation.parse(req.body);
|
||||||
|
|
||||||
const body = req.body;
|
const teamId = requireTeamId(req.user?.teamId);
|
||||||
|
const userId = requireUserId(req.user?.id);
|
||||||
|
|
||||||
const teamId = req?.user?.teamId;
|
const notification = await this.notificationsService.createNotification(validatedBody, userId, teamId);
|
||||||
if (!teamId) {
|
|
||||||
throw new AppError({ message: "Team ID is required", status: 400 });
|
|
||||||
}
|
|
||||||
|
|
||||||
const userId = req?.user?.id;
|
|
||||||
if (!userId) {
|
|
||||||
throw new AppError({ message: "User ID is required", status: 400 });
|
|
||||||
}
|
|
||||||
body.userId = userId;
|
|
||||||
body.teamId = teamId;
|
|
||||||
|
|
||||||
const notification = await this.notificationsService.createNotification(body);
|
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
msg: "Notification created successfully",
|
msg: "Notification created successfully",
|
||||||
@@ -71,11 +61,7 @@ class NotificationController {
|
|||||||
|
|
||||||
getNotificationsByTeamId = async (req: Request, res: Response, next: NextFunction) => {
|
getNotificationsByTeamId = async (req: Request, res: Response, next: NextFunction) => {
|
||||||
try {
|
try {
|
||||||
const teamId = req?.user?.teamId;
|
const teamId = requireTeamId(req.user?.teamId);
|
||||||
if (!teamId) {
|
|
||||||
throw new AppError({ message: "Team ID is required", status: 400 });
|
|
||||||
}
|
|
||||||
|
|
||||||
const notifications = await this.notificationsService.findNotificationsByTeamId(teamId);
|
const notifications = await this.notificationsService.findNotificationsByTeamId(teamId);
|
||||||
|
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
@@ -90,17 +76,10 @@ class NotificationController {
|
|||||||
|
|
||||||
deleteNotification = async (req: Request, res: Response, next: NextFunction) => {
|
deleteNotification = async (req: Request, res: Response, next: NextFunction) => {
|
||||||
try {
|
try {
|
||||||
const teamId = req?.user?.teamId;
|
const teamId = requireTeamId(req.user?.teamId);
|
||||||
if (!teamId) {
|
const validatedParams = deleteNotificationParamValidation.parse(req.params);
|
||||||
throw new AppError({ message: "Team ID is required", status: 400 });
|
|
||||||
}
|
|
||||||
|
|
||||||
const notificationId = req.params.id as string;
|
await this.notificationsService.deleteById(validatedParams.id, teamId);
|
||||||
if (!notificationId) {
|
|
||||||
throw new AppError({ message: "Notification ID is required", status: 400 });
|
|
||||||
}
|
|
||||||
|
|
||||||
await this.notificationsService.deleteById(notificationId, teamId);
|
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
msg: "Notification deleted successfully",
|
msg: "Notification deleted successfully",
|
||||||
@@ -113,12 +92,9 @@ class NotificationController {
|
|||||||
getNotificationById = async (req: Request, res: Response, next: NextFunction) => {
|
getNotificationById = async (req: Request, res: Response, next: NextFunction) => {
|
||||||
try {
|
try {
|
||||||
const teamId = requireTeamId(req.user?.teamId);
|
const teamId = requireTeamId(req.user?.teamId);
|
||||||
const notificationId = req.params.id as string;
|
const validatedParams = getNotificationByIdParamValidation.parse(req.params);
|
||||||
if (!notificationId) {
|
|
||||||
throw new AppError({ message: "Notification ID is required", status: 400 });
|
|
||||||
}
|
|
||||||
|
|
||||||
const notification = await this.notificationsService.findById(notificationId, teamId);
|
const notification = await this.notificationsService.findById(validatedParams.id, teamId);
|
||||||
|
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
@@ -132,14 +108,15 @@ class NotificationController {
|
|||||||
|
|
||||||
editNotification = async (req: Request, res: Response, next: NextFunction) => {
|
editNotification = async (req: Request, res: Response, next: NextFunction) => {
|
||||||
try {
|
try {
|
||||||
createNotificationBodyValidation.parse(req.body);
|
const validatedBody = createNotificationBodyValidation.parse(req.body);
|
||||||
|
const validatedParams = editNotificationParamValidation.parse(req.params);
|
||||||
|
|
||||||
const teamId = requireTeamId(req.user?.teamId);
|
const teamId = requireTeamId(req.user?.teamId);
|
||||||
const notificationId = req.params.id as string;
|
const notificationId = validatedParams.id;
|
||||||
if (!notificationId) {
|
if (!notificationId) {
|
||||||
throw new AppError({ message: "Notification ID is required", status: 400 });
|
throw new AppError({ message: "Notification ID is required", status: 400 });
|
||||||
}
|
}
|
||||||
const editedNotification = await this.notificationsService.updateById(notificationId, teamId, req.body);
|
const editedNotification = await this.notificationsService.updateById(notificationId, teamId, validatedBody);
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
msg: "Notification updated successfully",
|
msg: "Notification updated successfully",
|
||||||
@@ -152,13 +129,11 @@ class NotificationController {
|
|||||||
|
|
||||||
testAllNotifications = async (req: Request, res: Response, next: NextFunction) => {
|
testAllNotifications = async (req: Request, res: Response, next: NextFunction) => {
|
||||||
try {
|
try {
|
||||||
const monitorId = req.body.monitorId;
|
const validatedBody = testAllNotificationsBodyValidation.parse(req.body);
|
||||||
const teamId = req?.user?.teamId;
|
|
||||||
if (!teamId) {
|
|
||||||
throw new AppError({ message: "Team ID is required", status: 400 });
|
|
||||||
}
|
|
||||||
|
|
||||||
const monitor = await this.monitorsRepository.findById(monitorId, teamId);
|
const teamId = requireTeamId(req.user?.teamId);
|
||||||
|
|
||||||
|
const monitor = await this.monitorsRepository.findById(validatedBody.monitorId, teamId);
|
||||||
|
|
||||||
const notifications = monitor.notifications;
|
const notifications = monitor.notifications;
|
||||||
if (notifications.length === 0) {
|
if (notifications.length === 0) {
|
||||||
|
|||||||
@@ -8,14 +8,14 @@ import { ILogger } from "@/utils/logger.js";
|
|||||||
import type { INotificationMessageBuilder } from "@/service/infrastructure/notificationMessageBuilder.js";
|
import type { INotificationMessageBuilder } from "@/service/infrastructure/notificationMessageBuilder.js";
|
||||||
|
|
||||||
export interface INotificationsService {
|
export interface INotificationsService {
|
||||||
createNotification: (notificationData: Partial<Notification>) => Promise<Notification>;
|
createNotification: (notificationData: Partial<Notification>, userId: string, teamId: string) => Promise<Notification>;
|
||||||
findById: (id: string, teamId: string) => Promise<Notification>;
|
findById: (id: string, teamId: string) => Promise<Notification>;
|
||||||
findNotificationsByTeamId: (teamId: string) => Promise<Notification[]>;
|
findNotificationsByTeamId: (teamId: string) => Promise<Notification[]>;
|
||||||
updateById(id: string, teamId: string, updateData: Partial<Notification>): Promise<Notification>;
|
updateById(id: string, teamId: string, updateData: Partial<Notification>): Promise<Notification>;
|
||||||
deleteById: (id: string, teamId: string) => Promise<Notification>;
|
deleteById: (id: string, teamId: string) => Promise<Notification>;
|
||||||
handleNotifications: (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse, decision: MonitorActionDecision) => Promise<boolean>;
|
handleNotifications: (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse, decision: MonitorActionDecision) => Promise<boolean>;
|
||||||
|
|
||||||
sendTestNotification: (notification: Notification) => Promise<boolean>;
|
sendTestNotification: (notification: Partial<Notification>) => Promise<boolean>;
|
||||||
testAllNotifications: (notificationIds: string[]) => Promise<boolean>;
|
testAllNotifications: (notificationIds: string[]) => Promise<boolean>;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -174,7 +174,9 @@ export class NotificationsService implements INotificationsService {
|
|||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
createNotification = async (notificationData: Partial<Notification>): Promise<Notification> => {
|
createNotification = async (notificationData: Partial<Notification>, userId: string, teamId: string): Promise<Notification> => {
|
||||||
|
notificationData.userId = userId;
|
||||||
|
notificationData.teamId = teamId;
|
||||||
return await this.notificationsRepository.create(notificationData);
|
return await this.notificationsRepository.create(notificationData);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -67,6 +67,22 @@ export const createNotificationBodyValidation = z.discriminatedUnion("type", [
|
|||||||
}),
|
}),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
export const testNotificationBodyValidation = createNotificationBodyValidation;
|
||||||
|
|
||||||
|
export const deleteNotificationParamValidation = z.object({
|
||||||
|
id: z.string().min(1, "Notification ID is required"),
|
||||||
|
});
|
||||||
|
export const getNotificationByIdParamValidation = z.object({
|
||||||
|
id: z.string().min(1, "Notification ID is required"),
|
||||||
|
});
|
||||||
|
export const editNotificationParamValidation = z.object({
|
||||||
|
id: z.string().min(1, "Notification ID is required"),
|
||||||
|
});
|
||||||
|
|
||||||
|
export const testAllNotificationsBodyValidation = z.object({
|
||||||
|
monitorId: z.string().min(1, "Monitor ID is required"),
|
||||||
|
});
|
||||||
|
|
||||||
export const sendTestEmailBodyValidation = z.object({
|
export const sendTestEmailBodyValidation = z.object({
|
||||||
to: z.string().min(1, "To field is required"),
|
to: z.string().min(1, "To field is required"),
|
||||||
systemEmailHost: z.string().optional(),
|
systemEmailHost: z.string().optional(),
|
||||||
|
|||||||
Reference in new issue
Block a user