angular/projects/researchdatabox/portal-ng-common/src/lib/record.service.ts
        
Record Service
Author: Shilo Banihit
| Properties | 
| 
 | 
| Methods | 
| 
 | 
| constructor(http: HttpClient, rootContext: string, utilService: UtilityService, configService: ConfigService, loggerService: LoggerService) | ||||||||||||||||||
| 
                                    Parameters :
                                     
 | 
| Public Async destroyDeletedRecord | ||||||
| destroyDeletedRecord(oid: string) | ||||||
| 
                                Parameters :
                                
                                 
 
                            Returns :      unknown | 
| Public Async getAllDashboardTypes | 
| getAllDashboardTypes() | 
| 
                            Returns :      unknown | 
| Public Async getAllTypes | 
| getAllTypes() | 
| 
                            Returns :      unknown | 
| Public Async getDashboardType | ||||||
| getDashboardType(dashboardType: string) | ||||||
| 
                                Parameters :
                                
                                 
 
                            Returns :      unknown | 
| Public Async getDeletedRecords | ||||||||||||||||||||||||||||||||||||
| getDeletedRecords(recordType: string, state: string, pageNumber: number, packageType: string, sort: string, filterFields: string, filterString: string, filterMode: string) | ||||||||||||||||||||||||||||||||||||
| 
                                Parameters :
                                
                                 
 
                            Returns :          Promise<RecordResponseTable> | 
| Private getDocMetadata | ||||||
| getDocMetadata(doc: any) | ||||||
| 
                                Parameters :
                                
                                 
 
                            Returns :          any | 
| Private getHttpOptions | 
| getHttpOptions() | 
| Retrieves the default HTTP request options that includes the CSRF-TOKEN-HEADER. 
                            Returns :          anyA cloned copy of the default request options. | 
| Public Async getRecords | ||||||||||||||||||||||||||||||||||||||||
| getRecords(recordType: string, state: string, pageNumber: number, packageType: string, sort: string, filterFields: string, filterString: string, filterMode: string, secondarySort: string) | ||||||||||||||||||||||||||||||||||||||||
| 
                                Parameters :
                                
                                 
 
                            Returns :      unknown | 
| Public Async getRelatedRecords | ||||||
| getRelatedRecords(oid: string) | ||||||
| 
                                Parameters :
                                
                                 
 
                            Returns :      unknown | 
| Public Async getWorkflowSteps | ||||||
| getWorkflowSteps(name: string) | ||||||
| 
                                Parameters :
                                
                                 
 
                            Returns :      unknown | 
| Public Async restoreDeletedRecord | ||||||
| restoreDeletedRecord(oid: string) | ||||||
| 
                                Parameters :
                                
                                 
 
                            Returns :      unknown | 
| Public Async waitForInit | 
| waitForInit() | 
| Inherited from          HttpClientService | 
| Defined in          HttpClientService:54 | 
| 
                            Returns :      Promise<any> | 
| Protected enableCsrfHeader | 
| enableCsrfHeader() | 
| Inherited from          HttpClientService | 
| Defined in          HttpClientService:110 | 
| Call from extending class to enable CSRF in the header 
                            Returns :          void | 
| Public getConfig | 
| getConfig() | 
| Inherited from          HttpClientService | 
| Defined in          HttpClientService:74 | 
| Returns the config block 
                            Returns :          any | 
| Public getInitSubject | 
| getInitSubject() | 
| Inherited from          HttpClientService | 
| Defined in          HttpClientService:78 | 
| 
                            Returns :      Subject<any> | 
| Public isInitializing | 
| isInitializing() | 
| Inherited from          HttpClientService | 
| Defined in          HttpClientService:104 | 
| Default checks if we've loaded the config. Extensions can add more conditions as needed. 
                            Returns :          booleantrue if service is ready | 
| Private requestOptions | 
| Type : any | 
| Default value : null as any | 
| Public rootContext | 
| Type : string | 
| Decorators : 
                            @Inject(APP_BASE_HREF) | 
