|
| 1 | +/** |
| 2 | + * Metadata extractor |
| 3 | + */ |
| 4 | +const _ = require('lodash') |
| 5 | +const { prizeSetTypes } = require('../constants') |
| 6 | + |
| 7 | +/** |
| 8 | + * Get metadata entry by key |
| 9 | + * @param {Array} metadata the metadata array |
| 10 | + * @param {String} key the metadata key |
| 11 | + */ |
| 12 | +const getMeta = (metadata = [], key) => _.find(metadata, meta => meta.name === key) |
| 13 | + |
| 14 | +/** |
| 15 | + * Convert string to bool |
| 16 | + * @param {String} v the value |
| 17 | + */ |
| 18 | +const toBool = v => _.toString(v).toLowerCase() === 'true' |
| 19 | + |
| 20 | +/** |
| 21 | + * Extract billing project |
| 22 | + * @param {Object} challenge the challenge object |
| 23 | + * @param {Any} defaultValue the default value |
| 24 | + */ |
| 25 | +function extractBillingProject (challenge, defaultValue) { |
| 26 | + return _.get(challenge, 'billingAccountId', _.get(challenge, 'billing.billingAccountId', defaultValue)) |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * Extract submission limit |
| 31 | + * @param {Object} challenge the challenge object |
| 32 | + * @param {Any} defaultValue the default value |
| 33 | + */ |
| 34 | +function extractSubmissionLimit (challenge, defaultValue) { |
| 35 | + const entry = getMeta(challenge.metadata, 'submissionLimit') |
| 36 | + if (!entry) return defaultValue |
| 37 | + try { |
| 38 | + const parsedEntryValue = JSON.parse(entry.value) |
| 39 | + if (parsedEntryValue.limit) { |
| 40 | + entry.value = parsedEntryValue.count |
| 41 | + } else { |
| 42 | + entry.value = null |
| 43 | + } |
| 44 | + } catch (e) { |
| 45 | + entry.value = null |
| 46 | + } |
| 47 | + return entry.value || defaultValue |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * Extract spec review cost |
| 52 | + * @param {Object} challenge the challenge object |
| 53 | + * @param {Any} defaultValue the default value |
| 54 | + */ |
| 55 | +function extractSpecReviewCost (challenge, defaultValue) { |
| 56 | + return _.get(_.find(_.get(challenge, 'prizeSets', []), p => p.type === prizeSetTypes.SpecReview) || {}, 'prizes[0].value', defaultValue) |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * Extract DR points |
| 61 | + * @param {Object} challenge the challenge object |
| 62 | + * @param {Any} defaultValue the default value |
| 63 | + */ |
| 64 | +function extractDrPoints (challenge, defaultValue) { |
| 65 | + const entry = getMeta(challenge.metadata, 'drPoints') |
| 66 | + if (!entry) return defaultValue |
| 67 | + return entry.value || defaultValue |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Extract Approval required |
| 72 | + * @param {Object} challenge the challenge object |
| 73 | + * @param {Any} defaultValue the default value |
| 74 | + */ |
| 75 | +function extractApprovalRequired (challenge, defaultValue) { |
| 76 | + const entry = getMeta(challenge.metadata, 'approvalRequired') |
| 77 | + if (!entry) return defaultValue |
| 78 | + return toBool(entry.value) |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * Extract Post-mortem required |
| 83 | + * @param {Object} challenge the challenge object |
| 84 | + * @param {Any} defaultValue the default value |
| 85 | + */ |
| 86 | +function extractPostMortemRequired (challenge, defaultValue) { |
| 87 | + const entry = getMeta(challenge.metadata, 'postMortemRequired') |
| 88 | + if (!entry) return defaultValue |
| 89 | + return toBool(entry.value) |
| 90 | +} |
| 91 | + |
| 92 | +/** |
| 93 | + * Extract track late deliverables required |
| 94 | + * @param {Object} challenge the challenge object |
| 95 | + * @param {Any} defaultValue the default value |
| 96 | + */ |
| 97 | +function extractTrackLateDeliverablesRequired (challenge, defaultValue) { |
| 98 | + const entry = getMeta(challenge.metadata, 'trackLateDeliverables') |
| 99 | + if (!entry) return defaultValue |
| 100 | + return toBool(entry.value) |
| 101 | +} |
| 102 | + |
| 103 | +/** |
| 104 | + * Extract allow stock art required |
| 105 | + * @param {Object} challenge the challenge object |
| 106 | + * @param {Any} defaultValue the default value |
| 107 | + */ |
| 108 | +function extractAllowStockArtRequired (challenge, defaultValue) { |
| 109 | + const entry = getMeta(challenge.metadata, 'allowStockArt') |
| 110 | + if (!entry) return defaultValue |
| 111 | + return toBool(entry.value) |
| 112 | +} |
| 113 | + |
| 114 | +/** |
| 115 | + * Extract submission viewable |
| 116 | + * @param {Object} challenge the challenge object |
| 117 | + * @param {Any} defaultValue the default value |
| 118 | + */ |
| 119 | +function extractSubmissionViewable (challenge, defaultValue) { |
| 120 | + const entry = getMeta(challenge.metadata, 'submissionViewable') |
| 121 | + if (!entry) return defaultValue |
| 122 | + return toBool(entry.value) |
| 123 | +} |
| 124 | + |
| 125 | +/** |
| 126 | + * Extract review feedback |
| 127 | + * @param {Object} challenge the challenge object |
| 128 | + * @param {Any} defaultValue the default value |
| 129 | + */ |
| 130 | +function extractReviewFeedback (challenge, defaultValue) { |
| 131 | + const entry = getMeta(challenge.metadata, 'reviewFeedback') |
| 132 | + if (!entry) return defaultValue |
| 133 | + return toBool(entry.value) |
| 134 | +} |
| 135 | + |
| 136 | +/** |
| 137 | + * Extract environment |
| 138 | + * @param {Object} challenge the challenge object |
| 139 | + * @param {Any} defaultValue the default value |
| 140 | + */ |
| 141 | +function extractEnvironment (challenge, defaultValue) { |
| 142 | + const entry = getMeta(challenge.metadata, 'environment') |
| 143 | + if (!entry) return defaultValue |
| 144 | + return entry.value |
| 145 | +} |
| 146 | + |
| 147 | +/** |
| 148 | + * Extract code repo |
| 149 | + * @param {Object} challenge the challenge object |
| 150 | + * @param {Any} defaultValue the default value |
| 151 | + */ |
| 152 | +function extractCodeRepo (challenge, defaultValue) { |
| 153 | + const entry = getMeta(challenge.metadata, 'codeRepo') |
| 154 | + if (!entry) return defaultValue |
| 155 | + return entry.value |
| 156 | +} |
| 157 | + |
| 158 | +/** |
| 159 | + * Extract estimate effort hours |
| 160 | + * @param {Object} challenge the challenge object |
| 161 | + * @param {Any} defaultValue the default value |
| 162 | + */ |
| 163 | +function extractEstimateEffortHours (challenge, defaultValue) { |
| 164 | + const entry = getMeta(challenge.metadata, 'effortHoursEstimate') |
| 165 | + if (!entry) return defaultValue |
| 166 | + return _.toNumber(entry.value) |
| 167 | +} |
| 168 | + |
| 169 | +/** |
| 170 | + * Extract estimate effort days offshore |
| 171 | + * @param {Object} challenge the challenge object |
| 172 | + * @param {Any} defaultValue the default value |
| 173 | + */ |
| 174 | +function extractEstimateEffortOffshore (challenge, defaultValue) { |
| 175 | + const entry = getMeta(challenge.metadata, 'effortHoursOffshore') |
| 176 | + if (!entry) return defaultValue |
| 177 | + return _.toNumber(entry.value) |
| 178 | +} |
| 179 | + |
| 180 | +/** |
| 181 | + * Extract estimate effort days Onsite |
| 182 | + * @param {Object} challenge the challenge object |
| 183 | + * @param {Any} defaultValue the default value |
| 184 | + */ |
| 185 | +function extractEstimateEffortOnsite (challenge, defaultValue) { |
| 186 | + const entry = getMeta(challenge.metadata, 'effortHoursOnshore') |
| 187 | + if (!entry) return defaultValue |
| 188 | + return _.toNumber(entry.value) |
| 189 | +} |
| 190 | + |
| 191 | +module.exports = { |
| 192 | + extractBillingProject, |
| 193 | + extractSubmissionLimit, |
| 194 | + extractSpecReviewCost, |
| 195 | + extractDrPoints, |
| 196 | + extractApprovalRequired, |
| 197 | + extractPostMortemRequired, |
| 198 | + extractTrackLateDeliverablesRequired, |
| 199 | + extractAllowStockArtRequired, |
| 200 | + extractSubmissionViewable, |
| 201 | + extractReviewFeedback, |
| 202 | + extractEnvironment, |
| 203 | + extractCodeRepo, |
| 204 | + extractEstimateEffortHours, |
| 205 | + extractEstimateEffortOffshore, |
| 206 | + extractEstimateEffortOnsite |
| 207 | +} |
0 commit comments