File

lib/core/src/items/symptom-checks/symptom-check.class.ts

Extends

Item

Index

Properties
Methods
Accessors

Constructor

constructor(config: SymptomCheckConfig)
Parameters :
Name Type Optional
config SymptomCheckConfig No

Properties

Static acceptsAsConfig
Default value : isSymptomCheckConfig
Public id
Type : string
Inherited from Item
Defined in Item:41
Public meta
Type : SymptomCheckMeta
Public questionSchedules
Type : QuestionSchedule[]
Public update$
Default value : this.updateSubject.asObservable()
Inherited from Item
Defined in Item:48
Protected updateSubject
Default value : new Subject<string>()
Inherited from Item
Defined in Item:47
Private Optional _config
Type : C
Inherited from Item
Defined in Item:54

Methods

Public addQuestionId
addQuestionId(id: string, config?: ScheduleConfig)
Parameters :
Name Type Optional
id string No
config ScheduleConfig Yes
Returns : void
Public coversQuestionIds
coversQuestionIds(ids: string[])
Parameters :
Name Type Optional
ids string[] No
Returns : boolean
Public getDueQuestionIds
getDueQuestionIds(date: Date)
Parameters :
Name Type Optional
date Date No
Returns : string[]
Public togglePause
togglePause(force?: boolean)
Parameters :
Name Type Optional
force boolean Yes
Returns : void
Public update
update(message?: string)

Trigger emit message on .update$ observable.

Parameters :
Name Type Optional
message string Yes
Returns : void
Static acceptsAsConfig
acceptsAsConfig(x)
Inherited from Item
Defined in Item:27
Parameters :
Name Optional
x No
Static assertData
assertData(data)
Inherited from Item
Defined in Item:40
Parameters :
Name Optional
data No
Returns : never
Static findConfigs
findConfigs(data)
Inherited from Item
Defined in Item:32
Type parameters :
  • U
Parameters :
Name Optional
data No
Returns : U[]
Public matches
matches(query)
Inherited from Item
Defined in Item:80

TODO: Is this useful or needed anywhere?

Parameters :
Name Optional
query No
Returns : boolean
Public toJSON
toJSON()
Inherited from Item
Defined in Item:75
Returns : string

Accessors

config
getconfig()
setconfig(config: SymptomCheckConfig)
Parameters :
Name Type Optional
config SymptomCheckConfig No
Returns : void
questionIds
getquestionIds()
import	{	Item						}	from '../item.class'

import	{
			Schedule,
			ScheduleConfig
		}									from '../schedules'


import	{	Subject						}	from 'rxjs'

import	{
			SymptomCheckConfig,
			QuestionScheduleConfig,
			isSymptomCheckConfig,
			assertSymptomCheckConfig,
			isQuestionScheduleConfig,
			defaultSymptomCheckConfig
		}									from './symptom-checks.commons'



// Is there duplication with the interfaces in interfaces.ts?
// Answer: No. The following interfaces are NOT Configs! they make
// use of Classes like Date or Schedule.
export interface SymptomCheckMeta {
	label			: string 	| null
	paused			: boolean 	| null
	defaultSchedule	: Schedule 	| null
	creationDate	: Date 		| null
	reminder		: string	| null //HH:MM
}

export interface QuestionSchedule {
	questionId		: string,
	schedule		: Schedule
}


export class SymptomCheck extends Item<SymptomCheckConfig> {

	declare public id				:	string

	public meta!					:	SymptomCheckMeta
	public questionSchedules!		: 	QuestionSchedule[]


	protected 	updateSubject	= new Subject<string>()
	public 		update$			= this.updateSubject.asObservable()


	public static acceptsAsConfig = isSymptomCheckConfig

	constructor(config: SymptomCheckConfig = defaultSymptomCheckConfig){ super(config) }


	set config(config: SymptomCheckConfig){

		assertSymptomCheckConfig(config)

		this.meta = {
			label:				config.meta.label 		|| null,
			paused:				config.meta.paused 		|| false,
			reminder:			config.meta.reminder 	|| null,
			creationDate:		config.meta.creationDate
								? 	new Date(config.meta.creationDate)
								: 	null,
			defaultSchedule:	new Schedule(config.meta.defaultSchedule)
		}

		this.questionSchedules = []

		config.questions.forEach( (q: string | QuestionScheduleConfig) => {
			return	isQuestionScheduleConfig(q)
					?	this.addQuestionId(q.id, q.schedule)
					:	this.addQuestionId(q)
		})

		this.id = config.id

		this.update('set config')

	}

	get config(): SymptomCheckConfig {

		const meta		=	{
								label:				this.meta.label || null,
								creationDate:		this.meta.creationDate && this.meta.creationDate.toISOString() || null,
								reminder:			this.meta.reminder,
								paused:				this.meta.paused ? true : false,
								defaultSchedule:	this.meta.defaultSchedule && this.meta.defaultSchedule.config  || null
							}

		//Remove empty keys:
		for(const key in meta) if(meta[key as keyof typeof meta ] === null) delete meta[key as keyof typeof meta ]


		const	questions	=	this.questionSchedules.map( item => {

								return 	item.schedule && !item.schedule.matches(meta.defaultSchedule)
										?	{id: item.questionId, schedule:  item.schedule.config}
										:	item.questionId

							})

		const	id			=	this.id

		return 	{ meta, questions, id }
	}


	/**
	 * Trigger emit message on .update$ observable.
	 */
	public update(message?: string) : void {

		// update() may be called from the constructor
		// this.updateSubject will then not yet exist.
		if(this.updateSubject) this.updateSubject.next(message)
	}

	public addQuestionId(id: string, config?: ScheduleConfig): void {

		const schedule =	config && !this.meta.defaultSchedule.matches(config)
							?	new Schedule(config)
							:	this.meta.defaultSchedule

		this.questionSchedules.push({questionId: id, schedule })
	}

	public togglePause(force?: boolean) : void {
		this.meta.paused = 	typeof force == 'boolean'
							?	force
							:	!this.meta.paused

		this.update('meta.paused')
	}

	public coversQuestionIds(ids: string[]) : boolean {
		return	this.questionSchedules
				.map( questionSchedule => questionSchedule.questionId )
				.some( id => ids.includes(id) )
	}


	public getDueQuestionIds(date: Date): string[] {

		return	this.questionSchedules
				.filter(	qs => qs.schedule.matches(date) )
				.map( 		qs => qs.questionId )

	}


	get questionIds(): string[]	{ return this.questionSchedules.map( item => item.questionId) }


}

results matching ""

    No results matching ""