Skip to content

Commit

Permalink
Endpoint de listar alterações em listas de email
Browse files Browse the repository at this point in the history
  • Loading branch information
lubien authored and urielfcampos committed Jun 21, 2019
1 parent f39759d commit 91e3dfb
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 5 deletions.
21 changes: 17 additions & 4 deletions seeds/students.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ exports.seed = async function(knex, Promise) {
cancelled: false,
prescribed: false,
mailingList: 'none',
mailingListToRemove: 'none',
mailingListToAdd: 'active',
entryDate: '05-28-2019',
advisor: 'Orientador Válido',
defenseDate: null,
Expand All @@ -44,6 +46,8 @@ exports.seed = async function(knex, Promise) {
cancelled: false,
prescribed: false,
mailingList: 'none',
mailingListToRemove: 'none',
mailingListToAdd: 'active',
entryDate: '05-27-2019',
advisor: null,
defenseDate: null,
Expand All @@ -67,7 +71,9 @@ exports.seed = async function(knex, Promise) {
academicHighlight: false,
cancelled: false,
prescribed: false,
mailingList: 'none',
mailingList: 'active',
mailingListToRemove: 'active',
mailingListToAdd: 'concluding',
entryDate: '01-28-2019',
advisor: 'Mais um orientador',
defenseDate: null,
Expand All @@ -92,8 +98,9 @@ exports.seed = async function(knex, Promise) {
cancelled: false,
prescribed: false,
mailingList: 'none',
mailingListToRemove: 'none',
mailingListToAdd: 'active',
entryDate: '04-20-2019',

advisor: null,
defenseDate: null,
term: null,
Expand All @@ -116,7 +123,9 @@ exports.seed = async function(knex, Promise) {
academicHighlight: false,
cancelled: true,
prescribed: false,
mailingList: 'none',
mailingList: 'active',
mailingListToRemove: 'active',
mailingListToAdd: 'none',
entryDate: '05-18-2018',
advisor: null,
defenseDate: null,
Expand All @@ -141,6 +150,8 @@ exports.seed = async function(knex, Promise) {
cancelled: false,
prescribed: false,
mailingList: 'none',
mailingListToRemove: 'none',
mailingListToAdd: 'active',
entryDate: null,
advisor: null,
defenseDate: null,
Expand All @@ -164,7 +175,9 @@ exports.seed = async function(knex, Promise) {
academicHighlight: false,
cancelled: false,
prescribed: false,
mailingList: 'none',
mailingList: 'active',
mailingListToRemove: 'active',
mailingListToAdd: 'concluding',
entryDate: '02-01-2019',
advisor: null,
defenseDate: null,
Expand Down
49 changes: 49 additions & 0 deletions server/controllers/students/EmailChanges.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const Student = require('../../models/Student')
const Solicitation = require('../../models/Solicitation')
const errors = require('../../../shared/errors')

module.exports = async function emailChanges(ctx) {
const { mailingList } = ctx.request.query
if (
mailingList === undefined ||
!['active', 'concluding', 'freshman'].includes(mailingList)
) {
ctx.status = 400
ctx.body = { code: errors.INVALID_FILTER, filter: 'mailingList' }
return
}

const promises = [
Student.where('mailingListToAdd', mailingList).fetchAll(),
Student.where('mailingListToRemove', mailingList).fetchAll()
]

if (['concluding', 'freshman'].includes(mailingList)) {
promises.push(Solicitation.where('type', mailingList).fetchAll())
}

const [
additionsViaApp,
deletionsViaApp,
additionsViaSolicitations = []
] = await Promise.all(promises)

const prepare = models =>
models.toJSON
? models.toJSON().map(({ name, email }) => ({
name,
email
}))
: []

const additions = prepare(additionsViaApp).concat(
prepare(additionsViaSolicitations)
)
const deletions = prepare(deletionsViaApp)

ctx.status = 200
ctx.body = {
additions,
deletions
}
}
4 changes: 3 additions & 1 deletion server/controllers/students/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ const FromCsv = require('./FromCsv')
const Update = require('./Update')
const ActiveMailList = require('./ActiveMailList')
const UpdateAcademicHighlight = require('./UpdateAcademicHighlight')
const EmailChanges = require('./EmailChanges')

module.exports = {
List,
Show,
FromCsv,
Update,
ActiveMailList,
UpdateAcademicHighlight
UpdateAcademicHighlight,
EmailChanges
}
1 change: 1 addition & 0 deletions server/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ api.get('/users/:id', users.Show)
api.post('/users/', bodyJson, users.Create)

// Student Routes
api.get('/students/email-changes', students.EmailChanges)
api.get('/students/actives-mailing-list', students.ActiveMailList)
api.get('/students/', students.List)
api.get('/students/:id', students.Show)
Expand Down
88 changes: 88 additions & 0 deletions test/server/controllers/students.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1152,4 +1152,92 @@ describe('/api/students', () => {
expect(res1.body[1].academicHighlight).toEqual(1)
done()
})
test('GET /email-changes?mailingList=active', async done => {
const { token } = await testUtils.user('admin')
const res = await chai
.request(server.listen())
.get('/api/students/email-changes')
.query({ mailingList: 'active' })
.set('Authorization', `Bearer ${token}`)
expect(res.status).toEqual(200)
expect(res.type).toEqual('application/json')
expect(res.body).toBeDefined()
expect(res.body.additions.length).toBeDefined()
expect(res.body.deletions.length).toBeDefined()

expect(
[
'FELIPE SOUZA FERREIRA',
'LAURA CARDOSO CASTRO',
'ENZO FERREIRA ALVES',
'EDUARDO ALVES LIMA'
].filter(
name => !res.body.additions.find(student => student.name === name)
)
).toEqual([])

expect(
[
'JOSE FERREIRA SILVA',
'KAUAN CARVALHO SANTOS',
'JULIAN BARBOSA SANTOS'
].filter(
name => !res.body.deletions.find(student => student.name === name)
)
).toEqual([])

done()
})
})

test('GET /email-changes?mailingList=concluding', async done => {
const { token } = await testUtils.user('admin')

const res = await chai
.request(server.listen())
.get('/api/students/email-changes')
.query({ mailingList: 'concluding' })
.set('Authorization', `Bearer ${token}`)
expect(res.status).toEqual(200)
expect(res.type).toEqual('application/json')
expect(res.body).toBeDefined()
expect(res.body.additions.length).toBeDefined()
expect(res.body.deletions.length).toBeDefined()
expect(res.body.deletions.length).toEqual(0)

expect(
[
'JOSE FERREIRA SILVA',
'JULIAN BARBOSA SANTOS',
'Ana Goncalves Gomes',
'Gabriela Dias Cunha',
'Rodrigo Rodrigues Santos'
].filter(name => !res.body.additions.find(student => student.name === name))
).toEqual([])

done()
})

test('GET /email-changes?mailingList=freshman', async done => {
const { token } = await testUtils.user('admin')

const res = await chai
.request(server.listen())
.get('/api/students/email-changes')
.query({ mailingList: 'freshman' })
.set('Authorization', `Bearer ${token}`)
expect(res.status).toEqual(200)
expect(res.type).toEqual('application/json')
expect(res.body).toBeDefined()
expect(res.body.additions.length).toBeDefined()
expect(res.body.deletions.length).toBeDefined()
expect(res.body.deletions.length).toEqual(0)

expect(
['Victor Silva Carvalho', 'Marisa Correia Castro'].filter(
name => !res.body.additions.find(student => student.name === name)
)
).toEqual([])

done()
})

0 comments on commit 91e3dfb

Please sign in to comment.