UNPKG

purespectrum-lib

Version:

shared code between front and backend projects

208 lines 12.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.QuotaChildGenderAgeMapper = void 0; var tslib_1 = require("tslib"); var quota_mapper_constants_1 = require("./quota-mapper.constants"); var child_age_gender_mapper_constants_1 = require("../child-age-gender-mapper.constants"); var child_1 = require("../child/child"); var children_1 = require("../children/children"); var util_1 = require("../../shared/util"); var QuotaChildGenderAgeMapper = /** @class */ (function () { function QuotaChildGenderAgeMapper() { } /** * map old children quota to new child gender age quota */ QuotaChildGenderAgeMapper.prototype.toChildAgeGender = function (quotas) { var _this = this; return quotas.map(function (_a) { var _b; var criteria = _a.criteria, rest = tslib_1.__rest(_a, ["criteria"]); var hasChildrenCriteria = criteria.find(function (criteria) { return criteria.qualification_code === quota_mapper_constants_1.HAVE_CHILDREN_QUALIFICATION_ID; }); var genderCriteria = criteria.find(function (criteria) { return criteria.qualification_code === quota_mapper_constants_1.CHILD_GENDER_QUALIFICATION_ID; }); var ageCriteria = criteria.find(function (criteria) { return criteria.qualification_code === quota_mapper_constants_1.CHILD_AGE_QUALIFICATION_ID; }); // is other type of criteria if (!hasChildrenCriteria || (hasChildrenCriteria && !hasChildrenCriteria.condition_codes)) { return tslib_1.__assign({ criteria: criteria }, rest); } // invalid has children criteria if (hasChildrenCriteria && hasChildrenCriteria.condition_codes && hasChildrenCriteria.condition_codes.some(function (code) { return Number(code) === quota_mapper_constants_1.YES_HAVE_CHILDREN_CONDITION_CODE; }) && (!genderCriteria || !ageCriteria)) { throw new Error('Invalid quota: has children but not gender and age'); } // invalid child gender criteria if (genderCriteria && !genderCriteria.condition_codes) { throw new Error('Invalid quota: gender criteria with no conditions'); } // invalid child age criteria if (ageCriteria && !ageCriteria.range_sets) { throw new Error('Invalid quota: age criteria with no range set'); } // invalid age range criteria if (ageCriteria && !((_b = ageCriteria.range_sets) === null || _b === void 0 ? void 0 : _b.length)) { throw new Error('Invalid quota: invalid criteria for gender or age'); } // invalid scopes out of the way, let's proceed with child gender age mapping var childGenderAgeCriteria = _this.toChildAgeGenderCriteria(hasChildrenCriteria.condition_codes, genderCriteria === null || genderCriteria === void 0 ? void 0 : genderCriteria.condition_codes, ageCriteria === null || ageCriteria === void 0 ? void 0 : ageCriteria.range_sets[0]); return tslib_1.__assign({ criteria: [childGenderAgeCriteria] }, rest); }); }; /** * Core mapper of old children quota to new child gender age quota */ QuotaChildGenderAgeMapper.prototype.toChildAgeGenderCriteria = function (hasChildrenConditionCodes, genderConditionCodes, ageRange, criteria) { if (!(hasChildrenConditionCodes === null || hasChildrenConditionCodes === void 0 ? void 0 : hasChildrenConditionCodes.length)) { throw new Error('Invalid quota: has children criteria condition codes is empty'); } if (!criteria) { criteria = {}; } // prepare new child gender age criteria as will be always one criteria instead of 3 var childGenderAgeCriteria = tslib_1.__assign(tslib_1.__assign({}, criteria), { qualification_code: child_age_gender_mapper_constants_1.CHILD_AGE_GENDER_QUALIFICATION_ID, qualification_name: child_age_gender_mapper_constants_1.CHILD_AGE_GENDER_QUALIFICATION_NAME, q_type: quota_mapper_constants_1.CRITERIA_QUOTA_TYPE_NORMAL, range_sets: [], condition_codes: [] }); // no children if (hasChildrenConditionCodes.some(function (code) { return Number(code) === quota_mapper_constants_1.NO_HAVE_CHILDREN_CONDITION_CODE; })) { childGenderAgeCriteria.condition_codes = [ child_1.Child.createNoChildren().code.toString(), ]; // no children always comes separated into its own quota return childGenderAgeCriteria; } if (!(genderConditionCodes === null || genderConditionCodes === void 0 ? void 0 : genderConditionCodes.length)) { throw new Error('Invalid quota: gender criteria condition codes is empty'); } if (!ageRange) { throw new Error('Invalid quota: age criteria range is empty'); } var isBoy = genderConditionCodes.some(function (id) { return Number(id) === quota_mapper_constants_1.BOY_CHILD_GENDER_CONDITION_CODE; }); var isGirl = genderConditionCodes.some(function (id) { return Number(id) === quota_mapper_constants_1.GIRL_CHILD_GENDER_CONDITION_CODE; }); var isBaby = ageRange.units === quota_mapper_constants_1.MONTHS_CHILD_AGE_UNIT_CODE; var from = ageRange.from, to = ageRange.to; childGenderAgeCriteria.condition_codes = this._getNewAgeFromToConditionCodes(from, to, isBaby, isBoy, isGirl); return childGenderAgeCriteria; }; QuotaChildGenderAgeMapper.prototype._getNewAgeFromToConditionCodes = function (from, to, isBaby, isBoy, isGirl) { var gender; if (isBoy && isGirl) { gender = children_1.ChildGenderEnum.both; } else { gender = isBoy ? children_1.ChildGenderEnum.boy : children_1.ChildGenderEnum.girl; } var children = children_1.Children.create(isBaby, gender, from, to); return children.toEachChild().map(function (_a) { var code = _a.code; return code.toString(); }); }; /** * map new child gender age quota to old children quota */ QuotaChildGenderAgeMapper.prototype.toChildren = function (quotas) { var _this = this; return quotas.map(function (_a) { var criteria = _a.criteria, rest = tslib_1.__rest(_a, ["criteria"]); var newChildAgeGenderCriteria = _this._getChildGenderAgeCriteriaIfValid(criteria); if (!newChildAgeGenderCriteria) { return tslib_1.__assign({ criteria: criteria }, rest); } var conditionCodes = newChildAgeGenderCriteria.conditions.map(function (_a) { var id = _a.id; return id.toString(); }); var childrenCriterias = _this.toChildrenCriterias(conditionCodes, newChildAgeGenderCriteria).map(function (_a) { var condition_codes = _a.condition_codes, props = tslib_1.__rest(_a, ["condition_codes"]); return (tslib_1.__assign(tslib_1.__assign({}, props), { conditions: condition_codes.map(function (code) { return ({ id: code }); }) })); }); return tslib_1.__assign({ criteria: tslib_1.__spreadArray(tslib_1.__spreadArray([], tslib_1.__read(criteria.filter(function (_a) { var qualification_code = _a.qualification_code; return qualification_code !== 232; })), false), tslib_1.__read(childrenCriterias), false) }, rest); }); }; /** * get new child gender age criteria from criteria array, as child age gender * always have only one criterias, return this single criteria or null in case * of invalid or more than one criteria. */ QuotaChildGenderAgeMapper.prototype._getChildGenderAgeCriteriaIfValid = function (criterias) { var newChildAgeGenderCriterias = criterias.filter(function (criteria) { return criteria.qualification_code === child_age_gender_mapper_constants_1.CHILD_AGE_GENDER_QUALIFICATION_ID; }); // new child age gender always have only one criteria if (!newChildAgeGenderCriterias.length || newChildAgeGenderCriterias.length > 1) { return null; } var _a = tslib_1.__read(newChildAgeGenderCriterias, 1), newChildAgeGenderCriteria = _a[0]; // not new child age gender if (newChildAgeGenderCriteria.qualification_code !== child_age_gender_mapper_constants_1.CHILD_AGE_GENDER_QUALIFICATION_ID) { return null; } return newChildAgeGenderCriteria; }; /** * map new child gender age criteria to old children 3 criterias */ QuotaChildGenderAgeMapper.prototype.fromNewChildGenderAgeCriteriaToChildrenCriterias = function (criteria) { var newChildAgeGenderCriteria = this._getChildGenderAgeCriteriaIfValid(criteria); if (!newChildAgeGenderCriteria) { return criteria; } return this.toChildrenCriterias(newChildAgeGenderCriteria.condition_codes, newChildAgeGenderCriteria); }; /** * Core mapper of new child gender age condition codes to old 3 children criterias */ QuotaChildGenderAgeMapper.prototype.toChildrenCriterias = function (conditionCodes, criteria) { if (criteria.qualification_code !== child_age_gender_mapper_constants_1.CHILD_AGE_GENDER_QUALIFICATION_ID) { throw new Error('Invalid quota: criteria is not a new child age gender qualification '); } var eachChild = conditionCodes.map(function (code) { return new child_1.Child(Number(code)); }); var children = new children_1.Children(eachChild); var hasChildrenCriteria = tslib_1.__assign(tslib_1.__assign({}, criteria), { qualification_code: quota_mapper_constants_1.HAVE_CHILDREN_QUALIFICATION_ID, qualification_name: quota_mapper_constants_1.HAVE_CHILDREN_QUALIFICATION_NAME, q_type: quota_mapper_constants_1.CRITERIA_QUOTA_TYPE_NORMAL, condition_codes: [], range_sets: [] }); if (children.noChildren) { hasChildrenCriteria.condition_codes = [ quota_mapper_constants_1.NO_HAVE_CHILDREN_CONDITION_CODE.toString(), ]; // no children always comes separated into its own quota return [hasChildrenCriteria]; } hasChildrenCriteria.condition_codes = [ quota_mapper_constants_1.YES_HAVE_CHILDREN_CONDITION_CODE.toString(), ]; var genderCriteria = (0, util_1.clone)(hasChildrenCriteria); genderCriteria.qualification_code = quota_mapper_constants_1.CHILD_GENDER_QUALIFICATION_ID; genderCriteria.condition_codes = []; var ageCriteria = (0, util_1.clone)(genderCriteria); ageCriteria.qualification_code = quota_mapper_constants_1.CHILD_AGE_QUALIFICATION_ID; this._setOldGenderAgeCriterias(children, genderCriteria, ageCriteria); return [hasChildrenCriteria, genderCriteria, ageCriteria]; }; QuotaChildGenderAgeMapper.prototype._setOldGenderAgeCriterias = function (children, genderCriteria, ageCriteria) { if (children.gender === children_1.ChildGenderEnum.both) { genderCriteria.condition_codes.push(quota_mapper_constants_1.BOY_CHILD_GENDER_CONDITION_CODE.toString(), quota_mapper_constants_1.GIRL_CHILD_GENDER_CONDITION_CODE.toString()); } else { genderCriteria.condition_codes.push(children.gender === children_1.ChildGenderEnum.boy ? quota_mapper_constants_1.BOY_CHILD_GENDER_CONDITION_CODE.toString() : quota_mapper_constants_1.GIRL_CHILD_GENDER_CONDITION_CODE.toString()); } ageCriteria.q_type = quota_mapper_constants_1.CRITERIA_QUOTA_TYPE_RANGESET; var _a = children.age, from = _a.from, to = _a.to; ageCriteria.range_sets = [{ units: children.ageUnit, from: from, to: to }]; }; return QuotaChildGenderAgeMapper; }()); exports.QuotaChildGenderAgeMapper = QuotaChildGenderAgeMapper; //# sourceMappingURL=quota-mapper.js.map