import { DemoChildGenerator } from "../children/demo-data-generators/demo-child-generator.service" ; import { DemoDataGenerator } from "../../core/demo-data/demo-data-generator" ; import { Injectable } from "@angular/core" ; import { Child } from "../children/model/child" ; import { faker } from "../../core/demo-data/faker" ; import { Aser } from "./model/aser" ; import { ConfigurableEnumValue } from "../../core/configurable-enum/configurable-enum.interface" ; import { mathLevels } from "./model/mathLevels" ; import { readingLevels } from "./model/readingLevels" ; import { WarningLevel } from "../../core/entity/model/warning-level" ; /**
 * Generate ASER results every 12 months for each Child until passing.
 * Builds upon the generated demo Child entities.
 */ @ Injectable ( ) export class DemoAserGeneratorService extends DemoDataGenerator < Aser > { /**
     * This function returns a provider object to be used in an Angular Module configuration:
     *   `providers: [DemoAserGeneratorService.provider()]`
     */ static provider ( ) { return [ { provide : DemoAserGeneratorService , useClass : DemoAserGeneratorService } , ] ; } constructor ( private demoChildren ) { super ( ) ; } public generateEntities ( ) { const data = [ ] ; for ( const child of this . demoChildren . entities ) { data . push ( ... this . generateAserResultsForChild ( child ) ) ; } return data ; } private generateAserResultsForChild ( child ) { const data = [ ] ; let date = new Date ( child . admissionDate . getTime ( ) ) ; let previousResult = new Aser ( "" ) ; const firstLanguage = child [ "motherTongue" ] . toLowerCase ( ) ; do { const aserResult = new Aser ( ) ; aserResult . child = child . getId ( ) ; aserResult . date = date ; aserResult . math = this . selectNextSkillLevel ( mathLevels . slice ( 1 ) , previousResult . math ) ; aserResult . english = this . selectNextSkillLevel ( readingLevels . slice ( 1 ) , previousResult . english ) ; aserResult [ firstLanguage ] = this . selectNextSkillLevel ( readingLevels . slice ( 1 ) , previousResult [ firstLanguage ] ) ; data . push ( aserResult ) ; date = new Date ( date . getFullYear ( ) + 1 , 2 , 1 ) ; previousResult = aserResult ; } while ( date < faker . getEarlierDateOrToday ( child . dropoutDate ) && previousResult . getWarningLevel ( ) !== WarningLevel . OK ) ; return data ; } param skillRange param previousSkillLevel private selectNextSkillLevel ( skillRange , previousSkillLevel ) { const previousSkillLevelIndex = skillRange . indexOf ( previousSkillLevel ) ; let nextSkillLevelIndex ; const random = faker . datatype . number ( 100 ) ; if ( random < 20 ) { nextSkillLevelIndex = previousSkillLevelIndex ; } else if ( random < 90 ) { nextSkillLevelIndex = previousSkillLevelIndex + 1 ; } else { nextSkillLevelIndex = previousSkillLevelIndex + 2 ; } return skillRange [ this . trimToValidIndex ( nextSkillLevelIndex , skillRange ) ] ; } param index param array private trimToValidIndex ( index , array ) { if ( index < 0 ) { return 0 ; } return Math . min ( index , array . length - 1 ) ; } }