@auxcode/auxta 1.3.23-beta.1 → 1.3.23-beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -25,6 +25,7 @@ declare class AuxTA extends FunctionHelper {
25
25
  password: string;
26
26
  };
27
27
  };
28
+ emailHelper: import("./macros/helpers/emailHelper").EmailHelper;
28
29
  private readonly uploadModel;
29
30
  constructor();
30
31
  run(event: any, overrideConfig?: any): Promise<{
@@ -48,6 +48,7 @@ const start_suite_helper_1 = require("./auxta/utilities/start-suite.helper");
48
48
  const report_service_1 = require("./auxta/services/report.service");
49
49
  const Aux2faAuth_1 = __importDefault(require("./macros/helpers/Aux2faAuth"));
50
50
  const AuxGoogleAuth_1 = require("./macros/helpers/AuxGoogleAuth");
51
+ const emailHelper_1 = __importDefault(require("./macros/helpers/emailHelper"));
51
52
  dotenv.config();
52
53
  class AuxTA extends code_helper_1.FunctionHelper {
53
54
  constructor() {
@@ -56,6 +57,7 @@ class AuxTA extends code_helper_1.FunctionHelper {
56
57
  this.aux2fa = Aux2faAuth_1.default;
57
58
  this.google = AuxGoogleAuth_1.AuxGoogleAuth;
58
59
  this.config = config_1.config;
60
+ this.emailHelper = emailHelper_1.default;
59
61
  let file;
60
62
  if (!process.env.RPA) {
61
63
  try {
@@ -1,6 +1,5 @@
1
1
  import { StatusOfStep } from "../../auxta/enums/status-of.step";
2
2
  import { ExtendDefaultPage } from "./extend-default-page";
3
- import { gmail_v1 } from "googleapis";
4
3
  export declare class FunctionHelper extends ExtendDefaultPage {
5
4
  log(keyword: string, name: string, status: StatusOfStep): void;
6
5
  suggest(name: string): void;
@@ -17,18 +16,6 @@ export declare class FunctionHelper extends ExtendDefaultPage {
17
16
  clickAndWaitForPageToBeCreated(selector: string, page?: import("puppeteer").Page): Promise<void>;
18
17
  closeLastPage(page?: import("puppeteer").Page): Promise<void>;
19
18
  microsoftLogin(button: string, email: string, password: string): Promise<void>;
20
- verifyEmail(from_name: string, from_email: string, subject: string, body: string): Promise<{
21
- id: string;
22
- threadId: string;
23
- } | undefined>;
24
- replyEmail(ids: {
25
- id: string;
26
- threadId: string;
27
- }, body: string): Promise<void>;
28
- private getHeader;
29
- private getBody;
30
- }
31
- export interface gmailResponse extends gmail_v1.Gmail {
32
19
  }
33
20
  declare const _default: FunctionHelper;
34
21
  export default _default;
@@ -19,9 +19,6 @@ const status_of_step_1 = require("../../auxta/enums/status-of.step");
19
19
  const AuxTA_1 = require("../../AuxTA");
20
20
  const extend_default_page_1 = require("./extend-default-page");
21
21
  const puppeteer_2 = require("puppeteer");
22
- const AuxGoogleAuth_1 = require("./AuxGoogleAuth");
23
- const buffer_1 = require("buffer");
24
- const MailComposer = require('nodemailer/lib/mail-composer');
25
22
  class FunctionHelper extends extend_default_page_1.ExtendDefaultPage {
26
23
  log(keyword, name, status) {
27
24
  log_service_1.default.push(keyword, name, status);
@@ -190,96 +187,6 @@ class FunctionHelper extends extend_default_page_1.ExtendDefaultPage {
190
187
  yield loginPage.waitForNetworkIdle();
191
188
  });
192
189
  }
193
- verifyEmail(from_name, from_email, subject, body) {
194
- return __awaiter(this, void 0, void 0, function* () {
195
- yield AuxGoogleAuth_1.AuxGoogleAuth.setup();
196
- const res = yield AuxGoogleAuth_1.AuxGoogleAuth.gmailClient.users.messages.list({
197
- userId: "me",
198
- maxResults: 3
199
- });
200
- const messagesIds = res.data.messages;
201
- if (!messagesIds || messagesIds.length === 0) {
202
- throw new Error('No messagesIds found.');
203
- }
204
- let timeoutCount = 12000;
205
- yield this.timeout(12000);
206
- while (timeoutCount <= 60000) {
207
- for (const messageId of messagesIds) {
208
- if (messageId.id) {
209
- const gmailResponse = yield AuxGoogleAuth_1.AuxGoogleAuth.gmailClient.users.messages.get({
210
- userId: "me",
211
- id: messageId.id.toString(),
212
- format: 'FULL'
213
- });
214
- if (gmailResponse.data.payload) {
215
- const message_sender = this.getHeader('from', gmailResponse.data.payload.headers);
216
- const message_subject = this.getHeader('subject', gmailResponse.data.payload.headers);
217
- const message_body = this.getBody(from_name, from_email, subject, gmailResponse);
218
- if (message_sender.includes(from_email) && message_sender.includes(from_name.toLocaleLowerCase()) &&
219
- message_subject.includes(subject.toLocaleLowerCase()) && message_body.includes(body.toLocaleLowerCase())) {
220
- yield this.log('Then', `Email from: ${from_name} ${from_email} with subject: ${subject} and body: ${body} is found`, AuxTA_1.StepStatus.PASSED);
221
- return { id: gmailResponse.data.id.toString(), threadId: gmailResponse.data.threadId.toString() };
222
- }
223
- }
224
- break;
225
- }
226
- }
227
- yield this.timeout(12000);
228
- timeoutCount += 12000;
229
- }
230
- yield this.log('Then', `Email from: ${from_name} ${from_email} with subject: ${subject} and body: ${body} is found`, AuxTA_1.StepStatus.FAILED);
231
- });
232
- }
233
- replyEmail(ids, body) {
234
- return __awaiter(this, void 0, void 0, function* () {
235
- yield AuxGoogleAuth_1.AuxGoogleAuth.setup();
236
- const gmailResponse = yield AuxGoogleAuth_1.AuxGoogleAuth.gmailClient.users.messages.get({
237
- userId: "me",
238
- id: ids.id,
239
- format: 'FULL'
240
- });
241
- if (gmailResponse.data.payload) {
242
- const message_to = this.getHeader('to', gmailResponse.data.payload.headers);
243
- const message_sender = this.getHeader('from', gmailResponse.data.payload.headers);
244
- const message_subject = this.getHeader('subject', gmailResponse.data.payload.headers);
245
- const message_messageID = this.getHeader('Message-ID', gmailResponse.data.payload.headers);
246
- const options = {
247
- "to": message_to,
248
- "from": message_sender,
249
- "subject": message_subject,
250
- "Reference": message_messageID,
251
- "In-Reply-To": message_messageID,
252
- "threadId": ids.threadId,
253
- "html": body
254
- };
255
- const mailComposer = new MailComposer(options);
256
- const message = yield mailComposer.compile().build();
257
- const encodeMessage = Buffer.from(message).toString('base64').replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
258
- yield AuxGoogleAuth_1.AuxGoogleAuth.gmailClient.users.messages.send({
259
- userId: "me",
260
- requestBody: {
261
- raw: encodeMessage
262
- }
263
- });
264
- }
265
- });
266
- }
267
- getHeader(value, headers) {
268
- for (const header of headers) {
269
- if (header.name.toLocaleLowerCase() === value.toLocaleLowerCase()) {
270
- return header.value.toLocaleLowerCase();
271
- }
272
- }
273
- }
274
- getBody(from_name, from_email, subject, gmailResponse) {
275
- const body = gmailResponse.data.payload.parts[0].body.data;
276
- if (body) {
277
- return (0, buffer_1.atob)(body).toLocaleLowerCase();
278
- }
279
- else {
280
- throw new Error(`Email from: ${from_name} ${from_email} with subject: ${subject} body is empty`);
281
- }
282
- }
283
190
  }
284
191
  exports.FunctionHelper = FunctionHelper;
285
192
  exports.default = new FunctionHelper();
@@ -0,0 +1,17 @@
1
+ import { gmail_v1 } from "googleapis";
2
+ export declare class EmailHelper {
3
+ verifyEmail(from_name: string, from_email: string, subject: string, body: string): Promise<{
4
+ id: string;
5
+ threadId: string;
6
+ } | undefined>;
7
+ replyEmail(ids: {
8
+ id: string;
9
+ threadId: string;
10
+ }, body: string): Promise<void>;
11
+ private getHeader;
12
+ private getBody;
13
+ }
14
+ export interface gmailResponse extends gmail_v1.Gmail {
15
+ }
16
+ declare const _default: EmailHelper;
17
+ export default _default;
@@ -0,0 +1,121 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.EmailHelper = void 0;
16
+ const log_service_1 = __importDefault(require("../../auxta/services/log.service"));
17
+ const AuxGoogleAuth_1 = require("./AuxGoogleAuth");
18
+ const buffer_1 = require("buffer");
19
+ const AuxTA_1 = require("../../AuxTA");
20
+ const MailComposer = require('nodemailer/lib/mail-composer');
21
+ class EmailHelper {
22
+ verifyEmail(from_name, from_email, subject, body) {
23
+ return __awaiter(this, void 0, void 0, function* () {
24
+ yield AuxGoogleAuth_1.AuxGoogleAuth.setup();
25
+ const res = yield AuxGoogleAuth_1.AuxGoogleAuth.gmailClient.users.messages.list({
26
+ userId: "me",
27
+ maxResults: 3
28
+ });
29
+ const messagesIds = res.data.messages;
30
+ if (!messagesIds || messagesIds.length === 0) {
31
+ throw new Error('No messagesIds found.');
32
+ }
33
+ let timeoutCount = 12000;
34
+ while (timeoutCount <= 60000) {
35
+ for (const messageId of messagesIds) {
36
+ if (messageId.id) {
37
+ const gmailResponse = yield AuxGoogleAuth_1.AuxGoogleAuth.gmailClient.users.messages.get({
38
+ userId: "me",
39
+ id: messageId.id.toString(),
40
+ format: 'FULL'
41
+ });
42
+ if (gmailResponse.data.payload && gmailResponse.data.id && gmailResponse.data.threadId) {
43
+ const message_sender = this.getHeader('from', gmailResponse.data.payload.headers);
44
+ const message_subject = this.getHeader('subject', gmailResponse.data.payload.headers);
45
+ const message_body = this.getBody(from_name, from_email, subject, gmailResponse);
46
+ if (message_sender.includes(from_email) && message_sender.includes(from_name.toLocaleLowerCase()) &&
47
+ message_subject.includes(subject.toLocaleLowerCase()) && message_body.includes(body.toLocaleLowerCase())) {
48
+ log_service_1.default.push('Then', `Email from: ${from_name} ${from_email} with subject: ${subject} and body: ${body} is found`, AuxTA_1.StepStatus.PASSED);
49
+ yield AuxGoogleAuth_1.AuxGoogleAuth.gmailClient.users.messages.modify({
50
+ userId: "me",
51
+ id: gmailResponse.data.id,
52
+ requestBody: {
53
+ 'addLabelIds': [],
54
+ 'removeLabelIds': ['UNREAD']
55
+ }
56
+ });
57
+ return { id: gmailResponse.data.id.toString(), threadId: gmailResponse.data.threadId.toString() };
58
+ }
59
+ }
60
+ break;
61
+ }
62
+ }
63
+ new Promise(r => setTimeout(r, 12000));
64
+ timeoutCount += 12000;
65
+ }
66
+ yield log_service_1.default.push('Then', `Email from: ${from_name} ${from_email} with subject: ${subject} and body: ${body} is found`, AuxTA_1.StepStatus.FAILED);
67
+ });
68
+ }
69
+ replyEmail(ids, body) {
70
+ return __awaiter(this, void 0, void 0, function* () {
71
+ yield AuxGoogleAuth_1.AuxGoogleAuth.setup();
72
+ const gmailResponse = yield AuxGoogleAuth_1.AuxGoogleAuth.gmailClient.users.messages.get({
73
+ userId: "me",
74
+ id: ids.id,
75
+ format: 'FULL'
76
+ });
77
+ if (gmailResponse.data.payload) {
78
+ const message_sender = this.getHeader('from', gmailResponse.data.payload.headers);
79
+ const message_subject = this.getHeader('subject', gmailResponse.data.payload.headers);
80
+ const message_messageID = this.getHeader('Message-ID', gmailResponse.data.payload.headers);
81
+ const reply_to = this.getHeader('Reply-To', gmailResponse.data.payload.headers);
82
+ const options = {
83
+ "to": reply_to,
84
+ "from": message_sender,
85
+ "subject": `Re: ${message_subject}`,
86
+ "Reference": message_messageID,
87
+ "Reply-To": reply_to,
88
+ "threadId": ids.threadId,
89
+ "html": body
90
+ };
91
+ const mailComposer = new MailComposer(options);
92
+ const message = yield mailComposer.compile().build();
93
+ const encodeMessage = Buffer.from(message).toString('base64').replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
94
+ yield AuxGoogleAuth_1.AuxGoogleAuth.gmailClient.users.messages.send({
95
+ userId: "me",
96
+ requestBody: {
97
+ raw: encodeMessage
98
+ }
99
+ });
100
+ }
101
+ });
102
+ }
103
+ getHeader(value, headers) {
104
+ for (const header of headers) {
105
+ if (header.name.toLocaleLowerCase() === value.toLocaleLowerCase()) {
106
+ return header.value.toLocaleLowerCase();
107
+ }
108
+ }
109
+ }
110
+ getBody(from_name, from_email, subject, gmailResponse) {
111
+ const body = gmailResponse.data.payload.parts[0].body.data;
112
+ if (body) {
113
+ return (0, buffer_1.atob)(body).toLocaleLowerCase();
114
+ }
115
+ else {
116
+ throw new Error(`Email from: ${from_name} ${from_email} with subject: ${subject} body is empty`);
117
+ }
118
+ }
119
+ }
120
+ exports.EmailHelper = EmailHelper;
121
+ exports.default = new EmailHelper();
@@ -25,6 +25,7 @@ declare class AuxTA extends FunctionHelper {
25
25
  password: string;
26
26
  };
27
27
  };
28
+ emailHelper: import("./macros/helpers/emailHelper").EmailHelper;
28
29
  private readonly uploadModel;
29
30
  constructor();
30
31
  run(event: any, overrideConfig?: any): Promise<{
@@ -19,6 +19,7 @@ import { startSuite } from "./auxta/utilities/start-suite.helper";
19
19
  import { createEmptyReport } from "./auxta/services/report.service";
20
20
  import Aux2faAuth from "./macros/helpers/Aux2faAuth";
21
21
  import { AuxGoogleAuth } from "./macros/helpers/AuxGoogleAuth";
22
+ import emailHelper from "./macros/helpers/emailHelper";
22
23
  dotenv.config();
23
24
  class AuxTA extends FunctionHelper {
24
25
  constructor() {
@@ -27,6 +28,7 @@ class AuxTA extends FunctionHelper {
27
28
  this.aux2fa = Aux2faAuth;
28
29
  this.google = AuxGoogleAuth;
29
30
  this.config = c;
31
+ this.emailHelper = emailHelper;
30
32
  let file;
31
33
  if (!process.env.RPA) {
32
34
  try {
@@ -1,6 +1,5 @@
1
1
  import { StatusOfStep } from "../../auxta/enums/status-of.step";
2
2
  import { ExtendDefaultPage } from "./extend-default-page";
3
- import { gmail_v1 } from "googleapis";
4
3
  export declare class FunctionHelper extends ExtendDefaultPage {
5
4
  log(keyword: string, name: string, status: StatusOfStep): void;
6
5
  suggest(name: string): void;
@@ -17,18 +16,6 @@ export declare class FunctionHelper extends ExtendDefaultPage {
17
16
  clickAndWaitForPageToBeCreated(selector: string, page?: import("puppeteer").Page): Promise<void>;
18
17
  closeLastPage(page?: import("puppeteer").Page): Promise<void>;
19
18
  microsoftLogin(button: string, email: string, password: string): Promise<void>;
20
- verifyEmail(from_name: string, from_email: string, subject: string, body: string): Promise<{
21
- id: string;
22
- threadId: string;
23
- } | undefined>;
24
- replyEmail(ids: {
25
- id: string;
26
- threadId: string;
27
- }, body: string): Promise<void>;
28
- private getHeader;
29
- private getBody;
30
- }
31
- export interface gmailResponse extends gmail_v1.Gmail {
32
19
  }
33
20
  declare const _default: FunctionHelper;
34
21
  export default _default;
@@ -13,9 +13,6 @@ import { StatusOfStep } from "../../auxta/enums/status-of.step";
13
13
  import { StepStatus } from "../../AuxTA";
14
14
  import { ExtendDefaultPage } from "./extend-default-page";
15
15
  import { KnownDevices } from "puppeteer";
16
- import { AuxGoogleAuth } from "./AuxGoogleAuth";
17
- import { atob } from "buffer";
18
- const MailComposer = require('nodemailer/lib/mail-composer');
19
16
  export class FunctionHelper extends ExtendDefaultPage {
20
17
  log(keyword, name, status) {
21
18
  log.push(keyword, name, status);
@@ -184,95 +181,5 @@ export class FunctionHelper extends ExtendDefaultPage {
184
181
  yield loginPage.waitForNetworkIdle();
185
182
  });
186
183
  }
187
- verifyEmail(from_name, from_email, subject, body) {
188
- return __awaiter(this, void 0, void 0, function* () {
189
- yield AuxGoogleAuth.setup();
190
- const res = yield AuxGoogleAuth.gmailClient.users.messages.list({
191
- userId: "me",
192
- maxResults: 3
193
- });
194
- const messagesIds = res.data.messages;
195
- if (!messagesIds || messagesIds.length === 0) {
196
- throw new Error('No messagesIds found.');
197
- }
198
- let timeoutCount = 12000;
199
- yield this.timeout(12000);
200
- while (timeoutCount <= 60000) {
201
- for (const messageId of messagesIds) {
202
- if (messageId.id) {
203
- const gmailResponse = yield AuxGoogleAuth.gmailClient.users.messages.get({
204
- userId: "me",
205
- id: messageId.id.toString(),
206
- format: 'FULL'
207
- });
208
- if (gmailResponse.data.payload) {
209
- const message_sender = this.getHeader('from', gmailResponse.data.payload.headers);
210
- const message_subject = this.getHeader('subject', gmailResponse.data.payload.headers);
211
- const message_body = this.getBody(from_name, from_email, subject, gmailResponse);
212
- if (message_sender.includes(from_email) && message_sender.includes(from_name.toLocaleLowerCase()) &&
213
- message_subject.includes(subject.toLocaleLowerCase()) && message_body.includes(body.toLocaleLowerCase())) {
214
- yield this.log('Then', `Email from: ${from_name} ${from_email} with subject: ${subject} and body: ${body} is found`, StepStatus.PASSED);
215
- return { id: gmailResponse.data.id.toString(), threadId: gmailResponse.data.threadId.toString() };
216
- }
217
- }
218
- break;
219
- }
220
- }
221
- yield this.timeout(12000);
222
- timeoutCount += 12000;
223
- }
224
- yield this.log('Then', `Email from: ${from_name} ${from_email} with subject: ${subject} and body: ${body} is found`, StepStatus.FAILED);
225
- });
226
- }
227
- replyEmail(ids, body) {
228
- return __awaiter(this, void 0, void 0, function* () {
229
- yield AuxGoogleAuth.setup();
230
- const gmailResponse = yield AuxGoogleAuth.gmailClient.users.messages.get({
231
- userId: "me",
232
- id: ids.id,
233
- format: 'FULL'
234
- });
235
- if (gmailResponse.data.payload) {
236
- const message_to = this.getHeader('to', gmailResponse.data.payload.headers);
237
- const message_sender = this.getHeader('from', gmailResponse.data.payload.headers);
238
- const message_subject = this.getHeader('subject', gmailResponse.data.payload.headers);
239
- const message_messageID = this.getHeader('Message-ID', gmailResponse.data.payload.headers);
240
- const options = {
241
- "to": message_to,
242
- "from": message_sender,
243
- "subject": message_subject,
244
- "Reference": message_messageID,
245
- "In-Reply-To": message_messageID,
246
- "threadId": ids.threadId,
247
- "html": body
248
- };
249
- const mailComposer = new MailComposer(options);
250
- const message = yield mailComposer.compile().build();
251
- const encodeMessage = Buffer.from(message).toString('base64').replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
252
- yield AuxGoogleAuth.gmailClient.users.messages.send({
253
- userId: "me",
254
- requestBody: {
255
- raw: encodeMessage
256
- }
257
- });
258
- }
259
- });
260
- }
261
- getHeader(value, headers) {
262
- for (const header of headers) {
263
- if (header.name.toLocaleLowerCase() === value.toLocaleLowerCase()) {
264
- return header.value.toLocaleLowerCase();
265
- }
266
- }
267
- }
268
- getBody(from_name, from_email, subject, gmailResponse) {
269
- const body = gmailResponse.data.payload.parts[0].body.data;
270
- if (body) {
271
- return atob(body).toLocaleLowerCase();
272
- }
273
- else {
274
- throw new Error(`Email from: ${from_name} ${from_email} with subject: ${subject} body is empty`);
275
- }
276
- }
277
184
  }
278
185
  export default new FunctionHelper();
@@ -0,0 +1,17 @@
1
+ import { gmail_v1 } from "googleapis";
2
+ export declare class EmailHelper {
3
+ verifyEmail(from_name: string, from_email: string, subject: string, body: string): Promise<{
4
+ id: string;
5
+ threadId: string;
6
+ } | undefined>;
7
+ replyEmail(ids: {
8
+ id: string;
9
+ threadId: string;
10
+ }, body: string): Promise<void>;
11
+ private getHeader;
12
+ private getBody;
13
+ }
14
+ export interface gmailResponse extends gmail_v1.Gmail {
15
+ }
16
+ declare const _default: EmailHelper;
17
+ export default _default;
@@ -0,0 +1,114 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import log from "../../auxta/services/log.service";
11
+ import { AuxGoogleAuth } from "./AuxGoogleAuth";
12
+ import { atob } from "buffer";
13
+ import { StepStatus } from "../../AuxTA";
14
+ const MailComposer = require('nodemailer/lib/mail-composer');
15
+ export class EmailHelper {
16
+ verifyEmail(from_name, from_email, subject, body) {
17
+ return __awaiter(this, void 0, void 0, function* () {
18
+ yield AuxGoogleAuth.setup();
19
+ const res = yield AuxGoogleAuth.gmailClient.users.messages.list({
20
+ userId: "me",
21
+ maxResults: 3
22
+ });
23
+ const messagesIds = res.data.messages;
24
+ if (!messagesIds || messagesIds.length === 0) {
25
+ throw new Error('No messagesIds found.');
26
+ }
27
+ let timeoutCount = 12000;
28
+ while (timeoutCount <= 60000) {
29
+ for (const messageId of messagesIds) {
30
+ if (messageId.id) {
31
+ const gmailResponse = yield AuxGoogleAuth.gmailClient.users.messages.get({
32
+ userId: "me",
33
+ id: messageId.id.toString(),
34
+ format: 'FULL'
35
+ });
36
+ if (gmailResponse.data.payload && gmailResponse.data.id && gmailResponse.data.threadId) {
37
+ const message_sender = this.getHeader('from', gmailResponse.data.payload.headers);
38
+ const message_subject = this.getHeader('subject', gmailResponse.data.payload.headers);
39
+ const message_body = this.getBody(from_name, from_email, subject, gmailResponse);
40
+ if (message_sender.includes(from_email) && message_sender.includes(from_name.toLocaleLowerCase()) &&
41
+ message_subject.includes(subject.toLocaleLowerCase()) && message_body.includes(body.toLocaleLowerCase())) {
42
+ log.push('Then', `Email from: ${from_name} ${from_email} with subject: ${subject} and body: ${body} is found`, StepStatus.PASSED);
43
+ yield AuxGoogleAuth.gmailClient.users.messages.modify({
44
+ userId: "me",
45
+ id: gmailResponse.data.id,
46
+ requestBody: {
47
+ 'addLabelIds': [],
48
+ 'removeLabelIds': ['UNREAD']
49
+ }
50
+ });
51
+ return { id: gmailResponse.data.id.toString(), threadId: gmailResponse.data.threadId.toString() };
52
+ }
53
+ }
54
+ break;
55
+ }
56
+ }
57
+ new Promise(r => setTimeout(r, 12000));
58
+ timeoutCount += 12000;
59
+ }
60
+ yield log.push('Then', `Email from: ${from_name} ${from_email} with subject: ${subject} and body: ${body} is found`, StepStatus.FAILED);
61
+ });
62
+ }
63
+ replyEmail(ids, body) {
64
+ return __awaiter(this, void 0, void 0, function* () {
65
+ yield AuxGoogleAuth.setup();
66
+ const gmailResponse = yield AuxGoogleAuth.gmailClient.users.messages.get({
67
+ userId: "me",
68
+ id: ids.id,
69
+ format: 'FULL'
70
+ });
71
+ if (gmailResponse.data.payload) {
72
+ const message_sender = this.getHeader('from', gmailResponse.data.payload.headers);
73
+ const message_subject = this.getHeader('subject', gmailResponse.data.payload.headers);
74
+ const message_messageID = this.getHeader('Message-ID', gmailResponse.data.payload.headers);
75
+ const reply_to = this.getHeader('Reply-To', gmailResponse.data.payload.headers);
76
+ const options = {
77
+ "to": reply_to,
78
+ "from": message_sender,
79
+ "subject": `Re: ${message_subject}`,
80
+ "Reference": message_messageID,
81
+ "Reply-To": reply_to,
82
+ "threadId": ids.threadId,
83
+ "html": body
84
+ };
85
+ const mailComposer = new MailComposer(options);
86
+ const message = yield mailComposer.compile().build();
87
+ const encodeMessage = Buffer.from(message).toString('base64').replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
88
+ yield AuxGoogleAuth.gmailClient.users.messages.send({
89
+ userId: "me",
90
+ requestBody: {
91
+ raw: encodeMessage
92
+ }
93
+ });
94
+ }
95
+ });
96
+ }
97
+ getHeader(value, headers) {
98
+ for (const header of headers) {
99
+ if (header.name.toLocaleLowerCase() === value.toLocaleLowerCase()) {
100
+ return header.value.toLocaleLowerCase();
101
+ }
102
+ }
103
+ }
104
+ getBody(from_name, from_email, subject, gmailResponse) {
105
+ const body = gmailResponse.data.payload.parts[0].body.data;
106
+ if (body) {
107
+ return atob(body).toLocaleLowerCase();
108
+ }
109
+ else {
110
+ throw new Error(`Email from: ${from_name} ${from_email} with subject: ${subject} body is empty`);
111
+ }
112
+ }
113
+ }
114
+ export default new EmailHelper();