Skip to content

Commit bac0af5

Browse files
committed
feat: add subscription writer
1 parent 40b50f0 commit bac0af5

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

src/writer/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import * as proposal from './proposal';
99
import * as settings from './settings';
1010
import * as statement from './statement';
1111
import * as subscribe from './subscribe';
12+
import * as subscription from './subscription';
1213
import * as unfollow from './unfollow';
1314
import * as unsubscribe from './unsubscribe';
1415
import * as updateProposal from './update-proposal';
@@ -27,6 +28,7 @@ export default {
2728
unfollow,
2829
subscribe,
2930
unsubscribe,
31+
subscription,
3032
'update-subscription': updateSubscription,
3133
'delete-subscription': deleteSubscription,
3234
alias,

src/writer/subscription.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { envelopDB } from '../helpers/mysql';
2+
import { jsonParse } from '../helpers/utils';
3+
4+
type Message = { msg: string; address: string };
5+
6+
const VALID_TYPES = ['email'];
7+
8+
function extractPayload(message: Message) {
9+
return jsonParse(message.msg).payload;
10+
}
11+
12+
export async function verify(message: Message): Promise<any> {
13+
const payload = extractPayload(message);
14+
15+
if (!VALID_TYPES.includes(payload.type)) {
16+
return Promise.reject('invalid type');
17+
}
18+
19+
const result = await envelopDB.queryAsync(
20+
`SELECT * FROM subscribers WHERE address = ? AND email = ? LIMIT 1`,
21+
[message.address, payload.value]
22+
);
23+
24+
if (result[0]) {
25+
return Promise.reject('user already subscribed');
26+
}
27+
28+
return true;
29+
}
30+
31+
export async function action(message: Message): Promise<void> {
32+
const payload = extractPayload(message);
33+
34+
await envelopDB.queryAsync(`INSERT INTO subscribers (email, address, created) VALUES(?, ?, ?)`, [
35+
payload.value,
36+
message.address,
37+
(Date.now() / 1e3).toFixed()
38+
]);
39+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import db, { envelopDB, sequencerDB } from '../../../src/helpers/mysql';
2+
import { action, verify } from '../../../src/writer/subscription';
3+
4+
describe('writer/subscription', () => {
5+
const TEST_PREFIX = 'test-subscription';
6+
const msg = JSON.stringify({ payload: { value: '[email protected]', type: 'email' } });
7+
8+
afterAll(async () => {
9+
await envelopDB.queryAsync('DELETE FROM subscribers');
10+
await envelopDB.endAsync();
11+
await db.endAsync();
12+
await sequencerDB.endAsync();
13+
});
14+
15+
describe('verify()', () => {
16+
const address = `${TEST_PREFIX}-0x0`;
17+
const invalidMsg = JSON.stringify({
18+
payload: { type: 'hello', value: '[email protected]' }
19+
});
20+
21+
beforeAll(async () => {
22+
await envelopDB.queryAsync(
23+
'INSERT INTO subscribers SET address = ?, email = ?, subscriptions = ?, created = ?, verified = ?',
24+
[address, '[email protected]', '[]', 0, 0]
25+
);
26+
});
27+
28+
it('rejects when the address is already subscribed', () => {
29+
return expect(verify({ address: address, msg })).rejects.toEqual(`user already subscribed`);
30+
});
31+
32+
it('rejects when the subscription type is not valid', () => {
33+
return expect(verify({ address: address, msg: invalidMsg })).rejects.toEqual('invalid type');
34+
});
35+
36+
it('resolves when all args are valid', () => {
37+
return expect(verify({ address: `${TEST_PREFIX}-0x1`, msg })).resolves.toBe(true);
38+
});
39+
});
40+
41+
describe('action()', () => {
42+
const address = `${TEST_PREFIX}-0x1`;
43+
44+
it('creates a subscription', async () => {
45+
await action({
46+
address: address,
47+
msg
48+
});
49+
50+
const result = await envelopDB.queryAsync(
51+
`SELECT * FROM subscribers WHERE address = ? LIMIT 1`,
52+
[address]
53+
);
54+
55+
expect(result[0].email).toEqual('[email protected]');
56+
expect(result[0].verified).toEqual(0);
57+
});
58+
});
59+
});

0 commit comments

Comments
 (0)