license.service.ts 15.9 KB
import { Injectable, Inject } from '@angular/core';
//import { HttpClient, HttpParams, HttpRequest} from "@angular/common/http";
import { Http, Response, Headers, RequestOptions, HttpModule } from '@angular/http';
import { DatePipe } from '@angular/common';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';
import 'rxjs/add/operator/do';
import { Observable } from 'rxjs/Observable';
import { GlobalService } from '../../shared/global';

@Injectable()
export class LicenseService{
   
    datePipe: DatePipe = new DatePipe('en-US');
    
    constructor(private http: Http, private commonService: GlobalService ) { }
  
    GetLicenses(obj: any, pageNo: number, pageLength: number) {
        if(obj.subscriptionStartDate == ''){
            obj.subscriptionStartDate = '1/1/1';
        }
        if(obj.subscriptionEndDate == ''){
            obj.subscriptionEndDate = '1/1/9999';
        }
        obj.subscriptionStartDate = this.datePipe.transform(obj.subscriptionStartDate, 'MM/dd/yyyy');
        obj.subscriptionEndDate = this.datePipe.transform(obj.subscriptionEndDate, 'MM/dd/yyyy');
        return this.http.get(this.commonService.resourceBaseUrl + "License/Licenses?accountNumber=" + obj.accountNumber +
        "&licenseeFirstName=" + obj.licenseeFirstName + "&licenseeLastName=" + obj.licenseeLastName +
        "&licenseTypeId=" + obj.licenseTypeId + "&institutionName=" + obj.institutionName +
        "&stateId=" + obj.stateId + "&countryId=" + obj.countryId + "&emailId=" + obj.emailId + 
        "&subscriptionStartDate=" + obj.subscriptionStartDate + "&subscriptionEndDate=" + obj.subscriptionEndDate + 
          "&isActive=" + obj.isActive + "&pageNo=" + pageNo + "&pageLength=" + pageLength)
        .map(this.extractData)
        .catch((res: Response) => this.handleError(res));
    }

    GetLicenseById(Id: number){
        return this.http.get(this.commonService.resourceBaseUrl + "License/GetLicense?LicenseId=" + Id)
        .map(this.extractData)
        .catch((res: Response) => this.handleError(res));
    }

    GetLicenceType()
    {        
        return this.http.get(this.commonService.resourceBaseUrl + "License/LicenseTypes")
            .map(this.extractData)
            .catch((res: Response) => this.handleError(res));
    }

    GetAccountType()
    {        
        return this.http.get(this.commonService.resourceBaseUrl + "Account/AccountTypes")
            .map(this.extractData)
            .catch((res: Response) => this.handleError(res));
    }

    GetCountry() {
        return this.http.get(this.commonService.resourceBaseUrl + "Common/Countries")
            .map(this.extractData)
            .catch((res: Response) => this.handleError(res));
    }

    GetState() {
        return this.http.get(this.commonService.resourceBaseUrl + "Common/States")
            .map(this.extractData)
            .catch((res: Response) => this.handleError(res));
    }

    GetSecurityQuestions() {
        return this.http.get(this.commonService.resourceBaseUrl + "Common/SecurityQuestions")
            .map(this.extractData)
            .catch((res: Response) => this.handleError(res));
    }

    GetEditions() {
        return this.http.get(this.commonService.resourceBaseUrl + "Edition/Editions")
            .map(this.extractData)
            .catch((res: Response) => this.handleError(res));
    }

    GetLicenseAccounts(licenseType: Number) {
        return this.http.get(this.commonService.resourceBaseUrl + "License/LicenseAccounts?LicenseType=" + licenseType)
            .map(this.extractData)
            .catch((res: Response) => this.handleError(res));
    }

    GetSiteById(siteId: number) {
        return this.http.get(this.commonService.resourceBaseUrl + "Site/SiteDetail?SiteId=" + siteId)
            .map(this.extractData)
            .catch((res: Response) => this.handleError(res));
    }

