|
| 1 | + |
| 2 | +/* globals Promise */ |
| 3 | + |
| 4 | +import _ from 'lodash'; |
| 5 | +import config from 'config'; |
| 6 | +import { middleware as tcMiddleware } from 'tc-core-library-js'; |
| 7 | +import util from '../../util'; |
| 8 | + |
| 9 | +/** |
| 10 | +/** |
| 11 | + * API to handle retrieving a single project by id |
| 12 | + * |
| 13 | + * Permissions: |
| 14 | + * Only users that have access to the project can retrieve it. |
| 15 | + * |
| 16 | + */ |
| 17 | + |
| 18 | +// var permissions = require('tc-core-library-js').middleware.permissions |
| 19 | +const permissions = tcMiddleware.permissions; |
| 20 | +const ES_PROJECT_INDEX = config.get('elasticsearchConfig.indexName'); |
| 21 | +const ES_PROJECT_TYPE = config.get('elasticsearchConfig.docType'); |
| 22 | + |
| 23 | +/** |
| 24 | + * Get the request body for the specified index name |
| 25 | + * @private |
| 26 | + * |
| 27 | + * @param {String} indexName the index name |
| 28 | + * @param {String} docType document type |
| 29 | + * @return {Object} the request body for the specified index name |
| 30 | + */ |
| 31 | +function getRequestBody(indexName, docType) { |
| 32 | + const projectMapping = { |
| 33 | + _all: { enabled: false }, |
| 34 | + properties: { |
| 35 | + actualPrice: { |
| 36 | + type: 'double', |
| 37 | + }, |
| 38 | + attachments: { |
| 39 | + type: 'nested', |
| 40 | + properties: { |
| 41 | + category: { |
| 42 | + type: 'string', |
| 43 | + index: 'not_analyzed', |
| 44 | + }, |
| 45 | + contentType: { |
| 46 | + type: 'string', |
| 47 | + index: 'not_analyzed', |
| 48 | + }, |
| 49 | + createdAt: { |
| 50 | + type: 'date', |
| 51 | + format: 'strict_date_optional_time||epoch_millis', |
| 52 | + }, |
| 53 | + createdBy: { |
| 54 | + type: 'integer', |
| 55 | + }, |
| 56 | + description: { |
| 57 | + type: 'string', |
| 58 | + }, |
| 59 | + filePath: { |
| 60 | + type: 'string', |
| 61 | + }, |
| 62 | + id: { |
| 63 | + type: 'long', |
| 64 | + }, |
| 65 | + projectId: { |
| 66 | + type: 'long', |
| 67 | + }, |
| 68 | + size: { |
| 69 | + type: 'double', |
| 70 | + }, |
| 71 | + title: { |
| 72 | + type: 'string', |
| 73 | + }, |
| 74 | + updatedAt: { |
| 75 | + type: 'date', |
| 76 | + format: 'strict_date_optional_time||epoch_millis', |
| 77 | + }, |
| 78 | + updatedBy: { |
| 79 | + type: 'integer', |
| 80 | + }, |
| 81 | + }, |
| 82 | + }, |
| 83 | + billingAccountId: { |
| 84 | + type: 'long', |
| 85 | + }, |
| 86 | + bookmarks: { |
| 87 | + type: 'nested', |
| 88 | + properties: { |
| 89 | + address: { |
| 90 | + type: 'string', |
| 91 | + }, |
| 92 | + title: { |
| 93 | + type: 'string', |
| 94 | + }, |
| 95 | + }, |
| 96 | + }, |
| 97 | + cancelReason: { |
| 98 | + type: 'string', |
| 99 | + }, |
| 100 | + challengeEligibility: { |
| 101 | + type: 'nested', |
| 102 | + properties: { |
| 103 | + groups: { |
| 104 | + type: 'long', |
| 105 | + }, |
| 106 | + role: { |
| 107 | + type: 'string', |
| 108 | + index: 'not_analyzed', |
| 109 | + }, |
| 110 | + users: { |
| 111 | + type: 'long', |
| 112 | + }, |
| 113 | + }, |
| 114 | + }, |
| 115 | + createdAt: { |
| 116 | + type: 'date', |
| 117 | + format: 'strict_date_optional_time||epoch_millis', |
| 118 | + }, |
| 119 | + createdBy: { |
| 120 | + type: 'integer', |
| 121 | + }, |
| 122 | + description: { |
| 123 | + type: 'string', |
| 124 | + }, |
| 125 | + details: { |
| 126 | + type: 'nested', |
| 127 | + properties: { |
| 128 | + TBD_features: { |
| 129 | + type: 'nested', |
| 130 | + properties: { |
| 131 | + description: { |
| 132 | + type: 'string', |
| 133 | + }, |
| 134 | + id: { |
| 135 | + type: 'integer', |
| 136 | + }, |
| 137 | + isCustom: { |
| 138 | + type: 'boolean', |
| 139 | + }, |
| 140 | + title: { |
| 141 | + type: 'string', |
| 142 | + }, |
| 143 | + }, |
| 144 | + }, |
| 145 | + TBD_usageDescription: { |
| 146 | + type: 'string', |
| 147 | + }, |
| 148 | + appDefinition: { |
| 149 | + properties: { |
| 150 | + goal: { |
| 151 | + properties: { |
| 152 | + value: { |
| 153 | + type: 'string', |
| 154 | + }, |
| 155 | + }, |
| 156 | + }, |
| 157 | + primaryTarget: { |
| 158 | + type: 'string', |
| 159 | + }, |
| 160 | + users: { |
| 161 | + properties: { |
| 162 | + value: { |
| 163 | + type: 'string', |
| 164 | + }, |
| 165 | + }, |
| 166 | + }, |
| 167 | + }, |
| 168 | + }, |
| 169 | + hideDiscussions: { |
| 170 | + type: 'boolean', |
| 171 | + }, |
| 172 | + products: { |
| 173 | + type: 'string', |
| 174 | + }, |
| 175 | + summary: { |
| 176 | + type: 'string', |
| 177 | + }, |
| 178 | + utm: { |
| 179 | + type: 'nested', |
| 180 | + properties: { |
| 181 | + code: { |
| 182 | + type: 'string', |
| 183 | + }, |
| 184 | + }, |
| 185 | + }, |
| 186 | + }, |
| 187 | + }, |
| 188 | + directProjectId: { |
| 189 | + type: 'long', |
| 190 | + }, |
| 191 | + estimatedPrice: { |
| 192 | + type: 'double', |
| 193 | + }, |
| 194 | + external: { |
| 195 | + properties: { |
| 196 | + data: { |
| 197 | + type: 'string', |
| 198 | + }, |
| 199 | + id: { |
| 200 | + type: 'string', |
| 201 | + index: 'not_analyzed', |
| 202 | + }, |
| 203 | + type: { |
| 204 | + type: 'string', |
| 205 | + index: 'not_analyzed', |
| 206 | + }, |
| 207 | + }, |
| 208 | + }, |
| 209 | + id: { |
| 210 | + type: 'long', |
| 211 | + }, |
| 212 | + members: { |
| 213 | + type: 'nested', |
| 214 | + properties: { |
| 215 | + createdAt: { |
| 216 | + type: 'date', |
| 217 | + format: 'strict_date_optional_time||epoch_millis', |
| 218 | + }, |
| 219 | + createdBy: { |
| 220 | + type: 'integer', |
| 221 | + }, |
| 222 | + email: { |
| 223 | + type: 'string', |
| 224 | + index: 'not_analyzed', |
| 225 | + }, |
| 226 | + firstName: { |
| 227 | + type: 'string', |
| 228 | + }, |
| 229 | + handle: { |
| 230 | + type: 'string', |
| 231 | + index: 'not_analyzed', |
| 232 | + }, |
| 233 | + id: { |
| 234 | + type: 'long', |
| 235 | + }, |
| 236 | + isPrimary: { |
| 237 | + type: 'boolean', |
| 238 | + }, |
| 239 | + lastName: { |
| 240 | + type: 'string', |
| 241 | + }, |
| 242 | + projectId: { |
| 243 | + type: 'long', |
| 244 | + }, |
| 245 | + role: { |
| 246 | + type: 'string', |
| 247 | + index: 'not_analyzed', |
| 248 | + }, |
| 249 | + updatedAt: { |
| 250 | + type: 'date', |
| 251 | + format: 'strict_date_optional_time||epoch_millis', |
| 252 | + }, |
| 253 | + updatedBy: { |
| 254 | + type: 'integer', |
| 255 | + }, |
| 256 | + userId: { |
| 257 | + type: 'long', |
| 258 | + }, |
| 259 | + }, |
| 260 | + }, |
| 261 | + name: { |
| 262 | + type: 'string', |
| 263 | + }, |
| 264 | + status: { |
| 265 | + type: 'string', |
| 266 | + index: 'not_analyzed', |
| 267 | + }, |
| 268 | + terms: { |
| 269 | + type: 'integer', |
| 270 | + }, |
| 271 | + type: { |
| 272 | + type: 'string', |
| 273 | + index: 'not_analyzed', |
| 274 | + }, |
| 275 | + updatedAt: { |
| 276 | + type: 'date', |
| 277 | + format: 'strict_date_optional_time||epoch_millis', |
| 278 | + }, |
| 279 | + updatedBy: { |
| 280 | + type: 'integer', |
| 281 | + }, |
| 282 | + utm: { |
| 283 | + properties: { |
| 284 | + campaign: { |
| 285 | + type: 'string', |
| 286 | + }, |
| 287 | + medium: { |
| 288 | + type: 'string', |
| 289 | + }, |
| 290 | + source: { |
| 291 | + type: 'string', |
| 292 | + }, |
| 293 | + }, |
| 294 | + }, |
| 295 | + }, |
| 296 | + }; |
| 297 | + const result = { |
| 298 | + index: indexName, |
| 299 | + updateAllTypes: true, |
| 300 | + body: { |
| 301 | + mappings: { }, |
| 302 | + }, |
| 303 | + }; |
| 304 | + result.body.mappings[docType] = projectMapping; |
| 305 | + return result; |
| 306 | +} |
| 307 | + |
| 308 | + |
| 309 | +module.exports = [ |
| 310 | + permissions('project.admin'), |
| 311 | + /** |
| 312 | + * GET projects/{projectId} |
| 313 | + * Get a project by id |
| 314 | + */ |
| 315 | + (req, res, next) => { // eslint-disable-line no-unused-vars |
| 316 | + const logger = req.log; |
| 317 | + logger.debug('Entered Admin#createIndex'); |
| 318 | + const indexName = _.get(req, 'body.param.indexName', ES_PROJECT_INDEX); |
| 319 | + const docType = _.get(req, 'body.param.docType', ES_PROJECT_TYPE); |
| 320 | + logger.debug('indexName', indexName); |
| 321 | + logger.debug('docType', docType); |
| 322 | + |
| 323 | + const esClient = util.getElasticSearchClient(); |
| 324 | + esClient.indices.create(getRequestBody(indexName, docType)); |
| 325 | + res.status(200).json(util.wrapResponse(req.id, { message: 'Create index request successfully submitted' })); |
| 326 | + }, |
| 327 | +]; |
0 commit comments