-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathsurvey.controller.ts
More file actions
160 lines (149 loc) · 4.76 KB
/
survey.controller.ts
File metadata and controls
160 lines (149 loc) · 4.76 KB
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
import { Request, Response } from 'express';
import { SurveyType } from '../models/survey.model.js';
import logger from '../services/logger.js';
import surveyService from '../services/survey.service.js';
import app from '../index.js';
import mongoose from 'mongoose';
class SurveyController {
async updateSurveyGitHub(req: Request, res: Response): Promise<void> {
let survey: SurveyType;
try {
const sanitizedBody = {
id: req.body.id,
userId: req.body.userId,
org: req.body.org,
repo: req.body.repo,
prNumber: req.body.prNumber,
usedCopilot: req.body.usedCopilot,
percentTimeSaved: req.body.percentTimeSaved,
reason: req.body.reason,
timeUsedFor: req.body.timeUsedFor,
kudos: req.body.kudos,
hits: 0,
status: 'completed'
};
const _survey = await surveyService.updateSurvey(sanitizedBody);
if (!_survey) throw new Error('Survey not found');
survey = _survey;
res.status(201).json(survey);
} catch (error) {
res.status(500).json(error);
return;
}
try {
const { installation, octokit } = await app.github.getInstallation(survey.org);
const surveyUrl = new URL(`copilot/surveys/${survey.id}`, app.baseUrl);
if (!survey.repo || !survey.org || !survey.prNumber) {
logger.warn('Cannot process survey comment: missing survey data');
return;
}
const comments = await octokit.rest.issues.listComments({
owner: survey.org,
repo: survey.repo,
issue_number: survey.prNumber
});
const comment = comments.data.find(comment => comment.user?.login.startsWith(installation.app_slug));
if (comment) {
octokit.rest.issues.updateComment({
owner: survey.org,
repo: survey.repo,
comment_id: comment.id,
body: `Thanks for filling out the [copilot survey](${surveyUrl.toString()}) @${survey.userId}!`
});
} else {
logger.info(`No comment found for survey from ${survey.org}`);
}
} catch (error) {
logger.error('Error updating survey comment', error);
throw error;
}
}
async createSurvey(req: Request, res: Response): Promise<void> {
try {
const newSurvey = req.body;
// TODO: validate the user belong to the org.
const survey = surveyService.createSurvey(newSurvey);
res.status(201).json(survey);
} catch (error) {
res.status(500).json((error as Error).message);
return;
}
}
async getAllSurveys(req: Request, res: Response): Promise<void> {
try {
const { org, team, reasonLength, since, until, status, userId } = req.query as { [key: string]: string | undefined };
const surveys = await surveyService.getAllSurveys({
org,
team,
reasonLength,
since,
until,
status,
userId
});
res.status(200).json(surveys);
} catch (error) {
res.status(500).json(error);
}
}
async getSurveyById(req: Request, res: Response): Promise<void> {
try {
const { id } = req.params;
if (isNaN(Number(id))) {
res.status(400).json({ message: 'Invalid survey ID' });
return;
}
const Survey = mongoose.model('Survey');
const survey = await Survey.findOne({ id: { $eq: Number(id) } }); // Use $eq operator
if (!survey) {
res.status(404).json({ message: 'Survey not found' });
return;
}
res.status(200).json(survey);
} catch (error) {
res.status(500).json(error);
}
}
async updateSurvey(req: Request, res: Response): Promise<void> {
try {
const Survey = mongoose.model('Survey');
const { id } = req.params;
const updated = await Survey.findOneAndUpdate(
{ id: { $eq: Number(id) } },
{
$set: {
...req.body,
hits: 0,
status: 'completed'
}
}
);
if (updated) {
res.status(200).json({ _id: id, ...req.body });
} else {
res.status(404).json({ error: 'Survey not found' });
}
} catch (error) {
res.status(500).json(error);
}
}
async deleteSurvey(req: Request, res: Response): Promise<void> {
try {
const Survey = mongoose.model('Survey');
const { id } = req.params;
if (isNaN(Number(id))) {
res.status(400).json({ message: 'Invalid survey ID' });
return;
}
const deleted = await Survey.findOneAndDelete({ id: { $eq: Number(id) } }); // Use $eq operator
if (deleted) {
res.status(204).send();
} else {
res.status(404).json({ error: 'Survey not found' });
}
} catch (error) {
res.status(500).json(error);
}
}
}
export default new SurveyController();