@@ -66,6 +66,14 @@ const createProjectValdiations = {
6666 buildingBlockKey : Joi . string ( ) . required ( ) ,
6767 metadata : Joi . object ( ) . optional ( ) ,
6868 } ) ) . optional ( ) ,
69+ attachments : Joi . array ( ) . items ( Joi . object ( ) . keys ( {
70+ category : Joi . string ( ) . required ( ) ,
71+ contentType : Joi . string ( ) . required ( ) ,
72+ description : Joi . string ( ) . allow ( null ) . allow ( '' ) . optional ( ) ,
73+ filePath : Joi . string ( ) . required ( ) ,
74+ size : Joi . number ( ) . required ( ) ,
75+ title : Joi . string ( ) . required ( ) ,
76+ } ) ) . optional ( ) ,
6977 } ) . required ( ) ,
7078 } ,
7179} ;
@@ -105,6 +113,21 @@ function createProjectAndPhases(req, project, projectTemplate, productTemplates)
105113 } ) ;
106114 }
107115 return Promise . resolve ( newProject ) ;
116+ } ) . then ( ( newProject ) => {
117+ if ( project . attachments && ( project . attachments . length > 0 ) ) {
118+ req . log . debug ( 'creating project attachments' ) ;
119+ const attachments = project . attachments . map ( attachment => Object . assign ( {
120+ projectId : newProject . id ,
121+ createdBy : req . authUser . userId ,
122+ updatedBy : req . authUser . userId ,
123+ } , attachment ) ) ;
124+ return models . ProjectAttachment . bulkCreate ( attachments , { returning : true } ) . then ( ( projectAttachments ) => {
125+ result . attachments = _ . map ( projectAttachments , attachment =>
126+ _ . omit ( attachment . toJSON ( ) , [ 'deletedAt' , 'deletedBy' ] ) ) ;
127+ return Promise . resolve ( newProject ) ;
128+ } ) ;
129+ }
130+ return Promise . resolve ( newProject ) ;
108131 } ) . then ( ( newProject ) => {
109132 result . newProject = newProject ;
110133
@@ -155,7 +178,8 @@ function createProjectAndPhases(req, project, projectTemplate, productTemplates)
155178 } ) ;
156179 } ) ;
157180 } ) ) ;
158- } ) . then ( ( ) => Promise . resolve ( result ) ) ;
181+ } )
182+ . then ( ( ) => Promise . resolve ( result ) ) ;
159183}
160184
161185/**
@@ -263,6 +287,7 @@ module.exports = [
263287 let newProject = null ;
264288 let newPhases ;
265289 let projectEstimations ;
290+ let projectAttachments ;
266291 models . sequelize . transaction ( ( ) => {
267292 req . log . debug ( 'Create Project - Starting transaction' ) ;
268293 // Validate the templates
@@ -276,6 +301,7 @@ module.exports = [
276301 newProject = createdProjectAndPhases . newProject ;
277302 newPhases = createdProjectAndPhases . newPhases ;
278303 projectEstimations = createdProjectAndPhases . estimations ;
304+ projectAttachments = createdProjectAndPhases . attachments ;
279305
280306 req . log . debug ( 'new project created (id# %d, name: %s)' , newProject . id , newProject . name ) ;
281307 // create direct project with name and description
@@ -316,8 +342,8 @@ module.exports = [
316342 newProject = newProject . get ( { plain : true } ) ;
317343 // remove utm details & deletedAt field
318344 newProject = _ . omit ( newProject , [ 'deletedAt' , 'utm' ] ) ;
319- // add an empty attachments array
320- newProject . attachments = [ ] ;
345+ // adds the project attachments, if any
346+ newProject . attachments = projectAttachments ;
321347 // set phases array
322348 newProject . phases = newPhases ;
323349 // sets estimations array
0 commit comments