@@ -9,35 +9,44 @@ import { STATUS_HISTORY_REFERENCES } from '../constants';
99 * NOTE that this function mutates milestone
1010 *
1111 * @param {Array|Object } milestone one milestone or list of milestones
12+ * @param {Object } options options which has been used to call main method
1213 *
1314 * @returns {Promise } promise
1415 */
15- const populateWithStatusHistory = async ( milestone ) => {
16+ const populateWithStatusHistory = async ( milestone , options ) => {
17+ // depend on this option `milestone` is a sequlize ORM object or plain JS object
18+ const isRaw = ! ! _ . get ( options , 'raw' ) ;
19+ const getMilestoneId = m => (
20+ isRaw ? m . id : m . dataValues . id
21+ ) ;
22+ const formatMilestone = statusHistory => (
23+ isRaw ? { statusHistory } : { dataValues : { statusHistory } }
24+ ) ;
1625 if ( Array . isArray ( milestone ) ) {
1726 const allStatusHistory = await models . StatusHistory . findAll ( {
1827 where : {
19- referenceId : { $in : milestone . map ( m => m . dataValues . id ) } ,
28+ referenceId : { $in : milestone . map ( getMilestoneId ) } ,
2029 reference : 'milestone' ,
2130 } ,
2231 order : [ [ 'createdAt' , 'desc' ] ] ,
2332 raw : true ,
2433 } ) ;
2534
2635 return milestone . map ( ( m , index ) => {
27- const statusHistory = allStatusHistory . filter ( s => s . referenceId === m . dataValues . id ) ;
28- return _ . merge ( milestone [ index ] , { dataValues : { statusHistory } } ) ;
36+ const statusHistory = _ . filter ( allStatusHistory , { referenceId : getMilestoneId ( m ) } ) ;
37+ return _ . merge ( milestone [ index ] , formatMilestone ( statusHistory ) ) ;
2938 } ) ;
3039 }
3140
3241 const statusHistory = await models . StatusHistory . findAll ( {
3342 where : {
34- referenceId : milestone . dataValues . id ,
43+ referenceId : getMilestoneId ( milestone ) ,
3544 reference : 'milestone' ,
3645 } ,
3746 order : [ [ 'createdAt' , 'desc' ] ] ,
3847 raw : true ,
3948 } ) ;
40- return _ . merge ( milestone , { dataValues : { statusHistory } } ) ;
49+ return _ . merge ( milestone , formatMilestone ( statusHistory ) ) ;
4150} ;
4251
4352/**
@@ -131,7 +140,7 @@ module.exports = (sequelize, DataTypes) => {
131140 updatedBy : milestone . updatedBy ,
132141 } , {
133142 transaction : options . transaction ,
134- } ) . then ( ( ) => populateWithStatusHistory ( milestone ) ) ,
143+ } ) . then ( ( ) => populateWithStatusHistory ( milestone , options ) ) ,
135144
136145 afterBulkCreate : ( milestones , options ) => {
137146 const listStatusHistory = milestones . map ( ( { dataValues } ) => ( {
@@ -145,7 +154,7 @@ module.exports = (sequelize, DataTypes) => {
145154
146155 return models . StatusHistory . bulkCreate ( listStatusHistory , {
147156 transaction : options . transaction ,
148- } ) . then ( ( ) => populateWithStatusHistory ( milestones ) ) ;
157+ } ) . then ( ( ) => populateWithStatusHistory ( milestones , options ) ) ;
149158 } ,
150159
151160 afterUpdate : ( milestone , options ) => {
@@ -161,12 +170,12 @@ module.exports = (sequelize, DataTypes) => {
161170 transaction : options . transaction ,
162171 } ) . then ( ( ) => populateWithStatusHistory ( milestone ) ) ;
163172 }
164- return populateWithStatusHistory ( milestone ) ;
173+ return populateWithStatusHistory ( milestone , options ) ;
165174 } ,
166175
167- afterFind : ( milestone ) => {
176+ afterFind : ( milestone , options ) => {
168177 if ( ! milestone ) return Promise . resolve ( ) ;
169- return populateWithStatusHistory ( milestone ) ;
178+ return populateWithStatusHistory ( milestone , options ) ;
170179 } ,
171180 } ,
172181 } ) ;
0 commit comments