    GetSiteAccountEditions(siteId: number, licenseId: number) {
        return this.http.get(this.commonService.resourceBaseUrl + "Site/SiteAccountEditions?SiteId=" + siteId + "&LicenseId=" + licenseId)
            .map(this.extractData)
            .catch((res: Response) => this.handleError(res));
    }

    GetLicenseSites(accountNo: string, pageNo: number, pageLength: number) {
        return this.http.get(this.commonService.resourceBaseUrl + "License/LicenseSites?AccountNo=" + accountNo + "&pageNo=" + pageNo + "&pageLength=" + pageLength)
            .map(this.extractData)
            .catch((res: Response) => this.handleError(res));
    }

    GetLicenseSiteAdmin(accountNo: string) {
        return this.http.get(this.commonService.resourceBaseUrl + "License/LicenseSiteAdmin?AccountNo=" + accountNo)
            .map(this.extractData)
            .catch((res: Response) => this.handleError(res));
    }

    GetLicenseModestySettings(licenseId: number, buildingLevelId: number) {
        return this.http.get(this.commonService.resourceBaseUrl + "License/LicenseModestySettings?LicenseId=" + licenseId + "&BuildingLevelId=" + buildingLevelId)
            .map(this.extractData)
            .catch((res: Response) => this.handleError(res));
    }

    GetLicenseModulesStatus(licenseId: number) {
        return this.http.get(this.commonService.resourceBaseUrl + "License/LicenseModulesStatus?LicenseId=" + licenseId)
            .map(this.extractData)
            .catch((res: Response) => this.handleError(res));
    }
    GetLicenseAodCourse(licenseId: number) {
        return this.http.get(this.commonService.resourceBaseUrl + "License/GetLicenseAodCourse?LicenseId=" + licenseId)
            .map(this.extractData)
            .catch((res: Response) => this.handleError(res));
    }

    InsertLicense(obj: any) {    
        //let options = new RequestOptions({ headers: this.headers });
        var jsonData = {
            'licenseId': obj.licenseId, 
            'accountNumber': obj.accountNumber, 
            'licenseeFirstName': obj.licenseeFirstName, 
            'licenseeLastName': obj.licenseeLastName, 
            'licenseTypeId': obj.licenseTypeId, 
            'accountTypeId': obj.accountTypeId, 
            'institutionName': obj.institutionName, 
            'address1': obj.address1, 
            'address2': obj.address2, 
            'city': obj.city,
            'zip': obj.zip,
            'stateId': obj.stateId,
            'countryId': obj.countryId,
            'phone': obj.phone,
            'email': obj.emailId,
            'totalLogins': obj.totalLogins,
            'editionLogins': obj.editionLogins,
            'price': obj.subscriptionPrice,
            'productKey': obj.productId,
            'masterSiteUrl': obj.masterSiteUrl,
            'siteFromUrl': obj.siteUrlFrom,  
            'siteToUrl': obj.siteUrlTo,  
            'noOfImages': obj.noOfImages,  
            'loginId': obj.login,  
            'password': obj.password,  
            'subscriptionStartDate': obj.subscriptionStartDate,  
            'subscriptionEndDate': obj.subscriptionEndDate, 
            'securityQuestionId': obj.securityQuestionId, 
            'answer': obj.answer, 
            'testLicenseEditionId': obj.testLicenseEditionId,
            'creatorId': obj.creatorId,
            'isActive': obj.isActive,
        };
        var headers = new Headers({
          'Content-Type': 'application/json'
        });
        return this.http.post(this.commonService.resourceBaseUrl + "License/InsertLicense",  
        JSON.stringify(jsonData), {headers: headers})
          .map(this.extractData)
          .catch((res: Response) => this.handleError(res));
    }
    
