Skip to content

Commit

Permalink
build scheduleEmail.
Browse files Browse the repository at this point in the history
  • Loading branch information
iiio2 committed Mar 9, 2023
1 parent 2f54c20 commit e77b729
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 29 deletions.
4 changes: 2 additions & 2 deletions services/firebase/emails/weekly.html
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@
<table
cellpadding="0" cellspacing="0" width="100%" border="0" style="color:#000000;font-family:Helvetica Neue;font-size:13px;line-height:22px;table-layout:auto;width:100%;border:none;"
>
<tr><td colspan="3"><div style="font-size:16px;line-height:24px;font-weight:700;padding:8px 0;"><span style="color:#f1023d;"></span></div></td></tr><!--[--><!--]-->
<tr><td colspan="3"><div style="font-size:16px;line-height:24px;font-weight:700;padding:8px 0;"><span style="color:#f1023d;">Friday</span> • 10 Mar</div></td></tr><!--[--><tr style="border-top:solid 1px #f4f4f5;"><td style="width:50px;vertical-align:top;font-size:16px;padding-top:4px;">02:00</td><td style="vertical-align:top;padding-left:8px;padding-right:8px;padding-top:4px;"><div style="font-size:16px;font-weight:bold;"><a href="20?utm_campaign=weekly&amp;utm_medium=email&amp;utm_source=newsletter&amp;utm_content=event-card" style="color:#000;">john smith</a></div><div style="overflow:hidden;height:1.5em;font-size:14px;">Alex Razbakov • Munich</div><div style="overflow:hidden;height:1.5em;font-size:14px;"><span style="color:#f1023d;">Festival</span> • AfroHouse</div></td><td style="width:100px;padding-top:4px;"><img width="100px" src="https://firebasestorage.googleapis.com/v0/b/wedancedev-aa00e.appspot.com/o/media%2FG4hwpZo4HNTy05vKygtqz2YsSEu2%2F5ec3ab5c-8d34-4f4a-bb07-8e0b53426177?alt=media&amp;token=f80e2961-09d5-433f-9a73-e14e5b4f420f"></td></tr><!--]-->
</table>

</td>
Expand All @@ -274,7 +274,7 @@
<table
cellpadding="0" cellspacing="0" width="100%" border="0" style="color:#000000;font-family:Helvetica Neue;font-size:13px;line-height:22px;table-layout:auto;width:100%;border:none;"
>
<tr><td colspan="3"><div style="font-size:16px;line-height:24px;font-weight:700;padding:8px 0;"><span style="color:#f1023d;"></span></div></td></tr><!--[--><!--]-->
<tr><td colspan="3"><div style="font-size:16px;line-height:24px;font-weight:700;padding:8px 0;"><span style="color:#f1023d;">Monday</span> • 13 Mar</div></td></tr><!--[--><tr style="border-top:solid 1px #f4f4f5;"><td style="width:50px;vertical-align:top;font-size:16px;padding-top:4px;">02:30</td><td style="vertical-align:top;padding-left:8px;padding-right:8px;padding-top:4px;"><div style="font-size:16px;font-weight:bold;"><a href="undefined?utm_campaign=weekly&amp;utm_medium=email&amp;utm_source=newsletter&amp;utm_content=event-card" style="color:#000;">Aina</a></div><div style="overflow:hidden;height:1.5em;font-size:14px;">Aina • Munich</div><div style="overflow:hidden;height:1.5em;font-size:14px;"><span style="color:#f1023d;">Party</span> • Balboa • Afrobeats • AfroHouse</div></td><td style="width:100px;padding-top:4px;"><img width="100px" src="https://firebasestorage.googleapis.com/v0/b/wedancedev-aa00e.appspot.com/o/media%2FG4hwpZo4HNTy05vKygtqz2YsSEu2%2F3ddf5a61-8654-4bc5-9167-7018d6a86b68?alt=media&amp;token=8f4c722e-1bf2-421b-9165-5a2a50895812"></td></tr><!--]-->
</table>