| Inherited from          HttpClientService | 
| Defined in          HttpClientService:45 | 
| Public baseUrl | 
| Type : string | 
| Inherited from          HttpClientService | 
| Defined in          HttpClientService:47 | 
| Public baseUrlWithContext | 
| Type : string | 
| Inherited from          HttpClientService | 
| Defined in          HttpClientService:49 | 
| Public brandingAndPortalUrl | 
| Type : string | 
| Inherited from          HttpClientService | 
| Defined in          HttpClientService:48 | 
| Protected config | 
| Type : any | 
| Inherited from          HttpClientService | 
| Defined in          HttpClientService:46 | 
| Protected httpContext | 
| Type : HttpContext | 
| Default value : null as any | 
| Inherited from          HttpClientService | 
| Defined in          HttpClientService:50 | 
| Protected initSubject | 
| Type : any | 
| Inherited from          HttpClientService | 
| Defined in          HttpClientService:45 | 
| Protected reqOptsJsonBodyOnly | 
| Type : any | 
| Default value : {responseType: 'json', observe: 'body'} | 
| Inherited from          HttpClientService | 
| Defined in          HttpClientService:52 | 
import { map, firstValueFrom } from 'rxjs';
import { Injectable, Inject } from '@angular/core';
import { APP_BASE_HREF } from '@angular/common';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { ConfigService } from './config.service';
import { UtilityService } from './utility.service';
import { LoggerService } from './logger.service';
import { HttpClientService } from './httpClient.service';
import { merge as _merge, isUndefined as _isUndefined, isEmpty as _isEmpty, get as _get, isArray as _isArray, clone as _clone, isString as _isString, isNumber as _isNumber } from 'lodash-es';
import { RecordResponseTable } from "./dashboard-models";
export interface RecordTypeConf {
  name: string;
}
/**
 * Record Service
 *
 * Author: <a href='https://github.com/shilob' target='_blank'>Shilo Banihit</a>
 */
