Verified Commit f89742c8 authored by aguestuser's avatar aguestuser

[215] add `channelRepository.findManyDeep`

parent f2a96239
import { Op } from 'sequelize'
const app = require('../../../app')
const { loggerOf } = require('../../util')
const { memberTypes } = require('./membership')
......@@ -53,6 +55,17 @@ const findAllDeep = () =>
],
})
const findManyDeep = phoneNumbers =>
app.db.channel.findAll({
where: { phoneNumber: { [Op.in]: phoneNumbers } },
include: [
{ model: app.db.deauthorization },
{ model: app.db.invite },
{ model: app.db.membership },
{ model: app.db.messageCount },
],
})
const findByPhoneNumber = phoneNumber => app.db.channel.findOne({ where: { phoneNumber } })
const findDeep = phoneNumber =>
......@@ -105,6 +118,7 @@ module.exports = {
findAllDeep,
findByPhoneNumber,
findDeep,
findManyDeep,
getAllAdminsExcept,
getAdminMemberships,
getAdminPhoneNumbers,
......
......@@ -3,7 +3,7 @@ import { describe, it, before, beforeEach, after, afterEach } from 'mocha'
import chaiAsPromised from 'chai-as-promised'
import { channelFactory, deepChannelFactory } from '../../../support/factories/channel'
import { genPhoneNumber } from '../../../support/factories/phoneNumber'
import { omit, keys, times } from 'lodash'
import { omit, keys, times, map } from 'lodash'
import channelRepository, { isSysadmin } from '../../../../app/db/repositories/channel'
import app from '../../../../app'
import testApp from '../../../support/testApp'
......@@ -227,6 +227,26 @@ describe('channel repository', () => {
})
})
describe('#findManyDeep', () => {
const includedPhoneNumbers = times(2, genPhoneNumber)
const excludesPhoneNumbers = times(3, genPhoneNumber)
beforeEach(async () => {
await Promise.all(
[...includedPhoneNumbers, ...excludesPhoneNumbers].map(phoneNumber =>
db.channel.create(deepChannelFactory({ phoneNumber }), {
include: [{ model: db.membership }],
}),
),
)
})
it('retrieves all channels with given phone numbers', async () => {
const results = await channelRepository.findManyDeep(includedPhoneNumbers)
expect(map(results, 'phoneNumber')).to.eql(includedPhoneNumbers)
expect(results[0].memberships).not.to.be.empty
})
})
describe('#isSysadmin', () => {
let supportChannel, adminPhoneNumber, subscriberPhoneNumber
......
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