generated from helsingborg-stad/gdi-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Petter Andersson
committed
Nov 11, 2022
1 parent
3305466
commit 2492f48
Showing
9 changed files
with
105 additions
and
103 deletions.
There are no files selected for viewing
4 changes: 2 additions & 2 deletions
4
src/__tests/sms-content-service.test.ts → src/__tests/content-service.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
import { createSmsContentServiceFromEnv } from './services/sms-content-service' | ||
import { createSmsListenerServiceFromEnv } from './services/sms-listener-service' | ||
import { createSmsSendServiceFromEnv } from './services/sms-send-service' | ||
import { getContentServiceFromEnv } from './services/content-service' | ||
import { getListenerServiceFromEnv } from './services/listener-service' | ||
import { getSendServiceFromEnv } from './services/send-service' | ||
import { Services } from './types' | ||
|
||
const createServicesFromEnv = (): Services => ({ | ||
smsListenerService: createSmsListenerServiceFromEnv(), | ||
smsContentService: createSmsContentServiceFromEnv(), | ||
smsSendService: createSmsSendServiceFromEnv(), | ||
listenerService: getListenerServiceFromEnv(), | ||
contentService: getContentServiceFromEnv(), | ||
sendService: getSendServiceFromEnv(), | ||
}) | ||
|
||
export { createServicesFromEnv } |
8 changes: 4 additions & 4 deletions
8
src/services/sms-content-service.ts → src/services/content-service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,17 @@ | ||
import { getEnv } from '@helsingborg-stad/gdi-api-node' | ||
import { MqMessageBody, SmsContentService } from '../types' | ||
import { MqMessageBody, ContentService } from '../types' | ||
|
||
const formatContent = (verificationCode: string, smsBasePath: string): string =>`Hej! | ||
Tack för att du angett ditt telefonnummer på helsingborg.se | ||
För att verifiera att det är ditt nummer, vänligen bekräfta genom | ||
att klicka på denna länk: ${smsBasePath}/${verificationCode}` | ||
|
||
const createSmsContentServiceFromEnv = (): SmsContentService => createSmsContentService( | ||
const getContentServiceFromEnv = (): ContentService => getContentService( | ||
getEnv('SMS_BASEPATH') | ||
) | ||
|
||
const createSmsContentService = (smsBasePath: string): SmsContentService => ({ | ||
const getContentService = (smsBasePath: string): ContentService => ({ | ||
build: async (message: MqMessageBody): Promise<string> => formatContent(message.verificationCode, smsBasePath), | ||
}) | ||
|
||
export { createSmsContentService, createSmsContentServiceFromEnv, formatContent } | ||
export { getContentService, getContentServiceFromEnv, formatContent } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
|
||
import { ListenerService, MqMessageEnvelope, MqEngine } from '../types' | ||
import { getEnv } from '@helsingborg-stad/gdi-api-node' | ||
import { createAmqpEngine } from '../helpers/amqp-engine' | ||
|
||
type ListenerServiceParams = { | ||
uri: string; | ||
exchange: string; | ||
queue: string; | ||
filter: string; | ||
} | ||
|
||
const getListenerServiceFromEnv = (): ListenerService => getListenerService({ | ||
uri: getEnv('AMQP_URI'), | ||
exchange: getEnv('AMQP_EXCHANGE'), | ||
queue: getEnv('AMQP_QUEUE'), | ||
filter: getEnv('AMQP_FILTER'), | ||
}, createAmqpEngine()) | ||
|
||
const getListenerService = ({ uri, exchange, queue, filter }: ListenerServiceParams, engine: MqEngine, debug: (data: string) => void = console.debug, infinite = true): ListenerService => ({ | ||
listen: async (handler) => { | ||
debug(`Connecting to ${uri}...`) | ||
await engine.connect(uri) | ||
// Assign graceful close on Ctrl+C) | ||
process.once('SIGINT', async () => { | ||
await engine.close() | ||
}) | ||
|
||
debug('Creating channel...') | ||
await engine.createChannel() | ||
|
||
debug(`Asserting durable topic exchange (${exchange}) ...`) | ||
await engine.assertExchange(exchange) | ||
|
||
debug(`Asserting durable queue (${queue})...`) | ||
await engine.assertQueue(queue) | ||
|
||
debug(`Binding queue with filter (${filter})...`) | ||
await engine.bindQueue(queue, exchange, filter) | ||
|
||
debug('waiting for messages. Ctrl-C to exit...') | ||
|
||
// Message loop (Breaks on CTRL+C) | ||
let didFail = false | ||
do { | ||
await engine.consume(queue, async (message: MqMessageEnvelope) => { | ||
debug(message.content.toString()) | ||
|
||
try { | ||
await handler(JSON.parse(message.content.toString())) | ||
} | ||
catch(error) { | ||
debug(`Failed to send message (${error})`) | ||
} | ||
finally { | ||
await engine.ack(message).catch(() => debug('Failed to ack message')) | ||
} | ||
}).catch(() => didFail = true) | ||
} while (infinite && !didFail) | ||
}, | ||
}) | ||
|
||
export { getListenerServiceFromEnv, getListenerService } | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters