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

fixes #1 Add webhooks for querying release packages #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
29 changes: 26 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,21 @@ This key will be provided on demand by the Bandung OC team.

`https://webhooks.mongodb-stitch.com/api/client/v2.0/app/birms-cvrbm/service/query-birms/incoming_webhook/get-by-ocid?secret=[secret]&ocid=[ocid]`

Get one OCDS release document for the release with the given `ocid`.
Get one OCDS release document for the release with the given `ocid`.

Example:

### Gets the release with ocid `ocds-afzrfb-s-2016-5043143`

`https://webhooks.mongodb-stitch.com/api/client/v2.0/app/birms-cvrbm/service/query-birms/incoming_webhook/get-by-ocid?secret=[secret]&ocid=ocds-afzrfb-s-2016-5043143`
## Get release package by ocid

`https://webhooks.mongodb-stitch.com/api/client/v2.0/app/birms-cvrbm/service/query-birms/incoming_webhook/get-release-package-by-ocid?secret=[secret]&ocid=[ocid]`

Example

### Gets the release package with ocid `ocds-afzrfb-s-2016-5043143`

`https://webhooks.mongodb-stitch.com/api/client/v2.0/app/birms-cvrbm/service/query-birms/incoming_webhook/get-release-package-by-ocid?secret=[secret]&ocid=ocds-afzrfb-s-2016-5043143`

## Find releases using an optional MongoDB query and pagination.

Expand Down Expand Up @@ -50,6 +58,21 @@ We know for the first page, the internal MongoDB `_id` property of the last docu

`https://webhooks.mongodb-stitch.com/api/client/v2.0/app/birms-cvrbm/service/query-birms/incoming_webhook/find-releases?secret=[secret]&q={"tender.value.amount":{"$exists":true}}&limit=2&fromId=5b7528ecb7d7c3728366810f`

## Find release packages using an optional MongoDB query and pagination.

This is very similar to the previous option but it will search release packages instead. Careful here, if you want to narrow down the search by using
release filters, remember packages are wrapping releases into a releases[] array. So all your queries will have to start with `releases.`.

Example, this will find the release packages with a custom query:

`https://webhooks.mongodb-stitch.com/api/client/v2.0/app/birms-cvrbm/service/query-birms/incoming_webhook/find-release-packages?secret=[secret]&q={"releases.ocid":"ocds-afzrfb-s-2016-5022700"}`

## Get the release package by ocid

This will return the release package by the ocid of the wrapped release

` https://webhooks.mongodb-stitch.com/api/client/v2.0/app/birms-cvrbm/service/query-birms/incoming_webhook/get-release-package-by-ocid?secret=[secret]&ocid=ocds-afzrfb-s-2016-5022700`

## Count releases using an optional MongoDB query

This is similar to find releases endpoint above, but it just counts the items, hence
Expand All @@ -71,4 +94,4 @@ Examples:

## Count errors during data import

`https://webhooks.mongodb-stitch.com/api/client/v2.0/app/birms-cvrbm/service/query-birms/incoming_webhook/count-errors?secret=[secret]`
`https://webhooks.mongodb-stitch.com/api/client/v2.0/app/birms-cvrbm/service/query-birms/incoming_webhook/count-errors?secret=[secret]`
12 changes: 12 additions & 0 deletions services/query-birms/count-release-packages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
exports = function (payload, response) {

const mongodb = context.services.get("mongodb-atlas");
const releases = mongodb.db("birms").collection("package");
var q = payload.query.q || '{}';
response.setHeader('content-type', 'text/plain');

return releases.count(EJSON.parse(q)).then((num_returned) => {
response.setBody(num_returned.toString());
});

};
26 changes: 26 additions & 0 deletions services/query-birms/find-release-packages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
exports = function (payload, response) {

const mongodb = context.services.get("mongodb-atlas");
const releases = mongodb.db("birms").collection("package");
var q = payload.query.q || '{}';
var fromId = payload.query.fromId || '';
var limit = +(payload.query.limit) || 20;

response.setHeader('content-type', 'application/json');

var query;
if (fromId === '') {
query = q;
} else {
query = '{ \"$and\": [ {\"_id\": {\"$gt\": { \"$oid\": \"' + fromId + '\"}}},' + q + ' ] }';
}

releases.find(EJSON.parse(query)).limit(limit).toArray().then(docs => {
docs.map(doc => {
doc._id = doc._id.toString();
});
response.setBody(JSON.stringify(docs));
});


};
13 changes: 13 additions & 0 deletions services/query-birms/get-release-package-by-ocid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
exports = function (payload, response) {
const mongodb = context.services.get("mongodb-atlas");
const releases = mongodb.db("birms").collection("package");
var ocid = payload.query.ocid || '';
response.setHeader('content-type', 'application/json');

return releases.findOne({ "releases.ocid": ocid }).then(doc => {
if (!doc)
throw new Error('No record found.');
doc._id = doc._id.toString();
response.setBody(JSON.stringify(doc));
});
};