diff --git a/README.md b/README.md index 3d7ffb9..1b3ab3c 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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 @@ -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]` \ No newline at end of file +`https://webhooks.mongodb-stitch.com/api/client/v2.0/app/birms-cvrbm/service/query-birms/incoming_webhook/count-errors?secret=[secret]` diff --git a/services/query-birms/count-release-packages.js b/services/query-birms/count-release-packages.js new file mode 100644 index 0000000..0e9a4f1 --- /dev/null +++ b/services/query-birms/count-release-packages.js @@ -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()); + }); + +}; diff --git a/services/query-birms/find-release-packages.js b/services/query-birms/find-release-packages.js new file mode 100644 index 0000000..80f69cc --- /dev/null +++ b/services/query-birms/find-release-packages.js @@ -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)); + }); + + +}; diff --git a/services/query-birms/get-release-package-by-ocid.js b/services/query-birms/get-release-package-by-ocid.js new file mode 100644 index 0000000..4f1b8cd --- /dev/null +++ b/services/query-birms/get-release-package-by-ocid.js @@ -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)); + }); +};