|
| 1 | +// import _ from 'lodash'; |
| 2 | +// import { setValue } from '../../../shared/dao'; |
| 3 | +// import { checkContext, isObjectEmpty } from '../../../utils/index'; |
| 4 | +// import constants from '../../../constants/index'; |
| 5 | +// import { validateSchema } from '../../../utils/index'; |
| 6 | +// import { logger } from '../../../shared/logger'; |
| 7 | +// import issue_subcategory from '../../issue_subcategories'; |
| 8 | +// import { |
| 9 | +// checkOrganizationNameandDomain, |
| 10 | +// compareContextTimeStampAndUpdatedAt, |
| 11 | +// compareCreatedAtAndUpdationTime, |
| 12 | +// compareUpdatedAtAndContextTimeStamp, |
| 13 | +// } from '../igmHelpers'; |
| 14 | + |
| 15 | +// const checkIssueV2 = (data: any) => { |
| 16 | +// const issueObj: any = {}; |
| 17 | +// let res: any = {}; |
| 18 | + |
| 19 | +// if (!data || isObjectEmpty(data)) { |
| 20 | +// return { [constants.RET_ISSUE]: 'JSON cannot be empty' }; |
| 21 | +// } |
| 22 | + |
| 23 | +// try { |
| 24 | +// const issue: any = data; |
| 25 | + |
| 26 | +// try { |
| 27 | +// logger.info(`Validating Schema for ${constants.ISSUE_1} API`); |
| 28 | +// const vs = validateSchema('igm', constants.ISSUE_1, issue); |
| 29 | +// if (vs !== 'error') { |
| 30 | +// Object.assign(issueObj, vs); |
| 31 | +// } |
| 32 | +// } catch (error: any) { |
| 33 | +// logger.error(`Schema validation error for /${constants.ISSUE_1}, ${error.stack}`); |
| 34 | +// } |
| 35 | + |
| 36 | +// try { |
| 37 | +// logger.info(`Checking context for ${constants.ISSUE_1} API`); |
| 38 | +// res = checkContext(issue.context, constants.RET_ISSUE); |
| 39 | +// if (!res.valid) { |
| 40 | +// Object.assign(issueObj, res.ERRORS); |
| 41 | +// } |
| 42 | +// } catch (error: any) { |
| 43 | +// logger.error(`Error checking context for /${constants.ISSUE_1}, ${error.stack}`); |
| 44 | +// } |
| 45 | + |
| 46 | +// try { |
| 47 | +// logger.info(`Storing necessary context values for /${constants.ISSUE_1}`); |
| 48 | +// setValue('igmTxnId', issue.context.transaction_id); |
| 49 | +// setValue('igmTmpstmp', issue.context.timestamp); |
| 50 | +// setValue('igmCoreVersion', issue.context.core_version); |
| 51 | +// setValue('igmDomain', issue.context.domain); |
| 52 | +// setValue('igmIssueMsgId', issue.context.message_id); |
| 53 | +// setValue('seller_bpp_id', issue.context.bpp_id); |
| 54 | +// setValue('seller_bpp_uri', issue.context.bpp_uri); |
| 55 | + |
| 56 | +// if (issue.message) { |
| 57 | +// setValue('igmIssueType', issue.message.issue.level); |
| 58 | +// } |
| 59 | +// } catch (error: any) { |
| 60 | +// logger.error(`Error storing context values for /${constants.RET_ISSUE}, ${error.stack}`); |
| 61 | +// } |
| 62 | + |
| 63 | +// try { |
| 64 | +// logger.info(`Validating issue category and subcategory for /${constants.RET_ISSUE}`); |
| 65 | +// if (!issue.message.issue.description || !issue.message.issue.description.code) { |
| 66 | +// issueObj.ctgrySubCategory = 'Missing or invalid issue description code'; |
| 67 | +// } |
| 68 | +// } catch (error: any) { |
| 69 | +// logger.error(`Error validating issue category and subcategory, ${error.stack}`); |
| 70 | +// } |
| 71 | + |
| 72 | +// checkOrganizationNameandDomain({ |
| 73 | +// endpoint: constants.RET_ISSUE, |
| 74 | +// actionPayload: issue.message.issue.actors, |
| 75 | +// contextSubscriberId: issue.context.bap_id, |
| 76 | +// contextDomain: issue.context.domain, |
| 77 | +// issueReportObj: issueObj, |
| 78 | +// IdType: 'BAP', |
| 79 | +// }); |
| 80 | + |
| 81 | +// compareUpdatedAtAndContextTimeStamp({ |
| 82 | +// endpoint: constants.RET_ISSUE, |
| 83 | +// actionPayload: issue.message.issue.actions, |
| 84 | +// messageUpdatedAt: issue.message.issue.updated_at, |
| 85 | +// issueReportObj: issueObj, |
| 86 | +// }); |
| 87 | + |
| 88 | +// compareCreatedAtAndUpdationTime({ |
| 89 | +// endpoint: constants.RET_ISSUE, |
| 90 | +// created_at: issue.message.issue.created_at, |
| 91 | +// contextTimeStamp: issue.context.timestamp, |
| 92 | +// messageUpdatedAt: issue.message.issue.updated_at, |
| 93 | +// domain: issue.context.domain, |
| 94 | +// issueReportObj: issueObj, |
| 95 | +// }); |
| 96 | + |
| 97 | +// compareContextTimeStampAndUpdatedAt({ |
| 98 | +// endpoint: constants.RET_ISSUE, |
| 99 | +// contextTimeStamp: issue.context.timestamp, |
| 100 | +// issue_updated_at: issue.message.issue.updated_at, |
| 101 | +// issueReportObj: issueObj, |
| 102 | +// }); |
| 103 | + |
| 104 | +// try { |
| 105 | +// logger.info(`Checking for mandatory images in issue description`); |
| 106 | +// if (!issue.message.issue.description.images || !issue.message.issue.description.images.length) { |
| 107 | +// issueObj.mndtryImages = 'Issue description images are mandatory'; |
| 108 | +// } |
| 109 | +// } catch (error: any) { |
| 110 | +// logger.error(`Error checking mandatory images, ${error.stack}`); |
| 111 | +// } |
| 112 | + |
| 113 | +// try { |
| 114 | +// logger.info(`Validating phone numbers for /${constants.ISSUE_1}`); |
| 115 | +// const complainantPhone = issue.message.issue.actors.find((actor: any) => actor.id === issue.message.issue.complainant_id)?.info.contact.phone; |
| 116 | +// if (!_.inRange(Number(complainantPhone), 1000000000, 99999999999)) { |
| 117 | +// issueObj.Phn = `Invalid phone number in complainant info`; |
| 118 | +// } |
| 119 | +// setValue('igmPhn', complainantPhone); |
| 120 | +// } catch (error: any) { |
| 121 | +// logger.error(`Error validating phone number, ${error.stack}`); |
| 122 | +// } |
| 123 | + |
| 124 | +// setValue('igmCreatedAt', issue.message.issue.created_at); |
| 125 | +// return issueObj; |
| 126 | +// } catch (err: any) { |
| 127 | +// logger.error(`Unexpected error in /${constants.ISSUE_1} API`, err); |
| 128 | +// } |
| 129 | +// }; |
| 130 | + |
| 131 | +// export default checkIssueV2; |
| 132 | +import _ from 'lodash' |
| 133 | +import { setValue } from '../../../shared/dao' |
| 134 | +import { checkContext, isObjectEmpty } from '../../../utils/index' |
| 135 | +import constants, { IGMApiSequence } from '../../../constants/index' |
| 136 | +import { validateSchema } from '../../../utils/index' |
| 137 | +import { logger } from '../../../shared/logger' |
| 138 | +import issue_subcategory from '../../issue_subcategories' |
| 139 | +// import messages from '../../utils/messages_constants' |
| 140 | +import { |
| 141 | + checkOrganizationNameandDomain, |
| 142 | + compareContextTimeStampAndUpdatedAt, |
| 143 | + compareCreatedAtAndUpdationTime, |
| 144 | + compareUpdatedAtAndContextTimeStamp, |
| 145 | +} from '../igmHelpers' |
| 146 | + |
| 147 | +const checkIssue = (data: any) => { |
| 148 | + const issueObj: any = {} |
| 149 | + let res: any = {} |
| 150 | + |
| 151 | + if (!data || isObjectEmpty(data)) { |
| 152 | + return { [IGMApiSequence.RET_ISSUE]: 'JSON cannot be empty' } |
| 153 | + } |
| 154 | + |
| 155 | + try { |
| 156 | + const issue: any = data |
| 157 | + |
| 158 | + try { |
| 159 | + logger.info(`Validating Schema for ${constants.RET_ISSUE} API`) |
| 160 | + const vs = validateSchema('igm', constants.RET_ISSUE, issue) |
| 161 | + if (vs != 'error') { |
| 162 | + Object.assign(issueObj, vs) |
| 163 | + } |
| 164 | + } catch (error: any) { |
| 165 | + logger.error(`!!Error occurred while performing schema validation for /${constants.RET_ISSUE}, ${error.stack}`) |
| 166 | + } |
| 167 | + try { |
| 168 | + logger.info(`Checking context for ${constants.RET_ISSUE} API`) //checking context |
| 169 | + res = checkContext(issue.context, constants.RET_ISSUE) |
| 170 | + if (!res.valid) { |
| 171 | + Object.assign(issueObj, res.ERRORS) |
| 172 | + } |
| 173 | + } catch (error: any) { |
| 174 | + logger.error(`Some error occurred while checking /${constants.RET_ISSUE} context, ${error.stack}`) |
| 175 | + } |
| 176 | + |
| 177 | + try { |
| 178 | + logger.info( |
| 179 | + `Storing igmTxnID igmTmpstmp igmType igmCoreVersion igmDomain igmIssueMesgId in /${constants.RET_ISSUE}`, |
| 180 | + ) //storing IgmTxnId IgmTmpstmp igmType igmCoreVersion igmDomain |
| 181 | + setValue('igmTxnId', issue.context.transaction_id) |
| 182 | + setValue('igmTmpstmp', issue.context.timestamp) |
| 183 | + setValue('igmCoreVersion', issue.context.core_version) |
| 184 | + setValue('igmDomain', issue.context.domain) |
| 185 | + setValue('igmIssueMsgId', issue.context.message_id) |
| 186 | + setValue('seller_bpp_id', issue.context.bpp_id) |
| 187 | + setValue('seller_bpp_uri', issue.context.bpp_uri) |
| 188 | + |
| 189 | + if (issue.message) { |
| 190 | + setValue('igmIssueType', issue.message.issue.issue_type) |
| 191 | + } |
| 192 | + // msgIdSet.add(issue.context.message_id); |
| 193 | + if (!res.valid) { |
| 194 | + Object.assign(issueObj, res.ERRORS) |
| 195 | + } |
| 196 | + } catch (error: any) { |
| 197 | + logger.error(`!!Some error occurred while checking /${constants.RET_ISSUE} context, ${error.stack}`) |
| 198 | + } |
| 199 | + |
| 200 | + try { |
| 201 | + logger.info(`Validating category and subcategory in /${constants.RET_ISSUE}`) |
| 202 | + |
| 203 | + if ( |
| 204 | + (issue.message.category === 'ITEM' && |
| 205 | + !issue_subcategory.issueItmSubCategories.includes(issue.message.sub_category)) || |
| 206 | + (issue.message.category === 'FULFILLMENT' && |
| 207 | + !issue_subcategory.issueFlmSubcategories.includes(issue.message.sub_category)) |
| 208 | + ) { |
| 209 | + issueObj.ctgrySubCategory = `Invalid sub_category ${issue.sub_category} for issue category "${issue.category}"` |
| 210 | + } |
| 211 | + |
| 212 | + if (issue.message.issue.category === 'ITEM') { |
| 213 | + if (issue.message.issue.order_details.items.length === 0) { |
| 214 | + issueObj.items = `Items in issue.message.issue.order_details.items should not be empty when message category is ITEM` |
| 215 | + } |
| 216 | + } |
| 217 | + if (issue.message.issue.category === 'FULFILLMENT') { |
| 218 | + if (issue.message.issue.order_details.fulfillments.length === 0) { |
| 219 | + issueObj.items = `Fulfillments in issue.message.issue.order_details.fulfillments should not be empty when message category is FULFILLMENT` |
| 220 | + } |
| 221 | + } |
| 222 | + } catch (error: any) { |
| 223 | + logger.error(`!!Error while validating category and subcategory in /${constants.RET_ISSUE}, ${error.stack}`) |
| 224 | + } |
| 225 | + |
| 226 | + const complainant_actions = issue.message.issue.issue_actions.complainant_actions |
| 227 | + |
| 228 | + checkOrganizationNameandDomain({ |
| 229 | + endpoint: constants.RET_ISSUE, |
| 230 | + actionPayload: complainant_actions, |
| 231 | + contextSubscriberId: issue.context.bap_id, |
| 232 | + contextDomain: issue.context.domain, |
| 233 | + issueReportObj: issueObj, |
| 234 | + IdType: 'BAP', |
| 235 | + }) |
| 236 | + |
| 237 | + compareUpdatedAtAndContextTimeStamp({ |
| 238 | + endpoint: constants.RET_ISSUE, |
| 239 | + actionPayload: complainant_actions, |
| 240 | + messageUpdatedAt: issue.message.issue.updated_at, |
| 241 | + issueReportObj: issueObj, |
| 242 | + }) |
| 243 | + |
| 244 | + compareCreatedAtAndUpdationTime({ |
| 245 | + endpoint: constants.RET_ISSUE, |
| 246 | + created_at: issue.message.issue.created_at, |
| 247 | + contextTimeStamp: issue.context.timestamp, |
| 248 | + messageUpdatedAt: issue.message.issue.updated_at, |
| 249 | + domain: issue.context.domain, |
| 250 | + issueReportObj: issueObj, |
| 251 | + }) |
| 252 | + |
| 253 | + compareContextTimeStampAndUpdatedAt({ |
| 254 | + endpoint: constants.RET_ISSUE, |
| 255 | + contextTimeStamp: issue.context.timestamp, |
| 256 | + issue_updated_at: issue.message.issue.updated_at, |
| 257 | + issueReportObj: issueObj, |
| 258 | + }) |
| 259 | + |
| 260 | + try { |
| 261 | + logger.info(`Checking conditional mandatory images for certain issue sub-categories`) |
| 262 | + if (['ITM02', 'ITM03', 'ITM04', 'ITM05', 'FLM04'].includes(issue.sub_category)) { |
| 263 | + const has = Object.prototype.hasOwnProperty |
| 264 | + if (!has.call(issue.description, 'images') || !issue.description.images.length) { |
| 265 | + issueObj.mndtryImages = `issue/description/images are mandatory for issue sub_category ${issue.sub_category}` |
| 266 | + } |
| 267 | + } |
| 268 | + } catch (error: any) { |
| 269 | + logger.error(`Error while checking conditional mandatory images for certain issue sub-categories, ${error.stack}`) |
| 270 | + } |
| 271 | + |
| 272 | + try { |
| 273 | + logger.info(`Phone Number Check for /${constants.RET_ISSUE}`) |
| 274 | + // on_issue.message.issue.issue_actions.respondent_actions[0].updated_by.contact.phone |
| 275 | + if (!_.inRange(issue.message.issue.complainant_info.contact.phone, 1000000000, 99999999999)) { |
| 276 | + issueObj.Phn = `Phone Number for /${constants.RET_ISSUE} api is not in the valid Range` |
| 277 | + } |
| 278 | + setValue('igmPhn', issue.message.issue.complainant_info.contact.phone) |
| 279 | + } catch (error: any) { |
| 280 | + logger.error(`Error while checking phone number for /${constants.RET_ISSUE} api, ${error.stack}`) |
| 281 | + } |
| 282 | + |
| 283 | + setValue('igmCreatedAt', issue.message.issue.created_at) |
| 284 | + |
| 285 | + //setValue("issueObj", issueObj); |
| 286 | + return issueObj |
| 287 | + } catch (err: any) { |
| 288 | + if (err.code === 'ENOENT') { |
| 289 | + logger.info(`!!File not found for /${constants.RET_ISSUE} API!`) |
| 290 | + } else { |
| 291 | + logger.error(`!!Some error occurred while checking /${constants.RET_ISSUE} API`, err) |
| 292 | + } |
| 293 | + } |
| 294 | +} |
| 295 | + |
| 296 | +export default checkIssue |
0 commit comments