-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathcreateProject.js
More file actions
107 lines (88 loc) · 2.75 KB
/
Copy pathcreateProject.js
File metadata and controls
107 lines (88 loc) · 2.75 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
import Project from '../../models/project';
import {
toModel,
FileValidationError,
ProjectValidationError
} from '../../domain-objects/Project';
import {
commitPendingAssets,
rewritePendingFileUrls
} from '../../utils/pendingAssets';
export default async function createProject(req, res) {
try {
const projectValues = Object.assign({}, req.body, { user: req.user._id });
if (projectValues.files) {
await commitPendingAssets(req.user.id, projectValues.files);
projectValues.files = rewritePendingFileUrls(
projectValues.files,
req.user.id
);
}
const newProject = await Project.create(projectValues);
const newProjectWithUser = await Project.populate(newProject, {
path: 'user',
select: 'username'
});
res.json(newProjectWithUser);
} catch (err) {
res.status(400).json({ success: false });
}
}
// TODO: What happens if you don't supply any files?
export async function apiCreateProject(req, res) {
const params = Object.assign({}, req.body, { user: req.user._id });
const sendValidationErrors = (err, type, code = 422) => {
res.status(code).json({
message: `${type} Validation Failed`,
detail: err.message,
errors: err.files
});
};
// TODO: Error handling to match spec
const sendFailure = (err) => {
res.status(500).end();
};
const handleErrors = (err) => {
if (err instanceof FileValidationError) {
sendValidationErrors(err, 'File', err.code);
} else if (err instanceof ProjectValidationError) {
sendValidationErrors(err, 'Sketch', err.code);
} else {
sendFailure();
}
};
const checkUserHasPermission = () => {
if (req.user.username !== req.params.username) {
console.warn('no permission');
const error = new ProjectValidationError(
`'${req.user.username}' does not have permission to create for '${req.params.username}'`
);
error.code = 401;
throw error;
}
};
try {
checkUserHasPermission();
if (!params.files || typeof params.files !== 'object') {
const error = new FileValidationError("'files' must be an object");
throw error;
}
const model = toModel(params);
const { isUnique, conflictingIds } = await model.isSlugUnique();
if (!isUnique) {
const error = new ProjectValidationError(
`Slug "${model.slug}" is not unique. Check ${conflictingIds.join(', ')}`
);
error.code = 409;
throw error;
}
if (model.files) {
await commitPendingAssets(req.user.id, model.files);
model.files = rewritePendingFileUrls(model.files, req.user.id);
}
const newProject = await model.save();
res.status(201).json({ id: newProject.id });
} catch (err) {
handleErrors(err);
}
}