|
| 1 | +public class InsuranceRatingApexService implements Callable { |
| 2 | + |
| 3 | + private static final String POST_RATING_METHOD = 'postRating'; |
| 4 | + private static final String PATCH_RATING_METHOD = 'patchRating'; |
| 5 | + |
| 6 | + /** |
| 7 | + * @description AuraEnabled method for LWC to call POST rating |
| 8 | + * @param postPayloadJson JSON string containing rating request parameters |
| 9 | + * @return Map<String, Object> containing the rating response |
| 10 | + */ |
| 11 | + @AuraEnabled |
| 12 | + public static Map<String, Object> postRatingFromLwc(String postPayloadJson) { |
| 13 | + InsuranceRatingApexService service = new InsuranceRatingApexService(); |
| 14 | + Map<String, Object> output = new Map<String, Object>(); |
| 15 | + Map<String, Object> inputs = new Map<String, Object>(); |
| 16 | + |
| 17 | + // Deserialize JSON string to Map to avoid type conversion issues |
| 18 | + Map<String, Object> postPayload = (Map<String, Object>) JSON.deserializeUntyped(postPayloadJson); |
| 19 | + inputs.put('postPayload', postPayload); |
| 20 | + |
| 21 | + service.invokeMethod(POST_RATING_METHOD, inputs, output, new Map<String, Object>()); |
| 22 | + |
| 23 | + return output; |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * @description AuraEnabled method for LWC to call PATCH rating |
| 28 | + * @param patchPayloadJson JSON string containing rating request parameters |
| 29 | + * @return Map<String, Object> containing the rating response |
| 30 | + */ |
| 31 | + @AuraEnabled |
| 32 | + public static Map<String, Object> patchRatingFromLwc(String patchPayloadJson) { |
| 33 | + InsuranceRatingApexService service = new InsuranceRatingApexService(); |
| 34 | + Map<String, Object> output = new Map<String, Object>(); |
| 35 | + Map<String, Object> inputs = new Map<String, Object>(); |
| 36 | + |
| 37 | + // Deserialize JSON string to Map to avoid type conversion issues |
| 38 | + Map<String, Object> patchPayload = (Map<String, Object>) JSON.deserializeUntyped(patchPayloadJson); |
| 39 | + inputs.put('patchPayload', patchPayload); |
| 40 | + |
| 41 | + service.invokeMethod(PATCH_RATING_METHOD, inputs, output, new Map<String, Object>()); |
| 42 | + return output; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * @description Implementation of Callable interface for OmniStudio |
| 47 | + * @param action Method name to invoke |
| 48 | + * @param args Map containing input, output, and options |
| 49 | + * @return Object result of the method invocation |
| 50 | + */ |
| 51 | + public Object call(String action, Map<String, Object> args) { |
| 52 | + System.debug('InsuranceRatingApexService: call ' + action); |
| 53 | + |
| 54 | + Map<String, Object> input = (Map<String, Object>) args.get('input'); |
| 55 | + Map<String, Object> output = (Map<String, Object>) args.get('output'); |
| 56 | + Map<String, Object> options = (Map<String, Object>) args.get('options'); |
| 57 | + |
| 58 | + return invokeMethod(action, input, output, options); |
| 59 | + } |
| 60 | + // Invoke methods, and if the method is not supported, it will be ignored. |
| 61 | + public Boolean invokeMethod( |
| 62 | + String methodName, |
| 63 | + Map<String, Object> inputs, |
| 64 | + Map<String, Object> output, |
| 65 | + Map<String, Object> options |
| 66 | + ) { |
| 67 | + Boolean response = false; |
| 68 | + |
| 69 | + System.debug('Insurance Rating Apex Service - Method: ' + methodName); |
| 70 | + |
| 71 | + try { |
| 72 | + if (POST_RATING_METHOD.equals(methodName)) { |
| 73 | + response = createRating(inputs, output, options); |
| 74 | + } else if (PATCH_RATING_METHOD.equals(methodName)) { |
| 75 | + response = reprice(inputs, output, options); |
| 76 | + } else { |
| 77 | + throw new UnsupportedOperationException('this method name is incorrect or not supported'); |
| 78 | + } |
| 79 | + } catch (Exception e) { |
| 80 | + response = false; |
| 81 | + output.put('success', false); |
| 82 | + output.put('errorMessage', 'Exception occurred: ' + e.getMessage()); |
| 83 | + System.debug(LoggingLevel.ERROR, 'Exception in invokeMethod: ' + methodName + ', Error: ' + e.getMessage()); |
| 84 | + System.debug(LoggingLevel.ERROR, 'Stack trace: ' + e.getStackTraceString()); |
| 85 | + } |
| 86 | + System.debug('Output from invoke:' + output); |
| 87 | + return response; |
| 88 | + } |
| 89 | + |
| 90 | + private Boolean createRating(Map<String, Object> inputs, Map<String, Object> output, Map<String, Object> options) { |
| 91 | + Map<String, Object> postPayload = (Map<String, Object>) getValueFromMap('postPayload', inputs, options); |
| 92 | + System.debug('Rating post Input: ' + postPayload); |
| 93 | + |
| 94 | + // Handle null postPayload |
| 95 | + if (postPayload == null) { |
| 96 | + output.put('success', false); |
| 97 | + output.put('errorMessage', 'postPayload is required for rating patch'); |
| 98 | + output.put('errorType', 'VALIDATION_ERROR'); |
| 99 | + return false; |
| 100 | + } |
| 101 | + |
| 102 | + try { |
| 103 | + System.debug('Calling Invocable Action: createInsuranceRating'); |
| 104 | + |
| 105 | + // Create and configure the invocable action |
| 106 | + Invocable.Action action = Invocable.Action.createStandardAction('createInsuranceRating'); |
| 107 | + |
| 108 | + |
| 109 | + System.debug('RatingInputs::::::::' + postPayload.get('ratingInputs')); |
| 110 | + setInvocationParameter(action, postPayload, 'ratingInputs', true); |
| 111 | + |
| 112 | + action.setInvocationParameter('transactionType', postPayload.get('transactionType')); |
| 113 | + |
| 114 | + setInvocationParameter(action, postPayload, 'additionalFields', true); |
| 115 | + |
| 116 | + action.setInvocationParameter('effectiveDate', postPayload.get('effectiveDate')); |
| 117 | + |
| 118 | + // Set options from payload |
| 119 | + if (postPayload.containsKey('ratingOptions')) { |
| 120 | + System.debug('Options:' + postPayload.get('ratingOptions')); |
| 121 | + Map<String, Object> ratingOptions = (Map<String, Object>) postPayload.get('ratingOptions'); |
| 122 | + //ratingOptions.put('executePricing', false); |
| 123 | + action.setInvocationParameter('ratingOptions', ratingOptions); |
| 124 | + } |
| 125 | + |
| 126 | + // Execute the action |
| 127 | + List<Invocable.Action.Result> results = action.invoke(); |
| 128 | + |
| 129 | + if (results != null && !results.isEmpty()) { |
| 130 | + Invocable.Action.Result result = results[0]; |
| 131 | + |
| 132 | + if (result.isSuccess()) { |
| 133 | + System.debug('Rating post successfully via Invocable Action'); |
| 134 | + output.put('ratingData', result.getOutputParameters()); |
| 135 | + |
| 136 | + // Extract key information from the result |
| 137 | + Map<String, Object> outputParams = result.getOutputParameters(); |
| 138 | + System.debug('Rating post data extracted: ' + outputParams); |
| 139 | + List<Invocable.Action.Error> actionErrors = result.getErrors(); |
| 140 | + if (actionErrors != null && !actionErrors.isEmpty()) { |
| 141 | + output.put('success', false); |
| 142 | + output.put('error', result.getErrors()); |
| 143 | + return false; |
| 144 | + } |
| 145 | + output.put('success', true); |
| 146 | + return true; |
| 147 | + |
| 148 | + } else { |
| 149 | + // Action failed - get error details |
| 150 | + List<Invocable.Action.Error> actionErrors = result.getErrors(); |
| 151 | + String errorMessage = 'Rating post Invocable Action failed'; |
| 152 | + |
| 153 | + if (actionErrors != null && !actionErrors.isEmpty()) { |
| 154 | + List<String> errorMessages = new List<String>(); |
| 155 | + List<Map<String, Object>> errorDetailsList = new List<Map<String, Object>>(); |
| 156 | + |
| 157 | + for (Invocable.Action.Error actionError : actionErrors) { |
| 158 | + errorMessages.add(actionError.getMessage()); |
| 159 | + errorDetailsList.add(new Map<String, Object>{ |
| 160 | + 'message' => actionError.getMessage(), |
| 161 | + 'code' => actionError.getCode() |
| 162 | + }); |
| 163 | + } |
| 164 | + |
| 165 | + errorMessage = String.join(errorMessages, '; '); |
| 166 | + output.put('actionErrors', errorDetailsList); |
| 167 | + } |
| 168 | + |
| 169 | + output.put('success', false); |
| 170 | + output.put('errorMessage', errorMessage); |
| 171 | + |
| 172 | + System.debug(LoggingLevel.ERROR, 'Rating post Invocable Action failed: ' + errorMessage); |
| 173 | + return false; |
| 174 | + } |
| 175 | + |
| 176 | + } else { |
| 177 | + output.put('success', false); |
| 178 | + output.put('errorMessage', 'Rating post Invocable Action returned null or empty results'); |
| 179 | + System.debug(LoggingLevel.WARN, 'Rating post Invocable Action returned null/empty results'); |
| 180 | + return false; |
| 181 | + } |
| 182 | + |
| 183 | + } catch (Exception e) { |
| 184 | + String errorMsg = 'Exception: ' + e.getMessage(); |
| 185 | + output.put('success', false); |
| 186 | + output.put('errorMessage', errorMsg); |
| 187 | + output.put('errorType', 'EXCEPTION'); |
| 188 | + System.debug(LoggingLevel.ERROR, 'Exception while rating post: ' + errorMsg); |
| 189 | + return false; |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + |
| 194 | + private Boolean reprice(Map<String, Object> inputs, Map<String, Object> output, Map<String, Object> options) { |
| 195 | + Map<String, Object> patchPayload = (Map<String, Object>) getValueFromMap('patchPayload', inputs, options); |
| 196 | + System.debug('Rating patch Input: ' + patchPayload); |
| 197 | + |
| 198 | + // Handle null patchPayload |
| 199 | + if (patchPayload == null) { |
| 200 | + output.put('success', false); |
| 201 | + output.put('errorMessage', 'patchPayload is required for rating patch'); |
| 202 | + output.put('errorType', 'VALIDATION_ERROR'); |
| 203 | + return false; |
| 204 | + } |
| 205 | + |
| 206 | + try { |
| 207 | + System.debug('Calling Invocable Action: repriceInsuranceProduct'); |
| 208 | + |
| 209 | + // Create and configure the invocable action |
| 210 | + Invocable.Action action = Invocable.Action.createStandardAction('repriceInsuranceProduct'); |
| 211 | + |
| 212 | + // Set required invocation parameters |
| 213 | + System.debug('Context Id::::::::' + patchPayload.get('contextId')); |
| 214 | + action.setInvocationParameter('contextId', patchPayload.get('contextId')); |
| 215 | + |
| 216 | + System.debug('Updated Nodes::::::::' + patchPayload.get('updatedNodes')); |
| 217 | + setInvocationParameter(action, patchPayload, 'updatedNodes', true); |
| 218 | + |
| 219 | + System.debug('Added Nodes::::::::' + patchPayload.get('addedNodes')); |
| 220 | + setInvocationParameter(action, patchPayload, 'addedNodes', true); |
| 221 | + |
| 222 | + System.debug('Deleted Nodes::::::::' + patchPayload.get('deletedNodes')); |
| 223 | + setInvocationParameter(action, patchPayload, 'deletedNodes', true); |
| 224 | + |
| 225 | + // Set options from payload |
| 226 | + if (patchPayload.containsKey('executeRating')) { |
| 227 | + System.debug('Options::::::::' + patchPayload.get('executeRating')); |
| 228 | + action.setInvocationParameter('executeRating', patchPayload.get('executeRating')); |
| 229 | + } |
| 230 | + |
| 231 | + if (patchPayload.containsKey('executeConfigurationRules')) { |
| 232 | + System.debug('Options::::::::' + patchPayload.get('executeConfigurationRules')); |
| 233 | + action.setInvocationParameter('executeConfigurationRules', patchPayload.get('executeConfigurationRules')); |
| 234 | + } |
| 235 | + |
| 236 | + if (patchPayload.containsKey('returnContextJSON')) { |
| 237 | + System.debug('Options::::::::' + patchPayload.get('returnContextJSON')); |
| 238 | + action.setInvocationParameter('returnContextJSON', patchPayload.get('returnContextJSON')); |
| 239 | + } |
| 240 | + |
| 241 | + if (patchPayload.containsKey('returnProductDetails')) { |
| 242 | + System.debug('Options::::::::' + patchPayload.get('returnProductDetails')); |
| 243 | + action.setInvocationParameter('returnProductDetails', patchPayload.get('returnProductDetails')); |
| 244 | + } |
| 245 | + |
| 246 | + if (patchPayload.containsKey('returnRatingResults')) { |
| 247 | + System.debug('Options::::::::' + patchPayload.get('returnRatingResults')); |
| 248 | + action.setInvocationParameter('returnRatingResults', patchPayload.get('returnRatingResults')); |
| 249 | + } |
| 250 | + |
| 251 | + // Execute the action |
| 252 | + List<Invocable.Action.Result> results = action.invoke(); |
| 253 | + |
| 254 | + if (results != null && !results.isEmpty()) { |
| 255 | + Invocable.Action.Result result = results[0]; |
| 256 | + |
| 257 | + if (result.isSuccess()) { |
| 258 | + System.debug('Rating patch successfully via Invocable Action'); |
| 259 | + output.put('ratingData', result.getOutputParameters()); |
| 260 | + |
| 261 | + // Extract key information from the result |
| 262 | + Map<String, Object> outputParams = result.getOutputParameters(); |
| 263 | + System.debug('Rating Patch data extracted: ' + outputParams); |
| 264 | + if (outputParams != null) { |
| 265 | + if (outputParams.containsKey('contextId')) { |
| 266 | + output.put('contextId', outputParams.get('contextId')); |
| 267 | + } |
| 268 | + if (outputParams.containsKey('contextJSON')) { |
| 269 | + output.put('contextJSON', outputParams.get('contextJSON')); |
| 270 | + } |
| 271 | + if (outputParams.containsKey('pricingResult')) { |
| 272 | + output.put('pricingResult', outputParams.get('pricingResult')); |
| 273 | + } |
| 274 | + if (outputParams.containsKey('ratingResult')) { |
| 275 | + output.put('ratingResult', outputParams.get('ratingResult')); |
| 276 | + } |
| 277 | + if (outputParams.containsKey('productDetails')) { |
| 278 | + output.put('productDetails', outputParams.get('productDetails')); |
| 279 | + } |
| 280 | + |
| 281 | + System.debug('Rating Patch data extracted: ' + outputParams); |
| 282 | + } |
| 283 | + List<Invocable.Action.Error> actionErrors = result.getErrors(); |
| 284 | + if (actionErrors != null && !actionErrors.isEmpty()) { |
| 285 | + output.put('success', false); |
| 286 | + output.put('error', result.getErrors()); |
| 287 | + return false; |
| 288 | + } |
| 289 | + output.put('success', true); |
| 290 | + return true; |
| 291 | + |
| 292 | + } else { |
| 293 | + // Action failed - get error details |
| 294 | + List<Invocable.Action.Error> actionErrors = result.getErrors(); |
| 295 | + String errorMessage = 'Rating Patch Invocable Action failed'; |
| 296 | + |
| 297 | + if (actionErrors != null && !actionErrors.isEmpty()) { |
| 298 | + List<String> errorMessages = new List<String>(); |
| 299 | + List<Map<String, Object>> errorDetailsList = new List<Map<String, Object>>(); |
| 300 | + |
| 301 | + for (Invocable.Action.Error actionError : actionErrors) { |
| 302 | + errorMessages.add(actionError.getMessage()); |
| 303 | + errorDetailsList.add(new Map<String, Object>{ |
| 304 | + 'message' => actionError.getMessage(), |
| 305 | + 'code' => actionError.getCode() |
| 306 | + }); |
| 307 | + } |
| 308 | + |
| 309 | + errorMessage = String.join(errorMessages, '; '); |
| 310 | + output.put('actionErrors', errorDetailsList); |
| 311 | + } |
| 312 | + |
| 313 | + output.put('success', false); |
| 314 | + output.put('errorMessage', errorMessage); |
| 315 | + |
| 316 | + System.debug(LoggingLevel.ERROR, 'Rating Patch Invocable Action failed: ' + errorMessage); |
| 317 | + return false; |
| 318 | + } |
| 319 | + |
| 320 | + } else { |
| 321 | + output.put('success', false); |
| 322 | + output.put('errorMessage', 'Rating Patch Invocable Action returned null or empty results'); |
| 323 | + System.debug(LoggingLevel.WARN, 'Rating Patch Invocable Action returned null/empty results'); |
| 324 | + return false; |
| 325 | + } |
| 326 | + |
| 327 | + } catch (Exception e) { |
| 328 | + String errorMsg = 'Exception: ' + e.getMessage(); |
| 329 | + output.put('success', false); |
| 330 | + output.put('errorMessage', errorMsg); |
| 331 | + output.put('errorType', 'EXCEPTION'); |
| 332 | + System.debug(LoggingLevel.ERROR, 'Exception while rating patch: ' + errorMsg); |
| 333 | + return false; |
| 334 | + } |
| 335 | + } |
| 336 | + |
| 337 | + private Object getValueFromMap(String key, Map<String, Object> inputs, Map<String, Object> options) { |
| 338 | + if (inputs != null && inputs.get(key) != null && String.isNotBlank(String.valueOf(inputs.get(key)))) { |
| 339 | + return inputs.get(key); |
| 340 | + } |
| 341 | + else if (options != null && options.get(key) != null && String.isNotBlank(String.valueOf(options.get(key)))) { |
| 342 | + return options.get(key); |
| 343 | + } |
| 344 | + |
| 345 | + return null; |
| 346 | + } |
| 347 | + |
| 348 | + private void setInvocationParameter(Invocable.Action action, Map<String, Object> payload, String inputAttribute, boolean serializeJSON) { |
| 349 | + if (payload.containsKey(inputAttribute) && payload.get(inputAttribute) != null) { |
| 350 | + System.debug(inputAttribute + ': ' + JSON.serialize(payload.get(inputAttribute))); |
| 351 | + if (serializeJSON) { |
| 352 | + action.setInvocationParameter(inputAttribute, JSON.serialize(payload.get(inputAttribute))); |
| 353 | + } else { |
| 354 | + action.setInvocationParameter(inputAttribute, payload.get(inputAttribute)); |
| 355 | + } |
| 356 | + } else { |
| 357 | + System.debug('payload does not contain attribute: ' + inputAttribute); |
| 358 | + } |
| 359 | + } |
| 360 | +} |
0 commit comments