Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const constants = {
TOKEN_ERROR: "Error occurred during token generation.",
LOGIN_ERROR: "Error occurred during login",
ROUTE_ERROR: "Sorry, the requested resource is not available.",
PROJECT_NOT_FOUND: "Sorry, the requested project does not exists.",
NO_PROJECT: "resource not found with the given ID(s).",
MIGRATION_CREATED: "Project's migration created successfully.",
MIGRATION_UPDATED: "Project's migration updated successfully.",
Expand Down
2 changes: 2 additions & 0 deletions src/models/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ interface ProjectDocument extends Document {
status: boolean;
migration: Migration;
execution_log: ExecutionLog;
created_at: Date;
updated_at: Date;
}

const projectSchema = new Schema<ProjectDocument>(
Expand Down
85 changes: 79 additions & 6 deletions src/services/projects.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { Request } from "express";
import ProjectModel from "../models/project";
import { NotFoundError } from "../utils/custom-errors.utils";
import { constants } from "../constants";

const getAllProjects = async (req: Request) => {
const orgId = req?.params?.orgId;
Expand All @@ -8,23 +11,93 @@ const getAllProjects = async (req: Request) => {
const getProject = async (req: Request) => {
const orgId = req?.params?.orgId;
const projectId = req?.params?.projectId;
const decodedToken = req.body.token_payload;
const { user_id = "test-123", region = "NA" } = decodedToken;
// Find the project based on both orgId and projectId, region, owner
const project = await ProjectModel.findOne({
org_id: orgId,
_id: projectId,
region,
owner: user_id,
});

//Add logic to get Project from DB
return { orgId, projectId };
if (!project) throw new NotFoundError(constants.HTTP_TEXTS.PROJECT_NOT_FOUND);

return {
name: project?.name,
description: project?.description,
id: project?.id,
status: project?.status,
created_at: project?.created_at,
modified_at: project?.updated_at,
};
};
const createProject = async (req: Request) => {
const orgId = req?.params?.orgId;

const { name, description } = req.body;
const decodedToken = req.body.token_payload;
const { user_id = "test-123", region = "NA" } = decodedToken;
const projectData = {
region,
org_id: orgId,
owner: user_id,
created_by: user_id,
name,
description,
};
//Add logic to create Project from DB
return { orgId };
const project = await ProjectModel.create(projectData);

if (!project) throw new NotFoundError(constants.HTTP_TEXTS.PROJECT_NOT_FOUND);
return {
status: "success",
message: "Project created successfully",
project: {
name: project.name,
id: project.id,
status: project.status,
created_at: project.created_at,
modified_at: project.updated_at,
// Add other properties as needed
},
};
};

const updateProject = async (req: Request) => {
const orgId = req?.params?.orgId;
const projectId = req?.params?.projectId;
const updateData = req?.body;
const decodedToken = req.body.token_payload;
const { user_id = "test-123", region = "NA" } = decodedToken;
// Find the project based on both orgId and projectId
const project = await ProjectModel.findOne({
org_id: orgId,
_id: projectId,
region,
owner: user_id,
});

//Add logic to update Project from DB
return { orgId, projectId };
if (!project) throw new NotFoundError("Project not found!");

// Update the project fields
project.name = updateData.name || project.name;
project.description = updateData.description || project.description;

// Save the updated project
const updatedProject = await project.save();

return {
status: "success",
message: "Project updated successfully",
project: {
name: updatedProject.name,
id: updatedProject.id,
status: updatedProject.status,
created_at: updatedProject.created_at,
modified_at: updatedProject.updated_at,
// Add other properties as needed
},
};
};

const deleteProject = async (req: Request) => {
Expand Down