292 lines
10 KiB
JavaScript
292 lines
10 KiB
JavaScript
const { onRequest } = require("firebase-functions/v2/https");
|
|
const logger = require("firebase-functions/logger");
|
|
const functions = require("firebase-functions");
|
|
const admin = require("firebase-admin");
|
|
const moment = require('moment');
|
|
const fs = require('fs');
|
|
const csv = require('csv-parser');
|
|
const html_to_pdf = require('html-pdf-node');
|
|
|
|
admin.initializeApp();
|
|
|
|
exports.registerUser = functions.region("asia-south1").https.onRequest(async (req, res) => {
|
|
res.set('Access-Control-Allow-Origin', "*")
|
|
res.set('Access-Control-Allow-Methods', 'GET, POST');
|
|
|
|
if (req.method === "OPTIONS") {
|
|
// stop preflight requests here
|
|
res.set("Access-Control-Allow-Headers", "Content-Type, x-client");
|
|
res.status(204).send('');
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const token = "e16a1bd15af2ee640f5a7a18c8d8333f5f01f7c66e003bbc21ee52870f1bce27";
|
|
logger.info(`User-Agent ${req.get("User-Agent")}`);
|
|
logger.info(`token ${req.get("x-client")}`);
|
|
if (req.get("x-client") !== token) {
|
|
res.json({ status: "403", message: "auth error", result: "true" });
|
|
}
|
|
else {
|
|
const { body } = req;
|
|
const { _id } = body;
|
|
logger.info(`_id ${_id}`);
|
|
await admin
|
|
.firestore()
|
|
.collection("patientData")
|
|
.add(Object.assign(Object.assign({}, body), { createdAt: moment().utcOffset("+05:30").format("YYYY-MM-DD HH:mm:ss") }))
|
|
res.json({ status: "200" });
|
|
}
|
|
}
|
|
catch (err) {
|
|
logger.info(`error ${err}`);
|
|
res.json({ status: "400", message: "some error.", result: "true" });
|
|
}
|
|
});
|
|
|
|
exports.addBloodTest = functions.region("asia-south1").https.onRequest(async (req, res) => {
|
|
res.set('Access-Control-Allow-Origin', "*")
|
|
res.set('Access-Control-Allow-Methods', 'GET, POST');
|
|
|
|
if (req.method === "OPTIONS") {
|
|
// stop preflight requests here
|
|
res.set("Access-Control-Allow-Headers", "Content-Type, x-client");
|
|
res.status(204).send('');
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const token = "e16a1bd15af2ee640f5a7a18c8d8333f5f01f7c66e003bbc21ee52870f1bce27";
|
|
logger.info(`User-Agent ${req.get("User-Agent")}`);
|
|
logger.info(`token ${req.get("x-client")}`);
|
|
if (req.get("x-client") !== token) {
|
|
res.json({ status: "403", message: "auth error", result: "true" });
|
|
}
|
|
else {
|
|
const { body } = req;
|
|
const { _id } = body;
|
|
logger.info(`_id ${_id}`);
|
|
await admin
|
|
.firestore()
|
|
.collection("testData")
|
|
.add(Object.assign(Object.assign({}, body), { testTime: moment().utcOffset("+05:30").format("YYYY-MM-DD HH:mm:ss") }))
|
|
res.json({ status: "200" });
|
|
}
|
|
}
|
|
catch (err) {
|
|
logger.info(`error ${err}`);
|
|
res.json({ status: "400", message: "some error.", result: "true" });
|
|
}
|
|
});
|
|
|
|
exports.reportDownload = functions.region("asia-south1").https.onRequest(async (req, res) => {
|
|
res.set('Access-Control-Allow-Origin', "*")
|
|
res.set('Access-Control-Allow-Methods', 'GET, POST');
|
|
|
|
if (req.method === "OPTIONS") {
|
|
// stop preflight requests here
|
|
res.set("Access-Control-Allow-Headers", "Content-Type, x-client");
|
|
res.status(204).send('');
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const token = "e16a1bd15af2ee640f5a7a18c8d8333f5f01f7c66e003bbc21ee52870f1bce27";
|
|
logger.info(`User-Agent ${req.get("User-Agent")}`);
|
|
logger.info(`token ${req.get("x-client")}`);
|
|
if (req.get("x-client") !== token) {
|
|
res.json({ status: "403", message: "auth error", result: "true" });
|
|
}
|
|
else {
|
|
const { body } = req;
|
|
const { data } = body;
|
|
logger.info(`_id ${_id}`);
|
|
const fileName = `${data["_id"]}.pdf`;
|
|
// await makePdf();
|
|
res.sendFile(fileName, options, function (err) {
|
|
if (err) {
|
|
next(err);
|
|
} else {
|
|
console.log('Sent:', fileName);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
catch (err) {
|
|
logger.info(`error ${err}`);
|
|
res.json({ status: "400", message: "some error.", result: "true" });
|
|
}
|
|
});
|
|
|
|
/**
|
|
* 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");
|
|
// const moment = require('moment');
|
|
|
|
// 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();
|
|
let traitCount = 0;
|
|
currentKitTests?.docs.forEach((x) => {
|
|
if (x.data().classificationResult?.includes("Trait")) {
|
|
traitCount++;
|
|
}
|
|
});
|
|
console.log("traitCount", traitCount);
|
|
if (traitCount > 2) {
|
|
await admin.firestore().collection("alerts").add({
|
|
name: 'Kit Alert',
|
|
kit: kitSerial.stringValue,
|
|
traitCount: traitCount,
|
|
message: `${kitSerial.stringValue} has ${traitCount} traits`,
|
|
createdAt: moment().utcOffset("+05:30").format("YYYY-MM-DD HH:mm:ss")
|
|
});
|
|
}
|
|
}
|
|
};
|
|
|
|
|
|
/**
|
|
* 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");
|
|
// const moment = require('moment');
|
|
|
|
// admin.initializeApp();
|
|
|
|
exports.dataRegularity = 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, _id, deviceId, led1Buffer, led2Buffer, led1Sample, led2Sample } = event.value.fields;
|
|
console.log('_id', _id?.stringValue);
|
|
if (led1Buffer?.stringValue > 27000 || led2Buffer?.stringValue > 28000 || led1Sample?.stringValue) {
|
|
const currentKitTests = await admin.firestore().collection("testData").where("kitSerial", "==", kitSerial.stringValue).get();
|
|
let traitCount = 0;
|
|
currentKitTests?.docs.forEach((x) => {
|
|
if (x.data().classificationResult?.includes("Trait")) {
|
|
traitCount++;
|
|
}
|
|
});
|
|
await admin.firestore().collection("alerts").add({
|
|
name: 'Data Regularity',
|
|
kit: kitSerial.stringValue,
|
|
deviceId: deviceId,
|
|
message: `${_id.stringValue} has some irregularity in data`,
|
|
createdAt: moment().utcOffset("+05:30").format("YYYY-MM-DD HH:mm:ss")
|
|
});
|
|
}
|
|
};
|
|
|
|
|
|
|
|
const axios = require('axios');
|
|
|
|
exports.migrateUsers = 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 { _id, name } = event.value.fields;
|
|
const NO_OF_COPIES = 3;
|
|
for (let i = 0; i < NO_OF_COPIES; i++) {
|
|
await axios.post("https://asia-south1-hpos-preprod.cloudfunctions.net/registerUser",
|
|
{
|
|
"_id": `${_id.stringValue}${i}`,
|
|
"aadharId": "2808202300001",
|
|
"abhaId": "12434949797979",
|
|
"birthYear": "2000",
|
|
"bloodGroup": "",
|
|
"careOf": "",
|
|
"caste": "",
|
|
"city": "Bengaluru",
|
|
"district": "Bengaluru",
|
|
"gender": "Male",
|
|
"house": "42",
|
|
"maritalStatus": "Single",
|
|
"name": name.stringValue,
|
|
"phoneNumber": "9123456789",
|
|
"pinCode": "560060",
|
|
"registeredBy": "admin",
|
|
"registrationCenterName": "SMI",
|
|
"sickleCellHistory": null,
|
|
"state": "Karnataka",
|
|
"subCaste": "",
|
|
"testStatus": false,
|
|
"underMedication": false,
|
|
"underTransfusion": false,
|
|
"userImageURL": "https://hpos-prod.web.app/logo.png"
|
|
}, {
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
"x-client": "e16a1bd15af2ee640f5a7a18c8d8333f5f01f7c66e003bbc21ee52870f1bce27"
|
|
}
|
|
})
|
|
}
|
|
};
|
|
|
|
|
|
/**
|
|
* Triggered by a change to a Firestore document.
|
|
*
|
|
* @param {!Object} event Event payload.
|
|
* @param {!Object} context Metadata for the event.
|
|
*/
|
|
|
|
const admin = require("firebase-admin");
|
|
|
|
admin.initializeApp();
|
|
|
|
exports.retestMarking = 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 { _id, classificationResult } = event.value.fields;
|
|
console.log("classificationResult", classificationResult.stringValue);
|
|
if (classificationResult?.stringValue?.includes("Negative Borderline")) {
|
|
const docs = await admin.firestore().collection("patientData").where("_id", "==", _id.stringValue).get();
|
|
docs?.forEach(async (doc) => {
|
|
await admin.firestore().collection("patientData").doc(doc.id).update({
|
|
testStatus: false,
|
|
retested: true
|
|
// incubationTime: admin.firestore.FieldValue.delete()
|
|
});
|
|
})
|
|
} else if (classificationResult?.stringValue?.includes("Inconclusive")) {
|
|
const docs = await admin.firestore().collection("patientData").where("_id", "==", _id.stringValue).get();
|
|
docs?.forEach(async (doc) => {
|
|
await admin.firestore().collection("patientData").doc(doc.id).update({
|
|
testStatus: false,
|
|
retested: true
|
|
// incubationTime: admin.firestore.FieldValue.delete()
|
|
});
|
|
})
|
|
}
|
|
};
|