kit alert function
This commit is contained in:
@@ -118,3 +118,42 @@ exports.reportDownload = functions.region("asia-south1").https.onRequest(async (
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Triggered by a change to a Firestore document.
|
||||
*
|
||||
* @param {!Object} event Event payload.
|
||||
* @param {!Object} context Metadata for the event.
|
||||
*/
|
||||
|
||||
// const functions = require("firebase-functions");
|
||||
// const admin = require("firebase-admin");
|
||||
|
||||
// admin.initializeApp();
|
||||
|
||||
exports.kitAlert = async (event, context) => {
|
||||
const resource = context.resource;
|
||||
// log out the resource string that triggered the function
|
||||
console.log('Function triggered by change to: ' + resource);
|
||||
// now log the full event object
|
||||
console.log(JSON.stringify(event));
|
||||
const { kitSerial, classificationResult } = event.value.fields;
|
||||
console.log('kitSerial', kitSerial);
|
||||
console.log('classificationResult', classificationResult);
|
||||
if (classificationResult?.stringValue?.includes("Trait")) {
|
||||
const currentKitTests = await admin.firestore().collection("testData").where("kitSerial", "==", kitSerial.stringValue).get();
|
||||
const traitCount = 0;
|
||||
console.log(currentKitTests);
|
||||
currentKitTests.forEach((x) => {
|
||||
if (x.classificationResult?.includes("Trait")) {
|
||||
traitCount++;
|
||||
}
|
||||
});
|
||||
if (traitCount > 2) {
|
||||
await admin.firestore().collection("alerts").add({
|
||||
name: 'Kit Alert',
|
||||
traitCount: traitCount,
|
||||
message: `${kitSerial.stringValue} has ${traitCount} traits`
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user