Skip to content
Open
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
17 changes: 17 additions & 0 deletions simple-proxy-api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,23 @@ paths:
requestTemplates:
application/json: "{\"statusCode\": 200}"
type: mock
/locations/catalog:
x-amazon-apigateway-any-method:
produces:
- application/json
responses: {}
x-amazon-apigateway-integration:
uri: arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:710263499164:function:${stageVariables.ServerlessExpressLambdaFunctionName}/invocations
httpMethod: POST
type: aws_proxy
options:
produces:
- application/json
responses: {}
x-amazon-apigateway-integration:
uri: arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:710263499164:function:${stageVariables.ServerlessExpressLambdaFunctionName}/invocations
httpMethod: POST
type: aws_proxy
/{proxy+}:
x-amazon-apigateway-any-method:
produces:
Expand Down
13 changes: 11 additions & 2 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
import express from 'express';
import bodyParser from 'body-parser';
import cors from 'cors';
import morgan from 'morgan';
import awsServerlessExpressMiddleware from 'aws-serverless-express/middleware';
import setupRoutes from './routes';
import { publicApiCors, internalLocationCatalogCors } from './services/internal-location-catalog-cors';

const app = express();

app.use(morgan('dev'));

app.use(cors());
function isInternalLocationCatalogPath(path) {
return path === '/locations/catalog' || path.startsWith('/locations/catalog/');
}

app.use((req, res, next) => {
if (isInternalLocationCatalogPath(req.path)) {
return internalLocationCatalogCors(req, res, next);
}
return publicApiCors(req, res, next);
});
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

Expand Down
62 changes: 62 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,71 @@
import { parseBoolean, parseNumber } from './utils/strings';

const DEFAULT_INTERNAL_CATALOG_ALLOWED_HOSTS = ['sheets.doobneek.org'];
const DEFAULT_INTERNAL_CATALOG_ALLOWED_ORIGIN_PATTERNS = ['chrome-extension://*'];
const DEFAULT_INTERNAL_CATALOG_ALLOWED_GROUPS = [];
const DEFAULT_INTERNAL_CATALOG_MAX_RADIUS_METERS = Math.round(20 * 1609.344);
const DEFAULT_INTERNAL_CATALOG_DEFAULT_PAGE_SIZE = 500;
const DEFAULT_INTERNAL_CATALOG_MAX_PAGE_SIZE = 1000;

const parseCsv = (value, fallback = []) => {
if (typeof value !== 'string') return fallback;
const parsed = value
.split(',')
.map(entry => entry.trim())
.filter(Boolean);
return parsed.length ? parsed : fallback;
};

const cognitoUserPoolId = process.env.COGNITO_USER_POOL_ID || null;
const cognitoUserPoolRegion = process.env.COGNITO_USER_POOL_REGION
|| (cognitoUserPoolId && cognitoUserPoolId.includes('_') ? cognitoUserPoolId.split('_')[0] : null);
const cognitoUserPoolIssuer = process.env.COGNITO_USER_POOL_ISSUER || (cognitoUserPoolId
? `https://cognito-idp.${cognitoUserPoolRegion}.amazonaws.com/${cognitoUserPoolId}`
: null);
const internalCatalogMaxPageSize = parseNumber(
process.env.INTERNAL_LOCATION_CATALOG_MAX_PAGE_SIZE,
DEFAULT_INTERNAL_CATALOG_MAX_PAGE_SIZE,
);

