-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathsubscription-service.ts
73 lines (62 loc) · 2.21 KB
/
subscription-service.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import * as emailValidator from "email-validator";
import * as queryString from "querystring";
import * as helpers from "../common/helpers";
export class SubscriptionService implements ISubscriptionService {
constructor(private $httpClient: Server.IHttpClient,
private $prompter: IPrompter,
private $userSettingsService: IUserSettingsService,
private $logger: ILogger) {
}
public async subscribeForNewsletter(): Promise<void> {
if (await this.shouldAskForEmail()) {
this.$logger.out("Leave your e-mail address here to subscribe for NativeScript newsletter and product updates, tips and tricks:");
let email = await this.getEmail("(press Enter for blank)");
await this.$userSettingsService.saveSetting("EMAIL_REGISTERED", true);
await this.sendEmail(email);
}
}
/**
* Checks whether we should ask the current user if they want to subscribe to NativeScript newsletter.
* NOTE: This method is protected, not private, only because of our unit tests.
* @returns {Promise<boolean>}
*/
protected async shouldAskForEmail(): Promise<boolean> {
return helpers.isInteractive() && process.env.CLI_NOPROMPT !== "1" && !(await this.$userSettingsService.getSettingValue("EMAIL_REGISTERED"));
}
private async getEmail(prompt: string, options?: IPrompterOptions): Promise<string> {
let schema: IPromptSchema = {
message: prompt,
type: "input",
name: "inputEmail",
validate: (value: any) => {
if (value === "" || emailValidator.validate(value)) {
return true;
}
return "Please provide a valid e-mail or simply leave it blank.";
}
};
let result = await this.$prompter.get([schema]);
return result.inputEmail;
}
private async sendEmail(email: string): Promise<void> {
if (email) {
let postData = queryString.stringify({
'elqFormName': "dev_uins_cli",
'elqSiteID': '1325',
'emailAddress': email,
'elqCookieWrite': '0'
});
let options = {
url: 'https://s1325.t.eloqua.com/e/f2',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': postData.length
},
body: postData
};
await this.$httpClient.httpRequest(options);
}
}
}
$injector.register("subscriptionService", SubscriptionService);