Skip to content
This repository has been archived by the owner on Jul 17, 2020. It is now read-only.

Commit

Permalink
Don't delete customers with packages
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjiO committed Nov 22, 2017
1 parent 5e2f89c commit e03421a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
14 changes: 11 additions & 3 deletions server/controllers/customer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {extend, intersection, includes} from 'lodash'
import {ForbiddenError, NotFoundError} from '../lib/errors'
import {ADMIN_ROLE, clientRoles, volunteerRoles, customerStatus} from '../../common/constants'
import Customer from '../models/customer'
import Package from '../models/package'
import mailer from '../lib/mail/mail-helpers'
import User from '../models/user'
import {updateFields} from '../lib/update-linked-fields'
Expand Down Expand Up @@ -70,10 +71,17 @@ export default {
async delete(req, res) {
const id = req.customer._id

await User.findByIdAndRemove(id)
await Customer.findByIdAndRemove(id)
// Check to see if the customer is associated with any packages
const packages = await Package.find({customer: id})

res.json(req.customer)
if (packages.length > 0) {
// Don't delete the customer since a package references its data
res.status(409).json({message: "This customer has packages and can't be deleted"})
} else {
await User.findByIdAndRemove(id)
await Customer.findByIdAndRemove(id)
res.json(req.customer)
}
},
/**
* Customer middleware
Expand Down
32 changes: 32 additions & 0 deletions server/tests/integration/customer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import Customer from '../../models/customer'
import {createUserSession, createTestUser} from '../helpers'
import User from '../../models/user'
import Questionnaire from '../../models/questionnaire'
import Food, {FoodItem} from '../../models/food'
import Package from '../../models/package'

describe('Customer Api', function() {
before(async function() {
Expand Down Expand Up @@ -197,6 +199,36 @@ describe('Customer Api', function() {
.expect(200)
})

it('Won\'t delete a customer that has packages', async function () {
const foodItems = [new FoodItem({ name: 'Apple', quantity: 100, startDate: '2017-06-25', frequency: 1 })]
const food = await Food.create({ category: 'test', items: foodItems })

const customer = await Customer.create({
_id: 1,
firstName: 'George',
lastName: 'Washington',
email: '[email protected]',
})

await Package.create({
customer: customer._id,
status: 'Packed',
packedBy: 0,
contents: food.items.map(item => item._id.toString())
})

const testAdmin = createTestUser('admin', ADMIN_ROLE)
const session = await createUserSession(testAdmin)
const request = supertest.agent(session.app)

// delete the customer associated with the package
return request.delete(`/api/admin/customers/${customer._id}`)
.expect(409)
.expect(function(res) {
expect(res.body).to.have.property('message', 'This customer has packages and can\'t be deleted')
})
})

it('assigns customers', async function() {
const newAdmin = createTestUser('admin', ADMIN_ROLE)
const newCustomer = createTestUser('customer', clientRoles.CUSTOMER)
Expand Down

0 comments on commit e03421a

Please sign in to comment.