export default {
port: process.env.PORT || 3000,
slackWebhookUrl: process.env.SLACK_WEBHOOK_URL,
adminGroupName: process.env.ADMIN_GROUP_NAME || 'StreetlivesAdmins',
cognito: {
userPoolId: cognitoUserPoolId,
userPoolRegion: cognitoUserPoolRegion,
userPoolIssuer: cognitoUserPoolIssuer,
},
internalLocationCatalog: {
allowedOriginHosts: parseCsv(
process.env.INTERNAL_LOCATION_CATALOG_ALLOWED_HOSTS,
DEFAULT_INTERNAL_CATALOG_ALLOWED_HOSTS,
),
allowedOriginPatterns: parseCsv(
process.env.INTERNAL_LOCATION_CATALOG_ALLOWED_ORIGIN_PATTERNS,
DEFAULT_INTERNAL_CATALOG_ALLOWED_ORIGIN_PATTERNS,
),
allowedClientIds: parseCsv(
process.env.INTERNAL_LOCATION_CATALOG_ALLOWED_CLIENT_IDS,
[],
),
allowedGroupNames: parseCsv(
process.env.INTERNAL_LOCATION_CATALOG_ALLOWED_GROUP_NAMES,
DEFAULT_INTERNAL_CATALOG_ALLOWED_GROUPS,
),
maxRadiusMeters: parseNumber(
process.env.INTERNAL_LOCATION_CATALOG_MAX_RADIUS_METERS,
DEFAULT_INTERNAL_CATALOG_MAX_RADIUS_METERS,
),
defaultPageSize: Math.min(
parseNumber(
process.env.INTERNAL_LOCATION_CATALOG_DEFAULT_PAGE_SIZE,
DEFAULT_INTERNAL_CATALOG_DEFAULT_PAGE_SIZE,
),
internalCatalogMaxPageSize,
),
maxPageSize: internalCatalogMaxPageSize,
},
db: {
database: process.env.DATABASE_NAME || 'streetlives',
username: process.env.DATABASE_USER,
Expand Down
98 changes: 98 additions & 0 deletions src/controllers/locations.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Joi from 'joi';
import locationSchemas from './validation/locations';
import models from '../models';
import config from '../config';
import { updateInstance, createInstance, destroyInstance } from '../services/data-changes';
import {
getMetadataForLocation,
Expand All @@ -12,10 +13,68 @@ import geometry from '../utils/geometry';
import { parseBoolean } from '../utils/strings';
import { convertKeyValueArrayToObject } from '../utils/api-params';
import { NotFoundError, ValidationError } from '../utils/errors';
import authorizeInternalLocationCatalogRequest from '../services/internal-location-catalog-auth';

const DEFAULT_MAX_LOCATIONS_RETURNED = 1000;
const MAX_TAXONOMY_IDS = 200;

const setPaginationHeaders = (res, {
totalNumLocations,
pageNumber,
pageSize,
}) => {
const paginationCount = pageSize > 0
? Math.ceil(totalNumLocations / pageSize)
: 1;
const hasMore = pageNumber + 1 < paginationCount;
res.setHeader('Pagination-Count', paginationCount);
res.setHeader('Total-Count', totalNumLocations);
res.setHeader('Page-Number', pageNumber);
res.setHeader('Page-Size', pageSize);
res.setHeader('Has-More', hasMore ? 'true' : 'false');
if (hasMore) {
res.setHeader('Next-Page', pageNumber + 1);
} else {
res.removeHeader('Next-Page');
}
};

const getCoordinatesFromPosition = (position) => {
const coordinates = position && position.coordinates;
if (!Array.isArray(coordinates) || coordinates.length < 2) {
return {
latitude: null,
longitude: null,
};
}
return {
latitude: Number(coordinates[1]),
longitude: Number(coordinates[0]),
};
};

const formatCatalogLocation = (location) => {
const plainLocation = location.get({ plain: true });
const {
latitude,
longitude,
} = getCoordinatesFromPosition(plainLocation.position);
return {
id: plainLocation.id,
name: plainLocation.name,
slug: plainLocation.slug,
description: plainLocation.description,
additional_info: plainLocation.additional_info,
last_validated_at: plainLocation.last_validated_at,
position: plainLocation.position,
latitude,
longitude,
org: plainLocation.Organization ? plainLocation.Organization.name : null,
Organization: plainLocation.Organization || null,
PhysicalAddresses: plainLocation.PhysicalAddresses || [],
};
};

const isLocationClosed = (occasion, eventRelatedInfos, services) => {
if (!occasion) {
return false;
Expand Down Expand Up @@ -292,6 +351,45 @@ export default {
}
},

findCatalog: async (req, res, next) => {
try {
await Joi.validate(req, locationSchemas.findCatalog, { allowUnknown: true });
await authorizeInternalLocationCatalogRequest(req);

const {
latitude,
longitude,
radius,
pageNumber: rawPageNumber,
pageSize: rawPageSize,
sortBy,
} = req.query;

const pageSize = rawPageSize != null
? parseInt(rawPageSize, 10)
: config.internalLocationCatalog.defaultPageSize;
const pageNumber = rawPageNumber != null ? parseInt(rawPageNumber, 10) : 0;
const {
locations,
totalNumLocations,
} = await models.Location.findCatalog({
position: geometry.createPoint(Number(longitude), Number(latitude)),
radius: Number(radius),
limit: pageSize,
offset: pageNumber * pageSize,
sortBy,
});

setPaginationHeaders(res, {
totalNumLocations,
pageNumber,
pageSize,
});
res.send(locations.map(formatCatalogLocation));
} catch (err) {
next(err);
}
},
getInfo: async (req, res, next) => {
try {
await Joi.validate(req, locationSchemas.getInfo, { allowUnknown: true });
Expand Down
24 changes: 24 additions & 0 deletions src/controllers/validation/locations.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Joi from 'joi';
import { SORT_OPTIONS, SORT_ORDER } from '../sort-by';
import config from '../../config';

const updateMetadataSchema = Joi.object().keys({
source: Joi.string(),
Expand Down Expand Up @@ -60,6 +61,29 @@ export default {
}).required(),
},

findCatalog: {
query: Joi.object().keys({
latitude: Joi.number().required(),
longitude: Joi.number().required(),
radius: Joi.number()
.integer()
.positive()
.max(config.internalLocationCatalog.maxRadiusMeters)
.required(),
pageNumber: Joi.number()
.integer()
.min(0),
pageSize: Joi.number()
.integer()
.positive()
.max(config.internalLocationCatalog.maxPageSize),
sortBy: Joi.string().valid(
SORT_ORDER.NEARBY,
SORT_ORDER.MOST_RECENTLY_VALIDATED,
),
}).required(),
},

create: {
body: Joi.object().keys({
name: Joi.string(),
Expand Down
100 changes: 100 additions & 0 deletions src/models/location.js
Original file line number Diff line number Diff line change
Expand Up @@ -668,5 +668,105 @@ module.exports = (sequelize, DataTypes, Op) => {
};
};

Location.findCatalog = async ({
position,
radius,
limit,
offset,
sortBy,
}) => {
let distance;
const whereConditions = [{
hidden_from_search: { [Op.or]: [false, null] },
}];

if (position) {
distance = sequelize.fn(
'ST_DistanceSphere',
sequelize.col('position'),
sequelize.literal(`ST_GeomFromGeoJSON('${JSON.stringify(position)}')`),
);
}

if (position && radius) {
whereConditions.push(sequelize.where(distance, { [Op.lte]: radius }));
}

let order;
if (position && (!sortBy || sortBy === SORT_ORDER.NEARBY)) {
order = [[distance, 'ASC'], ['id', 'ASC']];
} else {
order = [['last_validated_at', 'DESC'], ['id', 'ASC']];
}

const where = sequelize.and(...whereConditions);
const include = [
{
model: sequelize.models.Organization,
attributes: ['id', 'name'],
},
{
model: sequelize.models.PhysicalAddress,
attributes: [
'id',
'address_1',
'city',
'region',
'state_province',
'postal_code',
'country',
],
},
];

const totalNumLocations = await Location.count({
where,
distinct: true,
col: 'Location.id',
});

const pageRows = await Location.findAll({
attributes: ['id'],
where,
order,
limit,
offset,
raw: true,
});

const locationIds = pageRows.map(location => location.id);
if (!locationIds.length) {
return {
locations: [],
totalNumLocations,
};
}

const locations = await Location.findAll({
attributes: [
'id',
'name',
'slug',
'description',
'additional_info',
'last_validated_at',
'position',
],
where: {
id: { [Op.in]: locationIds },
},
include,
});

const locationPositionById = new Map(locationIds.map((id, index) => [id, index]));
locations.sort((left, right) =>
locationPositionById.get(left.id) - locationPositionById.get(right.id));

return {
locations,
totalNumLocations,
};
};

return Location;
};
1 change: 1 addition & 0 deletions src/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export default (app) => {
app.get('/organizations/:organizationId/locations', organizations.getLocations);

app.get('/locations', locations.find);
app.get('/locations/catalog', locations.findCatalog);

app.post('/locations/suggestions', locations.suggestNew);
app.get('/locations-by-slug/:slug', locations.getInfoBySlug);
Expand Down
Loading
Loading