Verified Commit f2a96239 authored by aguestuser's avatar aguestuser

[215] extract notifier module

parent 46ad6fe2
const channelRepository = require('./db/repositories/channel')
const signal = require('./signal')
const { getAdminMemberships } = require('./db/repositories/channel')
const { getAdminPhoneNumbers } = require('./db/repositories/channel')
const { messagesIn } = require('./dispatcher/strings/messages')
const { sdMessageOf } = require('./signal/constants')
const {
signal: { supportPhoneNumber },
} = require('./config')
const notificationKeys = {
CHANNEL_DESTROYED: 'channelDestroyed',
CHANNEL_RECYCLED: 'channelRecycled',
CHANNEL_REDEEMED: 'channelRedeemed',
}
// (Database, Socket, Channel, String, String) -> Promise<Array<string>>
const notifyMembersExcept = async (channel, message, sender) => {
if (channel == null) return
const memberPhoneNumbers = channelRepository.getMemberPhoneNumbersExcept(channel, [sender])
await signal.broadcastMessage(memberPhoneNumbers, sdMessageOf(channel, message))
}
// (string) -> Promise<Array<string>>
const notifyMaintainers = async message => {
if (!supportPhoneNumber) return Promise.resolve([])
const supportChannel = await channelRepository.findDeep(supportPhoneNumber)
const maintainerPhoneNumbers = getAdminPhoneNumbers(supportChannel)
await signal.broadcastMessage(maintainerPhoneNumbers, sdMessageOf(supportChannel, message))
}
// (string, string) -> Promise<Array<string>>
const notifyAdmins = async (channel, notificationKey) =>
_notifyMany(channel, notificationKey, getAdminMemberships(channel))
// (Channel, string) -> Promise<Array<string>>
const notifyMembers = async (channel, notificationKey) =>
_notifyMany(channel, notificationKey, channel.memberships)
// (Channel, string, Array<Member>) => Promise<Array<string>>
const _notifyMany = (channel, notificationKey, recipients) =>
Promise.all(
recipients.map(recipient =>
signal.sendMessage(
recipient.memberPhoneNumber,
sdMessageOf(channel, messagesIn(recipient.language).notifications[notificationKey]),
),
),
)
module.exports = {
notifyAdmins,
notifyMembers,
notifyMaintainers,
notifyMembersExcept,
notificationKeys,
}
import { expect } from 'chai'
import { describe, it, beforeEach, afterEach } from 'mocha'
import sinon from 'sinon'
import channelRepository from '../../../../app/db/repositories/channel'
import signal from '../../../../app/signal'
import { deepChannelFactory } from '../../../support/factories/channel'
import { times } from 'lodash'
import channelRepository from '../../app/db/repositories/channel'
import signal from '../../app/signal'
import { deepChannelFactory } from '../support/factories/channel'
import {
adminMembershipFactory,
subscriberMembershipFactory,
} from '../../../support/factories/membership'
import common, { notificationKeys } from '../../../../app/registrar/phoneNumber/common'
import { sdMessageOf } from '../../../../app/signal/constants'
import { messagesIn } from '../../../../app/dispatcher/strings/messages'
import { defaultLanguage } from '../../../../app/config'
} from '../support/factories/membership'
import notifier, { notificationKeys } from '../../app/notifier'
import { sdMessageOf } from '../../app/signal/constants'
import { messagesIn } from '../../app/dispatcher/strings/messages'
describe('phone number registrar -- common module', () => {
describe('notifier module', () => {
const channel = deepChannelFactory({
memberships: [
adminMembershipFactory({ language: 'DE' }),
......@@ -23,10 +21,10 @@ describe('phone number registrar -- common module', () => {
subscriberMembershipFactory({ language: 'DE' }),
],
})
let findChannelStub, broadcastMessageStub, sendMessageStub
let broadcastMessageStub, sendMessageStub
beforeEach(() => {
findChannelStub = sinon.stub(channelRepository, 'findDeep').returns(Promise.resolve(channel))
sinon.stub(channelRepository, 'findDeep').returns(Promise.resolve(channel))
broadcastMessageStub = sinon
.stub(signal, 'broadcastMessage')
.callsFake(numbers => numbers.map(() => '42'))
......@@ -36,7 +34,7 @@ describe('phone number registrar -- common module', () => {
describe('#notifyAdmins', () => {
it('sends a notification to each admin in their language', async () => {
await common.notifyAdmins(channel, notificationKeys.CHANNEL_RECYCLED)
await notifier.notifyAdmins(channel, notificationKeys.CHANNEL_RECYCLED)
expect(sendMessageStub.callCount).to.eql(2)
expect(sendMessageStub.getCalls().map(x => x.args)).to.have.deep.members([
[
......@@ -53,7 +51,7 @@ describe('phone number registrar -- common module', () => {
describe('#notifyMembers', () => {
it('sends a notification to each member in their language', async () => {
await common.notifyMembers(channel, notificationKeys.CHANNEL_DESTROYED)
await notifier.notifyMembers(channel, notificationKeys.CHANNEL_DESTROYED)
expect(sendMessageStub.callCount).to.eql(4)
expect(sendMessageStub.getCalls().map(x => x.args)).to.have.deep.members([
[
......@@ -78,7 +76,7 @@ describe('phone number registrar -- common module', () => {
describe('#notifyMaintainers', () => {
it('sends an untranslated notification to sysadmins of the instance', async () => {
await common.notifyMaintainers('foo')
await notifier.notifyMaintainers('foo')
expect(broadcastMessageStub.callCount).to.eql(1)
expect(broadcastMessageStub.getCall(0).args).to.eql([
[channel.memberships[0].memberPhoneNumber, channel.memberships[1].memberPhoneNumber],
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment