Skip to content

Commit

Permalink
fix(api): unsubscribe all subscribed emails (#54953)
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaunSHamilton committed Jun 11, 2024
1 parent 5baca70 commit 63a3b8a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
40 changes: 28 additions & 12 deletions api/src/routes/email-subscription.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,27 @@ const urlEncodedSuccessMessage2 =

const unsubscribeId1 = 'abcde';
const unsubscribeId2 = 'abcdef';
const unsubscribeId3 = 'abcdefg';

const testUserData1: Prisma.userCreateInput[] = [
{
...createUserInput('user1@freecodecamp.org'),
unsubscribeId: unsubscribeId1,
sendQuincyEmail: true
},
{
...createUserInput('user1@freecodecamp.org'),
unsubscribeId: unsubscribeId2,
sendQuincyEmail: true
},
{
...createUserInput('user2@freecodecamp.org'),
unsubscribeId: unsubscribeId2,
sendQuincyEmail: true
},
{
...createUserInput('user3@freecodecamp.org'),
unsubscribeId: unsubscribeId2,
unsubscribeId: unsubscribeId3,
sendQuincyEmail: true
}
];
Expand All @@ -57,7 +63,7 @@ const testUserData2: Prisma.userCreateInput[] = [
describe('Email Subscription endpoints', () => {
setupServer();

describe('GET /ue/unsubscribe/:unsubscribeId', () => {
describe('GET /ue/:unsubscribeId', () => {
test('should 302 redirect with info message if no ID', async () => {
const response = await superRequest('/ue/', { method: 'GET' });
expect(response.headers.location).toStrictEqual(
Expand All @@ -74,7 +80,7 @@ describe('Email Subscription endpoints', () => {
expect(response.status).toBe(302);
});

test("should set 'sendQuincyEmail' to 'false' for user with matching ID and 302 redirect with success message", async () => {
test("1: should set 'sendQuincyEmail' to 'false' for users with matching email and 302 redirect with success message", async () => {
await fastifyTestInstance.prisma.user.createMany({
data: testUserData1
});
Expand All @@ -87,14 +93,15 @@ describe('Email Subscription endpoints', () => {
where: {
OR: [
{ unsubscribeId: unsubscribeId1 },
{ unsubscribeId: unsubscribeId2 }
{ unsubscribeId: unsubscribeId2 },
{ unsubscribeId: unsubscribeId3 }
]
}
});

expect(users).toHaveLength(3);
expect(users).toHaveLength(4);
users.forEach(user => {
if (user.unsubscribeId === unsubscribeId1) {
if (['user1@freecodecamp.org'].includes(user.email)) {
expect(user.sendQuincyEmail).toBe(false);
} else {
expect(user.sendQuincyEmail).toBe(true);
Expand All @@ -106,17 +113,19 @@ describe('Email Subscription endpoints', () => {
);

expect(response.status).toBe(302);
// TODO: If any assertions fail before this call, other tests will fail for no actual reason.
await fastifyTestInstance.prisma.user.deleteMany({
where: {
OR: [
{ unsubscribeId: unsubscribeId1 },
{ unsubscribeId: unsubscribeId2 }
{ unsubscribeId: unsubscribeId2 },
{ unsubscribeId: unsubscribeId3 }
]
}
});
});

test("should set 'sendQuincyEmail' to 'false' for all users with matching ID and 302 redirect with success message", async () => {
test("2: should set 'sendQuincyEmail' to 'false' for all users with matching email and 302 redirect with success message", async () => {
await fastifyTestInstance.prisma.user.createMany({
data: testUserData1
});
Expand All @@ -129,14 +138,19 @@ describe('Email Subscription endpoints', () => {
where: {
OR: [
{ unsubscribeId: unsubscribeId1 },
{ unsubscribeId: unsubscribeId2 }
{ unsubscribeId: unsubscribeId2 },
{ unsubscribeId: unsubscribeId3 }
]
}
});

expect(users).toHaveLength(3);
expect(users).toHaveLength(4);
users.forEach(user => {
if (user.unsubscribeId === unsubscribeId2) {
if (
['user1@freecodecamp.org', 'user2@freecodecamp.org'].includes(
user.email
)
) {
expect(user.sendQuincyEmail).toBe(false);
} else {
expect(user.sendQuincyEmail).toBe(true);
Expand All @@ -148,11 +162,13 @@ describe('Email Subscription endpoints', () => {
);

expect(response.status).toBe(302);
// TODO: If any assertions fail before this call, other tests will fail for no actual reason.
await fastifyTestInstance.prisma.user.deleteMany({
where: {
OR: [
{ unsubscribeId: unsubscribeId1 },
{ unsubscribeId: unsubscribeId2 }
{ unsubscribeId: unsubscribeId2 },
{ unsubscribeId: unsubscribeId3 }
]
}
});
Expand Down
10 changes: 5 additions & 5 deletions api/src/routes/email-subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,21 @@ export const emailSubscribtionRoutes: FastifyPluginCallbackTypebox = (
try {
const { origin } = getRedirectParams(req);
const { unsubscribeId } = req.params;
const users = await fastify.prisma.user.findMany({
const unsubUsers = await fastify.prisma.user.findMany({
where: { unsubscribeId }
});

if (!users.length) {
if (!unsubUsers.length) {
void reply.code(302);
return reply.redirectWithMessage(origin, {
type: 'info',
content: 'We could not find an account to unsubscribe.'
});
}

const userUpdatePromises = users.map(user =>
fastify.prisma.user.update({
where: { id: user.id },
const userUpdatePromises = unsubUsers.map(user =>
fastify.prisma.user.updateMany({
where: { email: user.email },
data: {
sendQuincyEmail: false
}
Expand Down

0 comments on commit 63a3b8a

Please sign in to comment.