purespectrum-lib
Version:
shared code between front and backend projects
106 lines (105 loc) • 5.87 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.QualificationChildGenderAgeMapper = void 0;
var tslib_1 = require("tslib");
var quota_mapper_constants_1 = require("../quota-mapper/quota-mapper.constants");
var child_age_gender_mapper_constants_1 = require("../child-age-gender-mapper.constants");
var qualification_codes_1 = require("../../shared/qualification-codes");
var util_1 = require("../../shared/util");
var children_1 = require("../children/children");
var QualificationChildGenderAgeMapper = /** @class */ (function () {
function QualificationChildGenderAgeMapper() {
}
/**
* Map old children qualification to new child gender age qualification.
*/
QualificationChildGenderAgeMapper.prototype.toChildAgeGender = function (qualifications) {
return qualifications.map(this.toChildAgeGenderMapper.bind(this));
};
/**
* Map old children qualification to new child gender age qualification.
* Mapper function, use to map one.
*/
QualificationChildGenderAgeMapper.prototype.toChildAgeGenderMapper = function (qualification) {
if (qualification.qualification_code === quota_mapper_constants_1.HAVE_CHILDREN_QUALIFICATION_ID) {
var mappedQualification = (0, util_1.clone)(qualification);
mappedQualification.condition_codes =
this.childrenConditionCodesToChildGenderAgeConditionCodes(mappedQualification.condition_codes);
return mappedQualification;
}
return qualification;
};
/**
* Core mapper of old children qualification to new child gender age qualification
*/
QualificationChildGenderAgeMapper.prototype.childrenConditionCodesToChildGenderAgeConditionCodes = function (conditionCodes) {
var yesHaveChildren = conditionCodes.some(function (code) { return Number(code) === quota_mapper_constants_1.YES_HAVE_CHILDREN_CONDITION_CODE; });
var noHaveChildren = conditionCodes.some(function (code) { return Number(code) === quota_mapper_constants_1.NO_HAVE_CHILDREN_CONDITION_CODE; });
var childGenderAgeConditionCodes = [];
if (yesHaveChildren) {
var allBabies = children_1.Children.create(true, children_1.ChildGenderEnum.both, 1, child_age_gender_mapper_constants_1.MONTHS).toEachChild();
var allChildren = children_1.Children.create(false, children_1.ChildGenderEnum.both, 1, child_age_gender_mapper_constants_1.YEARS).toEachChild();
var allChildrenConditionCodes = allBabies
.concat(allChildren)
.map(function (_a) {
var code = _a.code;
return code.toString();
});
childGenderAgeConditionCodes.push.apply(childGenderAgeConditionCodes, tslib_1.__spreadArray([], tslib_1.__read(allChildrenConditionCodes), false));
}
if (noHaveChildren) {
childGenderAgeConditionCodes.push(child_age_gender_mapper_constants_1.NO_CHILDREN_CODE.toString());
}
return childGenderAgeConditionCodes;
};
/**
* Map new child gender age qualification to old children qualification.
*/
QualificationChildGenderAgeMapper.prototype.toChildren = function (databaseQualifications) {
return databaseQualifications.map(this.toChildrenMapper.bind(this));
};
/**
* Map new child gender age qualification to old children qualification.
* Mapper function, use to map one.
*/
QualificationChildGenderAgeMapper.prototype.toChildrenMapper = function (qualification) {
if (qualification.qualification_code === child_age_gender_mapper_constants_1.CHILD_AGE_GENDER_QUALIFICATION_ID) {
var mappedQualification = (0, util_1.clone)(qualification);
mappedQualification.qualification_code = quota_mapper_constants_1.HAVE_CHILDREN_QUALIFICATION_ID;
mappedQualification.q_name = quota_mapper_constants_1.HAVE_CHILDREN_QUALIFICATION_NAME;
var conditions = mappedQualification.conditions;
if (!conditions || !conditions.length) {
throw new Error('Invalid qualification: child gender age qualification does not have conditions defined');
}
var childrenConditionCodes = this.childGenderAgeConditionCodesToChildrenConditionCodes(conditions.map(function (_a) {
var id = _a.id;
return id;
}));
mappedQualification.conditions = childrenConditionCodes.map(function (code) { return ({
id: code,
}); });
return mappedQualification;
}
return qualification;
};
/**
* Core mapper of new child gender age qualification to old children qualification
*/
QualificationChildGenderAgeMapper.prototype.childGenderAgeConditionCodesToChildrenConditionCodes = function (conditionCodes) {
var yesHaveChildren = conditionCodes.some(function (code) {
return qualification_codes_1.CHILD_AGE_GENDER.BOYS_CONDITIONS_CODES.includes(Number(code)) ||
qualification_codes_1.CHILD_AGE_GENDER.GIRLS_CONDITIONS_CODES.includes(Number(code));
});
var noHaveChildren = conditionCodes.some(function (code) { return Number(code) === child_age_gender_mapper_constants_1.NO_CHILDREN_CODE; });
var childrenConditionCodes = [];
if (yesHaveChildren) {
childrenConditionCodes.push(quota_mapper_constants_1.YES_HAVE_CHILDREN_CONDITION_CODE.toString());
}
if (noHaveChildren) {
childrenConditionCodes.push(quota_mapper_constants_1.NO_HAVE_CHILDREN_CONDITION_CODE.toString());
}
return childrenConditionCodes;
};
return QualificationChildGenderAgeMapper;
}());
exports.QualificationChildGenderAgeMapper = QualificationChildGenderAgeMapper;