import { HttpClient } from "@angular/common/http"; import { Inject, Injectable } from "@angular/core"; import "rxjs/Rx"; import { BrowserSecurityHelper } from "../../../browser.security.helper"; import { APIEndPointsService } from "../shared/api-endpoints.service"; import { Profile_CONSTANTS } from "../shared/profile.constants"; @Injectable() export class CollectionService { private environment: any; constructor( public http: HttpClient, @Inject(Profile_CONSTANTS) private constants, private apiEndPointsService: APIEndPointsService ) {} public setEnvironment = (environment: any) => { this.environment = environment; }; public getCollections = (isEditable: boolean, campusCode, userId) => { if (!userId) { userId = BrowserSecurityHelper.authUser.id; } if (isEditable) return this.http.get( this.apiEndPointsService .getCollectionsForCampusAPIEndPoint`${this.environment.COLLECTION_SERVICE_URL} ${campusCode}` ); else return this.http.get( this.apiEndPointsService .getCollectionsForRoleMemberAPIEndPoint`${this.environment.COLLECTION_SERVICE_URL} ${userId}` ); }; public createCollection = (collectionName, campus, userId) => { const body = { name: collectionName, campusCode: campus, createdBy: userId, }; return this.http.post( this.apiEndPointsService .createCollectionAPIEndPoint`${this.environment.COLLECTION_SERVICE_URL}`, body ); }; public getCollectionDetails = (collectionId) => { return this.http.get( this.apiEndPointsService .getCollectionDetailsAPIEndPoint`${this.environment.COLLECTION_SERVICE_URL} ${collectionId}` ); }; public searchPerson = (): string => { return this.apiEndPointsService .searchPersonAPIEndPoint`${this.environment.PEOPLE_SERVICE_URL} ${this.constants.display.maxSearchResults}`; }; public searchPersonByCampus = (campusCode): string => { if (campusCode) return this.apiEndPointsService .searchPersonByCampusAPIEndPoint`${this.environment.PEOPLE_SERVICE_URL} ${campusCode} ${this.constants.display.maxSearchResults}`; else return this.apiEndPointsService .searchPersonAPIEndPoint`${this.environment.PEOPLE_SERVICE_URL} ${this.constants.display.maxSearchResults}`; }; public searchGroupURL = (campusCode): string => { return this.apiEndPointsService .searchGroupAPIEndPoint`${this.environment.GROUP_SERVICE_URL} ${campusCode}`; }; public addSafetyCoordinator = (collectionId, userId) => { const body = { userId: userId, role: this.constants.roles.SafetyCoordinator.value, }; return this.http.post( this.apiEndPointsService .assignSafetyCoordinatorToCollectionAPIEndPoint`${this.environment.COLLECTION_SERVICE_URL} ${collectionId}`, body ); }; public addGroupToCollection = (collectionId, groupId) => { const body = { groupId: groupId }; return this.http.post( this.apiEndPointsService .addGroupToCollectionAPIEndPoint`${this.environment.COLLECTION_SERVICE_URL} ${collectionId}`, body ); }; public removeSCfromCollection = (collectionId, userId) => { return this.http.delete( this.apiEndPointsService .removeSafetyCoordinatorFromCollectionAPIEndPoint`${this.environment.COLLECTION_SERVICE_URL} ${collectionId} ${userId} ${this.constants.roles.SafetyCoordinator.value}` ); }; public removeGroupfromCollection = (collectionId, groupId) => { return this.http.delete( this.apiEndPointsService .removeGroupFromCollectionAPIEndPoint`${this.environment.COLLECTION_SERVICE_URL} ${collectionId} ${groupId}` ); }; public renameCollectionName = (collectionName, collectionId) => { return this.http.put( this.apiEndPointsService .renameCollectionAPIEndPoint`${this.environment.COLLECTION_SERVICE_URL} ${collectionId} ${collectionName}`, {} ); }; public removeCollection = (collectionId) => { return this.http.delete( this.apiEndPointsService .removeCollectionAPIEndPoint`${this.environment.COLLECTION_SERVICE_URL} ${collectionId}`, { responseType: "text" } ); }; public getModalBody = (): any => { return "Deleting this collection will mean losing all of the information associated with this collection and will affect other safety applications."; }; public highlightSearchText(people: any, searchText: string) { let displayText = people.firstName + " " + people.lastName + " (" + people.email + ")"; let SearchTextArr = [], displayTextArr = []; let result = ""; if (searchText.trim().indexOf(" ") > 0) { SearchTextArr = searchText.split(" "); displayTextArr = displayText.split(" "); displayTextArr.forEach((building) => { for (var i = 0; i < SearchTextArr.length; i++) { let res = this.replaceHighlightText(SearchTextArr[i], building); if (res == building && i == SearchTextArr.length - 1) result += building + " "; else if (this.containsHTML(res)) { result += res + " "; break; } } }); } else { result = this.replaceHighlightText(searchText, displayText); } return result; } private containsHTML(html: string) { var textMatcher = new RegExp(/ 0) { let tag = document.createElement("span"); tag.innerHTML = match; tag.className = "highlight-text-auto"; return tag.outerHTML; } else { return srcText; } }); return result; } else return srcText; } }