import { Injectable , Inject } from '@angular/core' ; import { Organization } from "./models/organization.class" ; import { Observable } from "rxjs" ; import { WsHttpService } from "./ws-http.service" ; import { Response , ResponseContentType } from "@angular/http" ; import { APP_CONFIG } from "../../app.config" ; import { ApiResponse } from './models/responses/api-response.interface' ; import { Network } from "./models/network.class" ; import { WsCacheService } from "../data-services/ws-cache.service" ; import { ApiResponseService } from "./api-response.service" ; import { DomainName } from "./models/domain-name.class" ; import { ManyApiResponse } from "./models/responses/many-api-response.interface" ; import { WebService } from "./models/web-service.interface" ; import { QueryHelperService } from "./query-helper.service" ; import { WebTechReport } from "./models/es/web-tech-report.interface" ; import { HttpTransactionAnalytics } from "./models/analytics/http-transaction-analytics.interface" ; import { HttpScreenshot } from "./models/es/http-screenshot.interface" ; import { QueryFilter } from "./models/requests/query-filter.class" ; import { WsNetworkService } from "./ws-network.service" ; import { WebServiceAnalytics } from "./models/analytics/web-service-analytics.class" ; import { OrganizationPermission } from "./models/organization-permission.class" ; import { SslSupport } from "./models/ssl-support.class" ; import { SslSupportAnalytics } from "./models/analytics/ssl-support-analytics.class" ; import { DownloadHelperService } from "../data-services/download-helper.service" ; import { QueryOrdering } from "./models/requests/query-ordering.class" ; import { Order } from "./models/order.class" ; import { PresentationResponse } from "./models/responses/presentation-response.interface" ; import { WsPresentationService } from "./ws-presentation.service" ; import { DomainNameReport } from "./models/domain-name-report.class" ; import { DomainNameReportAnalytics } from "./models/analytics/domain-name-report-analytics.class" ; @ Injectable ( ) export class OrganizationService { private _baseUrl : string ; constructor ( private wsHttp , private cacheService , private apiResponseService , private queryService , private networkService , private downloadHelper , private presentationService , @ Inject ( APP_CONFIG ) private config ) { this . _baseUrl = config . apiUrl + 'organizations/' ; } public addUser ( orgUuid , userEmail ) { let toSend = { operation : 'add_user' , user_email : userEmail } ; return this . wsHttp . patch ( this . baseUrl + orgUuid + '/users/' , toSend ) . map ( response => response . json ( ) as OrganizationPermission [ ] ) ; } public createDomainForOrganization ( uuid , name ) { let requestUrl = this . baseUrl + uuid + '/domain-names/' ; let toSend = { organization : uuid , name : name } ; return this . wsHttp . post ( requestUrl , toSend ) . map ( response => { console . log ( 'Got response from create domain.' ) ; console . log ( response ) ; return response . json ( ) as DomainName ; } ) ; } public createNetworkForOrganization ( uuid , name , address , maskLength ) { let requestUrl = this . baseUrl + uuid + '/networks/' ; let toSend = { address : address , mask_length : maskLength , name : name , organization : uuid , } ; return this . wsHttp . post ( requestUrl , toSend ) . map ( response => response . json ( ) as Network ) ; } public createOrderForOrganization ( orgUuid ) { let requestUrl = this . baseUrl + orgUuid + '/orders/' ; return this . wsHttp . post ( requestUrl , { } ) . map ( response => Order . fromObject ( response . json ( ) ) ) ; } public createOrganization ( name , description ) { let toSend = { name : name , description : description } ; return this . wsHttp . post ( this . baseUrl , toSend ) . map ( response => response . json ( ) as Organization ) ; } public deleteOrganization ( uuid ) { return this . wsHttp . delete ( this . baseUrl + uuid + '/' ) . map ( response => response . json ( ) ) ; } public exportDomainNames ( orgUuid , queryFilters = [ ] , exportType , searchTerm = null , queryOrdering = null ) { let requestUrl = this . baseUrl + orgUuid + '/domain-names/' ; let queryString = this . queryService . getExportQueryString ( exportType , queryFilters , searchTerm , queryOrdering ) ; requestUrl = requestUrl + '?' + queryString ; this . wsHttp . get ( requestUrl , { responseType : ResponseContentType . Blob } ) . subscribe ( response => this . downloadHelper . downloadFileFromResponse ( response ) ) ; } public exportNetworks ( orgUuid , queryFilters = [ ] , exportType , searchTerm = null , queryOrdering = null ) { let requestUrl = this . baseUrl + orgUuid + '/networks/' ; let queryString = this . queryService . getExportQueryString ( exportType , queryFilters , searchTerm , queryOrdering ) ; requestUrl = requestUrl + '?' + queryString ; this . wsHttp . get ( requestUrl , { responseType : ResponseContentType . Blob } ) . subscribe ( response => this . downloadHelper . downloadFileFromResponse ( response ) ) ; } public exportOrganizations ( queryFilters = [ ] , exportType , searchTerm = null , queryOrdering = null ) { let queryString = this . queryService . getExportQueryString ( exportType , queryFilters , searchTerm , queryOrdering ) ; let requestUrl = this . baseUrl + '?' + queryString ; this . wsHttp . get ( requestUrl , { responseType : ResponseContentType . Blob } ) . subscribe ( response => this . downloadHelper . downloadFileFromResponse ( response ) ) ; } public exportSslSupport ( uuid , queryFilters = [ ] , exportType , searchTerm = null , queryOrdering = null ) { let requestUrl = this . baseUrl + uuid + '/es/ssl-support/' ; let queryString = this . queryService . getExportQueryString ( exportType , queryFilters , searchTerm , queryOrdering ) ; requestUrl = requestUrl + '?' + queryString ; this . wsHttp . get ( requestUrl , { responseType : ResponseContentType . Blob } ) . subscribe ( response => this . downloadHelper . downloadFileFromResponse ( response ) ) ; } public exportWebApps ( uuid , queryFilters = [ ] , exportType , searchTerm = null , queryOrdering = null ) { let requestUrl = this . baseUrl + uuid + '/es/web-services/' ; let queryString = this . queryService . getExportQueryString ( exportType , queryFilters , searchTerm , queryOrdering ) ; requestUrl = requestUrl + '?' + queryString ; this . wsHttp . get ( requestUrl , { responseType : ResponseContentType . Blob } ) . subscribe ( response => this . downloadHelper . downloadFileFromResponse ( response ) ) ; } public getDomainNameReportAnalytics ( uuid , queryFilters = [ ] , searchTerm = null ) { let requestUrl = this . baseUrl + uuid + '/es/domain-names/analytics/' ; let queryString = this . queryService . getQueryStringFromFiltersAndSearch ( queryFilters , searchTerm ) ; requestUrl = requestUrl + '?' + queryString ; return this . wsHttp . get ( requestUrl ) . map ( response => { let data = response . json ( ) ; return DomainNameReportAnalytics . fromObject ( data ) ; } ) ; } public getDomainNameReports ( uuid , pageNumber = 1 , queryFilters = [ ] , searchTerm = null , queryOrdering = null ) { let requestUrl = this . baseUrl + uuid + '/es/domain-names/' ; let queryString = this . queryService . getQueryStringNew ( pageNumber , queryFilters , queryOrdering , searchTerm ) ; requestUrl = requestUrl + '?' + queryString ; return this . wsHttp . get ( requestUrl ) . map ( response => { let toReturn = response . json ( ) as ManyApiResponse < DomainNameReport [ ] > ; toReturn . results = DomainNameReport . fromObjects ( toReturn . results ) ; return toReturn ; } ) ; } public getDomainReportsExportUrl ( orgUuid ) { return this . baseUrl + orgUuid + '/es/domain-names/' ; } public getDomains ( uuid , pageNumber = 1 , queryFilters = [ ] , orderField = null , orderDirection = null ) { let requestUrl = this . baseUrl + uuid + '/domain-names/' ; let queryString = this . queryService . getQueryString ( pageNumber , queryFilters , orderField , orderDirection ) ; requestUrl = requestUrl + '?' + queryString ; return this . wsHttp . get ( requestUrl ) . map ( response => response . json ( ) as ManyApiResponse < DomainName [ ] > ) ; } public getDomainsUrl ( orgUuid ) { return this . baseUrl + orgUuid + '/domain-names/' ; } public getDomainsNew ( orgUuid , pageNumber = 1 , queryFilters = [ ] , queryOrdering = null , searchTerm = null ) { let requestUrl = this . baseUrl + orgUuid + '/domain-names/' ; let queryString = this . queryService . getQueryStringNew ( pageNumber , queryFilters , queryOrdering , searchTerm ) ; requestUrl = requestUrl + '?' + queryString ; return this . wsHttp . get ( requestUrl ) . map ( response => { let toReturn = response . json ( ) ; toReturn . results = DomainName . fromObjects ( toReturn . results ) ; return toReturn ; } ) ; } public getDomainsUploadUrl ( uuid ) { return this . baseUrl + uuid + '/domain-names/upload/' ; } public getNetworks ( uuid , pageNumber = 1 , queryFilters = [ ] , orderField = null , orderDirection = null ) { let requestUrl = this . baseUrl + uuid + '/networks/' ; let queryString = this . queryService . getQueryString ( pageNumber , queryFilters , orderField , orderDirection ) ; requestUrl = requestUrl + '?' + queryString ; return this . wsHttp . get ( requestUrl ) . map ( response => response . json ( ) as ManyApiResponse < Network [ ] > ) ; } public getNetworksUrl ( orgUuid ) { return this . baseUrl + orgUuid + '/networks/' ; } public getNetworksNew ( orgUuid , pageNumber = 1 , queryFilters = [ ] , queryOrdering = null , searchTerm = null ) { let requestUrl = this . baseUrl + orgUuid + '/networks/' ; let queryString = this . queryService . getQueryStringNew ( pageNumber , queryFilters , queryOrdering , searchTerm ) ; requestUrl = requestUrl + '?' + queryString ; return this . wsHttp . get ( requestUrl ) . map ( response => { let toReturn = response . json ( ) ; toReturn . results = Network . fromObjects ( toReturn . results ) ; return toReturn ; } ) ; } public getNetworksUploadUrl ( uuid ) { return this . baseUrl + uuid + '/networks/upload/' ; } public getOrganization ( uuid ) { return this . wsHttp . get ( this . baseUrl + uuid + '/' ) . map ( response => Organization . fromObject ( response . json ( ) ) ) ; } public getOrganizations ( pageNumber = 1 , queryFilters = [ ] , orderField = null , orderDirection = null ) { let queryString = this . queryService . getQueryString ( pageNumber , queryFilters , orderField , orderDirection ) ; let requestUrl = this . baseUrl + '?' + queryString ; return this . wsHttp . get ( requestUrl ) . map ( response => { let toReturn = response . json ( ) as ManyApiResponse < Organization [ ] > ; this . cacheService . cacheOrganizations ( toReturn . results ) ; return toReturn ; } ) ; } public getOrganizationsNew ( pageNumber = 1 , queryFilters = [ ] , searchTerm = null , queryOrdering = null ) { let queryString = this . queryService . getQueryStringNew ( pageNumber , queryFilters , queryOrdering , searchTerm ) ; let requestUrl = this . baseUrl + '?' + queryString ; return this . wsHttp . get ( requestUrl ) . map ( response => { let toReturn = response . json ( ) as ManyApiResponse < Organization [ ] > ; toReturn . results = Organization . fromObjects ( toReturn . results ) ; return toReturn ; } ) ; } public getPermissions ( uuid ) { return this . wsHttp . get ( this . baseUrl + uuid + '/permissions/' ) . map ( response => response . json ( ) as OrganizationPermission ) ; } public getPresentation ( ) { return this . presentationService . getPresentationByUrl ( 'organization' , this . baseUrl ) ; } public getSslSupport ( uuid , pageNumber = 1 , queryFilters = [ ] , searchTerm = null , queryOrdering = null ) { let requestUrl = this . baseUrl + uuid + '/es/ssl-support/' ; let queryString = this . queryService . getQueryStringNew ( pageNumber , queryFilters , queryOrdering , searchTerm ) ; requestUrl = requestUrl + '?' + queryString ; return this . wsHttp . get ( requestUrl ) . map ( response => { let toReturn = response . json ( ) as ManyApiResponse < SslSupport [ ] > ; toReturn . results = SslSupport . fromObjects ( toReturn . results ) ; return toReturn ; } ) ; } public getSslSupportAnalytics ( uuid , queryFilters = [ ] , searchTerm = null ) { let requestUrl = this . baseUrl + uuid + '/es/ssl-support/analytics/' ; let queryString = this . queryService . getQueryStringFromFiltersAndSearch ( queryFilters , searchTerm ) ; requestUrl = requestUrl + '?' + queryString ; return this . wsHttp . get ( requestUrl ) . map ( response => { let data = response . json ( ) ; return SslSupportAnalytics . fromObject ( data ) ; } ) ; } public getSslSupportExportUrl ( orgUuid ) { return this . baseUrl + orgUuid + '/es/ssl-support/' ; } public getUserPermissions ( uuid ) { return this . wsHttp . get ( this . baseUrl + uuid + '/users/' ) . map ( response => OrganizationPermission . fromObjects ( response . json ( ) ) ) ; } public getWebServices ( uuid , pageNumber = 1 , queryFilters = [ ] , orderField = null , orderDirection = null ) { let requestUrl = this . baseUrl + uuid + '/es/web-services/' ; let queryString = this . queryService . getQueryString ( pageNumber , queryFilters , orderField , orderDirection ) ; requestUrl = requestUrl + '?' + queryString ; return this . wsHttp . get ( requestUrl ) . map ( response => { let toReturn = response . json ( ) as ManyApiResponse < WebService [ ] > ; toReturn . results = WebService . fromObjects ( toReturn . results ) ; return toReturn ; } ) ; } public getWebServicesNew ( uuid , pageNumber = 1 , queryFilters = [ ] , searchTerm = null , queryOrdering = null ) { let requestUrl = this . baseUrl + uuid + '/es/web-services/' ; let queryString = this . queryService . getQueryStringNew ( pageNumber , queryFilters , queryOrdering , searchTerm ) ; requestUrl = requestUrl + '?' + queryString ; return this . wsHttp . get ( requestUrl ) . map ( response => { let toReturn = response . json ( ) as ManyApiResponse < WebService [ ] > ; toReturn . results = WebService . fromObjects ( toReturn . results ) ; return toReturn ; } ) ; } public getWebServicesExportUrl ( orgUuid ) { return this . baseUrl + orgUuid + '/es/web-services/' ; } public getWebServiceAnalytics ( uuid , queryFilters = [ ] , searchTerm = null ) { let requestUrl = this . baseUrl + uuid + '/es/web-services/analytics/' ; let queryString = this . queryService . getQueryStringFromFiltersAndSearch ( queryFilters , searchTerm ) ; requestUrl = requestUrl + '?' + queryString ; return this . wsHttp . get ( requestUrl ) . map ( response => response . json ( ) as WebServiceAnalytics ) ; } public getWebServiceAnalyticsNew ( uuid , queryFilters = [ ] , searchTerm = null ) { let requestUrl = this . baseUrl + uuid + '/es/web-services/analytics/' ; let queryString = this . queryService . getQueryStringFromFiltersAndSearch ( queryFilters , searchTerm ) ; requestUrl = requestUrl + '?' + queryString ; return this . wsHttp . get ( requestUrl ) . map ( response => { let data = response . json ( ) ; return WebServiceAnalytics . fromObject ( data ) ; } ) ; } public removeUser ( orgUuid , userUuid ) { let toSend = { operation : 'remove_user' , user_uuid : userUuid } ; return this . wsHttp . patch ( this . baseUrl + orgUuid + '/users/' , toSend ) . map ( response => response . json ( ) as OrganizationPermission [ ] ) ; } public scanOrganization ( uuid ) { return this . wsHttp . put ( this . baseUrl + uuid + '/scan/' , { } ) ; } public updateOrganization ( uuid , name ) { let requestUrl = this . baseUrl + uuid + '/' ; let toSend = { name : name } ; return this . wsHttp . patch ( requestUrl , toSend ) . map ( response => response . json ( ) as Organization ) ; } public updateUserPermissions ( orgUuid , userUuid , permissionsLevel ) { let toSend = { operation : 'update_user' , user_uuid : userUuid , permission_level : permissionsLevel } ; return this . wsHttp . patch ( this . baseUrl + orgUuid + '/users/' , toSend ) . map ( response => response . json ( ) as OrganizationPermission [ ] ) ; } public get baseUrl ( ) : string { return this . _baseUrl ; } }