diff --git a/README.md b/README.md new file mode 100644 index 0000000..1f2afbf --- /dev/null +++ b/README.md @@ -0,0 +1,8 @@ +# eventing-tracking + +Need to have the following setup: + +track-and-tracers bucket +metadata bucket + +Follow configuration in the screenshot for the eventing function. diff --git a/eventing-function.js b/eventing-function.js index 8dd6596..773656f 100644 --- a/eventing-function.js +++ b/eventing-function.js @@ -1,96 +1,23 @@ function OnUpdate(doc, meta) { - if(doc._type === "locationRegistration"){ - if(doc["location"]) { - if(doc["checkin"]){ - increment_venue_guest(doc); - } - if(doc["checkout"]){ - decrement_venue_guest(doc); - if(doc.infected){ - findPossibleTraces(doc); - } - } - } - } - -} -function venue_full_call_police(location) { - //rest call to the police - log("location is full: ", location); -} - -function increment_venue_guest(doc) { - - - //log("incrementing guests at: ", doc.location) - var location_doc = tnt[doc.location]; - - log("Current venue guests:", location_doc.guests) - if(location_doc.guests < location_doc.maxGuests) { - //log("guests incremented") - location_doc.guests = location_doc + 1; - } - else { - // venue is full, call the police - venue_full_call_police(doc.location); - } - - //log("Local guests:", location_doc.guests); + if(doc.type != "covidSuspectedUser") return; + if(doc.infected == false) return; - tnt[doc.location] = location_doc; - //log("updated the location doc"); + var documentIdToBeUpdated = "covidcase::" + doc.state + "::" + doc.createDate; - //log("location guests in cb: ", tnt[doc.location].guests); + var documentToBeUpdated = SELECT track_and_trace.* FROM track_and_trace USE KEYS [$documentIdToBeUpdated]; - //log("location doc local:", location_doc); - //log("location doc couchbase:", tnt[doc.location]); -} - -function decrement_venue_guest(doc) { - log("DECREMENTING") - var location_doc = tnt[doc["location"]]; - log("decrementing from: ", location_doc.guests); - location_doc["guests"] = location_doc["guests"]--; - log("decrementation complete: ", location_doc.guests) - tnt[doc["location"]] = location_doc; - log("location guests in cb: ", tnt[doc.location].guests); - -} + for(var i of documentToBeUpdated){ -function findPossibleTraces(doc) { - - var doc_checkin = doc.checkin; - var doc_checkout = doc.checkout; - var doc_phone = doc.phone; - var doc_location = doc.location; + var incPositive = parseInt(i.positive) + 1; + var incPositiveIncrease = parseInt(i.positiveIncrease) + 1; + + i.positive = incPositive + ""; + i.positiveIncrease = incPositiveIncrease + ""; - var possible_infected_visitors = - SELECT meta().id, phone, checkin, checkout - FROM `track-and-tracers`._default._default - WHERE ((checkin >= $doc_checkin AND checkin <= $doc_checkout) - OR (checkout >= $doc_checkin AND checkout <= $doc_checkout)) - AND location = $doc_location - AND phone != $doc_phone; - - for(var person of possible_infected_visitors) { - notify_via_sms(person); + targetBucket[documentIdToBeUpdated] = i; } - - log("Updating the infections doc"); - var location_doc = tnt[doc.location]; - log("Infections doc:", location_doc); - location_doc.infections = location_doc.infections + 1; - log("new infections", location_doc.infections); - tnt[doc.location] = location_doc; - -} - -function notify_via_sms(doc) { - log("Person might be infected, notifying", doc) - UPDATE `track-and-tracers`._default._default USE KEYS [$doc.id] SET notified = true; } function OnDelete(meta, options) { - log("Doc deleted/expired", meta.id); -} \ No newline at end of file +}