Skip to content

Feature/adding attachments during project creation #319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 25, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions src/routes/projects/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ const createProjectValdiations = {
buildingBlockKey: Joi.string().required(),
metadata: Joi.object().optional(),
})).optional(),
attachments: Joi.array().items(Joi.object().keys({
category: Joi.string().required(),
contentType: Joi.string().required(),
description: Joi.string().allow(null).allow('').optional(),
filePath: Joi.string().required(),
size: Joi.number().required(),
title: Joi.string().required(),
})).optional(),
}).required(),
},
};
Expand Down Expand Up @@ -105,6 +113,21 @@ function createProjectAndPhases(req, project, projectTemplate, productTemplates)
});
}
return Promise.resolve(newProject);
}).then((newProject) => {
if (project.attachments && (project.attachments.length > 0)) {
req.log.debug('creating project attachments');
const attachments = project.attachments.map(attachment => Object.assign({
projectId: newProject.id,
createdBy: req.authUser.userId,
updatedBy: req.authUser.userId,
}, attachment));
return models.ProjectAttachment.bulkCreate(attachments, { returning: true }).then((projectAttachments) => {
result.attachments = _.map(projectAttachments, attachment =>
_.omit(attachment.toJSON(), ['deletedAt', 'deletedBy']));
return Promise.resolve(newProject);
});
}
return Promise.resolve(newProject);
}).then((newProject) => {
result.newProject = newProject;

Expand Down Expand Up @@ -155,7 +178,8 @@ function createProjectAndPhases(req, project, projectTemplate, productTemplates)
});
});
}));
}).then(() => Promise.resolve(result));
})
.then(() => Promise.resolve(result));
}

/**
Expand Down Expand Up @@ -263,6 +287,7 @@ module.exports = [
let newProject = null;
let newPhases;
let projectEstimations;
let projectAttachments;
models.sequelize.transaction(() => {
req.log.debug('Create Project - Starting transaction');
// Validate the templates
Expand All @@ -276,6 +301,7 @@ module.exports = [
newProject = createdProjectAndPhases.newProject;
newPhases = createdProjectAndPhases.newPhases;
projectEstimations = createdProjectAndPhases.estimations;
projectAttachments = createdProjectAndPhases.attachments;

req.log.debug('new project created (id# %d, name: %s)', newProject.id, newProject.name);
// create direct project with name and description
Expand Down Expand Up @@ -316,8 +342,8 @@ module.exports = [
newProject = newProject.get({ plain: true });
// remove utm details & deletedAt field
newProject = _.omit(newProject, ['deletedAt', 'utm']);
// add an empty attachments array
newProject.attachments = [];
// adds the project attachments, if any
newProject.attachments = projectAttachments;
// set phases array
newProject.phases = newPhases;
// sets estimations array
Expand Down