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 11 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
72 changes: 32 additions & 40 deletions server/src/controllers/Events/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ import {
deleteEventReminders,
updateRemindAt,
} from '../../services/Reminders';
import {
generateToken,
UnsubscribeType,
} from '../../services/UnsubscribeToken';
import {
cancelCalendarEvent,
createCalendarEvent,
deleteCalendarEvent,
updateCalendarEvent,
} from '../../services/Google';
import {
getUnsubscribeOptions,
getChapterUnsubscribeToken,
} from '../../util/eventEmail';
import { updateCalendarEventAttendees } from '../../util/updateCalendarEventAttendees';
import { updateEventWaitlist } from '../../util/updateEventWaitlist';
import { EventInputs } from './inputs';
Expand Down Expand Up @@ -77,31 +77,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 +213,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 @@ -778,12 +752,15 @@ ${unsubscribeOptions}`,

@Authorized(Permission.EventEdit)
@Mutation(() => Event)
async cancelEvent(@Arg('id', () => Int) id: number): Promise<Event | null> {
async cancelEvent(
@Arg('id', () => Int) id: number,
@Ctx() ctx: Required<ResolverCtx>,
): Promise<Event | null> {
const event = await prisma.events.update({
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 +772,32 @@ ${unsubscribeOptions}`,
});
await deleteEventReminders(id);

const chapterURL = `${process.env.CLIENT_LOCATION}/chapters/${event.chapter.id}`;
const notCanceledRsvps = event.event_users;

const unsubScribeOptions = getUnsubscribeOptions({
chapterId: event.chapter_id,
eventId: event.id,
userId: ctx.user.id,
Sboonny marked this conversation as resolved.
Show resolved Hide resolved
});
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
36 changes: 36 additions & 0 deletions server/src/util/eventEmail.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { generateToken, UnsubscribeType } from '../services/UnsubscribeToken';

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 `
Unsubscribe Options</br>
- To manage notifications about this event, go to <a href="${eventUnsubscribeToken}">${eventUnsubscribeToken}</a>.<br />
- To manage notifications from this chapter about new events, go to <a href="${chapterUnsubscribeToken}">${chapterUnsubscribeToken}</a>.<br />`;
};

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