Skip to content

Commit 4903d3b

Browse files
committed
- add api for project's status update
1 parent 12f169d commit 4903d3b

2 files changed

Lines changed: 74 additions & 0 deletions

File tree

Servers/controllers/project.ctrl.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { IControl } from "../domain.layer/interfaces/i.control";
2525
import { IControlCategory } from "../domain.layer/interfaces/i.controlCategory";
2626
import { logProcessing, logSuccess, logFailure } from "../utils/logger/logHelper";
2727
import { createISO27001FrameworkQuery } from "../utils/iso27001.utils";
28+
import { ProjectStatus } from "../domain.layer/enums/project-status.enum";
2829

2930
export async function getAllProjects(req: Request, res: Response): Promise<any> {
3031
logProcessing({
@@ -725,3 +726,74 @@ export async function allProjectsAssessmentProgress(req: Request, res: Response)
725726
return res.status(500).json(STATUS_CODE[500]((error as Error).message));
726727
}
727728
}
729+
730+
export async function updateProjectStatus(req: Request, res: Response): Promise<any> {
731+
const transaction = await sequelize.transaction();
732+
const projectId = parseInt(req.params.id);
733+
const { status } = req.body;
734+
735+
logProcessing({
736+
description: `starting updateProjectStatus for ID ${projectId}`,
737+
functionName: "updateProjectStatus",
738+
fileName: "project.ctrl.ts",
739+
});
740+
741+
try {
742+
// Validate status value
743+
if (!status || !Object.values(ProjectStatus).includes(status)) {
744+
return res.status(400).json(
745+
STATUS_CODE[400]({ message: "Valid status is required" })
746+
);
747+
}
748+
749+
// Check if project exists
750+
const existingProject = await getProjectByIdQuery(projectId, req.tenantId!);
751+
if (!existingProject) {
752+
await logSuccess({
753+
eventType: "Update",
754+
description: `Project not found for status update: ID ${projectId}`,
755+
functionName: "updateProjectStatus",
756+
fileName: "project.ctrl.ts",
757+
});
758+
759+
return res.status(404).json(STATUS_CODE[404]({}));
760+
}
761+
762+
// Update project status
763+
const updatedProject = await updateProjectByIdQuery(
764+
projectId,
765+
{ status, last_updated: new Date(), last_updated_by: req.userId! },
766+
[], // no members update
767+
req.tenantId!,
768+
transaction
769+
);
770+
771+
if (updatedProject) {
772+
await transaction.commit();
773+
774+
await logSuccess({
775+
eventType: "Update",
776+
description: `Updated project status to ${status} for ID ${projectId}`,
777+
functionName: "updateProjectStatus",
778+
fileName: "project.ctrl.ts",
779+
});
780+
781+
return res.status(200).json(STATUS_CODE[200](updatedProject));
782+
}
783+
784+
await transaction.rollback();
785+
return res.status(500).json(STATUS_CODE[500]("Failed to update project status"));
786+
} catch (error) {
787+
await transaction.rollback();
788+
789+
await logFailure({
790+
eventType: "Update",
791+
description: "Failed to update project status",
792+
functionName: "updateProjectStatus",
793+
fileName: "project.ctrl.ts",
794+
error: error as Error,
795+
});
796+
797+
return res.status(500).json(STATUS_CODE[500]((error as Error).message));
798+
}
799+
}

Servers/routes/project.route.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
projectComplianceProgress,
1717
// saveControls,
1818
updateProjectById,
19+
updateProjectStatus,
1920
} from "../controllers/project.ctrl";
2021

2122
import authenticateJWT from "../middleware/auth.middleware";
@@ -65,6 +66,7 @@ router.post("/", authenticateJWT, createProject);
6566

6667
// Patches
6768
router.patch("/:id", authenticateJWT, updateProjectById);
69+
router.patch("/:id/status", authenticateJWT, updateProjectStatus);
6870

6971
// DELETEs
7072
router.delete("/:id", authenticateJWT, deleteProjectById);

0 commit comments

Comments
 (0)