Skip to content

Commit 36d1ab0

Browse files
committed
Adjustments to error handling, additional comments.
1 parent 07a4b6e commit 36d1ab0

File tree

1 file changed

+44
-22
lines changed

1 file changed

+44
-22
lines changed

server/controllers/project.controller.js

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,32 @@ export {
2121
} from './project.controller/getProjectsForUser';
2222

2323
export async function updateProject(req, res) {
24-
const project = await Project.findById(req.params.project_id).exec();
25-
if (!project.user.equals(req.user._id)) {
26-
res.status(403).send({
27-
success: false,
28-
message: 'Session does not match owner of project.'
29-
});
30-
return;
31-
}
32-
if (
33-
req.body.updatedAt &&
34-
isAfter(new Date(project.updatedAt), new Date(req.body.updatedAt))
35-
) {
36-
res.status(409).send({
37-
success: false,
38-
message: 'Attempted to save stale version of project.'
39-
});
40-
return;
41-
}
4224
try {
25+
const project = await Project.findById(req.params.project_id).exec();
26+
if (!project) {
27+
res.status(404).send({
28+
success: false,
29+
message: 'Project with that id does not exist.'
30+
});
31+
return;
32+
}
33+
if (!project.user.equals(req.user._id)) {
34+
res.status(403).send({
35+
success: false,
36+
message: 'Session does not match owner of project.'
37+
});
38+
return;
39+
}
40+
if (
41+
req.body.updatedAt &&
42+
isAfter(new Date(project.updatedAt), new Date(req.body.updatedAt))
43+
) {
44+
res.status(409).send({
45+
success: false,
46+
message: 'Attempted to save stale version of project.'
47+
});
48+
return;
49+
}
4350
const updatedProject = await Project.findByIdAndUpdate(
4451
req.params.project_id,
4552
{
@@ -68,7 +75,8 @@ export async function updateProject(req, res) {
6875
res.json(updatedProject);
6976
}
7077
} catch (error) {
71-
res.status(400).json({ success: false });
78+
console.error(error);
79+
res.status(500).json({ success: false });
7280
}
7381
}
7482

@@ -152,7 +160,7 @@ export async function projectExists(projectId) {
152160

153161
/**
154162
* @param {string} username
155-
* @param {string} projectId
163+
* @param {string} projectId - the database id or the slug or the project
156164
* @return {Promise<boolean>}
157165
*/
158166
export async function projectForUserExists(username, projectId) {
@@ -165,12 +173,18 @@ export async function projectForUserExists(username, projectId) {
165173
return project != null;
166174
}
167175

176+
/**
177+
* Adds URLs referenced in <script> tags to the `files` array of the project
178+
* so that they can be downloaded along with other remote files from S3.
179+
* @param {object} project
180+
* @void - modifies the `project` parameter
181+
*/
168182
function bundleExternalLibs(project) {
169183
const indexHtml = project.files.find((file) => file.name === 'index.html');
170184
const { window } = new JSDOM(indexHtml.content);
171185
const scriptTags = window.document.getElementsByTagName('script');
172186

173-
Object.values(scriptTags).forEach(async ({ src }, i) => {
187+
Object.values(scriptTags).forEach(({ src }) => {
174188
if (!isUrl(src)) return;
175189

176190
const path = src.split('/');
@@ -186,6 +200,13 @@ function bundleExternalLibs(project) {
186200
});
187201
}
188202

203+
/**
204+
* Recursively adds a file and all of its children to the JSZip instance.
205+
* @param {object} file
206+
* @param {Array<object>} files
207+
* @param {JSZip} zip
208+
* @return {Promise<void>} - modifies the `zip` parameter
209+
*/
189210
async function addFileToZip(file, files, zip) {
190211
if (file.fileType === 'folder') {
191212
const folderZip = file.name === 'root' ? zip : zip.folder(file.name);
@@ -237,9 +258,10 @@ async function buildZip(project, req, res) {
237258
}
238259

239260
export async function downloadProjectAsZip(req, res) {
240-
const project = await Project.findById(req.params.project_id);
261+
const project = await Project.findById(req.params.project_id).exec();
241262
if (!project) {
242263
res.status(404).send({ message: 'Project with that id does not exist' });
264+
return;
243265
}
244266
// save project to some path
245267
buildZip(project, req, res);

0 commit comments

Comments
 (0)