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 12 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
68 changes: 29 additions & 39 deletions server/src/controllers/Events/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,17 @@ import {
deleteEventReminders,
updateRemindAt,
} from '../../services/Reminders';
import {
generateToken,
UnsubscribeType,
} from '../../services/UnsubscribeToken';
import {
cancelCalendarEvent,
createCalendarEvent,
deleteCalendarEvent,
updateCalendarEvent,
} from '../../services/Google';
import {
getUnsubscribeOptions,
getChapterUnsubscribeToken,
NotificationContextText,
} from '../../util/eventEmail';
import { updateCalendarEventAttendees } from '../../util/updateCalendarEventAttendees';
import { updateEventWaitlist } from '../../util/updateEventWaitlist';
import { EventInputs } from './inputs';
Expand Down Expand Up @@ -77,31 +78,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 all notifications by unfollowing chapter</a>`;
};

const sendRsvpInvitation = async (
user: Required<ResolverCtx>['user'],
event: events & { venue: venues | null },
Expand Down Expand Up @@ -238,11 +214,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 = getChapterUnsubscribeToken({
chapterId: chapter_id,
userId: user.id,
});
const text = `${body}<br><a href="${process.env.CLIENT_LOCATION}/unsubscribe?token=${chapterUnsubscribeToken}Unsubscribe from chapter emails`;
Sboonny marked this conversation as resolved.
Show resolved Hide resolved
yield { email, subject, text };
}
Expand Down Expand Up @@ -783,7 +758,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 @@ -795,17 +770,32 @@ ${unsubscribeOptions}`,
});
await deleteEventReminders(id);

const chapterURL = `${process.env.CLIENT_LOCATION}/chapters/${event.chapter.id}`;
const eventURL = `${process.env.CLIENT_LOCATION}/events/${event.id}`;
Sboonny marked this conversation as resolved.
Show resolved Hide resolved
Sboonny marked this conversation as resolved.
Show resolved Hide resolved
const notCanceledRsvps = event.event_users;

const unsubScribeOptions = NotificationContextText({
linkToEvent: eventURL,
linkToChapter: chapterURL,
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should use getEventUnsubscribeOptions (and be unsubscribeOptions)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is complicated because this cancel RSVP without userId, so if we are using getEventUnsubscibeOptions, we will force user to log in

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we move the text to the resolver with comment of why we are doing this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't force users to login when they want to unsubscribe from email notifications. Fortunately, there's no need to. We have access to the userId via event_users. Since each email is different (different unsubscribe tokens) you can use batchSender to send them all. There's an example of that in sendEventInvite.

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

// ToDo: change "subscribed to event" to "join event" when join and leave function is on
Sboonny marked this conversation as resolved.
Show resolved Hide resolved

const cancelEventEmail = `The upcoming event ${event.name} has been canceled.<br />
<br />
View upcoming events for ${event.chapter.name}: <a href='${chapterURL}'>${event.chapter.name} chapter</a>.<br />
${unsubScribeOptions}
You received this email because you Subscribed to ${event.name} Event.<br />
<br />
See the options above to change your notifications.
Sboonny marked this conversation as resolved.
Show resolved Hide resolved
`;

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

Expand Down
49 changes: 49 additions & 0 deletions server/src/util/eventEmail.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { generateToken, UnsubscribeType } from '../services/UnsubscribeToken';

export const NotificationContextText = ({
linkToEvent,
linkToChapter,
}: {
linkToEvent: string;
linkToChapter: string;
}) => {
return `
Unsubscribe Options</br>
- To manage notifications about this event, go to <a href="${linkToEvent}">${linkToEvent}</a>.<br />
- To manage notifications from this chapter about new events, go to <a href="${linkToChapter}">${linkToChapter}</a>.<br />`;
};

Sboonny marked this conversation as resolved.
Show resolved Hide resolved
export const getUnsubscribeOptions = ({
Sboonny marked this conversation as resolved.
Show resolved Hide resolved
chapterId,
eventId,
userId,
}: {
chapterId: number;
eventId: number;
userId: number;
}) => {
const chapterUnsubscribeToken = generateToken(
UnsubscribeType.Chapter,
chapterId,
userId,
);
const eventUnsubscribeToken = generateToken(
UnsubscribeType.Event,
eventId,
userId,
);
return NotificationContextText({
linkToEvent: eventUnsubscribeToken,
linkToChapter: chapterUnsubscribeToken,
Sboonny marked this conversation as resolved.
Show resolved Hide resolved
});
};

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