Skip to content

Commit

Permalink
Add integration with backend-notify for sending simple emails. Closes #5
Browse files Browse the repository at this point in the history
  • Loading branch information
AlekNS committed Jan 25, 2018
1 parent 3187e9d commit d653372
Show file tree
Hide file tree
Showing 7 changed files with 263 additions and 4 deletions.
16 changes: 16 additions & 0 deletions docker-compose.test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ services:
- redis
- auth
- verify
- nsqd

auth:
image: jincort/backend-auth:production
Expand Down Expand Up @@ -76,3 +77,18 @@ services:
backendTokenWalletsTests:
tmpfs:
- /data/db

nsqlookupd:
image: nsqio/nsq:v1.0.0-compat
command: /nsqlookupd
networks:
backendTokenWalletsTests:

nsqd:
image: nsqio/nsq:v1.0.0-compat
networks:
backendTokenWalletsTests:
volumes:
- /data
command: >
/nsqd -max-deflate-level=6 -defalte -max-msg-size=5048576 --data-path=/data --lookupd-tcp-address=nsqlookupd:4160
159 changes: 157 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"morgan": "1.9.0",
"node-mailjet": "3.2.1",
"node-uuid": "^1.4.8",
"nsqjs": "0.10.0",
"qr-image": "3.2.0",
"redis": "2.8.0",
"reflect-metadata": "0.1.12",
Expand All @@ -69,6 +70,7 @@
"@types/jsonwebtoken": "7.2.5",
"@types/mocha": "2.2.46",
"@types/node-uuid": "0.0.28",
"@types/nsqjs": "0.9.0",
"@types/redis": "2.8.4",
"@types/winston": "2.3.7",
"chai": "4.1.2",
Expand Down
9 changes: 9 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const {
HTTP_IP,
HTTP_PORT,

ENVIRONMENT,
APP_API_PREFIX_URL,
APP_FRONTEND_PREFIX_URL,

Expand All @@ -18,6 +19,9 @@ const {
THROTTLER_MAX,
THROTTLER_MIN_DIFF,

NSQ_HOST,
NSQ_PORT,

REDIS_URL,

MONGO_URL,
Expand Down Expand Up @@ -56,9 +60,14 @@ export default {
colorize: LOGGING_COLORIZE === 'true'
},
app: {
env: ENVIRONMENT || 'local',
frontendPrefixUrl: APP_FRONTEND_PREFIX_URL || 'http://token-wallets',
backendPrefixUrl: APP_API_PREFIX_URL || 'http://api.token-wallets'
},
nsqd: {
host: NSQ_HOST || 'nsqd',
port: parseInt(NSQ_HOST, 10) || 4150
},
server: {
httpPort: parseInt(HTTP_PORT, 10) || 3000,
httpIp: HTTP_IP || '0.0.0.0'
Expand Down
8 changes: 6 additions & 2 deletions src/ioc.container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
UserRepositoryInterface,
UserRepositoryType
} from './services/repositories/user.repository';
import { DummyMailService, EmailServiceInterface, EmailServiceType } from './services/external/email.service';
import { DummyMailService, EmailServiceInterface, EmailServiceType, NsqChannelMailService } from './services/external/email.service';

import { UserController } from './controllers/user.controller';
import { DashboardController } from './controllers/dashboard.controller';
Expand All @@ -47,7 +47,11 @@ export function buildServicesContainerModule(): ContainerModule {
) => {
bind<AuthClientInterface>(AuthClientType).to(AuthClient);
bind<VerificationClientInterface>(VerificationClientType).to(VerificationClient);
bind<EmailServiceInterface>(EmailServiceType).to(DummyMailService).inSingletonScope();
if (config.app.env === 'test') {
bind<EmailServiceInterface>(EmailServiceType).to(DummyMailService).inSingletonScope();
} else {
bind<EmailServiceInterface>(EmailServiceType).to(NsqChannelMailService).inSingletonScope();
}
bind<EmailQueueInterface>(EmailQueueType).to(EmailQueue).inSingletonScope();
bind<Web3ClientInterface>(Web3ClientType).to(Web3Client).inSingletonScope();
bind<Web3EventInterface>(Web3EventType).to(Web3Event).inSingletonScope();
Expand Down
23 changes: 23 additions & 0 deletions src/services/external/email.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { injectable } from 'inversify';
import config from '../../config';
import { Logger } from '../../logger';
import { NsqQueueWriter } from '../queues/nsq.queue';

export interface EmailServiceInterface {
send(sender: string, recipient: string, subject: string, text: string): Promise<any>;
Expand All @@ -20,4 +21,26 @@ export class DummyMailService implements EmailServiceInterface {
}
}

const NSQ_TOPIC_NOTIFICATION_EMAIL = 'notifications.email.default';

@injectable()
export class NsqChannelMailService implements EmailServiceInterface {
private logger: Logger = Logger.getInstance('NSQ_CHANNELMAIL_SERVICE');
private nsq: NsqQueueWriter = new NsqQueueWriter();

/**
* @inheritdoc
*/
public send(sender: string, recipient: string, subject: string, text: string): Promise<any> {
this.logger.debug('Send email', sender, recipient, subject);

return this.nsq.publish(NSQ_TOPIC_NOTIFICATION_EMAIL, {
sender,
recipient,
subject,
body: text
});
}
}

export const EmailServiceType = Symbol('EmailServiceInterface');
Loading

0 comments on commit d653372

Please sign in to comment.