</td>
Expand Down
2 changes: 2 additions & 0 deletions services/firebase/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"mailgun-js": "^0.22.0",
"markdown-it": "^10.0.0",
"mjml": "^4.13.0",
"moment": "^2.29.4",
"node-fetch": "^3.2.10",
"png-to-jpeg": "^1.0.1",
"puppeteer": "^7.0.0",
Expand All @@ -50,6 +51,7 @@
"@types/dotenv": "^8.2.0",
"@types/mjml": "^4.7.0",
"@types/puppeteer": "^5.4.2",
"@types/uuid": "^9.0.1",
"@types/vue": "^2.0.0",
"copyfiles": "^2.4.1",
"firebase-functions-test": "^0.1.6",
Expand Down
83 changes: 83 additions & 0 deletions services/firebase/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { wrap } from './sentry'
import { announceEvent } from './lib/telegram'
import { announceEventIG } from './lib/instagram'
import { getInstagramWebProfileInfo } from './lib/browser'
import { renderEmail, getWeeklyData } from './lib/digest'

require('dotenv').config()

Expand Down Expand Up @@ -656,6 +657,88 @@ export const matchNotification = functions.firestore
await sendEmail(data)
})

export const scheduleEmail = functions.pubsub
.schedule('every monday 18:00')
.timeZone('America/New_York')
.onRun(async (context) => {
const cityDocs = (
await firestore.collection('profiles').where('type', '==', 'City').get()
).docs;
const cities: any = [];
for (let doc of cityDocs) {
cities.push({ id: doc.id, ...doc.data() });
}
let data;
let recipients: any = {};
for (let city of cities) {

if (!city.watch?.list) {
continue;
}

data = await getWeeklyData(city.username);
const html = await renderEmail('weekly', data);

let subscribers = Object.keys(city.watch?.list);

for (let subscriber of subscribers) {
const profilesOfSubscriber = (
await firestore
.collection('profiles')
.where('username', '==', subscriber)
.get()
).docs;

if (profilesOfSubscriber.length !== 1) {
continue;
}

const profileId = profilesOfSubscriber[0].id;

const accountDoc = await firestore
.collection('accounts')
.doc(profileId)
.get();

if (!accountDoc.exists) {
continue;
}

const account = accountDoc.data();

recipients[profileId] = {
name:account?.name,
email:account?.email
}

let userIds = [];
userIds.push(profileId);

const weeklyNewsLetter = await firestore
.collection('weekly-newsletters')
.add({
city: city?.username,
createdAt: Date.now(),
sentAt: Date.now(),
scheduledAt: Date.now(),
userIds: [...userIds],
});

const email: any = {
from: `WeDance <[email protected]>`,
recipients,
subject: 'Weekly Newsletter',
content: html,
id: weeklyNewsLetter.id,
type: 'City',
};
return await sendEmail(email);
}
}
return null;
});


// export const taskRunner = functions
// .runWith({ memory: '2GB' })
// .pubsub.schedule('* * * * *')
Expand Down
62 changes: 37 additions & 25 deletions services/firebase/src/lib/digest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ const { createSSRApp } = require("vue");
const { renderToString } = require("vue/server-renderer");
import mjml2html = require("mjml");
import * as fs from "fs";
import * as moment from "moment";
import { firestore } from "../firebase";


