import { action, computed, makeObservable } from 'mobx'; import { WorkRecordData } from '../../interfaces' import { ElementData, ElementSchemaVersion } from '../../interfaces/element.interface' import { GPSCoordinates } from '../../interfaces/gis.interface' import { ElementGetQueryParams } from '../../services/edocu/elements' import { Element } from '../element' import { CheckPoint } from './checkPoint' import { Ticket } from './ticket' export enum ChecklistMultiplicity { SINGLE = 'single', GROUP = 'group', } // DEPRECATED V1 API export const START_CHECKLIST_ACTION = 'StartChecklist' export class CheckList extends Element { // eslint-disable-next-line @typescript-eslint/no-explicit-any constructor(...args: any) { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore: this is fine super(...args); makeObservable(this, { generateChecklistTicket: action, showChecklistTickets: action, isInterruptible: computed, allowSkipCheckpoints: computed, multiplicity: computed, }) } static get TYPE() { return 'Checklist' } children: CheckPoint[] = [] static get ATTRIBUTES() { return { ...Element.ATTRIBUTES, EXECUTION_DUE: 'execution_due', IS_INTERRUPTIBLE: 'is_uninterruptible', ALLOW_SKIP_CHECKPOINTS: 'allow_skip_checkpoints', PERSON: 'person', MULTIPLICITY: 'checklist_subject_multiplicity', // V1 OVAK deprecated RESPONSIBLE_PERSON: 'responsible_person_ate', VERSION: 'version', DESCRIPTION: 'description_of_the_use_of_the_procedure', SHOULD_RECORD_START: 'write_time_of_start_to_attribute', // V1 deprecated } } loadChecklistTypes() { return this.store.userStore.schemaVersion === ElementSchemaVersion.V1 ? [ this.store.elementStore.getElementTypeData(Ticket.TYPE), this.store.elementStore.getElementTypeData(CheckList.TYPE), this.store.elementStore.getElementTypeData(CheckPoint.TYPE), ] : [ this.store.elementStore.getElementSchema(Ticket.TYPE), this.store.elementStore.getElementSchema(CheckList.TYPE), this.store.elementStore.getElementSchema(CheckPoint.TYPE), ] } // DEPRECATED V1 API checklistTickets: Map = new Map() // DEPRECATED V1 API async generateChecklistTicket( element: Element, coordinates?: GPSCoordinates, ): Promise { if (this.store.userStore.schemaVersion !== ElementSchemaVersion.V1) { console.warn('showChecklistTickets is deprecated for schema version 2') } if (!element.type) { return } const locationData: { coordinates?: [number, number] } = {} if (coordinates) { locationData.coordinates = [coordinates.latitude, coordinates.longitude] } const workRecordData: WorkRecordData = { actionID: 'StartChecklist', content: this.elementName, metadata: { checklist: { hash: this.hash, }, ...locationData, }, } const promises = [ ...(this.loadChecklistTypes()), this.loadRaw(), ] const response = await this.store.workRecords.create( element.type, element.hash, this.store.userStore.organization.id, workRecordData, ) if (!response.ok) { // ERRRO HERE return } await Promise.all(promises) // load new tickets const tickets = await this.showChecklistTickets(element.hash) // assume first is the one just generated const newestTicket = tickets[0] await newestTicket.loadData() return newestTicket } // DEPRECATED V1 API showChecklistTickets = async (subjectElement: string): Promise => { if (this.store.userStore.schemaVersion !== ElementSchemaVersion.V1) { console.warn('showChecklistTickets is deprecated for schema version 2') } const tickets = await this.store.tickets.show({ checklist: this.hash, element: subjectElement, limit: 5, }) if (!tickets.ok || !tickets.data?.data.length) { return [] } const elementTickets = tickets.data?.data.map((ticketData) => { const ticketElement = this.store.elementStore.instantiateElement( ticketData.hash, { _type: Ticket.TYPE, organization: ticketData.organization, } as ElementData, ) as Ticket ticketElement.setTicketData(ticketData) return ticketElement }) this.checklistTickets.set(subjectElement, elementTickets) return elementTickets } get isInterruptible() { const { IS_INTERRUPTIBLE } = (this.constructor as typeof CheckList).ATTRIBUTES return !this.attributeExists(IS_INTERRUPTIBLE) || this.getAttributeRawValue(IS_INTERRUPTIBLE) === 'Yes' ? true : false } get allowSkipCheckpoints() { return this.getAttributeRawValue( (this.constructor as typeof CheckList).ATTRIBUTES.ALLOW_SKIP_CHECKPOINTS, ) === 'Yes' ? true : false } get multiplicity(): ChecklistMultiplicity { return this.getAttributeRawValue( (this.constructor as typeof CheckList).ATTRIBUTES.MULTIPLICITY, ) as ChecklistMultiplicity } static GET_QUERY_PARAMS(): ElementGetQueryParams { return { childrenAtt: Object.values(CheckPoint.ATTRIBUTES), } } } // DEPRECATED V1 API export const getConditionalData = ( ticket: Ticket, ): { value: string; hash: string } | null => { const checklist = ticket.ticketData?.checklist?.value const value = checklist?.[CheckPoint.ATTRIBUTES.CONDITIONAL_VALUE] as string const hash = checklist?.[CheckPoint.ATTRIBUTES.CONDITIONAL_ATE] as string if (!checklist || !value || !hash) { return null } return { value, hash, } }