Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 9 additions & 1 deletion src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@ import awsServerlessExpressMiddleware from 'aws-serverless-express/middleware';
import setupRoutes from './routes';

const app = express();
const exposedHeaders = [
'Pagination-Count',
'Total-Count',
'Page-Number',
'Page-Size',
'Has-More',
'Next-Page',
];

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

app.use(cors());
app.use(cors({ exposedHeaders }));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Update API Gateway preflight for the new authenticated catalog

/locations/catalog is meant for browser clients (sheets.doobneek.org and the extension), but this change only updates Express CORS. I checked simple-proxy-api.yaml, and both mock OPTIONS handlers still return Access-Control-Allow-Origin: 'https://example.com' (lines 43-50 and 86-93). Because the new route requires an Authorization header, browsers will preflight it and reject requests from the intended origins before they ever reach Express, so the feature is unusable outside server-side callers.

Useful? React with 👍 / 👎.

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

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

const DEFAULT_COGNITO_USER_POOL_ID = 'us-east-1_EvBbozIjd';
const DEFAULT_INTERNAL_CATALOG_ALLOWED_HOSTS = ['sheets.doobneek.org'];
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 || DEFAULT_COGNITO_USER_POOL_ID;
const cognitoUserPoolRegion = process.env.COGNITO_USER_POOL_REGION
|| (cognitoUserPoolId.includes('_') ? cognitoUserPoolId.split('_')[0] : 'us-east-1');
const cognitoUserPoolIssuer = 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,
),
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
80 changes: 80 additions & 0 deletions src/models/location.js
Original file line number Diff line number Diff line change
Expand Up @@ -668,5 +668,85 @@ 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 locations = await Location.findAll({
attributes: [
'id',
'name',
'slug',
'description',
'additional_info',
'last_validated_at',
'position',
],
where,
include,
order,
limit,
offset,
});

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