Skip to content

Commit 6312c2f

Browse files
fix: pass ip as query parameter
1 parent d3f1e76 commit 6312c2f

File tree

3 files changed

+22
-19
lines changed

3 files changed

+22
-19
lines changed

config/config.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"DISABLE_HOOKS": false,
77
"UPLOAD_PLAYGROUND_FILES_ENDPOINT": "https://play.nativescript.org/api/files",
88
"SHORTEN_URL_ENDPOINT": "https://play.nativescript.org/api/shortenurl?longUrl=%s",
9-
"INSIGHTS_URL_ENDPOINT": "https://play.nativescript.org/api/insights",
9+
"INSIGHTS_URL_ENDPOINT": "https://play.nativescript.org/api/insights?ipAddress=%s",
1010
"WHOAMI_URL_ENDPOINT": "https://play.nativescript.org/api/whoami",
1111
"PREVIEW_APP_ENVIRONMENT": "live",
1212
"GA_TRACKING_ID": "UA-111455-51"

lib/controllers/company-insights-controller.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
12
import { AnalyticsEventLabelDelimiter } from "../constants";
23
import { cache } from "../common/decorators";
34
import * as path from "path";
5+
import * as util from "util";
46

57
export class CompanyInsightsController implements ICompanyInsightsController {
6-
private static CACHE_TIMEOUT = 48 * 60 * 60 * 1000; // 2 days in milliseconds
8+
private static CACHE_TIMEOUT = 30 * 24 * 60 * 60 * 1000; // 30 days in milliseconds
79
private get $jsonFileSettingsService(): IJsonFileSettingsService {
810
return this.$injector.resolve<IJsonFileSettingsService>("jsonFileSettingsService", {
911
jsonFileSettingsPath: path.join(this.$settingsService.getProfileDir(), "company-insights-data.json")
@@ -23,8 +25,8 @@ export class CompanyInsightsController implements ICompanyInsightsController {
2325

2426
companyData = await this.getCompanyDataFromCache(cacheKey);
2527

26-
if (!companyData) {
27-
companyData = await this.getCompanyDataFromPlaygroundInsightsEndpoint();
28+
if (!companyData && currentPublicIP) {
29+
companyData = await this.getCompanyDataFromPlaygroundInsightsEndpoint(currentPublicIP);
2830
if (companyData && currentPublicIP) {
2931
await this.$jsonFileSettingsService.saveSetting<ICompanyData>(cacheKey, companyData, { useCaching: true });
3032
}
@@ -62,11 +64,12 @@ export class CompanyInsightsController implements ICompanyInsightsController {
6264
}
6365

6466
@cache()
65-
private async getCompanyDataFromPlaygroundInsightsEndpoint(): Promise<ICompanyData> {
67+
private async getCompanyDataFromPlaygroundInsightsEndpoint(ipAddress: string): Promise<ICompanyData> {
6668
let companyData: ICompanyData = null;
6769

6870
try {
69-
const response = await this.$httpClient.httpRequest(this.$config.INSIGHTS_URL_ENDPOINT);
71+
const url = util.format(this.$config.INSIGHTS_URL_ENDPOINT, encodeURIComponent(ipAddress));
72+
const response = await this.$httpClient.httpRequest(url);
7073
const data = <IPlaygroundInsightsEndpointData>(JSON.parse(response.body));
7174
if (data.company) {
7275
const industries = _.isArray(data.company.industries) ? data.company.industries.join(AnalyticsEventLabelDelimiter) : null;

test/controllers/company-insights-controller.ts

+13-13
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import { LoggerStub } from "../stubs";
44
import { CompanyInsightsController } from "../../lib/controllers/company-insights-controller";
55

66
describe("companyInsightsController", () => {
7-
const insightsUrlEndpoint = "/api/insights";
7+
const insightsUrlEndpoint = "/api/insights?ipAddress=%s";
88
const currentIp = "8.8.8.8";
99
const profileDir = "profileDir";
10-
const cacheTimeout = 48 * 60 * 60 * 1000; // 2 days in milliseconds
10+
const cacheTimeout = 30 * 24 * 60 * 60 * 1000; // 2 days in milliseconds
1111
const defaultCompanyData = {
1212
company: {
1313
name: "Progress",
@@ -138,6 +138,17 @@ describe("companyInsightsController", () => {
138138
const companyData = await companyInsightsController.getCompanyData();
139139
assert.deepEqual(companyData, null);
140140
});
141+
142+
it("unable to get current ip address", async () => {
143+
const ipService = testInjector.resolve<IIPService>("ipService");
144+
ipService.getCurrentIPv4Address = async (): Promise<string> => { throw new Error("Unable to get current ip addreess"); };
145+
146+
const companyData = await companyInsightsController.getCompanyData();
147+
assert.deepEqual(companyData, null);
148+
assert.equal(httpRequestCounter, 0, "We should not have any http request");
149+
assert.deepEqual(dataPassedToGetSettingValue, [], "When we are unable to get IP, we should not try to get value from the cache.");
150+
assert.deepEqual(dataPassedToSaveSettingValue, [], "When we are unable to get IP, we should not persist anything.");
151+
});
141152
});
142153

143154
describe("returns correct data when", () => {
@@ -155,17 +166,6 @@ describe("companyInsightsController", () => {
155166

156167
describe("data for current ip does not exist in the cache and", () => {
157168

158-
it("unable to get current ip address, but still can get company information", async () => {
159-
const ipService = testInjector.resolve<IIPService>("ipService");
160-
ipService.getCurrentIPv4Address = async (): Promise<string> => { throw new Error("Unable to get current ip addreess"); };
161-
162-
const companyData = await companyInsightsController.getCompanyData();
163-
assert.deepEqual(companyData, defaultExpectedCompanyData);
164-
assert.equal(httpRequestCounter, 1, "We should have only one http request");
165-
assert.deepEqual(dataPassedToGetSettingValue, [], "When we are unable to get IP, we should not try to get value from the cache.");
166-
assert.deepEqual(dataPassedToSaveSettingValue, [], "When we are unable to get IP, we should not persist anything.");
167-
});
168-
169169
it("response contains company property", async () => {
170170
const companyData = await companyInsightsController.getCompanyData();
171171
assert.deepEqual(companyData, defaultExpectedCompanyData);

0 commit comments

Comments
 (0)