| 1 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  * StatusType Service API router | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @since 1.0.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 7 | 4 | View Code Duplication | const Joi = require('joi'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 | 4 |  | const StatusType = require('./../repository/StatusType'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 | 4 |  | const config = require('../config/server.config').url; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 | 4 |  | const dateUtil = require('../common/date-util'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 | 4 |  | const NotifierError = require('../common/Error'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 | 4 |  | const logger = require('winston'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 | 4 |  | module.exports = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |   { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     method: 'GET', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     path: `${config.statusTypeApiPrefix}`, | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 18 |  |  |     handler: (request, reply) => { | 
            
                                                                        
                            
            
                                    
            
            
                | 19 | 1 |  |       StatusType.find().then((list) => { | 
            
                                                                        
                            
            
                                    
            
            
                | 20 | 1 |  |         reply({ | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |           data: dateUtil.formatDates(list), | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |           totalCount: list.length, | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |         }); | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |       }).catch(err => reply(err)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |   }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |   { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     method: 'POST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     path: `${config.statusTypeApiPrefix}`, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     handler: (request, reply) => { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 3 |  |       const statusType = Object.assign({}, request.payload); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 3 |  |       StatusType.find({ value: statusType.value }) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         .then((result) => { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 3 |  |           if (result && result.length > 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 1 |  |             throw new NotifierError(NotifierError.Types.CONFLICT, { value: statusType.value }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |           } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 2 |  |           return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         }) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 2 |  |         .then(() => StatusType.add(statusType)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 2 |  |         .then(result => reply(result)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 1 |  |         .catch(err => reply(err)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     config: { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |       validate: { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         payload: { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |           label: Joi.string().required(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |           value: Joi.string().required(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |           template: Joi.string(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |       }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |   }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |   { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     method: 'PUT', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     path: `${config.statusTypeApiPrefix}/{statusTypeId}`, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     handler: (request, reply) => { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 4 |  |       const statusType = Object.assign({}, request.payload); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |       let unset; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 4 |  |       if (!statusType.template) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 2 |  |         unset = { template: 1 }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |       } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 4 |  |       StatusType.find({ value: statusType.value }) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         .then((result) => { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 4 |  |           if (result && result.length > 0 && request.params.statusTypeId !== result[0]._id) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 1 |  |             throw new NotifierError(NotifierError.Types.CONFLICT, { value: statusType.value }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |           } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 3 |  |           return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         }) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 3 |  |         .then(() => StatusType.update(request.params.statusTypeId, statusType, unset)) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 3 |  |         .then(result => reply(result)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 1 |  |         .catch(err => reply(err)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     config: { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |       validate: { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         params: { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |           statusTypeId: Joi.string().required(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         payload: { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |           label: Joi.string().required(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |           value: Joi.string().required(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |           template: Joi.string(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |       }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |   }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |   { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     method: 'DELETE', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     path: `${config.statusTypeApiPrefix}/{statusTypeId}`, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     handler: (request, reply) => { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 2 |  |       StatusType.remove(request.params.statusTypeId) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 2 |  |         .then(result => reply(result)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         .catch(err => reply(err)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     config: { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |       validate: { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         params: { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |           statusTypeId: Joi.string().required(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |       }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |   }, | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 102 |  |  | ]; | 
            
                                                        
            
                                    
            
            
                | 103 |  |  |  |