Skip to content
This repository has been archived by the owner on Jun 9, 2023. It is now read-only.

chore: cancel event email content #1517

Merged
merged 20 commits into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 38 additions & 52 deletions server/src/controllers/Events/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ import {
deleteEventReminders,
updateRemindAt,
} from '../../services/Reminders';
import {
generateToken,
UnsubscribeType,
} from '../../services/UnsubscribeToken';
import {
cancelCalendarEvent,
deleteCalendarEvent,
Expand All @@ -58,6 +54,10 @@ import {
} from '../../util/calendar';
import { updateWaitlistForUserRemoval } from '../../util/waitlist';
import { redactSecrets } from '../../util/redact-secrets';
import {
getChapterUnsubscribeOptions,
getEventUnsubscribeOptions,
} from '../../util/eventEmail';
import { EventInputs } from './inputs';

const eventUserIncludes = {
Expand Down Expand Up @@ -86,31 +86,6 @@ const isPhysical = (venue_type: events_venue_type_enum) =>
const isOnline = (venue_type: events_venue_type_enum) =>
venue_type !== events_venue_type_enum.Physical;

const getUnsubscribeOptions = ({
chapterId,
eventId,
userId,
}: {
chapterId: number;
eventId: number;
userId: number;
}) => {
const chapterUnsubscribeToken = generateToken(
UnsubscribeType.Chapter,
chapterId,
userId,
);
const eventUnsubscribeToken = generateToken(
UnsubscribeType.Event,
eventId,
userId,
);
return `
Unsubscribe Options</br>
- <a href="${process.env.CLIENT_LOCATION}/unsubscribe?token=${eventUnsubscribeToken}">Attend this event, but only turn off future notifications for this event</a></br>
- Or, <a href="${process.env.CLIENT_LOCATION}/unsubscribe?token=${chapterUnsubscribeToken}">stop receiving notifications about new events by unfollowing chapter</a>`;
};

const sendRsvpInvitation = async (
user: Required<ResolverCtx>['user'],
event: events & { venue: venues | null },
Expand All @@ -123,7 +98,7 @@ const sendRsvpInvitation = async (
};
if (event.venue?.name) linkDetails.location = event.venue?.name;

const unsubscribeOptions = getUnsubscribeOptions({
const unsubscribeOptions = getEventUnsubscribeOptions({
chapterId: event.chapter_id,
eventId: event.id,
userId: user.id,
Expand Down Expand Up @@ -247,11 +222,10 @@ const rsvpNotifyAdministrators = async (
await batchSender(function* () {
for (const { chapter_id, user } of chapterAdministrators) {
const email = user.email;
const chapterUnsubscribeToken = generateToken(
UnsubscribeType.Chapter,
chapter_id,
user.id,
);
const chapterUnsubscribeToken = getChapterUnsubscribeOptions({
chapterId: chapter_id,
userId: user.id,
});
const text = `${body}<br><a href="${process.env.CLIENT_LOCATION}/unsubscribe?token=${chapterUnsubscribeToken}Unsubscribe from chapter emails`;
yield { email, subject, text };
}
Expand Down Expand Up @@ -573,7 +547,7 @@ export class EventResolver {
include: { event: { include: { chapter: true } }, ...eventUserIncludes },
});

const unsubscribeOptions = getUnsubscribeOptions({
const unsubscribeOptions = getEventUnsubscribeOptions({
chapterId: updatedUser.event.chapter_id,
eventId: updatedUser.event_id,
userId,
Expand Down Expand Up @@ -762,12 +736,12 @@ ${unsubscribeOptions}`,
batchSender(function* () {
for (const { user } of event.event_users) {
const email = user.email;
const unsubScribeOptions = getUnsubscribeOptions({
const unsubscribeOptions = getEventUnsubscribeOptions({
chapterId: event.chapter_id,
eventId: event.id,
userId: user.id,
});
const text = `${body}<br>${unsubScribeOptions}`;
const text = `${body}<br>${unsubscribeOptions}`;
yield { email, subject, text };
}
});
Expand Down Expand Up @@ -811,7 +785,7 @@ ${unsubscribeOptions}`,
where: { id },
data: { canceled: true },
include: {
chapter: { select: { calendar_id: true } },
chapter: { select: { id: true, name: true, calendar_id: true } },
event_users: {
include: { user: true },
where: {
Expand All @@ -822,21 +796,33 @@ ${unsubscribeOptions}`,
},
});
await deleteEventReminders(id);

const notCanceledRsvps = event.event_users;

if (notCanceledRsvps.length) {
const emailList = notCanceledRsvps.map(({ user }) => user.email);
const subject = `Event ${event.name} canceled`;
const body = `The event ${event.name} was canceled`;

new MailerService({
emailList: emailList,
subject: subject,
htmlEmail: body,
}).sendEmail();
for (const { user } of notCanceledRsvps) {
const unsubscribeOptions = getEventUnsubscribeOptions({
chapterId: event.chapter_id,
eventId: event.id,
userId: user.id,
});
const emailList = notCanceledRsvps.map(({ user }) => user.email);
const subject = `Event ${event.name} is canceled`;

const cancelEventEmail = `The upcoming event ${event.name} has been canceled.<br />
<br />
View upcoming events for ${event.chapter.name}: <a href='${process.env.CLIENT_LOCATION}/chapters/${event.chapter.id}'>${event.chapter.name} chapter</a>.<br />
You received this email because you Subscribed to ${event.name} Event.<br />
<br />
${unsubscribeOptions}
`;

new MailerService({
emailList: emailList,
subject: subject,
htmlEmail: cancelEventEmail,
}).sendEmail();
}
}

if (event.chapter.calendar_id && event.calendar_event_id) {
try {
// TODO: consider not awaiting. Ideally the user would see the app
Expand Down Expand Up @@ -1009,12 +995,12 @@ ${unsubscribeOptions}`,
await batchSender(function* () {
for (const { user } of users) {
const email = user.email;
const unsubScribeOptions = getUnsubscribeOptions({
const unsubscribeOptions = getEventUnsubscribeOptions({
chapterId: event.chapter_id,
eventId: event.id,
userId: user.id,
});
const text = `${subsequentEventEmail}<br>${unsubScribeOptions}`;
const text = `${subsequentEventEmail}<br>${unsubscribeOptions}`;
yield { email, subject, text, options: { iCalEvent } };
}
});
Expand Down
37 changes: 37 additions & 0 deletions server/src/util/eventEmail.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { generateToken, UnsubscribeType } from '../services/UnsubscribeToken';

export const getEventUnsubscribeOptions = ({
chapterId,
eventId,
userId,
}: {
chapterId: number;
eventId: number;
userId: number;
}) => {
const chapterUnsubscribeToken = generateToken(
UnsubscribeType.Chapter,
chapterId,
userId,
);
const eventUnsubscribeToken = generateToken(
UnsubscribeType.Event,
eventId,
userId,
);
const linkToEvent = `${process.env.CLIENT_LOCATION}/unsubscribe?token=${eventUnsubscribeToken}`;
const linkToChapter = `${process.env.CLIENT_LOCATION}/unsubscribe?token=${chapterUnsubscribeToken}`;
return `</br>
- To stop receiving notifications about this event, go to <a href="${linkToEvent}">${linkToEvent}</a>.<br />
- To stop receiving notifications about new events in this chapter, go to <a href="${linkToChapter}">${linkToChapter}</a>.<br />`;
};

export const getChapterUnsubscribeOptions = ({
chapterId,
userId,
}: {
chapterId: number;
userId: number;
}) => {
generateToken(UnsubscribeType.Chapter, chapterId, userId);
};