Manual Reference Source Test

src/utilities/factories/ServiceFactory.js

// import Client from '../../Client';
import AppMenuService from '../../services/AppMenuService';
import EventService from '../../services/EventService';
import ConsultationService from '../../services/ConsultationService';
import DiscussService from '../../services/DiscussService';
import DeviceService from '../../services/DeviceService';
import JsonApiService from '../../services/JsonApiService';
import LocationService from '../../services/LocationService';
import LanguageService from '../../services/LanguageService';
import PlatformMenuService from '../../services/PlatformMenuService';
import RoleService from '../../services/RoleService';
import TemporaryIdService from '../../services/TemporaryIdService';
import UserService from '../../services/UserService';
// import { flattenJsonApi, getJsonApiType, isJsonApiFormat } from '../JsonApiUtility';

/**
 * Factory to convert responses to services
 */
class ServiceFactory {
  /** @type {Object} - A hashmap from object names to classes */
  static get map() {
    if (!ServiceFactory._map) {
      ServiceFactory._map = {
        // agreement__templates: JsonApiService,
        // agreement__template_fields: JsonApiService,
        // allowed_actions: JsonApiService,
        // awarded_badges: AwardedBadge,
        // commentable__comments: Comment,
        commentable__threads: DiscussService,
        video_consultations: ConsultationService,
        // data_collection__captured_value_groups: FlowDataGroup,
        // data_collection__flow_processes: Flow,
        // data_collection__linked_steps: FlowStep,
        devices: DeviceService,
        // dynamic_content__contents: Content,
        // dynamic_content__content_types: ContentType,
        // ediary__entries: Entry,
        // ediary__entry_groups: EntryGroup,
        // ediary__entry_templates: EntryTemplate,
        languages: LanguageService,
        locations: LocationService,
        mobile_users: UserService,
        menus: PlatformMenuService,
        navigation__app_menus: AppMenuService,
        // profiles: Profile,
        reminder__events: EventService,
        temporary_identifiers: TemporaryIdService,
        // reminder__rules: Rule,
        user_roles: RoleService,
        // scheduler__personalized_rule_schedules: Schedule,
        // trials__sites: JsonApiService,
        // trials__site_members: JsonApiService,
        users: UserService,
      };
    }
    return ServiceFactory._map;
  }

  static get options() {
    if (!ServiceFactory._options) {
      ServiceFactory._options = {
        adobe_sign__accounts: { title: 'SignAccountService' },
        agreement__templates: { title: 'AgreementTemplateService' },
        agreement__template_fields: { title: 'AgreementTemplateFieldService' },
        allowed_actions: { title: 'AllowedActionService' },
        badges: { title: 'BadgeService' },
        cohorts: { title: 'CohortService' },
        cohort_assignments: { title: 'CohortAssignmentService' },
        consent__approvers: { title: 'ConsentApproverService' },
        consent__approver_assignments: { title: 'ApproverAssignmentService' },
        consent__approver_group_assignments: { title: 'ApproverGroupAssignmentService' },
        consent__approver_groups: { title: 'ApproverGroupService' },
        consent__available_strategies: { title: 'AvailableStrategyService' },
        consent__forms: { title: 'ConsentFormService' },
        consent__form_versions: { title: 'ConsentFormVersionService' },
        consent__grants: { title: 'ConsentGrantService' },
        consent__additional_signatures: { title: 'ConsentAdditionalSignerService' },
        consent__strategies: { title: 'ConsentStrategyService' },
        dynamic_content__attribute_keys: { title: 'ContentAttributeService' },
        dynamic_content__content_types: { title: 'ContentTypeService' },
        dynamic_content__public_contents: { title: 'PublicContentsService' },
        ediary__entry_groups: { title: 'EDiaryService' },
        filters: { title: 'FilterService' },
        filter_groups: { title: 'FilterGroupService' },
        notification__deliveries: { title: 'NotificationsService' },
        settings: { title: 'SettingService' },
        sso_options: { title: 'SsoOptionService' },
        trials__sites: { title: 'SiteService' },
        trials__site_contacts: { title: 'SiteContactService' },
        trials__site_supported_languages: { title: 'SiteLanguageService' },
        trials__site_members: { title: 'SiteMemberService' },
        versions: { title: 'VersionService' },
        video_consultations: { title: 'ConsultationService' },
        video_consultation_participants: { title: 'ConsultationParticipantService' },
      };
    }
    return ServiceFactory._options;
  }

  /**
   * Convert the json to the appropriate class (deserialize)
   *
   * @param  {String} type      - The type to convert the object to (otherwise use json's data type)
   * @return {Promise<Object>}  - Returns an object with the appropriate type
   */
  get(type) {
    const _class = ServiceFactory.map[type] || JsonApiService;
    const options = {
      title: (new _class()).constructor.name,
      obj: this.getObjectName(type),
      tokenRequired: ['delete', 'get', 'insert', 'update', 'getChildren']
    };
    Object.assign(options, ServiceFactory.options[type] || {});
    return new _class(type, options);
  }

  getObjectName(type) {
    // return type.split('__')[type.split('__').length - 1].slice(0, -1);
    // Handle the case where name ends in ies, ie, strategies should be strategy, not strategie
    if (type.endsWith('ies')) {
      type = `${type.substring(0, type.length - 3)}y `;
    }
    const o = type.split('__')[type.split('__').length - 1].slice(0, -1);
    return o;
  }
}

/** @type {ServiceFactory} - An instance of ServiceFactory */
const serviceFactory = new ServiceFactory();

export { ServiceFactory, serviceFactory };