Files
Checkmate/Server/controllers/statusPageController.js
T

120 lines
3.1 KiB
JavaScript
Raw Normal View History

2024-11-11 12:17:06 +08:00
import { handleError, handleValidationError } from "./controllerUtils.js";
import {
createStatusPageBodyValidation,
getStatusPageParamValidation,
getStatusPageQueryValidation,
2025-02-03 15:06:25 -08:00
imageValidation,
2024-11-11 12:17:06 +08:00
} from "../validation/joi.js";
const SERVICE_NAME = "statusPageController";
2024-12-24 11:58:42 -08:00
class StatusPageController {
2025-01-30 08:37:42 +03:00
constructor(db, stringService) {
2024-12-24 11:58:42 -08:00
this.db = db;
2025-01-30 08:37:42 +03:00
this.stringService = stringService;
2024-11-11 12:17:06 +08:00
}
2024-12-24 11:58:42 -08:00
createStatusPage = async (req, res, next) => {
try {
await createStatusPageBodyValidation.validateAsync(req.body);
2025-02-03 15:06:25 -08:00
await imageValidation.validateAsync(req.file);
2024-12-24 11:58:42 -08:00
} catch (error) {
next(handleValidationError(error, SERVICE_NAME));
return;
}
2024-11-11 12:17:06 +08:00
2024-12-24 11:58:42 -08:00
try {
2025-02-03 15:06:25 -08:00
const statusPage = await this.db.createStatusPage(req.body, req.file);
2025-01-20 11:02:55 -08:00
return res.success({
2025-01-30 00:41:31 +03:00
msg: this.stringService.statusPageCreate,
2024-12-24 11:58:42 -08:00
data: statusPage,
});
} catch (error) {
next(handleError(error, SERVICE_NAME, "createStatusPage"));
}
};
2025-02-04 13:06:42 -08:00
updateStatusPage = async (req, res, next) => {
try {
await createStatusPageBodyValidation.validateAsync(req.body);
await imageValidation.validateAsync(req.file);
} catch (error) {
next(handleValidationError(error, SERVICE_NAME));
return;
}
try {
const statusPage = await this.db.updateStatusPage(req.body, req.file);
2025-02-05 12:07:28 -08:00
if (statusPage === null) {
2025-02-06 23:38:13 +03:00
const error = new Error(this.stringService.statusPageNotFound);
2025-02-05 12:07:28 -08:00
error.status = 404;
throw error;
}
return res.success({
2025-02-06 23:38:13 +03:00
msg: this.stringService.statusPageUpdate,
data: statusPage,
});
} catch (error) {
next(handleError(error, SERVICE_NAME, "updateStatusPage"));
}
};
2025-02-03 15:06:25 -08:00
getStatusPage = async (req, res, next) => {
2024-12-24 11:58:42 -08:00
try {
2025-02-03 15:06:25 -08:00
const statusPage = await this.db.getStatusPage();
2025-01-20 11:02:55 -08:00
return res.success({
2025-01-30 00:41:31 +03:00
msg: this.stringService.statusPageByUrl,
2024-12-24 11:58:42 -08:00
data: statusPage,
});
} catch (error) {
next(handleError(error, SERVICE_NAME, "getStatusPage"));
}
};
2025-02-04 13:06:42 -08:00
getStatusPageByUrl = async (req, res, next) => {
2025-02-08 08:04:36 -08:00
try {
await getStatusPageParamValidation.validateAsync(req.params);
await getStatusPageQueryValidation.validateAsync(req.query);
2025-02-08 08:04:36 -08:00
} catch (error) {
next(handleValidationError(error, SERVICE_NAME));
return;
}
try {
const statusPage = await this.db.getStatusPageByUrl(req.params.url, req.query.type);
return res.success({
2025-02-11 08:15:29 +03:00
msg: this.stringService.statusPageByUrl,
data: statusPage,
});
} catch (error) {
next(handleError(error, SERVICE_NAME, "getStatusPageByUrl"));
}
};
getStatusPagesByTeamId = async (req, res, next) => {
try {
const teamId = req.params.teamId;
const statusPages = await this.db.getStatusPagesByTeamId(teamId);
2025-02-11 07:16:43 -08:00
return res.success({
2025-02-11 08:15:29 +03:00
msg: this.stringService.statusPageByTeamId,
data: statusPages,
});
} catch (error) {
2025-02-10 14:25:02 -08:00
next(handleError(error, SERVICE_NAME, "getStatusPageByTeamId"));
}
};
2025-02-04 13:06:42 -08:00
deleteStatusPage = async (req, res, next) => {
try {
await this.db.deleteStatusPage(req.params.url);
2025-02-04 13:06:42 -08:00
return res.success({
2025-02-06 23:38:13 +03:00
msg: this.stringService.statusPageDelete,
2025-02-04 13:06:42 -08:00
});
} catch (error) {
next(handleError(error, SERVICE_NAME, "deleteStatusPage"));
}
};
2024-12-24 11:58:42 -08:00
}
2024-11-11 12:17:06 +08:00
2024-12-24 11:58:42 -08:00
export default StatusPageController;