2019-06-12 09:27:28 +00:00
|
|
|
import Project from '../../models/project';
|
2019-08-30 18:26:57 +00:00
|
|
|
import { toModel, FileValidationError, ProjectValidationError } from '../../domain-objects/Project';
|
2019-06-12 09:27:28 +00:00
|
|
|
|
|
|
|
export default function createProject(req, res) {
|
|
|
|
let projectValues = {
|
|
|
|
user: req.user._id
|
|
|
|
};
|
|
|
|
|
|
|
|
projectValues = Object.assign(projectValues, req.body);
|
|
|
|
|
|
|
|
function sendFailure() {
|
2020-08-03 20:42:28 +00:00
|
|
|
res.status(400).json({ success: false });
|
2019-06-12 09:27:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function populateUserData(newProject) {
|
|
|
|
return Project.populate(
|
|
|
|
newProject,
|
|
|
|
{ path: 'user', select: 'username' },
|
|
|
|
(err, newProjectWithUser) => {
|
|
|
|
if (err) {
|
|
|
|
sendFailure();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
res.json(newProjectWithUser);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return Project.create(projectValues)
|
|
|
|
.then(populateUserData)
|
|
|
|
.catch(sendFailure);
|
|
|
|
}
|
2019-08-30 18:26:57 +00:00
|
|
|
|
|
|
|
// TODO: What happens if you don't supply any files?
|
|
|
|
export function apiCreateProject(req, res) {
|
|
|
|
const params = Object.assign({ user: req.user._id }, req.body);
|
|
|
|
|
|
|
|
function 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
|
|
|
|
function sendFailure(err) {
|
|
|
|
res.status(500).end();
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleErrors(err) {
|
|
|
|
if (err instanceof FileValidationError) {
|
|
|
|
sendValidationErrors(err, 'File', err.code);
|
|
|
|
} else if (err instanceof ProjectValidationError) {
|
|
|
|
sendValidationErrors(err, 'Sketch', err.code);
|
|
|
|
} else {
|
|
|
|
sendFailure();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkUserHasPermission() {
|
|
|
|
if (req.user.username !== req.params.username) {
|
|
|
|
console.log('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();
|
|
|
|
|
|
|
|
const model = toModel(params);
|
|
|
|
|
|
|
|
return model.isSlugUnique()
|
|
|
|
.then(({ isUnique, conflictingIds }) => {
|
|
|
|
if (isUnique) {
|
|
|
|
return model.save()
|
|
|
|
.then((newProject) => {
|
|
|
|
res.status(201).json({ id: newProject.id });
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const error = new ProjectValidationError(`Slug "${model.slug}" is not unique. Check ${conflictingIds.join(', ')}`);
|
|
|
|
error.code = 409;
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
})
|
|
|
|
.then(checkUserHasPermission)
|
|
|
|
.catch(handleErrors);
|
|
|
|
} catch (err) {
|
|
|
|
handleErrors(err);
|
|
|
|
return Promise.reject(err);
|
|
|
|
}
|
|
|
|
}
|