UNPKG

@turnbuckle/aprs-calculator-services

Version:

Anchorpoint Risk Calculator Service.

106 lines (105 loc) 7.77 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.scoreAgeAnalysis = void 0; var assert_1 = require("../../common/assert"); var ts_stopwatch_1 = require("ts-stopwatch"); var scoring_table_type_enums_1 = require("../../enums/scoring-table-type.enums"); var sort_module_1 = require("../../common/sort.module"); /** * @AgeAnalysisScoringInputModel input * - ageAnalysis: array of age analysis complete with ageing steps * - scoringType: method to use when scoring age analysis; defaults to ageing * - scoringTable: age analysis scoring table for status, ageing or internal scoring * > - apply scoring according to the scoring table on the specified age analysis array * @AgeAnalysisOutputModel output * - ageAnalysis: age analysis array with scoring */ function scoreAgeAnalysis(input) { var _a, _b, _c, _d, _e, _f, _g, _h; return __awaiter(this, void 0, void 0, function () { var sw, ageAnalysis, _loop_1, i; return __generator(this, function (_j) { sw = new ts_stopwatch_1.Stopwatch(); sw.start(); (0, assert_1.assert)(((_a = input.ageAnalysis) === null || _a === void 0 ? void 0 : _a.length) > 0, "'ageAnalysis' parameter was not provided or is empty"); (0, assert_1.assert)(input.scoringType !== null, "'scoringType' parameter was not provided"); (0, assert_1.assert)(((_b = input.scoringTable) === null || _b === void 0 ? void 0 : _b.length) > 0, "'scoringTable' parameter was not provided or is empty"); ageAnalysis = (0, sort_module_1.sortByTransactionNumberAndThenDate)(input.ageAnalysis); _loop_1 = function (i) { var item = ageAnalysis[i]; switch ((input.scoringType || scoring_table_type_enums_1.ScoringTableTypeEnums.ageing)) { case scoring_table_type_enums_1.ScoringTableTypeEnums.ageing: (0, assert_1.assert)(item.ageing != null, "Age analysis item [".concat(i, "] does not have a 'ageing' value specified!")); (0, assert_1.assert)(input.ageAnalysis[0].transactionNumber != null, "Age analysis item [".concat(i, "] does not have a 'transactionNumber' value specified!")); if ((input.absorbingStates === 2) && (item.balance < 1) && (i === ageAnalysis.length - 1 || item.transactionNumber !== ageAnalysis[i + 1].transactionNumber)) { // paid rating only for two absorbing states AND // paid rating when balance is less than 1, and it's the last transaction item item.rating = input.scoringTable[input.scoringTable.length - 1].rating + 1; } else { item.rating = (_d = (_c = (input.scoringTable.find(function (score) { var _a, _b; return item.ageing > ((_a = score.minDays) !== null && _a !== void 0 ? _a : Number.NEGATIVE_INFINITY) && item.ageing <= ((_b = score.maxDays) !== null && _b !== void 0 ? _b : Number.POSITIVE_INFINITY); }))) === null || _c === void 0 ? void 0 : _c.rating) !== null && _d !== void 0 ? _d : 0; } break; case scoring_table_type_enums_1.ScoringTableTypeEnums.status: (0, assert_1.assert)(item.status != null, "Age analysis item [".concat(i, "] does not have a 'status' value specified!")); item.rating = (_f = (_e = (input.scoringTable.find(function (score) { return (score.contains || []).some(function (value) { return value === item.status; }); }))) === null || _e === void 0 ? void 0 : _e.rating) !== null && _f !== void 0 ? _f : 0; (0, assert_1.assert)(item.rating > 0, "Age analysis item [".concat(i, "] does not have a known 'status' value specified ['").concat(item.status, "']!")); break; case scoring_table_type_enums_1.ScoringTableTypeEnums.internalScore: (0, assert_1.assert)(item.score != null, "Age analysis item [".concat(i, "] does not have a 'score' value specified!")); item.rating = (_h = (_g = (input.scoringTable.find(function (score) { return (score.contains || []).some(function (value) { return value === item.score; }); }))) === null || _g === void 0 ? void 0 : _g.rating) !== null && _h !== void 0 ? _h : 0; (0, assert_1.assert)(item.rating > 0, "Age analysis item [".concat(i, "] does not have a known 'status' value specified ['").concat(item.score, "']!")); break; default: (0, assert_1.assert)(false, "Unknown scoring type [".concat(input.scoringType, "] specified!")); break; } }; for (i = 0; i < ageAnalysis.length; i++) { _loop_1(i); } sw.stop(); return [2 /*return*/, { executeDuration: sw.getTime(), ageAnalysis: ageAnalysis }]; }); }); } exports.scoreAgeAnalysis = scoreAgeAnalysis;