@Injectable()
export class RecordService extends HttpClientService {
  private requestOptions: any = null as any;
  constructor(
    @Inject(HttpClient) protected override http: HttpClient,
    @Inject(APP_BASE_HREF) public override rootContext: string,
    @Inject(UtilityService) protected override utilService: UtilityService,
    @Inject(ConfigService) protected override configService: ConfigService,
    @Inject(LoggerService) private loggerService: LoggerService
  ) {
    super(http, rootContext, utilService, configService);
    this.requestOptions = { responseType: 'json', observe: 'body' };
  }
  public override async waitForInit(): Promise<any> {
    await super.waitForInit();
    this.enableCsrfHeader();
    this.loggerService.debug('waitForInit RecordService');
    _merge(this.requestOptions, { context: this.httpContext });
    return this;
  }
  public async getWorkflowSteps(name: string) {
    let url = `${this.brandingAndPortalUrl}/record/wfSteps/${name}`;
    const result$ = this.http.get(url).pipe(map(res => res));
    let result = await firstValueFrom(result$);
    return result;
  }
  private getDocMetadata(doc: any) {
    let metadata: any = {};
    for (var key in doc) {
      if (key.indexOf('authorization_') != 0 && key.indexOf('metaMetadata_') != 0) {
        metadata[key] = doc[key];
      }
      if (key == 'authorization_editRoles') {
        metadata[key] = doc[key];
      }
    }
    return metadata;
  }
  public async getRelatedRecords(oid: string) {
    let url = `${this.brandingAndPortalUrl}/record/${oid}/relatedRecords`;
    const result$ = this.http.get(url).pipe(map(res => res));
    let relatedRecords = await firstValueFrom(result$);
    let response: any = {};
    let items = [];
    let childOrTreeLevel2: any = _get(relatedRecords, 'processedRelationships');
    for (let childNameStr of childOrTreeLevel2) {
      let childArr = _get(relatedRecords, 'relatedObjects.' + childNameStr);
      if (!_isUndefined(childArr) && _isArray(childArr)) {
        for (let child of childArr) {
          let item: any = {};
          item["oid"] = child["redboxOid"];
          item["title"] = child["metadata"]["title"];
          item["metadata"] = this.getDocMetadata(child);
          item["dateCreated"] = child["dateCreated"];
          item["dateModified"] = child["lastSaveDate"];
          //TODO double check that this is needed or not
          // item["hasEditAccess"] = RecordsService.hasEditAccess(brand, user, roles, doc);
          items.push(item);
        }
      }
    }
    response["items"] = items;
    return response;
  }
  public async getRecords(
    recordType: string,
    state: string,
    pageNumber: number,
    packageType: string = '',
    sort: string = '',
    filterFields: string = '',
    filterString: string = '',
    filterMode: string = '',
    secondarySort: string = '') {
    let rows = 10;
    let start = (pageNumber - 1) * rows;
    const items = {
      recordType: recordType,
      packageType: packageType,
      sort: sort,
      secondarySort: secondarySort,
      state: state,
      filterFields: filterFields,
      filter: filterString,
      filterMode: filterMode,
      start: start,
      rows: rows,
    };
    const listRecordsUrl = new URL(`${this.brandingAndPortalUrl}/listRecords`);
    for (const [key, value] of Object.entries(items)) {
      if (!_isUndefined(value)) {
        if (_isString(value) && !_isEmpty(value)) {
          listRecordsUrl.searchParams.set(key, value?.toString());
        } else {
          if (_isNumber(value)) {
            listRecordsUrl.searchParams.set(key, value?.toString());
          }
        }
      }
    }
    const result$ = this.http.get(listRecordsUrl.toString()).pipe(map(res => res));
    let result = await firstValueFrom(result$);
    return result;
  }
  public async getDeletedRecords(
    recordType: string,
    state: string,
    pageNumber: number,
    packageType: string = '',
    sort: string = '',
    filterFields: string = '',
    filterString: string = '',
    filterMode: string = ''
  ): Promise<RecordResponseTable> {
    let rows = 10;
    let start = (pageNumber - 1) * rows;
    const items = {
      recordType: recordType,
      packageType: packageType,
      sort: sort,
      state: state,
      filterFields: filterFields,
      filter: filterString,
      filterMode: filterMode,
      start: start,
      rows: rows,
    };
    const listDeletedRecordsUrl = new URL(`${this.brandingAndPortalUrl}/listDeletedRecords`);
    for (const [key, value] of Object.entries(items)) {
      if (!_isUndefined(value)) {
        if (_isString(value) && !_isEmpty(value)) {
          listDeletedRecordsUrl.searchParams.set(key, value?.toString());
        } else {
          if (_isNumber(value)) {
            listDeletedRecordsUrl.searchParams.set(key, value?.toString());
          }
        }
      }
    }
    const result$ = this.http.get(listDeletedRecordsUrl.toString()).pipe(map(res => res));
    let result: any = await firstValueFrom(result$);
    return result;
  }
  public async restoreDeletedRecord(oid: string) {
    const httpOptions = this.getHttpOptions();
    const restoreDeletedRecordUrl = new URL(`${this.brandingAndPortalUrl}/record/delete/${oid}`);
    const result$ = this.http.put(restoreDeletedRecordUrl.toString(), undefined, httpOptions).pipe(map(res => res));
    let result: any = await firstValueFrom(result$);
    return result;
  }
  public async destroyDeletedRecord(oid: string) {
    const httpOptions = this.getHttpOptions();
    const destroyDeletedRecordUrl = new URL(`${this.brandingAndPortalUrl}/record/destroy/${oid}`);
    const result$ = this.http.delete(destroyDeletedRecordUrl.toString(), httpOptions).pipe(map(res => res));
    let result: any = await firstValueFrom(result$);
    return result;
  }
  /**
   * Retrieves the default HTTP request options that includes the CSRF-TOKEN-HEADER.
   *
   * @returns {any} A cloned copy of the default request options.
   */
  private getHttpOptions(): any {
    return _clone(this.requestOptions);
  }
  public async getAllTypes() {
    let url = `${this.brandingAndPortalUrl}/record/type`;
    const result$ = this.http.get(url).pipe(map(res => res));
    let result: any = await firstValueFrom(result$);
    return result;
  }
  public async getDashboardType(dashboardType: string) {
    let url = `${this.brandingAndPortalUrl}/dashboard/type/${dashboardType}`;
    const result$ = this.http.get(url).pipe(map(res => res));
    let result = await firstValueFrom(result$);
    return result;
  }
  public async getAllDashboardTypes() {
    let url = `${this.brandingAndPortalUrl}/dashboard/type`;
    const result$ = this.http.get(url).pipe(map(res => res));
    let result = await firstValueFrom(result$);
    return result;
  }
}