    UpdateLicense(obj: any) {    
        //let options = new RequestOptions({ headers: this.headers });
        if(obj.renewDate == undefined || obj.renewDate == null){
            obj.renewDate = '1/1/1';
        }
        var jsonData = {
            'licenseId': obj.licenseId, 
            'accountNumber': obj.accountNumber, 
            'licenseeFirstName': obj.licenseeFirstName, 
            'licenseeLastName': obj.licenseeLastName, 
            'licenseTypeId': obj.licenseTypeId, 
            'accountTypeId': obj.accountTypeId, 
            'institutionName': obj.institutionName, 
            'address1': obj.address1, 
            'address2': obj.address2, 
            'city': obj.city,
            'zip': obj.zip,
            'stateId': obj.stateId,
            'countryId': obj.countryId,
            'phone': obj.phone,
            'email': obj.emailId,
            'totalLogins': obj.totalLogins,
            'editionLogins': obj.editionLogins,
            'price': obj.subscriptionPrice,
            'productKey': obj.productId,
            'masterSiteUrl': obj.masterSiteUrl,
            'siteUrlFrom': obj.siteUrlFrom,  
            'siteUrlTo': obj.siteUrlTo,  
            'noOfImages': obj.noOfImages,  
            'loginId': obj.login,  
            'password': obj.password,  
            'subscriptionStartDate': obj.subscriptionStartDate,  
            'subscriptionEndDate': obj.subscriptionEndDate, 
            'renewDate': obj.renewDate,
            'securityQuestionId': obj.securityQuestionId, 
            'answer': obj.answer, 
            'testLicenseEditionId': obj.testLicenseEditionId,
            'creatorId': obj.creatorId,
            'renew': obj.renew,
            'isActive': obj.isActive,
        };
        var headers = new Headers({
          'Content-Type': 'application/json'
        });
        return this.http.post(this.commonService.resourceBaseUrl + "License/UpdateLicense",  
        JSON.stringify(jsonData), {headers: headers})
          .map(this.extractData)
          .catch((res: Response) => this.handleError(res));
    }

    UpdateLicenseBasicSettings(obj: any) {    
        //let options = new RequestOptions({ headers: this.headers });
        var jsonData = {
            'licenseId': obj.licenseId, 
            'accountNumber': obj.accountNumber, 
            'licenseeFirstName': obj.licenseeFirstName, 
            'licenseeLastName': obj.licenseeLastName, 
            'institutionName': obj.institutionName, 
            'address1': obj.address1, 
            'address2': obj.address2, 
            'city': obj.city,
            'zip': obj.zip,
            'stateId': obj.stateId,
            'countryId': obj.countryId,
            'phone': obj.phone,
            'email': obj.emailId
        };
        var headers = new Headers({
          'Content-Type': 'application/json'
        });
        return this.http.post(this.commonService.resourceBaseUrl + "License/UpdateLicenseBasicSettings",  
        JSON.stringify(jsonData), {headers: headers})
          .map(this.extractData)
          .catch((res: Response) => this.handleError(res));
    }

    UpdateLicenseModestySettings(jsonObj: any) {    
        //let options = new RequestOptions({ headers: this.headers });
        var obj = [];
        jsonObj.lstModesty.forEach(element => {
            obj.push(
                {
                    'siteId': jsonObj.siteId, 
                    'licenseEditionId': element.m_Item1, 
                    'isModesty': element.m_Item2,
                    'isSiteUser': jsonObj.isSiteUser
                }
            );
        });
        var jsonData = { obj };
        var headers = new Headers({
          'Content-Type': 'application/json'
        });
        
        return this.http.post(this.commonService.resourceBaseUrl + "License/UpdateLicenseModestySettings",  
        JSON.stringify(jsonData), {headers: headers})
          .map(this.extractData)
          .catch((res: Response) => this.handleError(res));
    }

