Skip to content

Commit 5866dbb

Browse files
committed
feat: add delete-subscription writer
1 parent f97e238 commit 5866dbb

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

src/writer/delete-subscription.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import envelopDB from '../helpers/mysql';
2+
3+
export async function verify(message: { from: string }): Promise<any> {
4+
const result = await envelopDB.queryAsync(
5+
`SELECT * FROM subscribers WHERE address = ? AND verified > 0 LIMIT 1`,
6+
[message.from]
7+
);
8+
9+
if (!result[0]) {
10+
return Promise.reject('user not subscribed');
11+
}
12+
13+
return result[0];
14+
}
15+
16+
export async function action(message: { from: string }): Promise<void> {
17+
await envelopDB.queryAsync(`DELETE FROM subscribers WHERE address = ? AND verified > 0 LIMIT 1`, [
18+
message.from
19+
]);
20+
}

src/writer/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as alias from './alias';
22
import * as deleteProposal from './delete-proposal';
33
import * as deleteSpace from './delete-space';
4+
import * as deleteSubscription from './delete-subscription';
45
import * as flagProposal from './flag-proposal';
56
import * as follow from './follow';
67
import * as profile from './profile';
@@ -25,6 +26,7 @@ export default {
2526
unfollow,
2627
subscribe,
2728
unsubscribe,
29+
'delete-subscription': deleteSubscription,
2830
alias,
2931
profile,
3032
statement
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import db, { envelopDB, sequencerDB } from '../../../src/helpers/mysql';
2+
import { action, verify } from '../../../src/writer/delete-subscription';
3+
4+
describe('writer/delete-subscription', () => {
5+
const TEST_PREFIX = 'test-delete-subscription';
6+
7+
afterAll(async () => {
8+
await envelopDB.queryAsync('DELETE FROM subscribers');
9+
await envelopDB.endAsync();
10+
await db.endAsync();
11+
await sequencerDB.endAsync();
12+
});
13+
14+
describe('verify()', () => {
15+
beforeAll(async () => {
16+
await Promise.all([
17+
envelopDB.queryAsync(
18+
'INSERT INTO subscribers SET address = ?, email = ?, subscriptions = ?, created = ?, verified = ?',
19+
[`${TEST_PREFIX}-0x0`, '[email protected]', '[]', 0, 0]
20+
),
21+
envelopDB.queryAsync(
22+
'INSERT INTO subscribers SET address = ?, email = ?, subscriptions = ?, created = ?, verified = ?',
23+
[`${TEST_PREFIX}-0x1`, '[email protected]', '[]', 0, 1]
24+
)
25+
]);
26+
});
27+
28+
it('rejects when the address is not subscribed', () => {
29+
return expect(verify({ from: '0x0' })).rejects.toEqual(`user not subscribed`);
30+
});
31+
32+
it('rejects when the address is not verified', () => {
33+
return expect(verify({ from: `${TEST_PREFIX}-0x0` })).rejects.toEqual(`user not subscribed`);
34+
});
35+
36+
it('resolves when the address is verified', () => {
37+
return expect(verify({ from: `${TEST_PREFIX}-0x1` })).resolves.toHaveProperty('address');
38+
});
39+
});
40+
41+
describe('action()', () => {
42+
const address = `${TEST_PREFIX}-0x3`;
43+
44+
beforeAll(async () => {
45+
await Promise.all([
46+
envelopDB.queryAsync(
47+
'INSERT INTO subscribers SET address = ?, email = ?, subscriptions = ?, created = ?, verified = ?',
48+
[address, '[email protected]', '[]', 0, 0]
49+
),
50+
envelopDB.queryAsync(
51+
'INSERT INTO subscribers SET address = ?, email = ?, subscriptions = ?, created = ?, verified = ?',
52+
[address, '[email protected]', '[]', 0, 1]
53+
)
54+
]);
55+
});
56+
57+
it('deletes the subscription', async () => {
58+
await action({ from: address });
59+
60+
const results = await envelopDB.queryAsync('SELECT * FROM subscribers WHERE address = ?', [
61+
address
62+
]);
63+
64+
// Only delete the verified subscription
65+
expect(results.length).toBe(1);
66+
expect(results[0].email).toEqual('[email protected]');
67+
});
68+
});
69+
});

0 commit comments

Comments
 (0)