diff --git a/functions/src/controller/dashboard/index.js b/functions/src/controller/dashboard/index.js index 69bccdb4..7c85c5c5 100644 --- a/functions/src/controller/dashboard/index.js +++ b/functions/src/controller/dashboard/index.js @@ -1,16 +1,19 @@ +const { count } = require("console"); const { admin, collection } = require("../../init"); +const { statusListReverse } = require("../../api/const"); +const { success } = require("../../response/success"); exports.getAccumulative = async() => { const [g1,g2,r1,r2,y1,y2,users,unknown,legacyRef] = await Promise.all([getCount(collection.userCount,"G1"),getCount(collection.userCount,"G2"),getCount(collection.userCount,"R1"),getCount(collection.userCount,"R2"),getCount(collection.userCount,"Y1"),getCount(collection.userCount,"Y2"),getCount(collection.userCount,"users"),getCount(collection.userCount,"unknown"),getCount(collection.legacyStat,"stat")]); const temp = { - G1:g1, - G2:g2, - R1:r1, - R2:r2, - Y1:y1, - Y2:y2, - users:users, - unknown:unknown, + G1: g1, + G2: g2, + R1: r1, + R2: r2, + Y1: y1, + Y2: y2, + users: users, + unknown: unknown, legacyCount: legacyRef }; return temp; @@ -27,4 +30,49 @@ const getCount = async(collection, document) =>{ }else{ return 0; } -} \ No newline at end of file +} + +exports.resetUserCount = async() => { + const batch = admin.firestore().batch(); + const docs = ["G1","G2","R1","R2","Y1","Y2","users","unknown"]; + const data = await getPatient(); + const tempdoc = admin.firestore().collection(collection.userCount); + const temps = await Promise.all([tempdoc.doc("G1").get(),tempdoc.doc("G2").get(),tempdoc.doc("R1").get(),tempdoc.doc("R2").get(),tempdoc.doc("Y1").get(),tempdoc.doc("Y2").get(),tempdoc.doc("users").get(),tempdoc.doc("unknown").get()]); + let check = 0; + docs.forEach((doc)=>{ + const docRef = tempdoc.doc(doc); + if(temps[check].exists){ + console.log("Update " + doc); + batch.update(docRef,{count : data[doc]}); + }else{ + console.log("Create " + doc); + batch.create(docRef,{count : data[doc]}); + } + check++; + }); + await batch.commit(); + return success(); +} + +const getPatient = async() => { + const snapshot = await admin.firestore().collection(collection.patient).get(); + let counts={ + G1: 0, + G2: 0, + R1: 0, + R2: 0, + Y1: 0, + Y2: 0, + users:0, + unknown:0 + }; + snapshot.forEach((doc)=>{ + const count = doc.data(); + if(count.toAmed==0){ + counts.users += 1; + } + counts[statusListReverse[count.status]] += 1 ; + }); + console.log(counts); + return counts; +} diff --git a/functions/src/controller/exportController/index.js b/functions/src/controller/exportController/index.js index 63435e00..8ee3d45c 100644 --- a/functions/src/controller/exportController/index.js +++ b/functions/src/controller/exportController/index.js @@ -16,7 +16,6 @@ const { calculateAge, convertTZ, getDateID } = require("../../utils/date"); const utils = require("./utils"); const { success } = require("../../response/success"); - exports.exportR2R = async (data, _context) => { const { value, error } = validateExportRequestToCallSchema(data); if (error) { @@ -346,20 +345,37 @@ exports.exportRequestToCallDayOne = async (data, _context) => { exports.exportTimeSeries = async (req, res) => { try { - const snapshot = await admin.firestore().collection(collection.timeSeries).get(); + const snapshot = await admin + .firestore() + .collection(collection.timeSeries) + .get(); - const headers = ['date','active users','drop off Rate', 'r2cccount', 'terminate users','activebtw 36 to 72 hrs'] - const result = [headers] + const headers = [ + "date", + "active users", + "drop off Rate", + "r2cccount", + "terminate users", + "activebtw 36 to 72 hrs", + ]; + const result = [headers]; snapshot.forEach((doc) => { const data = doc.data(); - console.log(data) - result.push([doc.id,data.activeUser,data.dropoffrate,data.r2account,data.terminateUser,data.usersbtw36hrsto72hrs]) - }) - console.log(result) + console.log(data); + result.push([ + doc.id, + data.activeUser, + data.dropoffrate, + data.r2account, + data.terminateUser, + data.usersbtw36hrsto72hrs, + ]); + }); + console.log(result); const wb = XLSX.utils.book_new(); const ws = XLSX.utils.aoa_to_sheet(result); - XLSX.utils.book_append_sheet(wb, ws,"statistics"); + XLSX.utils.book_append_sheet(wb, ws, "statistics"); const filename = `daily_statistics.xlsx`; const opts = { bookType: "xlsx", type: "binary" }; @@ -376,7 +392,7 @@ exports.exportTimeSeries = async (req, res) => { res.setHeader("Content-Disposition", `attachment; filename="${filename}"`); stream.pipe(res); } catch (err) { - console.log(err) + console.log(err); return res.json({ success: false }); } -} \ No newline at end of file +}; diff --git a/functions/src/index.ts b/functions/src/index.ts index ae6df9cb..5fa31e85 100644 --- a/functions/src/index.ts +++ b/functions/src/index.ts @@ -116,8 +116,12 @@ exports.thisEndpointNeedsAuth = functions.region(region).https.onCall( ); exports.accumulativeData = functions -.region(region) -.https.onCall(authenticateVolunteer(dashboard.getAccumulative)); + .region(region) + .https.onCall(authenticateVolunteer(dashboard.getAccumulative)); + +exports.resetUserCount = functions + .region(region) + .https.onCall(authenticateVolunteer(dashboard.resetUserCount)); exports.backupFirestore = functions .region(region) diff --git a/functions/src/utils/zip.js b/functions/src/utils/zip.js index 5f437d82..324b9c6b 100644 --- a/functions/src/utils/zip.js +++ b/functions/src/utils/zip.js @@ -1,7 +1,7 @@ -import XLSX from "xlsx"; -import JSZip from "jszip"; +const XLSX = require("xlsx"); +const JSZip = require("jszip"); -export const makeAoA = (size) => { +const makeAoA = (size) => { const aoa = new Array(size); for (let i = 0; i < size; i++) { @@ -16,7 +16,7 @@ export const makeAoA = (size) => { * @param {any[][]} aoa * @param {any[]} data */ -export const fillWith = (aoa, data) => { +const fillWith = (aoa, data) => { const size = aoa.length; for (let i = 0; i < data.length; i++) { @@ -32,7 +32,7 @@ export const fillWith = (aoa, data) => { * @param {() => any[]} formatter * @returns */ -export const prepareZipFile = (zip, aoa, headers, formatter) => { +const prepareZipFile = (zip, aoa, headers, formatter) => { aoa.forEach((arr, i) => { const result = [[...headers]]; arr.forEach((el) => { @@ -53,12 +53,7 @@ export const prepareZipFile = (zip, aoa, headers, formatter) => { * @param {string[]} headers - array of headers * @param {() => any[]} formatter - function that return element of each row */ -export const generateZipFileRoundRobin = async ( - size, - data, - headers, - formatter -) => { +const generateZipFileRoundRobin = async (size, data, headers, formatter) => { const aoa = makeAoA(size); fillWith(aoa, data); @@ -70,3 +65,10 @@ export const generateZipFileRoundRobin = async ( const content = await zip.generateAsync({ type: "base64" }); return { ok: true, title: "report.zip", content }; }; + +module.exports = { + makeAoA, + fillWith, + prepareZipFile, + generateZipFileRoundRobin, +}; diff --git a/functions/src/utils/zip.test.ts b/functions/src/utils/zip.test.ts index f66d6799..0ef7be32 100644 --- a/functions/src/utils/zip.test.ts +++ b/functions/src/utils/zip.test.ts @@ -15,15 +15,15 @@ jest.mock("jszip", () => { }); }); +const { + makeAoA, + fillWith, + prepareZipFile, + generateZipFileRoundRobin, +} = require("./zip"); describe("zip", () => { - const { - makeAoA, - fillWith, - prepareZipFile, - generateZipFileRoundRobin, - } = require("./zip"); const JSZip = require("jszip");