@@ -66,6 +66,14 @@ const createProjectValdiations = {
66
66
buildingBlockKey : Joi . string ( ) . required ( ) ,
67
67
metadata : Joi . object ( ) . optional ( ) ,
68
68
} ) ) . 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 ( ) ,
69
77
} ) . required ( ) ,
70
78
} ,
71
79
} ;
@@ -105,6 +113,21 @@ function createProjectAndPhases(req, project, projectTemplate, productTemplates)
105
113
} ) ;
106
114
}
107
115
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 ) ;
108
131
} ) . then ( ( newProject ) => {
109
132
result . newProject = newProject ;
110
133
@@ -155,7 +178,8 @@ function createProjectAndPhases(req, project, projectTemplate, productTemplates)
155
178
} ) ;
156
179
} ) ;
157
180
} ) ) ;
158
- } ) . then ( ( ) => Promise . resolve ( result ) ) ;
181
+ } )
182
+ . then ( ( ) => Promise . resolve ( result ) ) ;
159
183
}
160
184
161
185
/**
@@ -263,6 +287,7 @@ module.exports = [
263
287
let newProject = null ;
264
288
let newPhases ;
265
289
let projectEstimations ;
290
+ let projectAttachments ;
266
291
models . sequelize . transaction ( ( ) => {
267
292
req . log . debug ( 'Create Project - Starting transaction' ) ;
268
293
// Validate the templates
@@ -276,6 +301,7 @@ module.exports = [
276
301
newProject = createdProjectAndPhases . newProject ;
277
302
newPhases = createdProjectAndPhases . newPhases ;
278
303
projectEstimations = createdProjectAndPhases . estimations ;
304
+ projectAttachments = createdProjectAndPhases . attachments ;
279
305
280
306
req . log . debug ( 'new project created (id# %d, name: %s)' , newProject . id , newProject . name ) ;
281
307
// create direct project with name and description
@@ -316,8 +342,8 @@ module.exports = [
316
342
newProject = newProject . get ( { plain : true } ) ;
317
343
// remove utm details & deletedAt field
318
344
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 ;
321
347
// set phases array
322
348
newProject . phases = newPhases ;
323
349
// sets estimations array
0 commit comments