-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathcompany-insights-controller.ts
55 lines (45 loc) · 1.74 KB
/
company-insights-controller.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { AnalyticsEventLabelDelimiter } from "../constants";
import { cache } from "../common/decorators";
import * as util from "util";
export class CompanyInsightsController implements ICompanyInsightsController {
constructor(private $config: IConfiguration,
private $httpClient: Server.IHttpClient,
private $ipService: IIPService,
private $logger: ILogger) { }
public async getCompanyData(): Promise<ICompanyData> {
let companyData: ICompanyData = null;
let currentPublicIP: string = null;
try {
currentPublicIP = await this.$ipService.getCurrentIPv4Address();
} catch (err) {
this.$logger.trace(`Unable to get current public ip address. Error is: `, err);
}
if (currentPublicIP) {
companyData = await this.getCompanyDataFromPlaygroundInsightsEndpoint(currentPublicIP);
}
return companyData;
}
@cache()
private async getCompanyDataFromPlaygroundInsightsEndpoint(ipAddress: string): Promise<ICompanyData> {
let companyData: ICompanyData = null;
try {
const url = util.format(this.$config.INSIGHTS_URL_ENDPOINT, encodeURIComponent(ipAddress));
const response = await this.$httpClient.httpRequest(url);
const data = <IPlaygroundInsightsEndpointData>(JSON.parse(response.body));
if (data.company) {
const industries = _.isArray(data.company.industries) ? data.company.industries.join(AnalyticsEventLabelDelimiter) : null;
companyData = {
name: data.company.name,
country: data.company.country,
revenue: data.company.revenue,
employeeCount: data.company.employeeCount,
industries
};
}
} catch (err) {
this.$logger.trace(`Unable to get data for company. Error is: ${err}`);
}
return companyData;
}
}
$injector.register("companyInsightsController", CompanyInsightsController);