Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feeds API for Zurich #17

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/api/routes/feeds/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,34 @@ export default ({ config, db, logger }) => {
})
);

export default ({ config, db, logger }) => {
let api = Router();

// Create a new Zurich record in the database
// TODO: What is mandatory around title / text, any rules AND/OR?
// TODO: Bulk endpoint for multiple POSTs
api.post('/zurich', validate({
body: Joi.object().keys({
post_id: Joi.number().integer().required(),
created_at: Joi.date().iso().required(),
title: Joi.string().allow(''),
text: Joi.string().allow(''),
image_url: Joi.string(),
city: Joi.string().valid(config.API_FEEDS_ZURICH_CITIES).required(),
disaster_type: Joi.string().valid(config.API_FEEDS_ZURICH_DISASTER_TYPES).required(),
location: Joi.object().required().keys({
lat: Joi.number().min(-90).max(90).required(),
lng: Joi.number().min(-180).max(180).required()
})
})
}),
(req, res, next) => feeds(config, db, logger).addQlueReport(req.body)
.then((data) => res.json(data))
.catch((err) => {
logger.error(err);
next(err);
})
);

return api;
}
25 changes: 25 additions & 0 deletions src/api/routes/feeds/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,28 @@ export default (config, db, logger) => ({
})
})
});

// Add a detik report
addZurichReport: (body) => new Promise((resolve, reject) => {

// Setup query
let query = `INSERT INTO ${config.TABLE_FEEDS_ZURICH}
(city, post_id, created_at, disaster_type, title, text, url, image_url, the_geom)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, ST_SetSRID(ST_POINT($9, $10),4326))`;

// Setup values
let values = [ body.city, body.post_id, body.created_at, body.disaster_type, body.title, body.text,
body.url, body.image_url, body.location.longitude, body.location.latitude ]

// Execute
logger.debug(query, values);
db.oneOrNone(query, values).timeout(config.PGTIMEOUT)
.then(() => resolve({ contribution_id: body.post_id, created: true }))
.catch((err) => {
if (err.constraint === 'reports_post_id_key')
resolve({ contribution_id: body.post_id, created: false, message: `${body.post_id} already exists in reports table`})
else
reject(err)
})
})
});
3 changes: 3 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export default {
API_FEEDS_QLUE_CITIES: (process.env.API_FEEDS_QLUE_CITIES || 'jabodetabek,bandung,surabaya').split(','),
API_FEEDS_QLUE_DISASTER_TYPES: (process.env.API_FEEDS_QLUE_DISASTER_TYPES || 'flood').split(','),
API_FEEDS_DETIK_DISASTER_TYPES: (process.env.API_FEEDS_DEIK_DISASTER_TYPES || 'flood').split(','),
API_FEEDS_ZURICH_CITIES: (process.env.ZURICH_FEEDS_QLUE_CITIES || 'jabodetabek,bandung,surabaya').split(','),
API_FEEDS_ZURICH_DISASTER_TYPES: (process.env.ZURICH_FEEDS_QLUE_DISASTER_TYPES || 'flood,fire,car_accident,power_failure,other').split(','),
API_REPORTS_TIME_WINDOW: process.env.API_REPORTS_TIME_WINDOW || 3600,
API_REPORTS_TIME_WINDOW_MAX: process.env.API_REPORTS_TIME_WINDOW_MAX || 604800, // 1w
API_REPORTS_LIMIT: process.env.API_REPORTS_LIMIT,
Expand Down Expand Up @@ -51,6 +53,7 @@ export default {
TABLE_FLOODGAUGE_REPORTS: process.env.TABLE_FLOODGAUGE_REPORTS || 'floodgauge.reports',
TABLE_FEEDS_QLUE: process.env.TABLE_FEEDS_QLUE || 'qlue.reports',
TABLE_FEEDS_DETIK: process.env.TABLE_FEEDS_DETIK || 'detik.reports',
TABLE_FEEDS_ZURICH: process.env.TABLE_FEEDS_DETIK || 'zurich.reports',
TABLE_GRASP_CARDS: process.env.TABLE_GRASP_CARDS || 'grasp.cards',
TABLE_GRASP_LOG: process.env.TABLE_GRASP_LOG || 'grasp.log',
TABLE_GRASP_REPORTS: process.env.TABLE_GRASP_REPORTS || 'grasp.reports',
Expand Down