Skip to content

Commit 83ff098

Browse files
committed
[Fix] removing invalid emails
1 parent 030f0eb commit 83ff098

1 file changed

Lines changed: 48 additions & 37 deletions

File tree

src/index.js

Lines changed: 48 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,10 @@ class MitimeError extends Error {
100100

101101
/**
102102
* @name logger
103-
* @param {Function} func
104-
* @param {string} message
105-
* @param {any[]} args
103+
* @description basic logger
104+
* @param {Function} func function
105+
* @param {string} message message
106+
* @param {any[]} args arguments to be additionally logged
106107
*/
107108
function logger(func, message, ...args) {
108109
// eslint-disable-next-line no-console
@@ -136,10 +137,11 @@ export function mitime(props) {
136137
MITIME_SETTINGS_URL: MITIME_TRIGGER_SETTINGS_URL,
137138
};
138139
}
140+
139141
/**
140142
* @name randomElement
141143
* @description returns random element from array
142-
* @param {T[]} array
144+
* @param {T[]} array array of elements
143145
* @returns {T} random element from array
144146
*/
145147
function randomElement(array) {
@@ -153,7 +155,7 @@ export function mitime(props) {
153155
/**
154156
* @name prepareEmail
155157
* @description replaces properties in string with values from properties object
156-
* @param {string} string
158+
* @param {string} string email content with properties to be replaced
157159
* @param {Record<string, string>} properties
158160
* @returns {string} string with replaced properties
159161
*/
@@ -175,8 +177,8 @@ export function mitime(props) {
175177
/**
176178
* @name generateEmailContent
177179
* @description generates email content from random elements of the template
178-
* @param {object} template
179-
* @param {string} throwbackContent
180+
* @param {object} template email template
181+
* @param {string} throwbackContent content of throwback email to be added to the template
180182
* @returns {string} email content
181183
*/
182184
function generateEmailContent(template = EMAIL_TEMPLATES.REGULAR, throwbackContent = '') {
@@ -228,9 +230,9 @@ export function mitime(props) {
228230
/**
229231
* @name getFilters
230232
* @description prepares filters object for mitime
231-
* @param {string} user
232-
* @param {string} alias
233-
* @param {string} labelId
233+
* @param {string} user user email
234+
* @param {string} alias user alias
235+
* @param {string} labelId label id
234236
* @returns {Filters} Filter object
235237
*/
236238
const getFilters = (user, alias, labelId) => {
@@ -277,8 +279,8 @@ export function mitime(props) {
277279
/**
278280
* @name checkLabel
279281
* @description check if label exist, if not create it
280-
* @param {string} user
281-
* @param {string} label
282+
* @param {string} user user email
283+
* @param {string} label label
282284
*/
283285
function checkLabel(user, label) {
284286
if (!user) throw new MitimeError(checkLabel, 'User is not defined');
@@ -293,8 +295,8 @@ export function mitime(props) {
293295
/**
294296
* @name getLabelId
295297
* @description get label ids of provided labels
296-
* @param {string} user
297-
* @param {GoogleAppsScript.Gmail.Schema.Label[] | undefined} labels
298+
* @param {string} user user email
299+
* @param {GoogleAppsScript.Gmail.Schema.Label[] | undefined} labels labels
298300
* @param {string} label label
299301
* @returns {string} label id
300302
*/
@@ -313,9 +315,10 @@ export function mitime(props) {
313315

314316
/**
315317
* @name checkFilters
316-
* @param {string} user
317-
* @param {GoogleAppsScript.Gmail.Schema.Filter[] | undefined}
318-
* @param {Filters} filterCriteria
318+
* @description checks for existing filters and creates them if missing
319+
* @param {string} user user email
320+
* @param {GoogleAppsScript.Gmail.Schema.Filter[] | undefined} filters object
321+
* @param {Filters} filterCriteria filter criteria object
319322
* @returns {void}
320323
*/
321324
function checkFilters(user, filters, filterCriteria) {
@@ -363,30 +366,38 @@ export function mitime(props) {
363366
}
364367

365368
/**
366-
* @name deleteForever
367-
* @param {string} user
368-
* @param {string} label
369+
* @name deleteEmails
370+
* @description removes message from trash
371+
* @param {string} user user email
372+
* @param {string} alias user alias
373+
* @param {string} label user label
369374
*/
370-
function deleteForever(user, label) {
371-
if (!user) throw new MitimeError(deleteForever, 'User is not defined');
372-
if (!label) throw new MitimeError(deleteForever, 'Label is not defined', user);
375+
function deleteEmails(user, alias, label) {
376+
if (!user) throw new MitimeError(deleteEmails, 'User is not defined');
377+
if (!alias) throw new MitimeError(deleteEmails, 'Alias is not defined', user);
378+
if (!label) throw new MitimeError(deleteEmails, 'Label is not defined', user);
373379

374380
const threads = GmailApp.search(`in:trash label:${label}`);
375381

376382
for (let i = 0; i < threads.length; i++) {
377-
const threadId = threads[i].getId();
383+
const messages = threads[i].getMessages();
384+
const message = messages[0];
385+
const messageId = message.getId();
386+
const fromMitime = `${MITIME} <${alias}>`;
378387

379-
logger(deleteForever, `Deleting thread`, user, threadId);
388+
if (message.getFrom() === fromMitime) {
389+
logger(deleteEmails, 'Deleting message', user, messageId);
380390

381-
Gmail.Users.Threads.remove(user, threadId);
391+
Gmail.Users.Messages.remove(user, messageId);
392+
}
382393
}
383394
}
384395

385396
/**
386397
* @name removeEmails
387-
* @param {string} user
388-
* @param {string} alias
389-
* @param {string} label
398+
* @param {string} user user email
399+
* @param {string} alias user alias
400+
* @param {string} label user label
390401
*/
391402
function removeEmails(user, alias, label) {
392403
if (!user) throw new MitimeError(removeEmails, 'User is not defined');
@@ -408,17 +419,17 @@ export function mitime(props) {
408419
}
409420
}
410421

411-
if (movedToTrash) deleteForever(user, label);
422+
if (movedToTrash) deleteEmails(user, label);
412423
}
413424

414425
/**
415426
* @name getDate
416-
* @param {string} locale
427+
* @param {string} locale user locale
417428
* @param {Date} date date to format
418429
* @returns {string} formatted date
419430
*/
420431
function getDate(locale, date = new Date()) {
421-
if (!locale) throw new MitimeError(getDate, 'Timezone is not defined');
432+
if (!locale) throw new MitimeError(getDate, 'Locale is not defined');
422433
if (!date) throw new MitimeError(getDate, 'Date is not defined');
423434

424435
logger(getDate, `Getting date for locale: ${locale} and date: ${date}`);
@@ -433,8 +444,8 @@ export function mitime(props) {
433444

434445
/**
435446
* @name getPreviousDate
436-
* @param {number} index
437-
* @param {string} locale
447+
* @param {number} index index
448+
* @param {string} locale user locale
438449
* @returns {{name: string, date: string}} object with random date name and date
439450
*/
440451
function getPreviousDate(index, locale) {
@@ -465,9 +476,9 @@ export function mitime(props) {
465476

466477
/**
467478
* @name getRandomIndex
468-
* @param {number} min
469-
* @param {number} max
470-
* @returns {number}
479+
* @param {number} min starting number
480+
* @param {number} max max number
481+
* @returns {number} random index
471482
*/
472483
function getRandomIndex(min, max) {
473484
if (min === null || min === undefined) throw new MitimeError(getRandomIndex, 'Min is not defined');

0 commit comments

Comments
 (0)