Source: clinicalServiceFactory.js

#!/usr/bin/env node

'use strict';

const AllergyService = require('./allergies/allergyService');
const ProblemService = require('./problems/problemService');
const VitalsService = require('./vitals/vitalsService');

/**
 *
 * The clinical service factory is used to create clinical service instances. Each clinical domain has its own
 * clinical service implementation.
 *
 * A clinical service factory keeps state by maintaining an internal service context object. This context object is
 * passed to a clinical service upon creation of the service instance.
 *
 * The userId and facilityId values are immutable and passed to the constructor.
 *
 * The patientId is mutable and can be changed through the selectPatient method.
 *
 */
class ClinicalServiceFactory {
    /**
     * Clinical service factory constructor.
     *
     * @param {Object} db VistA database instance.
     * @param {String} userId User identifier (e.g. 200-61)
     * @param {String} facilityId Facility identifier. (e.g. 4-2536)
     */
    constructor(db, userId, facilityId) {
        this.db = db;

        this.serviceContext = { // set user and facility ids in the service context
            userId,
            facilityId,
        };

        // private methods

        // security checks
        this.checkForUser = function checkForUser() {
            if (!this.serviceContext.userId || !this.serviceContext.facilityId) {
                throw new Error('No authenticated user');
            }
        };

        this.checkForUserAndPatient = function checkForUserAndPatient() {
            this.checkForUser();

            if (!this.serviceContext.patientId) {
                throw new Error('No selected patient');
            }
        };
    }

    /**
     * Selects a patient.
     *
     * This method currently just stores the provided patientId in the object's serviceContext.
     *
     * It does not communicate with fileman yet.
     *
     * TODO set patient TMP fileman values
     *
     * @param {String} patientId Patient identifier. (e.g. 2-61)
     */
    selectPatient(patientId) {
        this.checkForUser();

        // clear out patient context
        this.serviceContext.patientId = patientId;
    }

    /**
     * Creates an AllergyService instance.
     *
     * @returns {AllergyService} An AllergyService instance.
     * @throws Error if user hasn't been authenticated or a patient hasn't been selected.
     */
    createAllergyService() {
        this.checkForUserAndPatient();

        return new AllergyService(this.db, this.serviceContext);
    }

    /**
     * Creates a ProblemService instance.
     *
     * @returns {ProblemService} A ProblemService instance.
     * @throws Error if user hasn't been authenticated or a patient hasn't been selected.
     */
    createProblemService() {
        this.checkForUserAndPatient();

        return new ProblemService(this.db, this.serviceContext);
    }

    /**
     * Creates a VitalsService instance.
     *
     * @returns {VitalsService} A VitalsService instance.
     * @throws Error if user hasn't been authenticated or a patient hasn't been selected.
     */
    createVitalsService() {
        this.checkForUserAndPatient();

        return new VitalsService(this.db, this.serviceContext);
    }
}

module.exports = ClinicalServiceFactory;