Sha256: 3594518b961b8735aa902fe9cadf0dd130aa0c652e1974c54341ae373b87c5b2

Contents?: true

Size: 1.42 KB

Versions: 10

Compression:

Stored size: 1.42 KB

Contents

const mongoose = require('mongoose/browser');
const { PatientEntity } = require('../attributes/PatientEntity');
const { Practitioner } = require('../attributes/Practitioner');
const { CarePartner } = require('../attributes/CarePartner');
const { Organization } = require('../attributes/Organization');

function AnyEntity(key, options) {
  mongoose.SchemaType.call(this, key, options, 'AnyEntity');
}
AnyEntity.prototype = Object.create(mongoose.SchemaType.prototype);

AnyEntity.prototype.cast = (entity) => {
  if (entity == null) {
    return null;
  }

  if (entity instanceof PatientEntity || entity instanceof Practitioner || entity instanceof CarePartner || entity instanceof Organization) {
    return entity;
  }

  if (entity._type != null) {
    // copy _id to id if it isn't defined
    if (entity.id == null && entity._id != null) {
      entity.id = entity._id;
    }
    switch (entity._type) {
      case 'QDM::PatientEntity':
        return new PatientEntity(entity);
      case 'QDM::Practitioner':
        return new Practitioner(entity);
      case 'QDM::CarePartner':
        return new CarePartner(entity);
      case 'QDM::Organization':
        return new Organization(entity);
      default:
        throw new Error(`Could not find entity type "${entity._type}".`);
    }
  } else {
    throw new Error('Could not find _type indicator for entity.');
  }
};

mongoose.Schema.Types.AnyEntity = AnyEntity;
module.exports = AnyEntity;

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
cqm-models-3.1.2 app/assets/javascripts/basetypes/AnyEntity.js
cqm-models-3.1.1 app/assets/javascripts/basetypes/AnyEntity.js
cqm-models-3.1.0 app/assets/javascripts/basetypes/AnyEntity.js
cqm-models-3.0.6 app/assets/javascripts/basetypes/AnyEntity.js
cqm-models-3.0.5 app/assets/javascripts/basetypes/AnyEntity.js
cqm-models-3.0.4 app/assets/javascripts/basetypes/AnyEntity.js
cqm-models-3.0.3 app/assets/javascripts/basetypes/AnyEntity.js
cqm-models-3.0.2 app/assets/javascripts/basetypes/AnyEntity.js
cqm-models-3.0.1 app/assets/javascripts/basetypes/AnyEntity.js
cqm-models-3.0.0 app/assets/javascripts/basetypes/AnyEntity.js