export async function renderEmail(type: string, data: any, customUtms = {}) {
const template = fs.readFileSync(`./templates/${type}.mjml`, "utf8");
Expand Down Expand Up @@ -61,25 +63,29 @@ export async function getWeeklyData(city: string) {
for (const doc of profileDocs) {
profile = { id: doc.id, ...doc.data() };
}

let usernames = Object.keys(profile.watch?.list);

const eventDocs = (
await firestore
.collection("posts")
.where("startDate", ">", today)
.where("startDate", "<", sevenDaysFromNow)
.where("place", "==", profile.place)
.get()
).docs;

for (const doc of eventDocs) {
const event = {
id: doc.id,
...doc.data(),
} as any;
for(let username of usernames) {
const eventDocs = (
await firestore
.collection("posts")
.where("startDate", ">", today)
.where("startDate", "<", sevenDaysFromNow)
.where("username", "==", username)
.get()
).docs;

data.push(event);
for (const doc of eventDocs) {
const event = {
id: doc.id,
...doc.data(),
} as any;

data.push(event);
}
}

const events: any = {
intro:
"Hope you had a great weekend and are ready with your dancing shoes on for a fantastic week ahead.",
Expand All @@ -92,16 +98,22 @@ export async function getWeeklyData(city: string) {
city: `https://wedance.vip/${city}`,
},
days: data.map((event) => ({
title: event.name,
organizer: event.org.name,
venue: event.venue?.name,
format: event.eventType,
time: event.startDate,
link: event.link,
cover: event.cover,
styles: Object.keys(event.styles),
day: moment(event.startDate).format("dddd"),
date: moment(event.startDate).format("D MMM") ,
events: [
{
title: event.name,
organizer: event.org.name,
venue: event.venue?.name,
format: event.eventType,
time: moment(event.startDate).format("hh:mm") ,
link: event.link,
cover: event.cover,
styles: Object.keys(event.styles),
}
]
})),
};

return events;
}
}
1 change: 0 additions & 1 deletion services/firebase/src/lib/sendEmail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ export default async (data: any) => {
'v:campaignId': data.id,
'v:type': data.type,
})

jobs.push(job)
})

Expand Down
2 changes: 1 addition & 1 deletion services/firebase/templates/weekly.mjml
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,4 @@
</mj-column>
</mj-section>
</mj-body>
</mjml>
</mjml>
10 changes: 10 additions & 0 deletions services/firebase/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,11 @@
resolved "https://registry.yarnpkg.com/@types/tough-cookie/-/tough-cookie-4.0.2.tgz#6286b4c7228d58ab7866d19716f3696e03a09397"
integrity sha512-Q5vtl1W5ue16D+nIaW8JWebSSraJVlK+EthKn7e7UcD4KWsaSJ8BqGPXNaPghgtcn/fhvrN17Tv8ksUsQpiplw==

"@types/uuid@^9.0.1":
version "9.0.1"
resolved "https://registry.yarnpkg.com/@types/uuid/-/uuid-9.0.1.tgz#98586dc36aee8dacc98cc396dbca8d0429647aa6"
integrity sha512-rFT3ak0/2trgvp4yYZo5iKFEPsET7vKydKF+VRCxlQ9bpheehyAJH89dAkaLEq/j/RZXJIqcgsmPJKUP1Z28HA==

"@types/vue@^2.0.0":
version "2.0.0"
resolved "https://registry.yarnpkg.com/@types/vue/-/vue-2.0.0.tgz#ec77b3d89591deb9ca5cb052368aa9c32be088e7"
Expand Down Expand Up @@ -3494,6 +3499,11 @@ module-alias@^2.2.2:
resolved "https://registry.yarnpkg.com/module-alias/-/module-alias-2.2.2.tgz#151cdcecc24e25739ff0aa6e51e1c5716974c0e0"
integrity sha512-A/78XjoX2EmNvppVWEhM2oGk3x4lLxnkEA4jTbaK97QKSDjkIoOsKQlfylt/d3kKKi596Qy3NP5XrXJ6fZIC9Q==

moment@^2.29.4:
version "2.29.4"
resolved "https://registry.yarnpkg.com/moment/-/moment-2.29.4.tgz#3dbe052889fe7c1b2ed966fcb3a77328964ef108"
integrity sha512-5LC9SOxjSc2HF6vO2CyuTDNivEdoz2IvyJJGj6X8DJ0eFyfszE0QiEd+iXmBvUP3WHxSjFH/vIsA0EN00cgr8w==

[email protected]:
version "2.0.0"
resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"
Expand Down

0 comments on commit e77b729

Please sign in to comment.