-
Notifications
You must be signed in to change notification settings - Fork 63
feat: weekly newsletter with upcoming events #331
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
iiio2
wants to merge
21
commits into
main
Choose a base branch
from
email-render-ts
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
7d141a4
weekly newsletter
razbakov 8d64a6c
render email in ts.
iiio2 3a34a49
add firebase to get data.
iiio2 9c9a961
get events from firebase.
iiio2 fd68f7b
get events between seven days of a specific place
iiio2 c5eb367
get profile data.
iiio2 ba6c242
fix code formatting.
iiio2 2f54c20
remove repetitive code.
iiio2 e77b729
build scheduleEmail.
iiio2 620f326
refactor code.
iiio2 103be6d
add weekly.html in .gitignore
iiio2 a519056
fix some issues.
iiio2 9d523be
improve weekly newsletter
razbakov 54ed8e0
Merge pull request #359 from we-dance/email-render-ts-alex
iiio2 62269c6
clean up code.
iiio2 eb5c93f
format weekly.mjml
iiio2 ce4ad94
remove emails folder.
iiio2 0446dcb
format code.
iiio2 44aef5a
fix newsletter
razbakov 59f0295
fix emails
razbakov 96d4b75
fix newsletter
razbakov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -96,3 +96,4 @@ sw.* | |
|
|
||
| # Firebase | ||
| .vscode | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,3 +7,5 @@ typings/ | |
|
|
||
| node_modules/ | ||
| var/ | ||
|
|
||
| serviceAccountKey.json | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iiio2 marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,220 @@ | ||
| 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' | ||
| import sendEmail from './sendEmail' | ||
| import { orderBy } from 'lodash' | ||
|
|
||
| export async function renderEmail(type: string, data: any, customUtms = {}) { | ||
| const template = fs.readFileSync(`./src/templates/${type}.mjml`, 'utf8') | ||
|
|
||
| const defaultUtms = { | ||
| campaign: type, | ||
| medium: 'email', | ||
| source: 'newsletter', | ||
| } | ||
|
|
||
| const utm = { | ||
| ...defaultUtms, | ||
| ...customUtms, | ||
| } | ||
|
|
||
| const app = createSSRApp({ | ||
| data: () => { | ||
| return data | ||
| }, | ||
| template, | ||
| methods: { | ||
| link(url: string, utmContent = '') { | ||
| return ( | ||
| url + | ||
| '?utm_campaign=' + | ||
| utm.campaign + | ||
| '&utm_medium=' + | ||
| utm.medium + | ||
| '&utm_source=' + | ||
| utm.source + | ||
| '&utm_content=' + | ||
| utmContent | ||
| ) | ||
| }, | ||
| }, | ||
| }) | ||
|
|
||
| app.config.compilerOptions.isCustomElement = (tag: any) => | ||
| tag.startsWith('mj') | ||
|
|
||
| return mjml2html(await renderToString(app)).html | ||
| } | ||
|
|
||
| export async function getWeeklyData(city: string) { | ||
| let cityProfile: any = {} | ||
|
|
||
| const profileDocs = ( | ||
| await firestore | ||
| .collection('profiles') | ||
| .where('username', '==', city) | ||
iiio2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| .get() | ||
| ).docs | ||
|
|
||
| for (const doc of profileDocs) { | ||
| cityProfile = { id: doc.id, ...doc.data() } | ||
| } | ||
|
|
||
| const today = moment(new Date()) | ||
| const sevenDaysFromNow = moment(today).add(7, 'days') | ||
|
|
||
| let eventDocs = ( | ||
| await firestore | ||
| .collection('posts') | ||
| .where('startDate', '>', today.unix()) | ||
| .where('place', '==', cityProfile.place) | ||
| .get() | ||
| ).docs.map((doc: any) => ({ id: doc.id, ...doc.data() })) | ||
|
|
||
| eventDocs = orderBy(eventDocs, ['startDate'], ['asc']) | ||
| eventDocs = eventDocs.filter((event: any) => | ||
| ['Party', 'Festival', 'Show', 'Concert'].includes(event.eventType) | ||
| ) | ||
| eventDocs = eventDocs.filter( | ||
| (event: any) => | ||
| moment(event.startDate).unix() >= today.unix() && | ||
| moment(event.startDate).unix() <= sevenDaysFromNow.unix() | ||
| ) | ||
|
|
||
| const days: any = {} | ||
|
|
||
| for (const event of eventDocs) { | ||
| const group = moment(event.startDate).format('YYYY-MM-DD') | ||
|
|
||
| if (!days[group]) { | ||
| days[group] = { | ||
| day: moment(event.startDate).format('dddd'), | ||
| date: moment(event.startDate).format('D MMM'), | ||
| events: [], | ||
| } | ||
| } | ||
|
|
||
| days[group].events.push({ | ||
| title: event.name, | ||
| organizer: event.org.name, | ||
| venue: event.venue?.name, | ||
| format: event.eventType, | ||
| time: moment(event.startDate).format('HH:mm'), | ||
| link: `https://wedance.vip/events/${event.id}`, | ||
| cover: event.cover, | ||
| styles: Object.keys(event.styles), | ||
| }) | ||
| } | ||
|
|
||
| const events: any = { | ||
| intro: | ||
| 'Hope you had a great weekend and are ready with your dancing shoes on for a fantastic week ahead.', | ||
| title: `${city} Dance Calendar`, | ||
| links: { | ||
| telegram: cityProfile.telegram, | ||
| instagram: cityProfile.instagram, | ||
| facebook: cityProfile.facebook, | ||
| addEvent: 'https://wedance.vip/events/-/edit', | ||
| city: `https://wedance.vip/${city}`, | ||
| }, | ||
| days, | ||
| } | ||
|
|
||
| return events | ||
| } | ||
|
|
||
| export async function getSubscribers(cityProfile: any) { | ||
| const recipients = {} as any | ||
|
|
||
| const subscribers = cityProfile.watch?.usernames || [] | ||
|
|
||
| for (let subscriber of subscribers) { | ||
| const profilesOfSubscriber = ( | ||
| await firestore | ||
| .collection('profiles') | ||
| .where('username', '==', subscriber) | ||
| .get() | ||
| ).docs | ||
|
|
||
| if (profilesOfSubscriber.length !== 1) { | ||
| // console.log(`Subscriber ${subscriber} not found`) | ||
| continue | ||
| } | ||
|
|
||
| const profileId = profilesOfSubscriber[0].id | ||
|
|
||
| const accountDoc = await firestore | ||
| .collection('accounts') | ||
| .doc(profileId) | ||
| .get() | ||
|
|
||
| if (!accountDoc.exists) { | ||
| // console.log(`Account for ${subscriber} not found`) | ||
| continue | ||
| } | ||
|
|
||
| const account = accountDoc.data() | ||
|
|
||
| recipients[profileId] = { | ||
| name: account?.name, | ||
| email: account?.email, | ||
| } | ||
| } | ||
|
|
||
| return recipients | ||
| } | ||
|
|
||
| export async function scheduleWeeklyNewsletter() { | ||
| const cityDocs = ( | ||
| await firestore | ||
| .collection('profiles') | ||
| .where('username', '==', 'Munich') | ||
| .get() | ||
| ).docs | ||
iiio2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| for (let cityDoc of cityDocs) { | ||
| const cityProfile = cityDoc.data() | ||
|
|
||
| if (!cityProfile.watch?.usernames) { | ||
| continue | ||
| } | ||
|
|
||
| const weeklyEmailDetails = await getWeeklyData(cityProfile.username) | ||
|
|
||
| const html = await renderEmail('weekly', weeklyEmailDetails) | ||
| const recipients = await getSubscribers(cityProfile) | ||
|
|
||
| const emailDoc: any = { | ||
| status: 'scheduled', | ||
| scheduledAt: moment() | ||
| .hour(18) | ||
| .minute(0) | ||
| .second(0), | ||
| createdAt: Date.now(), | ||
| from: `WeDance <noreply@wedance.vip>`, | ||
| subject: 'Weekly Newsletter', | ||
| recipients, | ||
| html, | ||
| } | ||
|
|
||
| const emailRef = await firestore.collection('emails').add(emailDoc) | ||
|
|
||
| emailDoc.id = emailRef.id | ||
|
|
||
| await sendEmail(emailDoc) | ||
|
|
||
| await firestore | ||
| .collection('emails') | ||
| .doc(emailDoc.id) | ||
| .update({ | ||
| status: 'sent', | ||
| processedAt: Date.now(), | ||
| error: '', | ||
| }) | ||
| } | ||
|
|
||
| return null | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.