generated from CodeForBaltimore/ProjectTemplate
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #295 from CodeForBaltimore/188
Issue 188 - Return number of emails contacted
- Loading branch information
Showing
6 changed files
with
1,890 additions
and
1,455 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
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 |
---|---|---|
@@ -1,36 +1,65 @@ | ||
import { Router } from 'express' | ||
import {Router} from 'express' | ||
import utils from '../utils' | ||
import { parseAsync } from 'json2csv' | ||
import {parseAsync} from 'json2csv' | ||
import {Op} from 'sequelize' | ||
|
||
const router = new Router() | ||
router.use(utils.authMiddleware) | ||
|
||
// Gets a data dump from the passed in model (if it exists). | ||
router.get('/:model_type', async (req, res) => { | ||
|
||
const response = new utils.Response() | ||
const modelType = req.params.model_type | ||
|
||
try { | ||
|
||
/** @todo refactor this when we change how CSV's are delivered. */ | ||
// eslint-disable-next-line no-prototype-builtins | ||
if (req.context.models.hasOwnProperty(modelType) && modelType !== 'User' && modelType !== 'UserRole') { | ||
/** @todo add filtering */ | ||
const results = await req.context.models[modelType].findAll({ raw: true }) | ||
let options = {raw: true} | ||
// search by name if filter query param is included | ||
if (req.query && req.query.filter && req.query.filter.length) { | ||
options.where = { | ||
name: { | ||
[Op.iLike]: '%' + req.query.filter + '%' | ||
} | ||
} | ||
} | ||
|
||
const processedResults = await utils.processResults(results, modelType) | ||
/** @todo add a search filter for status once data has a status field. */ | ||
let results = await req.context.models[modelType].findAll(options) | ||
|
||
if (results.length !== 0) { | ||
response.setMessage = await parseAsync(JSON.parse(JSON.stringify(processedResults)), Object.keys(results[0]), {}) | ||
const processedResults = await utils.processResults(results, modelType) | ||
const fields = Object.keys(results[0]) | ||
parseAsync(processedResults, {fields}).then(csv => { | ||
response.setMessage(csv) | ||
const dateObj = new Date() | ||
const dateStr = `${dateObj.getUTCMonth() + 1}_${dateObj.getUTCDate()}_${dateObj.getUTCFullYear()}` | ||
res.setHeader('Content-disposition', `attachment; filename=HCRC_${modelType}_${dateStr}.csv`) | ||
res.set('Content-Type', 'text/csv') | ||
return res.status(response.getCode()).send(response.getMessage()) | ||
}, err => { | ||
response.setCode(500) | ||
response.setMessage('Not able to parse data: ' + err) | ||
return res.status(response.getCode()).send(response.getMessage()) | ||
}) | ||
} else { | ||
response.setCode(200) | ||
response.setMessage('No results found') | ||
return res.status(response.getCode()).send(response.getMessage()) | ||
} | ||
} else { | ||
response.setCode(400) | ||
response.setMessage('Model type is invalid') | ||
return res.status(response.getCode()).send(response.getMessage()) | ||
} | ||
} catch (e) { | ||
console.error(e) | ||
response.setCode(500) | ||
return res.status(response.getCode()).send(response.getMessage()) | ||
} | ||
|
||
return res.status(response.getCode()).send(response.getMessage()) | ||
}) | ||
|
||
export default router |
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
Oops, something went wrong.