Skip to content

Commit fb49cc2

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

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

src/writer/delete-subscription.ts

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

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: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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({ address: '0x0' })).rejects.toEqual(`user not subscribed`);
30+
});
31+
32+
it('rejects when the address is not verified', () => {
33+
return expect(verify({ address: `${TEST_PREFIX}-0x0` })).rejects.toEqual(
34+
`user not subscribed`
35+
);
36+
});
37+
38+
it('resolves when the address is verified', () => {
39+
return expect(verify({ address: `${TEST_PREFIX}-0x1` })).resolves.toHaveProperty('address');
40+
});
41+
});
42+
43+
describe('action()', () => {
44+
const address = `${TEST_PREFIX}-0x3`;
45+
46+
beforeAll(async () => {
47+
await Promise.all([
48+
envelopDB.queryAsync(
49+
'INSERT INTO subscribers SET address = ?, email = ?, subscriptions = ?, created = ?, verified = ?',
50+
[address, '[email protected]', '[]', 0, 0]
51+
),
52+
envelopDB.queryAsync(
53+
'INSERT INTO subscribers SET address = ?, email = ?, subscriptions = ?, created = ?, verified = ?',
54+
[address, '[email protected]', '[]', 0, 1]
55+
)
56+
]);
57+
});
58+
59+
it('deletes the subscription', async () => {
60+
await action({ address: address });
61+
62+
const results = await envelopDB.queryAsync('SELECT * FROM subscribers WHERE address = ?', [
63+
address
64+
]);
65+
66+
// Only delete the verified subscription
67+
expect(results.length).toBe(1);
68+
expect(results[0].email).toEqual('[email protected]');
69+
});
70+
});
71+
});

0 commit comments

Comments
 (0)