Spaces:
Sleeping

File size: 7,364 Bytes
4e83772
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import { Request, Response, NextFunction } from "express";

import { IProjectController } from "@/interfaces/controller/IProjectController.js";
import ProjectService from "@/services/project.service.js";
import {
	checkSubdomainDTO,
	CreateProjectDTO,
	ProjectDeploymentUpdateDTO,
	QueryProjectDTO,
	UpdateProjectDTO,
	UpdateSubdomainDTO,
} from "@/dtos/project.dto.js";
import { STATUS_CODES } from "@/utils/statusCodes.js";
import { ProjectMapper } from "@/mappers/ProjectMapper.js";
import AppError from "@/utils/AppError.js";
import { PROJECT_ERRORS } from "@/constants/errors.js";

class ProjectController implements IProjectController {
	private projectService: ProjectService;

	constructor(projectService: ProjectService) {
		this.projectService = projectService;
	}
	async createProject(req: Request, res: Response, next: NextFunction): Promise<void> {
		try {
			const dto = req.validatedBody as CreateProjectDTO;
			const user = req.user?.id;

			const dbResult = await this.projectService.createProject(dto, user || "");
			if (!dbResult) {
				throw new AppError(PROJECT_ERRORS.CREATE_FAILED, STATUS_CODES.INTERNAL_SERVER_ERROR);
			}
			const response = ProjectMapper.toProjectResponse(dbResult, "full");

			res.status(STATUS_CODES.CREATED).json(response);
		} catch (error) {
			next(error);
		}
	}
	async getAllProjects(req: Request, res: Response, next: NextFunction): Promise<void> {
		try {
			const userId = req.user?.id as string;
			const query = req.validatedQuery as unknown as QueryProjectDTO;
			const result = await this.projectService.getAllProjects(userId, query);
			const response = ProjectMapper.toProjectsResponse(
				result.projects,
				result.total,
				query.page,
				query.limit,
				query.full ? "full" : "overview",
			);

			res.status(STATUS_CODES.OK).json(response);
		} catch (err) {
			next(err);
		}
	}
	async getProject(req: Request, res: Response, next: NextFunction): Promise<void> {
		try {
			const userId = req.user?.id as string;
			const projectId = req.params.projectId;
			const include = req.query.include as string;

			const result = await this.projectService.getProjectById(projectId, userId, include);
			if (!result) {
				res.status(STATUS_CODES.NOT_FOUND).json({ project: null });
				return;
			}
			const response = ProjectMapper.toProjectResponse(result, "overview");
			res.status(STATUS_CODES.OK).json(response);
		} catch (err) {
			next(err);
		}
	}
	async getProjectComplete(req: Request, res: Response, next: NextFunction): Promise<void> {
		try {
			const userId = req.user?.id as string;
			const projectId = req.params.projectId;
			const include = req.query.include as string;

			const result = await this.projectService.getProjectById(projectId, userId, include, true);
			if (!result) {
				res.status(STATUS_CODES.NOT_FOUND).json({ project: null });
				return;
			}
			const response = ProjectMapper.toProjectResponse(result, "full");
			res.status(STATUS_CODES.OK).json(response);
		} catch (err) {
			next(err);
		}
	}

	async getProjectSettings(req: Request, res: Response, next: NextFunction): Promise<void> {
		try {
			const userId = req.user?.id as string;
			const projectId = req.params.projectId;
			const include = req.query.include as string;

			const result = await this.projectService.getProjectSettings(projectId, userId, include);
			if (!result) {
				res.status(STATUS_CODES.NOT_FOUND).json({ project: null });
				return;
			}
			const response = ProjectMapper.toProjectResponse(result, "setting");
			res.status(STATUS_CODES.OK).json(response);
		} catch (err) {
			next(err);
		}
	}
	async updateProject(req: Request, res: Response, next: NextFunction): Promise<void> {
		try {
			const userId = req.user?.id as string;
			const projectId = req.params.projectId;
			const dto = req.validatedBody as UpdateProjectDTO;

			const result = await this.projectService.updateProject(projectId, userId, dto);
			if (!result) {
				res.status(STATUS_CODES.NOT_FOUND).json({ project: null });
				return;
			}
			const response = ProjectMapper.toProjectResponse(result, "update");
			res.status(STATUS_CODES.OK).json(response);
		} catch (err) {
			next(err);
		}
	}
	async updateSubdomain(req: Request, res: Response, next: NextFunction): Promise<void> {
		try {
			const userId = req.user?.id as string;
			const dto = req.validatedBody as UpdateSubdomainDTO;
			const updatedProject = await this.projectService.changeProjectSubdomain(userId, dto.projectId, dto.newSubdomain.toLowerCase());
			if (!updatedProject) {
				res.status(STATUS_CODES.NOT_FOUND).json({ project: null });
				return;
			}
			const response = ProjectMapper.toProjectResponse(updatedProject, "update");
			res.status(STATUS_CODES.OK).json(response);
		} catch (error) {
			next(error);
		}
	}
	async checkSubdomainAvailable(req: Request, res: Response, next: NextFunction): Promise<void> {
		try {
			const dto = req.validatedQuery as checkSubdomainDTO;
			const result = await this.projectService.checkSubdomainAvaiable(dto.value.toLowerCase());
			res.json({ available: result });
		} catch (error) {
			next(error);
		}
	}
	async changeCurrentDeployment(req: Request, res: Response, next: NextFunction): Promise<void> {
		try {
			const dto = req.validatedBody as ProjectDeploymentUpdateDTO;
			const userId = req.user?.id as string;
			const projectId = req.params.projectId;
			const updatedProject = await this.projectService.changeProjectDeployment(userId, projectId, dto.newCurrentDeployment);
			if (!updatedProject) {
				res.status(STATUS_CODES.NOT_FOUND).json({ project: null });
				return;
			}
			const response = ProjectMapper.toProjectResponse(updatedProject, "update");
			res.status(STATUS_CODES.OK).json(response);
		} catch (error) {
			next(error);
		}
	}

	async getProjectSimpleStats(req: Request, res: Response, next: NextFunction): Promise<void> {
		try {
			const userId = req.user?.id as string;
			const projectId = req.params.projectId;

			const stats = await this.projectService.findProjectSimpleStats(userId, projectId);
			res.status(STATUS_CODES.OK).json({ stats });
		} catch (error) {
			next(error);
		}
	}
	async deleteProject(req: Request, res: Response, next: NextFunction): Promise<void> {
		try {
			const userId = req.user?.id as string;
			const projectId = req.params.projectId;

			const result = await this.projectService.deleteProject(projectId, userId);

			res.status(STATUS_CODES.NO_CONTENT).json({ deleted: result });
		} catch (err) {
			next(err);
		}
	}
	async totalUsage(req: Request, res: Response, next: NextFunction): Promise<void> {
		try {
			const userId = req.user?.id as string;
			const noOfmonthsData = 1;
			const results = await this.projectService.findTotalUsage(userId, noOfmonthsData);
			const data = ProjectMapper.toUsageMapper(results.projectRslts, results.deploys, noOfmonthsData);
			res.status(STATUS_CODES.OK).json(data);
		} catch (error) {
			next(error);
		}
	}
	async __getProjects(req: Request, res: Response, next: NextFunction): Promise<void> {
		try {
			const projectId = req.params.id;
			const project = await this.projectService.__getProjectById(projectId);
			if (project) {
				const response = ProjectMapper.toProjectResponse(project, "full");
				res.json(response);
				return;
			}
			res.json({ project: null });
		} catch (error) {
			next(error);
		}
	}
}

export default ProjectController;