    UpdateLicenseModulesStatus(jsonObj: any,isAodOn:boolean,CourseList:any) {    
        //let options = new RequestOptions({ headers: this.headers });
        var moduleStatusText = '';
        var CourseListText = '';
        jsonObj.lstModuleStatus.forEach(element => {
            moduleStatusText += element.m_Item1 + '-' + element.m_Item2 + '|';
        });
        if(moduleStatusText != '') {
            moduleStatusText = moduleStatusText.substr(0, moduleStatusText.length - 1);
        }

        if(isAodOn)
        {
            CourseList.forEach(element => {
                CourseListText += element.CourseId + ',' + element.Status + '|';
            });
            if(CourseListText != '') {
                CourseListText = CourseListText.substr(0, CourseListText.length - 1);
            }
    
        }
        
        var jsonData = {
            'licenseId': jsonObj.licenseId, 
            'moduleStatusText': moduleStatusText,
            'isAodOn':isAodOn,
            'CourseListText':CourseListText
        };

        var headers = new Headers({
          'Content-Type': 'application/json'
        });
        return this.http.post(this.commonService.resourceBaseUrl + "License/UpdateLicenseModulesStatus",  
        JSON.stringify(jsonData), {headers: headers})
          .map(this.extractData)
          .catch((res: Response) => this.handleError(res));
    }

    DeleteLicense(obj: any) {    
        return this.http.get(this.commonService.resourceBaseUrl + "License/DeleteLicense?LicenseId=" + obj.LicenseId)
          .map(this.extractData)
          .catch((res: Response) => this.handleError(res));
    }

    InsertUpdateSiteAccount(obj: any) {    
        //let options = new RequestOptions({ headers: this.headers });
        var temptext = '';
        obj.editionLoginArr.forEach(element => {
            if(element.Login == 1){
                temptext += element.Id + ',';
            }
        });
        if(temptext != ''){
            temptext = temptext.substr(0, temptext.length - 1);
        }
        if(obj.siteId == 0){
            obj.creationDate = new Date();
            obj.modifiedDate = new Date();
        }
        else{
            obj.modifiedDate = new Date();
        }
        var jsonData = {
            'licenseId': obj.licenseId, 
            'siteId': obj.siteId, 
            'title': obj.buildAccName, 
            'siteUrl': obj.siteUrl, 
            'siteUrlTo': obj.siteUrlTo, 
            'siteMasterUrlTo': obj.siteMasterUrlTo, 
            'institutionName': obj.institutionName, 
            'departmentName': obj.departmentName,
            'address1': obj.address1,
            'address2': obj.address2,
            'city': obj.city,
            'phone': obj.phone,
            'zip': obj.zip,
            'countryId': obj.countryId,
            'stateId': obj.stateId,
            'userId': obj.clientAdminId,
            'isActive': obj.isActive,
            'isMaster': obj.isMaster,
            'creationDate': obj.creationDate,
            'modifiedDate': obj.modifiedDate,
            'siteEditionIds': temptext
        };
        var headers = new Headers({
          'Content-Type': 'application/json'
        });
        return this.http.post(this.commonService.resourceBaseUrl + "Site/InsertUpdateSiteAccount",  
        JSON.stringify(jsonData), {headers: headers})
          .map(this.extractData)
          .catch((res: Response) => this.handleError(res));
    }

    DeleteSiteAccount(obj: any) {    
        return this.http.get(this.commonService.resourceBaseUrl + "Site/DeleteSiteAccount?SiteId=" + 
        obj.Id + "&LicenseId=" + obj.LicenseId + "&UserId=" + obj.SiteUserId)
          .map(this.extractData)
          .catch((res: Response) => this.handleError(res));
    }

    CheckAccountNumber(AccountNo: any) {
      return this.http.get(this.commonService.resourceBaseUrl + "License/CheckAccountNumber?AccountNo=" + AccountNo)
        .map(this.extractData)
        .catch((res: Response) => this.handleError(res));
    }
    extractData(res: Response) {
        //debugger; 
        let body = res.json();
        return body;
      }
    
      handleError(error: any) {
        // In a real world app, we might use a remote logging infrastructure
        // We'd also dig deeper into the error to get a better message
        let errMsg = (error.message) ? error.message :
         // error.status ? `${error.status} - ${error.statusText}` : 'Server error';
          error.status ? `${error._body}` : 'Server error';
        console.error(errMsg); // log to console instead
        return Observable.throw(errMsg);
      }
    
}