Spaces:
Sleeping

File size: 4,797 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
import { Request, Response, NextFunction } from "express";

import { IDeploymentController } from "@/interfaces/controller/IDeploymentController.js";
import { IDeploymentService } from "@/interfaces/service/IDeploymentService.js";
import { DeploymentMapper } from "@/mappers/DeploymentMapper.js";
import { STATUS_CODES } from "@/utils/statusCodes.js";
import { QueryDeploymentDTO } from "@/dtos/deployment.dto.js";
import { COMMON_ERRORS } from "@/constants/errors.js";

class DeploymentController implements IDeploymentController {
	private deploymentService: IDeploymentService;
	constructor(deployService: IDeploymentService) {
		this.deploymentService = deployService;
	}

	async createDeployment(req: Request, res: Response, next: NextFunction): Promise<void> {
		const userId = req.user?.id as string;
		const projectId = req.params.projectId;
		const isRedeploy = req.body.isRedeploy;
		try {
			const deployment = await this.deploymentService.newDeployment({}, userId, projectId, isRedeploy);

			if (!deployment) {
				res.status(STATUS_CODES.BAD_REQUEST).json({ deployment: null });
				return;
			}
			const response = DeploymentMapper.toDeploymentFullResponse(deployment);
			res.status(STATUS_CODES.CREATED).json(response);
		} catch (error) {
			next(error);
		}
	}

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

			const result = await this.deploymentService.getDeploymentById(deploymentId, userId, includesField);
			if (result) {
				const response = DeploymentMapper.toDeploymentFullResponse(result);
				res.status(STATUS_CODES.OK).json(response);
				return;
			}
			res.status(STATUS_CODES.NOT_FOUND).json({ deployment: null });
		} catch (error) {
			next(error);
		}
	}

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

			const projectId = req.params.projectId;

			const result = await this.deploymentService.getProjectDeployments(userId, projectId, query);
			const response = DeploymentMapper.toDeployments(
				result.deployments,
				{
					total: result.total,
					page: query.page,
					limit: query.limit,
				},
				query.full ? "full" : "overview",
			);
			res.status(STATUS_CODES.OK).json(response);
		} catch (error) {
			next(error);
		}
	}

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

			const result = await this.deploymentService.getDeploymentFiles(deploymentId, userId);
			if (!result) {
				res.status(STATUS_CODES.NOT_FOUND).json({
					status: 404,
					error: "not_found",
					message: COMMON_ERRORS.NOT_FOUND,
				});
				return;
			}
			const response = DeploymentMapper.toDeploymentFilesResponse(result);
			res.status(STATUS_CODES.OK).json(response);
		} catch (error) {
			next(error);
		}
	}
	async getAllDeployments(req: Request, res: Response, next: NextFunction): Promise<void> {
		try {
			const userId = req.user?.id as string;
			const query = req.validatedQuery as QueryDeploymentDTO;

			const result = await this.deploymentService.getAllDeployments(userId, query);
			const response = DeploymentMapper.toDeployments(
				result.deployments,
				{
					total: result.total,
					page: query.page,
					limit: query.limit,
				},
				query.full ? "full" : "overview",
			);
			res.status(STATUS_CODES.OK).json(response);
		} catch (error) {
			next(error);
		}
	}

	async deleteDeployment(req: Request, res: Response, next: NextFunction): Promise<void> {
		try {
			const deploymentId = req.params.deploymentId;
			const projectId = req.params.projectId;

			const result = await this.deploymentService.deleteDeployment(projectId, deploymentId, req.user?.id as string);
			if (result !== 0) {
				res.status(STATUS_CODES.NO_CONTENT).json({ deleted: true });
				return;
			}
			res.status(STATUS_CODES.NO_CONTENT).json({});
		} catch (error) {
			next(error);
		}
	}

	async __getDeployment(req: Request, res: Response, next: NextFunction): Promise<void> {
		try {
			const deploymentId = req.params.id;
			const deployment = await this.deploymentService.__getDeploymentById(deploymentId);
			if (deployment) {
				const response = DeploymentMapper.toDeploymentFullResponse(deployment);
				res.status(STATUS_CODES.OK).json(response);
				return;
			}
			res.status(STATUS_CODES.NOT_FOUND).json({ deployment: null });
			console.log("not found");
		} catch (error) {
			next(error);
		}
	}
}

export default DeploymentController;