-
-
Notifications
You must be signed in to change notification settings - Fork 470
Expand file tree
/
Copy pathideas.controllers.js
More file actions
289 lines (241 loc) · 8.66 KB
/
Copy pathideas.controllers.js
File metadata and controls
289 lines (241 loc) · 8.66 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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import Ideas from '../models/ideas.models.js';
import { apiResponse } from '../utils/response.utils.js';
// Submit a new idea
export const submitIdea = async (req, res) => {
try {
const { title, description, submittedBy, submitterEmail, tags, resourcesNeeded, mediaUrls } = req.body;
// Check if submission is open
if (!Ideas.isSubmissionOpen()) {
return res
.status(400)
.json(apiResponse(400, null, 'Idea submission is only allowed during the first week of each month'));
}
// Get current submission period
const { month, year } = Ideas.getCurrentSubmissionPeriod();
// Check if user already submitted an idea this month
const existingIdea = await Ideas.findOne({
submitterEmail,
submissionMonth: month,
submissionYear: year,
});
if (existingIdea) {
return res.status(400).json(apiResponse(400, null, 'You can only submit one idea per month'));
}
// Create new idea
const newIdea = new Ideas({
title,
description,
submittedBy,
submitterEmail,
tags: tags || [],
resourcesNeeded,
mediaUrls: mediaUrls || [],
submissionMonth: month,
submissionYear: year,
});
await newIdea.save();
res.status(201).json(apiResponse(201, newIdea, 'Idea submitted successfully'));
} catch (error) {
console.error('Error submitting idea:', error);
res.status(500).json(apiResponse(500, null, 'Internal server error'));
}
};
// Get all ideas for current month
export const getCurrentMonthIdeas = async (req, res) => {
try {
const { month, year } = Ideas.getCurrentSubmissionPeriod();
const { page = 1, limit = 10, sortBy = 'votes', order = 'desc' } = req.query;
const skip = (page - 1) * limit;
const sortOrder = order === 'desc' ? -1 : 1;
const ideas = await Ideas.find({
submissionMonth: month,
submissionYear: year,
isArchived: false,
})
.sort({ [sortBy]: sortOrder, createdAt: -1 })
.skip(skip)
.limit(parseInt(limit))
.select('-voters'); // Don't expose voter details
const totalIdeas = await Ideas.countDocuments({
submissionMonth: month,
submissionYear: year,
isArchived: false,
});
res.status(200).json(
apiResponse(
200,
{
ideas,
currentPage: parseInt(page),
totalPages: Math.ceil(totalIdeas / limit),
totalIdeas,
submissionPeriod: `${month} ${year}`,
isSubmissionOpen: Ideas.isSubmissionOpen(),
},
'Ideas retrieved successfully',
),
);
} catch (error) {
console.error('Error getting ideas:', error);
res.status(500).json(apiResponse(500, null, 'Internal server error'));
}
};
// Get trending ideas
export const getTrendingIdeas = async (req, res) => {
try {
const { limit = 5 } = req.query;
const trendingIdeas = await Ideas.getTrendingIdeas(parseInt(limit));
res.status(200).json(apiResponse(200, trendingIdeas, 'Trending ideas retrieved successfully'));
} catch (error) {
console.error('Error getting trending ideas:', error);
res.status(500).json(apiResponse(500, null, 'Internal server error'));
}
};
// Vote for an idea
export const voteForIdea = async (req, res) => {
try {
const { ideaId } = req.params;
const { userEmail } = req.body;
if (!userEmail) {
return res.status(400).json(apiResponse(400, null, 'User email is required'));
}
const idea = await Ideas.findById(ideaId);
if (!idea) {
return res.status(404).json(apiResponse(404, null, 'Idea not found'));
}
// Check if user can vote (hasn't voted already)
if (!idea.canUserVote(userEmail)) {
return res.status(400).json(apiResponse(400, null, 'You have already voted for this idea'));
}
// Add vote
idea.addVote(userEmail);
await idea.save();
res.status(200).json(apiResponse(200, { votes: idea.votes }, 'Vote added successfully'));
} catch (error) {
console.error('Error voting for idea:', error);
res.status(500).json(apiResponse(500, null, 'Internal server error'));
}
};
// Remove vote from an idea
export const removeVoteFromIdea = async (req, res) => {
try {
const { ideaId } = req.params;
const { userEmail } = req.body;
if (!userEmail) {
return res.status(400).json(apiResponse(400, null, 'User email is required'));
}
const idea = await Ideas.findById(ideaId);
if (!idea) {
return res.status(404).json(apiResponse(404, null, 'Idea not found'));
}
// Remove vote
const voteRemoved = idea.removeVote(userEmail);
if (!voteRemoved) {
return res.status(400).json(apiResponse(400, null, 'You have not voted for this idea'));
}
await idea.save();
res.status(200).json(apiResponse(200, { votes: idea.votes }, 'Vote removed successfully'));
} catch (error) {
console.error('Error removing vote:', error);
res.status(500).json(apiResponse(500, null, 'Internal server error'));
}
};
// Get idea by ID
export const getIdeaById = async (req, res) => {
try {
const { ideaId } = req.params;
const idea = await Ideas.findById(ideaId).populate('collaborators', 'name email role').select('-voters'); // Don't expose voter details
if (!idea) {
return res.status(404).json(apiResponse(404, null, 'Idea not found'));
}
res.status(200).json(apiResponse(200, idea, 'Idea retrieved successfully'));
} catch (error) {
console.error('Error getting idea:', error);
res.status(500).json(apiResponse(500, null, 'Internal server error'));
}
};
// Select idea for development (admin function)
export const selectIdeaForDevelopment = async (req, res) => {
try {
const { ideaId } = req.params;
const idea = await Ideas.findById(ideaId);
if (!idea) {
return res.status(404).json(apiResponse(404, null, 'Idea not found'));
}
// Mark as selected for development
idea.status = 'selected';
idea.selectedForDevelopment = true;
idea.developmentStartDate = new Date();
await idea.save();
res.status(200).json(apiResponse(200, idea, 'Idea selected for development'));
} catch (error) {
console.error('Error selecting idea:', error);
res.status(500).json(apiResponse(500, null, 'Internal server error'));
}
};
// Join idea collaboration
export const joinIdeaCollaboration = async (req, res) => {
try {
const { ideaId } = req.params;
const { name, email, role } = req.body;
const idea = await Ideas.findById(ideaId);
if (!idea) {
return res.status(404).json(apiResponse(404, null, 'Idea not found'));
}
if (idea.status !== 'selected' && idea.status !== 'in_development') {
return res.status(400).json(apiResponse(400, null, 'This idea is not open for collaboration yet'));
}
// Check if user already joined
const existingCollaborator = idea.collaborators.find((collaborator) => collaborator.email === email);
if (existingCollaborator) {
return res.status(400).json(apiResponse(400, null, 'You have already joined this project'));
}
// Add collaborator
idea.collaborators.push({ name, email, role });
if (idea.status === 'selected') {
idea.status = 'in_development';
}
await idea.save();
res.status(200).json(apiResponse(200, idea.collaborators, 'Successfully joined the collaboration'));
} catch (error) {
console.error('Error joining collaboration:', error);
res.status(500).json(apiResponse(500, null, 'Internal server error'));
}
};
// Get submission status
export const getSubmissionStatus = async (req, res) => {
try {
const { userEmail } = req.query;
const { month, year } = Ideas.getCurrentSubmissionPeriod();
const isSubmissionOpen = Ideas.isSubmissionOpen();
let hasSubmittedThisMonth = false;
if (userEmail) {
const existingIdea = await Ideas.findOne({
submitterEmail: userEmail,
submissionMonth: month,
submissionYear: year,
});
hasSubmittedThisMonth = !!existingIdea;
}
// Calculate days left for submission
const now = new Date();
const currentDay = now.getDate();
const daysLeftForSubmission = isSubmissionOpen ? 7 - currentDay + 1 : 0;
res.status(200).json(
apiResponse(
200,
{
isSubmissionOpen,
submissionPeriod: `${month} ${year}`,
hasSubmittedThisMonth,
daysLeftForSubmission,
submissionEndsAt: isSubmissionOpen ? new Date(now.getFullYear(), now.getMonth(), 7, 23, 59, 59) : null,
},
'Submission status retrieved successfully',
),
);
} catch (error) {
console.error('Error getting submission status:', error);
res.status(500).json(apiResponse(500, null, 'Internal server error'));
}
};