-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Endpoint de listar alterações em listas de email
- Loading branch information
1 parent
f39759d
commit 91e3dfb
Showing
5 changed files
with
158 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
})) | ||
: [] | ||
|
||
const additions = prepare(additionsViaApp).concat( | ||
prepare(additionsViaSolicitations) | ||
) | ||
const deletions = prepare(deletionsViaApp) | ||
|
||
ctx.status = 200 | ||
ctx.body = { | ||
additions, | ||
deletions | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters