From 4c2f900d85e56b1994630942c9ea53d0fd0b4cdb Mon Sep 17 00:00:00 2001 From: towfiqi Date: Sat, 9 Nov 2024 21:24:27 +0600 Subject: [PATCH] feat: Displays keyword's best position in email notification. closes 202 --- email/email.html | 1 + utils/generateEmail.ts | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/email/email.html b/email/email.html index 812b4cd..59dab69 100644 --- a/email/email.html +++ b/email/email.html @@ -442,6 +442,7 @@ Keyword Position + Best Updated {{keywordsTable}} diff --git a/utils/generateEmail.ts b/utils/generateEmail.ts index 2fc8e12..c4f40dc 100644 --- a/utils/generateEmail.ts +++ b/utils/generateEmail.ts @@ -68,6 +68,19 @@ const getPositionChange = (history:KeywordHistory, position:number) : number => return status; }; +const getBestKeywordPosition = (history: KeywordHistory) => { + let bestPos; + if (Object.keys(history).length > 0) { + const historyArray = Object.keys(history).map((itemID) => ({ date: itemID, position: history[itemID] })) + .sort((a, b) => a.position - b.position).filter((el) => (el.position > 0)); + if (historyArray[0]) { + bestPos = { ...historyArray[0] }; + } + } + + return bestPos?.position || '-'; +}; + /** * Generate the Email HTML based on given domain name and its keywords * @param {string} domainName - Keywords to scrape @@ -97,6 +110,7 @@ const generateEmail = async (domainName:string, keywords:KeywordType[], settings keywordsTable += ` ${countryFlag} ${deviceIcon} ${keyword.keyword} ${keyword.position}${posChangeIcon} + ${getBestKeywordPosition(keyword.history)} ${timeSince(new Date(keyword.lastUpdated).getTime() / 1000)} `; });