Verified Commit 6ec96fe5 authored by aguestuser's avatar aguestuser

[215] ensure that destroy methods destroy associated records

parent a0d6c4ad
......@@ -34,11 +34,10 @@ const update = (phoneNumber, attrs) =>
// (string, Transaction | null) => Promise<boolean>
const destroy = async (phoneNumber, transaction) => {
const numDestroyed = await app.db.channel.destroy({
where: { phoneNumber },
...(transaction ? { transaction } : {}),
})
return numDestroyed > 0
const channel = await findByPhoneNumber(phoneNumber)
return channel
? channel.destroy({ ...(transaction ? { transaction } : {}) }).then(() => true)
: false
}
const findAll = () => app.db.channel.findAll()
......
......@@ -12,11 +12,10 @@ const create = ({ phoneNumber, twilioSid, status }) =>
// (string, Transaction | null) => Promise<boolean>
const destroy = async (phoneNumber, transaction) => {
const numDestroyed = await app.db.phoneNumber.destroy({
where: { phoneNumber },
...(transaction ? { transaction } : {}),
})
return numDestroyed > 0
const phoneNumberRecord = await find(phoneNumber)
return phoneNumberRecord
? phoneNumber.destroy({ ...(transaction ? { transaction } : {}) }).then(() => true)
: false
}
const find = phoneNumber => app.db.phoneNumber.findOne({ where: { phoneNumber } })
......
......@@ -123,12 +123,18 @@ describe('channel repository', () => {
let channel, channelCount
describe('when given the phone number for an existing channel', () => {
beforeEach(async () => (channel = await db.channel.create(channelFactory())))
beforeEach(async () => {
channel = await db.channel.create(deepChannelFactory(), {
include: [{ model: db.membership }],
})
})
it('deletes the instance', async () => {
it('deletes the instance and its associations', async () => {
channelCount = await db.channel.count()
expect(await channelRepository.destroy(channel.phoneNumber)).to.eql(true)
expect(await db.channel.count()).to.eql(channelCount - 1)
expect(await db.membership.findOne({ where: { channelPhoneNumber: channel.phoneNumber } }))
.to.be.null
})
})
......
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