import BaseEntity from '../../common/entities/BaseEntity'; import ValidableInterface from '../../common/interfaces/ValidableInterface'; import Journey from '../../Journey/entity/Journey'; import Concession from '../../Concession/entity/Concession'; import JourneyGroupType from '../enums/JourneyGroupTypes'; export default class JourneyGroup extends BaseEntity implements ValidableInterface { protected _name: string; protected _journeys: Array = []; protected _concessions: Array; protected _settings: any = { allow_outbound_only: true, allow_return_only: true, allow_return: true }; protected _type: JourneyGroupType; protected _has_concessions_edited: boolean; get name(): string { return this._name; } set name(value: string) { this._name = value; } get journeys(): Array { return this._journeys; } set journeys(value: Array) { this._journeys = value; } get concessions(): Array { return this._concessions; } set concessions(value: Array) { this._concessions = value; } get settings(): any { return this._settings; } set settings(value: any) { this._settings = value; } get type(): JourneyGroupType { return this._type; } set type(value: JourneyGroupType) { this._type = value; } get has_concessions_edited(): boolean { return this._has_concessions_edited; } set has_concessions_edited(value: boolean) { this._has_concessions_edited = value; } isFullReturn() { return this._settings.allow_return; } isOnlyFullReturn() { return this._settings.allow_return && !this._settings.allow_outbound_only && !this._settings.allow_return_only; } isOutboundOnly() { return this._settings.allow_outbound_only; } isOnlyOutboundOnly() { return !this._settings.allow_return && this._settings.allow_outbound_only && !this._settings.allow_return_only; } isReturnOnly() { return this._settings.allow_return_only; } isOnlyReturnOnly() { return !this._settings.allow_return && !this._settings.allow_outbound_only && this._settings.allow_return_only; } hasLeg1AndLeg2() { return this._journeys[0] && this._journeys[0].enabled && this._journeys[1] && this._journeys[1].enabled; } hasOnlyLeg1() { return this._journeys[0] && this._journeys[0].enabled && (!this._journeys[1] || !this._journeys[1].enabled); } hasOnlyLeg2() { return (!this._journeys[0] || !this._journeys[0].enabled) && this._journeys[1] && this._journeys[1].enabled; } private buildConcessionsArray() { if (!Array.isArray(this.concessions)) { this.concessions = []; } } addConcession(item: Concession) { this.buildConcessionsArray(); this.concessions.push(item); } setConcession(index, item: Concession) { this.buildConcessionsArray(); this.concessions[index] = item; } removeConcession(index) { this.buildConcessionsArray(); this.concessions.splice(index, 1); } addJourney(item: Journey) { this.journeys.push(item); } removeJourney(index: number) { this.journeys.splice(index, 1); } setJourney(index, item: Journey) { this.journeys[index] = item; } hasOverridenConcessions() { return this.has_concessions_edited; } isValid() { return this.invalidFields().length === 0; } invalidFields(prefix = '') { let fields = []; // this.journeys.map((eachJourney, i) => fields.push(...eachJourney.invalidFields(`${prefix}journeys[${i}]__`))); return fields; } }