Skip to content

Commit

Permalink
fixes profile images with no parents and adds PUMAs to fallback logic
Browse files Browse the repository at this point in the history
  • Loading branch information
davelandry committed Apr 8, 2019
1 parent 12cd845 commit 5d3ea53
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions api/profileImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ module.exports = function(app) {
const {size, pid, pslug} = req.params;

/** Sends the finally found image, and includes fallbacks */
function sendImage(image) {
async function sendImage(image) {

const id = image ? image : pslug === "university" ? "2032" : "1849";
if (size === "json") db.images.findOne({where: {id}}).then(resp => res.json(resp));
if (size === "json") {
const resp = await db.images.findOne({where: {id}});
res.json(resp);
}
else {
res.sendFile(`${process.cwd()}/static/images/profile/${size}/${id}.jpg`, err => {
if (err) res.status(err.status);
Expand Down Expand Up @@ -77,7 +80,7 @@ module.exports = function(app) {
}
else if (pslug === "geo") {

const parents = axios.get(`${CANON_LOGICLAYER_CUBE}geoservice-api/relations/parents/${attr.id}`)
const parents = await axios.get(`${CANON_LOGICLAYER_CUBE}geoservice-api/relations/parents/${attr.id}?targetLevels=state,county,msa,puma,place`)
.then(d => d.data.reverse())
.then(d => d.map(p => p.geoid))
.catch(err => {
Expand All @@ -93,9 +96,9 @@ module.exports = function(app) {

const parentImage = parentAttrs
.sort((a, b) => parents.indexOf(a.id) - parents.indexOf(b.id))
.find(p => p.imageId).imageId;
.find(p => p.imageId);

sendImage(parentImage);
sendImage(parentImage ? parentImage.imageId : false);

}
else sendImage(false);
Expand Down

0 comments on commit 5d3ea53

Please sign in to comment.