mirror of
https://github.com/omnivore-app/omnivore.git
synced 2026-03-11 08:54:26 +00:00
Fix the clear old received emails trigger to only clear current users emails
This commit is contained in:
parent
94b5047ef5
commit
8da817e8b1
3 changed files with 125 additions and 21 deletions
|
|
@ -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)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
|
|||
22
packages/db/migrations/0109.do.received_email_trigger_query.sql
Executable file
22
packages/db/migrations/0109.do.received_email_trigger_query.sql
Executable file
|
|
@ -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;
|
||||
22
packages/db/migrations/0109.undo.received_email_trigger_query.sql
Executable file
22
packages/db/migrations/0109.undo.received_email_trigger_query.sql
Executable file
|
|
@ -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;
|
||||
Loading…
Reference in a new issue