diff --git a/packages/api/test/resolvers/recent_emails.test.ts b/packages/api/test/resolvers/recent_emails.test.ts index 3bae6f7b1..9ba02c061 100644 --- a/packages/api/test/resolvers/recent_emails.test.ts +++ b/packages/api/test/resolvers/recent_emails.test.ts @@ -8,6 +8,26 @@ import { ReceivedEmail } from '../../src/entity/received_email' import { NewsletterEmail } from '../../src/entity/newsletter_email' describe('Recent Emails Resolver', () => { + const recentEmailsQuery = ` + query { + recentEmails { + ... on RecentEmailsSuccess { + recentEmails { + id + from + to + subject + text + html + } + } + ... on RecentEmailsError { + errorCodes + } + } + } +` + let recentEmails: ReceivedEmail[] const username = 'fakeUser' let user: User @@ -41,27 +61,6 @@ describe('Recent Emails Resolver', () => { }) describe('recentEmails', () => { - const recentEmailsQuery = ` - query { - recentEmails { - ... on RecentEmailsSuccess { - recentEmails { - id - from - to - subject - text - html - } - } - ... on RecentEmailsError { - errorCodes - } - } - } - ` - let recentEmails: ReceivedEmail[] - before(async () => { // create fake emails const recentEmail = await getRepository(ReceivedEmail).save({ @@ -143,4 +142,65 @@ describe('Recent Emails Resolver', () => { expect(updatedRecentEmail?.type).to.eql('article') }) }) + + describe('old recentEmails are cleared', () => { + let user2: User + before(async () => { + user2 = await createTestUser('fake_02') + }) + after(async () => { + await deleteTestUser(user2.id) + }) + + before(async () => { + // create fake emails + const recentEmail = await getRepository(ReceivedEmail).save({ + user: { id: user.id }, + from: 'fake from', + subject: 'fake subject', + text: 'fake text', + html: 'fake html', + to: newsletterEmail.address, + type: 'article', + }) + const recentEmail2 = await getRepository(ReceivedEmail).save({ + user: { id: user.id }, + from: 'fake from 2', + subject: 'fake subject 2', + text: 'fake text 2', + html: 'fake html 2', + to: newsletterEmail2.address, + type: 'non-article', + }) + recentEmails = [recentEmail, recentEmail2] + }) + + it('when a second user receives an email the firsts are not deleted', async () => { + const res = await graphqlRequest(recentEmailsQuery, authToken).expect(200) + const { recentEmails: results } = res.body.data.recentEmails + + expect(results).to.have.lengthOf(2) + expect(results[0].id).to.eql(recentEmails[1].id) + expect(results[1].id).to.eql(recentEmails[0].id) + + await getRepository(ReceivedEmail).save({ + user: { id: user2.id }, + from: 'fake from', + subject: 'fake subject', + text: 'fake text', + html: 'fake html', + to: newsletterEmail.address, + type: 'article', + }) + + const res2 = await graphqlRequest(recentEmailsQuery, authToken).expect( + 200 + ) + const { recentEmails: results2 } = res2.body.data.recentEmails + + expect(results2).to.have.lengthOf(2) + expect(results2[0].id).to.eql(recentEmails[1].id) + expect(results2[1].id).to.eql(recentEmails[0].id) + }) + }) }) diff --git a/packages/db/migrations/0109.do.received_email_trigger_query.sql b/packages/db/migrations/0109.do.received_email_trigger_query.sql new file mode 100755 index 000000000..0226c73dd --- /dev/null +++ b/packages/db/migrations/0109.do.received_email_trigger_query.sql @@ -0,0 +1,22 @@ +-- Type: DO +-- Name: received_email_trigger_query +-- Description: Fix the received_email trigger query to only clear the current users old emails + +BEGIN; + +-- Create a trigger to keep the most recent 20 emails for each user +CREATE OR REPLACE FUNCTION omnivore.delete_old_received_emails() + RETURNS trigger AS $$ + BEGIN + DELETE FROM omnivore.received_emails + WHERE user_id = NEW.user_id AND id NOT IN ( + SELECT id FROM omnivore.received_emails + WHERE user_id = NEW.user_id + ORDER BY created_at DESC + LIMIT 20 + ); + RETURN NEW; + END; +$$ LANGUAGE plpgsql; + +COMMIT; diff --git a/packages/db/migrations/0109.undo.received_email_trigger_query.sql b/packages/db/migrations/0109.undo.received_email_trigger_query.sql new file mode 100755 index 000000000..9c7433502 --- /dev/null +++ b/packages/db/migrations/0109.undo.received_email_trigger_query.sql @@ -0,0 +1,22 @@ +-- Type: UNDO +-- Name: received_email_trigger_query +-- Description: Fix the received_email trigger query to only clear the current users old emails + +BEGIN; + +-- Create a trigger to keep the most recent 20 emails for each user +CREATE OR REPLACE FUNCTION omnivore.delete_old_received_emails() + RETURNS trigger AS $$ + BEGIN + DELETE FROM omnivore.received_emails + WHERE id NOT IN ( + SELECT id FROM omnivore.received_emails + WHERE user_id = NEW.user_id + ORDER BY created_at DESC + LIMIT 20 + ); + RETURN NEW; + END; +$$ LANGUAGE plpgsql; + +COMMIT;