Skip to content

Commit 129ebe0

Browse files
committed
[Fix] check initialRun
1 parent 1b9dfe1 commit 129ebe0

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

Code.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ function mitime(isInitialRun = !1) {
5454
function prepareEmailProperties(date2) {
5555
if (!date2)
5656
throw new MitimeError(prepareEmailProperties, "Date is empty");
57-
return logger(prepareEmailProperties, "Preparing email properties", `date: ${date2}`), {
57+
return logger(prepareEmailProperties, "Preparing email properties"), {
5858
MITIME_DATE: date2,
5959
MITIME,
6060
MITIME_URL,
@@ -92,7 +92,7 @@ function mitime(isInitialRun = !1) {
9292
throw new MitimeError(generateEmail, "isInitialEmail is empty");
9393
if (!date2)
9494
throw new MitimeError(generateEmail, "Date is empty");
95-
logger(generateEmail, "Generating email", `isInitialEmail: ${isInitialEmail}`, `date: ${date2}`);
95+
logger(generateEmail, "Generating email", `isInitialEmail: ${isInitialEmail}`);
9696
const emailContent = generateEmailContent(
9797
isInitialEmail ? EMAIL_TEMPLATES.INITIAL : EMAIL_TEMPLATES.REGULAR,
9898
throwbackContent
@@ -131,14 +131,14 @@ function mitime(isInitialRun = !1) {
131131
throw new MitimeError(createAlias, "User is not defined");
132132
if (!label2)
133133
throw new MitimeError(createAlias, "Alias is not defined", user2);
134-
return logger(createAlias, "Creating alias", user2, label2), `${user2.split("@")[0]}+${label2}@${user2.split("@")[1]}`;
134+
return logger(createAlias, "Creating alias", user2), `${user2.split("@")[0]}+${label2}@${user2.split("@")[1]}`;
135135
}
136136
function checkLabel(user2, label2) {
137137
if (!user2)
138138
throw new MitimeError(checkLabel, "User is not defined");
139139
if (!label2)
140140
throw new MitimeError(checkLabel, "Label is not defined", user2);
141-
logger(checkLabel, "Checking label", user2, label2);
141+
logger(checkLabel, `Checking label ${label2}`, user2);
142142
const foundLabel = GmailApp.getUserLabelByName(label2);
143143
foundLabel || GmailApp.createLabel(foundLabel);
144144
}
@@ -150,10 +150,10 @@ function mitime(isInitialRun = !1) {
150150
throw new MitimeError(getLabelId, "Labels are not defined", user2);
151151
if (!label2)
152152
throw new MitimeError(getLabelId, "Labels array is not defined");
153-
logger(getLabelId, "Getting label id", user2, label2);
153+
logger(getLabelId, `Getting label id of label ${label2}`, user2);
154154
const labelId2 = (_a = labels2.find((l) => l.name === label2)) == null ? void 0 : _a.id;
155155
if (!labelId2)
156-
throw new MitimeError(getLabelId, "Could not find label id", user2, label2);
156+
throw new MitimeError(getLabelId, `Could not find label id for label ${label2}`, user2);
157157
return labelId2;
158158
}
159159
function checkFilters(user2, filters2, filterCriteria2) {
@@ -208,7 +208,7 @@ function mitime(isInitialRun = !1) {
208208
let movedToTrash = !1;
209209
for (let i = 0; i < threads.length; i++) {
210210
const message = threads[i].getMessages()[0], fromMitime = `${MITIME} <${alias2}>`;
211-
message.getFrom() === fromMitime && (logger(removeEmails, "Moving to trash", user2, label2, message.getId()), message.moveToTrash(), movedToTrash = !0);
211+
message.getFrom() === fromMitime && (logger(removeEmails, "Moving to trash", user2, message.getId()), message.moveToTrash(), movedToTrash = !0);
212212
}
213213
movedToTrash && deleteForever(user2, label2);
214214
}

src/index.js

+18-3
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,25 @@ function logger(func, message, ...args) {
9191
/**
9292
* @name mitime
9393
* @description Main function for mitime
94-
* @param {boolean} isInitialRun decides if it's initial run or not and based on that it will send specific email
94+
* @param {Record<any, any> & {isInitialRun: boolean}} props decides if it's initial run or not and based on that it will send specific email
9595
* @returns {void}
9696
*/
97-
export function mitime(isInitialRun = false) {
97+
export function mitime(props) {
98+
/**
99+
* @name checkIsInitialRun
100+
* @description Check if function is run for the first time
101+
* @param {Record<any, any> & {isInitialRun: boolean}} properties decides if it's initial run or not and based on that it will send specific email
102+
* @returns {boolean} isInitialRun
103+
*/
104+
function checkIsInitialRun(properties) {
105+
let isInitialRun;
106+
if (!properties || Object.keys(properties).length === 0) isInitialRun = false;
107+
if (properties && properties.isInitialRun) isInitialRun = properties.isInitialRun;
108+
109+
return isInitialRun;
110+
}
111+
112+
const isInitialRun = checkIsInitialRun(props);
98113
logger(mitime, `Running mitime`, `isInitialRun: ${isInitialRun}`);
99114

100115
/**
@@ -542,7 +557,7 @@ export function doGet() {
542557

543558
setupTrigger(mitime.name);
544559
// Run mitime for the first time
545-
mitime(true);
560+
mitime({ isInitialRun: true });
546561

547562
logger(doGet, 'Finished doGet');
548563

0 commit comments

Comments
 (0)