Verified Commit 249cd2b2 authored by Mari's avatar Mari Committed by aguestuser

[215] notify admins of recycle enqueueing

parent 4dfdfc6a
......@@ -9,4 +9,7 @@ const enqueue = channelPhoneNumber =>
const dequeue = channelPhoneNumber =>
app.db.recycleablePhoneNumber.destroy({ where: { channelPhoneNumber } })
module.exports = { enqueue, dequeue }
const findByPhoneNumber = channelPhoneNumber =>
app.db.recycleablePhoneNumber.findOne({ where: { channelPhoneNumber } })
module.exports = { enqueue, dequeue, findByPhoneNumber }
......@@ -401,6 +401,9 @@ const notifications = {
channelDestructionFailed: phoneNumber =>
`Failed to destroy channel for phone number: ${phoneNumber}`,
channelEnqueuedForRecycling:
'Hello! This channel is about to be deactivated due to lack of use. To continue using the channel, use any command within the next 24 hours. For more info, visit signalboost.info/how-to.',
channelRecycled:
'Channel deactivated due to lack of use. To create a new channel, visit https://signalboost.info',
......
const channelRepository = require('../../db/repositories/channel')
const phoneNumberRepository = require('../../db/repositories/phoneNumber')
const recycleablePhoneNumberRepository = require('../../db/repositories/recycleablePhoneNumber')
const eventRepository = require('../../db/repositories/event')
const common = require('./common')
const { isEmpty } = require('lodash')
const { statuses } = require('../../util')
const { defaultLanguage } = require('../../config')
const signal = require('../../signal')
const { eventTypes } = require('../../db/models/event')
const { sdMessageOf } = require('../../signal/constants')
const { messagesIn } = require('../../dispatcher/strings/messages')
const phoneNumberRepository = require('../../db/repositories/phoneNumber')
const { findByPhoneNumber, enqueue } = require('../../db/repositories/recycleablePhoneNumber')
const channelRepository = require('../../db/repositories/channel')
// ({ string }) -> SignalboostStatus
const enqueueRecycleablePhoneNumber = async ({ phoneNumbers }) => {
return await Promise.all(
phoneNumbers.split(',').map(async phoneNumber => {
try {
const channel = await channelRepository.find(phoneNumber)
return recycleablePhoneNumberRepository.enqueue(channel.phoneNumber)
const channel = await channelRepository.findDeep(phoneNumber)
if (isEmpty(channel))
return {
status: statuses.ERROR,
message: `${phoneNumber} must be associated with a channel in order to be recycled.`,
}
const recycleablePhoneNumber = await findByPhoneNumber(phoneNumber)
if (!isEmpty(recycleablePhoneNumber))
return {
status: statuses.ERROR,
message: `${phoneNumber} has already been enqueued for recycling.`,
}
// notifyAdmins(channel)
await enqueue(channel.phoneNumber)
return {
status: statuses.SUCCESS,
message: `Successfully enqueued ${phoneNumber} for recycling.`,
}
} catch (e) {
return { status: 'ERROR', message: `Channel not found for ${phoneNumber}` }
return {
status: statuses.ERROR,
message: `There was an error trying to enqueue ${phoneNumber} for recycling.`,
}
}
}),
)
......@@ -31,27 +53,36 @@ const recycle = async channelPhoneNumber => {
.then(() => common.destroyChannel(channel))
.then(() => eventRepository.log(eventTypes.CHANNEL_DESTROYED, channelPhoneNumber))
.then(() => recordStatusChange(channelPhoneNumber, common.statuses.VERIFIED))
.then(phoneNumberStatus => ({ status: 'SUCCESS', data: phoneNumberStatus }))
.then(phoneNumberStatus => ({ status: statuses.SUCCESS, data: phoneNumberStatus }))
.catch(err => handleRecycleFailure(err, channelPhoneNumber))
} else {
return { status: 'ERROR', message: `Channel not found for ${channelPhoneNumber}` }
return { status: statuses.ERROR, message: `Channel not found for ${channelPhoneNumber}` }
}
}
/********************
* HELPER FUNCTIONS
********************/
// TODO @mari - convert these functions to use sendMessage in language per membership
// (Channel) -> Channel
const notifyMembers = async channel => {
const memberPhoneNumbers = channelRepository.getMemberPhoneNumbers(channel)
await signal.broadcastMessage(
memberPhoneNumbers,
sdMessageOf(channel, channelRecycledNotification),
channelRepository.getMemberPhoneNumbers(channel),
signal.sdMessageOf(channel, messagesIn(channel.language).notifications.channelRecycled),
)
}
const notifyAdmins = async channel => {
await signal.broadcastMessage(
channelRepository.getAdminPhoneNumbers(channel),
signal.sdMessageOf(
channel,
messagesIn(channel.language).notifications.channelEnqueuedForRecycling,
),
)
}
// String
const channelRecycledNotification = messagesIn(defaultLanguage).notifications.channelRecycled
// (Database, string, PhoneNumberStatus) -> PhoneNumberStatus
const recordStatusChange = async (phoneNumber, status) =>
......
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