import { DatabaseEntity } from "../../../core/entity/database-entity.decorator" ; import { Entity } from "../../../core/entity/model/entity" ; import { DatabaseField } from "../../../core/entity/database-field.decorator" ; import { INTERACTION_TYPE_CONFIG_ID , InteractionType , } from "./interaction-type.interface" ; import { EventAttendance } from "../../attendance/model/event-attendance" ; import { AttendanceLogicalStatus , NullAttendanceStatusType , } from "../../attendance/model/attendance-status" ; import { User } from "../../../core/user/user" ; import { Child } from "../../children/model/child" ; import { ConfigurableEnumValue } from "../../../core/configurable-enum/configurable-enum.interface" ; import { getWarningLevelColor , WarningLevel , } from "../../../core/entity/model/warning-level" ; import { School } from "../../schools/model/school" ; @ DatabaseEntity ( "Note" ) export class Note extends Entity { static create ( date , subject = "" , children = [ ] ) { const instance = new Note ( ) ; instance . date = date ; instance . subject = subject ; for ( const child of children ) { instance . addChild ( child ) ; } return instance ; } /** IDs of Child entities linked with this note */ @ DatabaseField ( { label : $localize `:Label for the children of a note:Children` , viewComponent : "DisplayEntityArray" , editComponent : "EditEntityArray" , additional : Child . ENTITY_TYPE , } ) children : string [ ] = [ ] ; /**
     * optional additional information about attendance at this event for each of the linked children
     *
     * No direct access to change this property. Use the `.getAttendance()` method to have safe access.
     */ @ DatabaseField ( { innerDataType : "schema-embed" , additional : EventAttendance } ) private childrenAttendance : Map < string , EventAttendance > = new Map ( ) ; @ DatabaseField ( { label : $localize `:Label for the date of a note:Date` } ) date : Date ; @ DatabaseField ( { label : $localize `:Label for the subject of a note:Subject` } ) subject : string = "" ; @ DatabaseField ( { label : $localize `:Label for the actual notes of a note:Notes` , editComponent : "EditLongText" , } ) text : string = "" ; /** IDs of users that authored this note */ @ DatabaseField ( { label : $localize `:Label for the social worker(s) who created the note:SW` , viewComponent : "DisplayEntityArray" , editComponent : "EditEntityArray" , additional : User . ENTITY_TYPE , } ) authors : string [ ] = [ ] ; @ DatabaseField ( { label : $localize `:Label for the category of a note:Category` , dataType : "configurable-enum" , innerDataType : INTERACTION_TYPE_CONFIG_ID , } ) category : InteractionType = { id : "" , label : "" } ; /**
     * id referencing a different entity (e.g. a recurring activity) this note is related to
     */ @ DatabaseField ( ) relatesTo : string ; /**
     * related school ids (e.g. to infer participants for event roll calls)
     */ @ DatabaseField ( { label : "Groups" , additional : School . ENTITY_TYPE , } ) schools : string [ ] = [ ] ; @ DatabaseField ( { label : "" , dataType : "configurable-enum" , innerDataType : "warning-levels" , } ) warningLevel : ConfigurableEnumValue ; getWarningLevel ( ) { if ( this . warningLevel ) { return WarningLevel [ this . warningLevel . id ] ; } else { return WarningLevel . NONE ; } } public getColor ( ) { const actualLevel = this . getWarningLevel ( ) ; if ( actualLevel === WarningLevel . OK || actualLevel === WarningLevel . NONE ) { return this . category . color ; } else { return super . getColor ( ) ; } } public getColorForId ( childId ) { if ( this . category . isMeeting && this . childrenAttendance . get ( childId ) ?. status . countAs === AttendanceLogicalStatus . ABSENT ) { return getWarningLevelColor ( WarningLevel . URGENT ) ; } return this . getColor ( ) ; } param childId removeChild ( childId ) { this . children = this . children . filter ( ( c ) => c !== childId ) ; this . childrenAttendance . delete ( childId ) ; } param childId addChild ( childId ) { if ( this . children . includes ( childId ) ) { return ; } this . children = this . children . concat ( childId ) ; } param childId getAttendance ( childId ) { if ( ! this . children . includes ( childId ) ) { return undefined ; } let attendance = this . childrenAttendance . get ( childId ) ; if ( ! attendance ) { attendance = new EventAttendance ( ) ; this . childrenAttendance . set ( childId , attendance ) ; } if ( ! ( attendance instanceof EventAttendance ) ) { attendance = Object . assign ( new EventAttendance ( ) , attendance ) ; } return attendance ; } /**
     * Whether the attendance context information available through `getAttendance` is missing data for some children.
     *
     * While getAttendance will always set and return at least a default value `hasUnknownAttendances` can be used
     * to flag events with incomplete data.
     */ hasUnknownAttendances ( ) { if ( this . childrenAttendance . size < this . children . length ) { return true ; } else { for ( const v of this . childrenAttendance . values ( ) ) { if ( v . status . id === NullAttendanceStatusType . id ) { return true ; } } } return false ; } param status returns countWithStatus ( status ) { const attendanceValues = this . childrenAttendance . values ( ) ; return [ ... attendanceValues ] . filter ( ( attendance ) => attendance . status . countAs === status ) . length ; } /**
     * Performs a deep copy of the note copying all simple data
     * (such as the date, author, e.t.c.) as well as copying the
     * child-array
     */ copy ( ) { const note = super . copy ( ) as Note ; note . children = [ ... this . children ] ; return note ; } }