File

src/app/modules/core/services/course/course.service.ts

Description

Service for course API calls.

Constructor

constructor(userService: UserService, learnerService: LearnerService, config: any, contentService: ContentService)

the "constructor"

Parameters :

Methods

Public getEnrolledCourses
getEnrolledCourses()

api call for enrolled courses.

Returns: void
Public initialize
initialize()

call enroll course api and subscribe. Behavior subject will emit enrolled course data

Returns: void
Public getQRCodeFile
getQRCodeFile()

api call for getting course QR code CSV file.

Returns: void
Public updateCourseProgress
updateCourseProgress(courseId: any, batchId: any, Progress: any)
Returns: void
Public setExtContentMsg
setExtContentMsg(isExtContent: boolean)
Returns: void
findEnrolledCourses
findEnrolledCourses(courseId: any)
Returns: void

Properties

Private _enrolledCourseData$
_enrolledCourseData$: BehaviorSubject<IEnrolledCourses>

BehaviorSubject Containing enrolled courses.

Private config
config: any

To get url, app configs.

Public enrolledCourseData$
enrolledCourseData$: Observable<IEnrolledCourses>

Read only observable Containing enrolled courses.

Private enrolledCourses
enrolledCourses: ICourses[]
Private learnerService
learnerService: LearnerService

To do learner service api call.

showExtContentMsg
showExtContentMsg: boolean
Default value: false

Notification message for external content onclick of Resume course button

userid
userid: string

user id

Private userService
userService: UserService

To get details about user profile.

import { catchError, map, skipWhile } from 'rxjs/operators';
import { Injectable } from '@angular/core';
import { Observable, BehaviorSubject } from 'rxjs';
import { LearnerService } from './../learner/learner.service';
import { UserService } from './../user/user.service';
import { ConfigService, ServerResponse } from '@sunbird/shared';
import { IEnrolledCourses, ICourses } from './../../interfaces';
import { ContentService } from '../content/content.service';
import {throwError as observableThrowError } from 'rxjs';
import * as _ from 'lodash-es';
/**
 *  Service for course API calls.
 */
@Injectable({
  providedIn: 'root'
})
export class CoursesService {
  private enrolledCourses: Array<ICourses>;
  /**
   * To get details about user profile.
   */
  private userService: UserService;
  /**
   *  To do learner service api call.
   */
  private learnerService: LearnerService;
  /**
   *  To get url, app configs.
   */
  private config: ConfigService;
  /**
   * user id
   */
  userid: string;
  /**
   * BehaviorSubject Containing enrolled courses.
   */
  private _enrolledCourseData$ = new BehaviorSubject<IEnrolledCourses>(undefined);
  /**
   * Read only observable Containing enrolled courses.
   */
  public readonly enrolledCourseData$: Observable<IEnrolledCourses> = this._enrolledCourseData$.asObservable()
  .pipe(skipWhile(data => data === undefined || data === null));
  /**
   * Notification message for external content onclick of Resume course button
   */
  showExtContentMsg = false;
  /**
  * the "constructor"
  *
  * @param {LearnerService} learnerService Reference of LearnerService.
  * @param {UserService} userService Reference of UserService.
  * @param {ConfigService} config Reference of ConfigService
  */
  constructor(userService: UserService, learnerService: LearnerService,
    config: ConfigService, contentService: ContentService) {
    this.config = config;
    this.userService = userService;
    this.learnerService = learnerService;
    this.userid = this.userService.userid;
  }
  /**
   *  api call for enrolled courses.
   */
  public getEnrolledCourses() {
    const option = {
      url: this.config.urlConFig.URLS.COURSE.GET_ENROLLED_COURSES + '/' + this.userid,
      param: { ...this.config.appConfig.Course.contentApiQueryParams, ...this.config.urlConFig.params.enrolledCourses }
    };
    return this.learnerService.get(option).pipe(
      map((apiResponse: ServerResponse) => {
        this.enrolledCourses = apiResponse.result.courses;
        this._enrolledCourseData$.next({ err: null, enrolledCourses: this.enrolledCourses });
        return apiResponse;
      }),
      catchError((err) => {
        this._enrolledCourseData$.next({ err: err, enrolledCourses: undefined });
        return err;
      }));
  }
  /**
   *  call enroll course api and subscribe. Behavior subject will emit enrolled course data
  */
  public initialize() {
    this.getEnrolledCourses().subscribe((date) => {
    });
  }

   /**
   *  api call for getting course QR code CSV file.
   */
  public getQRCodeFile() {
    const userId = [this.userService.userid] ;
    const option = {
      url: this.config.urlConFig.URLS.COURSE.GET_QR_CODE_FILE,
      data: {
        'request': {
          'filter': {
            'userIds': userId
          }
        }
      }
    };
    return this.learnerService.post(option).pipe(
      map((apiResponse: ServerResponse) => {
        return apiResponse;
      }),
      catchError((err) => {
        return observableThrowError(err);
      }));
  }

  public updateCourseProgress(courseId, batchId, Progress) {
    const index = _.findIndex(this.enrolledCourses, {courseId: courseId, batchId: batchId });
    if (this.enrolledCourses[index]) {
      this.enrolledCourses[index].progress = Progress;
      this._enrolledCourseData$.next({ err: null, enrolledCourses: this.enrolledCourses });
    }
  }
  public setExtContentMsg(isExtContent: boolean) {
    this.showExtContentMsg = isExtContent ? isExtContent : false;
  }
  findEnrolledCourses(courseId) {
    const enrInfo = _.reduce(this.enrolledCourses, (acc, cur) => {
      if (cur.courseId !== courseId) { // course donst match return
        return acc;
      }
      if (cur.batch.enrollmentType === 'invite-only') { // invite-only batch
        if (cur.batch.status === 2) { // && (!acc.invite.ended || latestCourse(acc.invite.ended.enrolledDate, cur.enrolledDate))
          acc.inviteOnlyBatch.expired.push(cur);
          acc.expiredBatchCount = acc.expiredBatchCount + 1;
        } else {
          acc.onGoingBatchCount = acc.onGoingBatchCount + 1;
          acc.inviteOnlyBatch.ongoing.push(cur);
        }
      } else {
        if (cur.batch.status === 2) {
          acc.expiredBatchCount = acc.expiredBatchCount + 1;
          acc.openBatch.expired.push(cur);
        } else {
          acc.onGoingBatchCount = acc.onGoingBatchCount + 1;
          acc.openBatch.ongoing.push(cur);
        }
      }
      return acc;
    }, { onGoingBatchCount: 0, expiredBatchCount: 0,
      openBatch: { ongoing: [], expired: []}, inviteOnlyBatch: { ongoing: [], expired: [] }});
    return enrInfo;
  }
}

results matching ""

    No results matching ""