Skip to content

Commit

Permalink
INSPIRE pipeline: Allow super users to view pending polygons (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
rogup authored Jul 4, 2024
1 parent 39a8bdf commit 1e57cd0
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 57 deletions.
15 changes: 9 additions & 6 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
{
"files.insertFinalNewline": true,
"mochaExplorer.envPath": ".env.test",
"mochaExplorer.require": "ts-node/register",
"mochaExplorer.configFile": ".mocharc.json",
"mocha-snippets.quote-type": "double",
"mocha-snippets.glob": "**/*.test.ts",
"files.insertFinalNewline": true,
"mochaExplorer.envPath": ".env.test",
"mochaExplorer.require": "ts-node/register",
"mochaExplorer.configFile": ".mocharc.json",
"mocha-snippets.quote-type": "double",
"mocha-snippets.glob": "**/*.test.ts",
"editor.formatOnSave": true,
"editor.formatOnSaveMode": "modifications",
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
30 changes: 29 additions & 1 deletion src/queries/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,35 @@ export const getPolygons = async (
}
);

return boundaryResponse.data[0];
return boundaryResponse.data;
};

/**
* Return the pending geojson polygons of land ownership within a given bounding box area. These are
* the new boundaries from the latest INSPIRE pipeline run that are waiting to be permanently saved.
*/
export const getPendingPolygons = async (
sw_lng: number,
sw_lat: number,
ne_lng: number,
ne_lat: number,
acceptedOnly: boolean = false
) => {
const boundaryResponse = await axios.get(
`${process.env.BOUNDARY_SERVICE_URL}/pending/boundaries`,
{
params: {
sw_lat,
sw_lng,
ne_lat,
ne_lng,
acceptedOnly,
secret: process.env.BOUNDARY_SERVICE_SECRET,
},
}
);

return boundaryResponse.data;
};

export const searchOwner = async (proprietorName: string) => {
Expand Down
63 changes: 44 additions & 19 deletions src/routes/maps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Validation } from "../validation";
import {
createPublicMapView,
getGeoJsonFeaturesForMap,
getPendingPolygons,
getPolygons,
searchOwner,
} from "../queries/query";
Expand Down Expand Up @@ -771,37 +772,61 @@ async function getUserMaps(
}
}

type getLandOwnershipPolygonsRequest = Request & {
query: {
sw_lng: number;
sw_lat: number;
ne_lng: number;
ne_lat: number;
pending?: boolean;
acceptedOnly?: boolean;
};
auth: {
credentials: {
user_id: number;
};
};
};

/**
* Get the geojson polygons of land ownership within a given bounding box area
*
* @param request
* @param h
* @param d
* @returns
*/
async function getLandOwnershipPolygons(
request: Request,
h: ResponseToolkit,
d: any
): Promise<ResponseObject> {
let validation = new Validation();
await validation.validateLandOwnershipPolygonRequest(request.query);

if (validation.fail()) {
return h.response(validation.errors).code(400);
}
const { sw_lng, sw_lat, ne_lng, ne_lat, pending, acceptedOnly } =
request.query;
const { user_id } = request.auth.credentials;

try {
const payload: any = request.query;
if (pending) {
// Only super users should be able to view pending polygons
const hasAccess = await User.findOne({
where: {
id: user_id,
is_super_user: true,
},
});
if (!hasAccess) {
return h.response("Unauthorised!").code(403);
}

const polygons = await getPolygons(
payload.sw_lng,
payload.sw_lat,
payload.ne_lng,
payload.ne_lat
);
const polygons = await getPendingPolygons(
sw_lng,
sw_lat,
ne_lng,
ne_lat,
acceptedOnly
);

return h.response(polygons).code(200);
return h.response(polygons).code(200);
} else {
const polygons = await getPolygons(sw_lng, sw_lat, ne_lng, ne_lat);

return h.response(polygons).code(200);
}
} catch (err: any) {
console.log(err.message);
return h.response("internal server error!").code(500);
Expand Down
31 changes: 0 additions & 31 deletions src/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,37 +293,6 @@ export class Validation {
return this;
}

/**
* Validate polygon
*
* @param data
* @returns
*/
async validateLandOwnershipPolygonRequest(data: any) {
// sw_lng, sw_lat, ne_lng, ne_lat required

if (Joi.number().validate(data?.sw_lng, { presence: "required" }).error) {
this.addErrorMessage("sw_lng", "The sw_lng field is required.");
}
if (Joi.number().validate(data?.sw_lat, { presence: "required" }).error) {
this.addErrorMessage("sw_lat", "The sw_lat field is required.");
}
if (Joi.number().validate(data?.ne_lng, { presence: "required" }).error) {
this.addErrorMessage("ne_lng", "The ne_lng field is required.");
}
if (Joi.number().validate(data?.ne_lat, { presence: "required" }).error) {
this.addErrorMessage("ne_lat", "The ne_lat field is required.");
}

return this;
}

/**
*
* @param key
* @param message
* @returns
*/
addErrorMessage(key: string, message: string) {
if (this.errors.hasOwnProperty(key)) {
this.errors[key].push(message);
Expand Down

0 comments on commit 1e57cd0

Please sign in to comment.