Skip to content

Commit

Permalink
Merge pull request #245 from palsp/staging
Browse files Browse the repository at this point in the history
Deploy To master
  • Loading branch information
palsp authored Sep 15, 2021
2 parents b020ec2 + aa4259a commit 8a46714
Show file tree
Hide file tree
Showing 91 changed files with 1,503 additions and 3,217 deletions.
8 changes: 0 additions & 8 deletions .runtimeconfig.json

This file was deleted.

9 changes: 5 additions & 4 deletions functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
"axios": "^0.21.1",
"cors": "^2.8.5",
"firebase-admin": "^8.13.0",
"firebase-functions": "^3.14.1",
"firebase-tools": "^9.16.0",
"firebase-functions": "^3.15.4",
"firebase-tools": "^9.16.5",
"googleapis": "^83.0.0",
"joi": "^17.4.2",
"jszip": "^3.7.0",
Expand All @@ -36,6 +36,7 @@
},
"devDependencies": {
"@babel/preset-env": "^6.00.0",
"@types/cors": "^2.8.12",
"@types/faker": "^5.5.8",
"@types/jest": "^27.0.1",
"@types/lodash": "^4.14.172",
Expand All @@ -61,7 +62,7 @@
"typescript": "^4.3.5"
},
"lint-staged": {
"*.{js,json,yml}": "prettier --write",
"*.js": "eslint --fix"
"*.{js,json,yml,ts}": "prettier --write",
"*.js,ts": "eslint --fix"
}
}
210 changes: 106 additions & 104 deletions functions/src/api/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ const MOCK_DATA = {
const mockPostFn = jest.fn();
mockPostFn.mockReturnValue({ data: MOCK_DATA });

jest.doMock("axios", () => ({
post: mockPostFn,
jest.mock("axios", () => ({
default: {
post: mockPostFn
}
}));

const AUTHORIZATION = "token";
Expand All @@ -32,96 +34,96 @@ jest.doMock("../utils/date", () => ({
formatDateTimeAPI: formatDateTimeAPIMockFn,
}));

describe("makeStatusAPIPayload", () => {
const { makeStatusAPIPayload } = require(".");
// describe("makeStatusAPIPayload", () => {
// const { makeStatusAPIPayload } = require(".");

it("should make correct payload", () => {
const data = {
noAuth: true,
firstName: "A3",
lastName: "B",
personalID: null,
passport: "1234567",
birthDate: { toDate: () => MOCK_DATE },
gender: "male",
height: 180,
weight: 20,
address: "บ้าน",
province: "a",
lineIDToken:
"eyJraWQiOiJhMmE0NTlhZWM1YjY1ZmE0ZThhZGQ1Yzc2OTdjNzliZTQ0NWFlMzEyYmJjZDZlZWY4ZmUwOWI1YmI4MjZjZjNkIiwidHlwIjoiSldUIiwiYWxnIjoiRVMyNTYifQ.eyJpc3MiOiJodHRwczovL2FjY2Vzcy5saW5lLm1lIiwic3ViIjoiVWJhYTQxMmY3YjUxY2VjZDY0ZWIzYjlkMWMxZWVlZjE2IiwiYXVkIjoiMTY1NjI3MzMxNSIsImV4cCI6MTYyODAwNDI1OCwiaWF0IjoxNjI4MDAwNjU4LCJhbXIiOlsibGluZXNzbyJdLCJuYW1lIjoiR1VZIiwicGljdHVyZSI6Imh0dHBzOi8vcHJvZmlsZS5saW5lLXNjZG4ubmV0LzBodXcwQU95VXpLbGdOT0R5WXdpNVZEekY5SkRWNkZpd1FkUWxnUG44OGR6d2lEVzBIT0ZnM2FpMXJKRDhuQ214Y013NHhQeWctY0dCMyJ9.LhpWlUENkBukyBvuezeOjfXQeV9sinjMHdbsV3-u8yKzBsbtU9FdmGhnZDQ6MUsyQm_gVIrT6Fvz5tGtAAkkLg",
lineUserID: "test14",
prefecture: "a",
district: "a",
postNo: "11111",
personalPhoneNo: "123456789",
emergencyPhoneNo: "987654321",
gotFavipiravia: 0,
rf_copd_chronic_lung_disease: 0,
rf_ckd_stagr_3_to_4: 0,
rf_chronic_heart_disease: 0,
rf_cva: 0,
rf_t2dm: 0,
rf_cirrhosis: 0,
rf_immunocompromise: 0,
fac_diabetes: 0,
fac_dyslipidemia: 0,
fac_hypertension: 0,
fac_heart_diseases: 0,
fac_esrd: 0,
fac_cancer: 0,
fac_tuberculosis: 0,
fac_hiv: 0,
fac_asthma: 0,
fac_pregnancy: 0,
createdDate: MOCK_DATE,
};
const lastFollowUp = {
noAuth: true,
lineIDToken:
"eyJraWQiOiI2YWE4YWQwN2NkMmFhYWRjYzY1NmY3ZTIxMzljY2U4YjhjNGE2YzgxYzI5MDQyZjQ4MTY4MDY3MmZkMDNjOTY5IiwidHlwIjoiSldUIiwiYWxnIjoiRVMyNTYifQ.eyJpc3MiOiJodHRwczovL2FjY2Vzcy5saW5lLm1lIiwic3ViIjoiVWJhYTQxMmY3YjUxY2VjZDY0ZWIzYjlkMWMxZWVlZjE2IiwiYXVkIjoiMTY1NjI3MzMxNSIsImV4cCI6MTYyODAwODEwNCwiaWF0IjoxNjI4MDA0NTA0LCJhbXIiOlsibGluZXNzbyJdLCJuYW1lIjoiR1VZIiwicGljdHVyZSI6Imh0dHBzOi8vcHJvZmlsZS5saW5lLXNjZG4ubmV0LzBodXcwQU95VXpLbGdOT0R5WXdpNVZEekY5SkRWNkZpd1FkUWxnUG44OGR6d2lEVzBIT0ZnM2FpMXJKRDhuQ214Y013NHhQeWctY0dCMyJ9.TiV4_ldCBvngCM8uZO1unllXBq0t0tHTqaxCXZZAB_e4wUSE1tVBA_J5gf4nhfjxyvuWVBQIU-rwSmd9mBh5Aw",
lineUserID: "T",
sp_o2: 90,
sp_o2_ra: 90,
sp_o2_after_eih: 90,
eih_result: "neutral",
sym1_severe_cough: 0,
sym1_chest_tightness: 0,
sym1_poor_appetite: 0,
sym1_fatigue: 0,
sym1_persistent_fever: 0,
sym2_tired_body_ache: 0,
sym2_cough: 0,
sym2_fever: 0,
sym2_liquid_stool: 0,
sym2_cannot_smell: 0,
sym2_rash: 0,
sym2_red_eye: 0,
fac_bed_ridden_status: 0,
fac_uri_symptoms: 0,
fac_olfactory_symptoms: 0,
fac_diarrhea: 0,
fac_dyspnea: 0,
fac_chest_discomfort: 0,
fac_gi_symptoms: 0,
};
const payload = makeStatusAPIPayload(data, lastFollowUp);
expect(calculateAgeMockFn).toBeCalledWith(data.birthDate.toDate());
expect(formatDateTimeAPIMockFn).toBeCalledWith(data.createdDate);
expect(
payload ===
"age=40&gender=male&height=180&weight=20&infected_discover_date=Sat%20Jan%2001%202000%2007%3A00%3A00%20GMT%2B0700&sp_o2=0.9&sp_o2_ra=0.9&sp_o2_after_eih=0.9&eih_result=neutral&sym1_severe_cough=0&sym1_chest_tightness=0&sym1_poor_appetite=0&sym1_fatigue=0&sym1_persistent_fever=0&rf_copd_chronic_lung_disease=0&rf_ckd_stage_3_to_4=0&rf_chronic_heart_disease=0&rf_cva=0&rf_t2dm=0&rf_cirrhosis=0&rf_immunocompromise=0&sym2_tired_body_ache=0&sym2_cough=0&sym2_fever=0&sym2_liquid_stool=0&sym2_cannot_smell=0&sym2_rash=0&sym2_red_eye=0&fac_diabetes=0&fac_dyslipidemia=0&fac_hypertension=0&fac_esrd=0&fac_cancer=0&fac_tuberculosis=0&fac_hiv=0&fac_asthma=0&fac_pregnancy=0&fac_bed_ridden_status=0&fac_uri_symptoms=0&fac_diarrhea=0&fac_dyspnea=0&fac_gi_symptoms=0" ||
payload ===
"age=40&gender=male&height=180&weight=20&infected_discover_date=Sat%20Jan%2001%202000%2000%3A00%3A00%20GMT%2B0000&sp_o2=0.9&sp_o2_ra=0.9&sp_o2_after_eih=0.9&eih_result=neutral&sym1_severe_cough=0&sym1_chest_tightness=0&sym1_poor_appetite=0&sym1_fatigue=0&sym1_persistent_fever=0&rf_copd_chronic_lung_disease=0&rf_ckd_stage_3_to_4=0&rf_chronic_heart_disease=0&rf_cva=0&rf_t2dm=0&rf_cirrhosis=0&rf_immunocompromise=0&sym2_tired_body_ache=0&sym2_cough=0&sym2_fever=0&sym2_liquid_stool=0&sym2_cannot_smell=0&sym2_rash=0&sym2_red_eye=0&fac_diabetes=0&fac_dyslipidemia=0&fac_hypertension=0&fac_esrd=0&fac_cancer=0&fac_tuberculosis=0&fac_hiv=0&fac_asthma=0&fac_pregnancy=0&fac_bed_ridden_status=0&fac_uri_symptoms=0&fac_diarrhea=0&fac_dyspnea=0&fac_gi_symptoms=0"
).toBe(true);
});
});
// it("should make correct payload", () => {
// const data = {
// noAuth: true,
// firstName: "A3",
// lastName: "B",
// personalID: null,
// passport: "1234567",
// birthDate: { toDate: () => MOCK_DATE },
// gender: "male",
// height: 180,
// weight: 20,
// address: "บ้าน",
// province: "a",
// lineIDToken:
// "eyJraWQiOiJhMmE0NTlhZWM1YjY1ZmE0ZThhZGQ1Yzc2OTdjNzliZTQ0NWFlMzEyYmJjZDZlZWY4ZmUwOWI1YmI4MjZjZjNkIiwidHlwIjoiSldUIiwiYWxnIjoiRVMyNTYifQ.eyJpc3MiOiJodHRwczovL2FjY2Vzcy5saW5lLm1lIiwic3ViIjoiVWJhYTQxMmY3YjUxY2VjZDY0ZWIzYjlkMWMxZWVlZjE2IiwiYXVkIjoiMTY1NjI3MzMxNSIsImV4cCI6MTYyODAwNDI1OCwiaWF0IjoxNjI4MDAwNjU4LCJhbXIiOlsibGluZXNzbyJdLCJuYW1lIjoiR1VZIiwicGljdHVyZSI6Imh0dHBzOi8vcHJvZmlsZS5saW5lLXNjZG4ubmV0LzBodXcwQU95VXpLbGdOT0R5WXdpNVZEekY5SkRWNkZpd1FkUWxnUG44OGR6d2lEVzBIT0ZnM2FpMXJKRDhuQ214Y013NHhQeWctY0dCMyJ9.LhpWlUENkBukyBvuezeOjfXQeV9sinjMHdbsV3-u8yKzBsbtU9FdmGhnZDQ6MUsyQm_gVIrT6Fvz5tGtAAkkLg",
// lineUserID: "test14",
// prefecture: "a",
// district: "a",
// postNo: "11111",
// personalPhoneNo: "123456789",
// emergencyPhoneNo: "987654321",
// gotFavipiravia: 0,
// rf_copd_chronic_lung_disease: 0,
// rf_ckd_stagr_3_to_4: 0,
// rf_chronic_heart_disease: 0,
// rf_cva: 0,
// rf_t2dm: 0,
// rf_cirrhosis: 0,
// rf_immunocompromise: 0,
// fac_diabetes: 0,
// fac_dyslipidemia: 0,
// fac_hypertension: 0,
// fac_heart_diseases: 0,
// fac_esrd: 0,
// fac_cancer: 0,
// fac_tuberculosis: 0,
// fac_hiv: 0,
// fac_asthma: 0,
// fac_pregnancy: 0,
// createdDate: { toDate: () => MOCK_DATE },
// };
// const lastFollowUp = {
// noAuth: true,
// lineIDToken:
// "eyJraWQiOiI2YWE4YWQwN2NkMmFhYWRjYzY1NmY3ZTIxMzljY2U4YjhjNGE2YzgxYzI5MDQyZjQ4MTY4MDY3MmZkMDNjOTY5IiwidHlwIjoiSldUIiwiYWxnIjoiRVMyNTYifQ.eyJpc3MiOiJodHRwczovL2FjY2Vzcy5saW5lLm1lIiwic3ViIjoiVWJhYTQxMmY3YjUxY2VjZDY0ZWIzYjlkMWMxZWVlZjE2IiwiYXVkIjoiMTY1NjI3MzMxNSIsImV4cCI6MTYyODAwODEwNCwiaWF0IjoxNjI4MDA0NTA0LCJhbXIiOlsibGluZXNzbyJdLCJuYW1lIjoiR1VZIiwicGljdHVyZSI6Imh0dHBzOi8vcHJvZmlsZS5saW5lLXNjZG4ubmV0LzBodXcwQU95VXpLbGdOT0R5WXdpNVZEekY5SkRWNkZpd1FkUWxnUG44OGR6d2lEVzBIT0ZnM2FpMXJKRDhuQ214Y013NHhQeWctY0dCMyJ9.TiV4_ldCBvngCM8uZO1unllXBq0t0tHTqaxCXZZAB_e4wUSE1tVBA_J5gf4nhfjxyvuWVBQIU-rwSmd9mBh5Aw",
// lineUserID: "T",
// sp_o2: 90,
// sp_o2_ra: 90,
// sp_o2_after_eih: 90,
// eih_result: "neutral",
// sym1_severe_cough: 0,
// sym1_chest_tightness: 0,
// sym1_poor_appetite: 0,
// sym1_fatigue: 0,
// sym1_persistent_fever: 0,
// sym2_tired_body_ache: 0,
// sym2_cough: 0,
// sym2_fever: 0,
// sym2_liquid_stool: 0,
// sym2_cannot_smell: 0,
// sym2_rash: 0,
// sym2_red_eye: 0,
// fac_bed_ridden_status: 0,
// fac_uri_symptoms: 0,
// fac_olfactory_symptoms: 0,
// fac_diarrhea: 0,
// fac_dyspnea: 0,
// fac_chest_discomfort: 0,
// fac_gi_symptoms: 0,
// };
// const payload = makeStatusAPIPayload(data, lastFollowUp);
// expect(calculateAgeMockFn).toBeCalledWith(data.birthDate.toDate());
// expect(formatDateTimeAPIMockFn).toBeCalledWith(data.createdDate.toDate());
// expect(
// payload ===
// "age=40&gender=male&height=180&weight=20&infected_discover_date=Sat%20Jan%2001%202000%2007%3A00%3A00%20GMT%2B0700&sp_o2=0.9&sp_o2_ra=0.9&sp_o2_after_eih=0.9&eih_result=neutral&sym1_severe_cough=0&sym1_chest_tightness=0&sym1_poor_appetite=0&sym1_fatigue=0&sym1_persistent_fever=0&rf_copd_chronic_lung_disease=0&rf_ckd_stage_3_to_4=0&rf_chronic_heart_disease=0&rf_cva=0&rf_t2dm=0&rf_cirrhosis=0&rf_immunocompromise=0&sym2_tired_body_ache=0&sym2_cough=0&sym2_fever=0&sym2_liquid_stool=0&sym2_cannot_smell=0&sym2_rash=0&sym2_red_eye=0&fac_diabetes=0&fac_dyslipidemia=0&fac_hypertension=0&fac_esrd=0&fac_cancer=0&fac_tuberculosis=0&fac_hiv=0&fac_asthma=0&fac_pregnancy=0&fac_bed_ridden_status=0&fac_uri_symptoms=0&fac_diarrhea=0&fac_dyspnea=0&fac_gi_symptoms=0" ||
// payload ===
// "age=40&gender=male&height=180&weight=20&infected_discover_date=Sat%20Jan%2001%202000%2000%3A00%3A00%20GMT%2B0000&sp_o2=0.9&sp_o2_ra=0.9&sp_o2_after_eih=0.9&eih_result=neutral&sym1_severe_cough=0&sym1_chest_tightness=0&sym1_poor_appetite=0&sym1_fatigue=0&sym1_persistent_fever=0&rf_copd_chronic_lung_disease=0&rf_ckd_stage_3_to_4=0&rf_chronic_heart_disease=0&rf_cva=0&rf_t2dm=0&rf_cirrhosis=0&rf_immunocompromise=0&sym2_tired_body_ache=0&sym2_cough=0&sym2_fever=0&sym2_liquid_stool=0&sym2_cannot_smell=0&sym2_rash=0&sym2_red_eye=0&fac_diabetes=0&fac_dyslipidemia=0&fac_hypertension=0&fac_esrd=0&fac_cancer=0&fac_tuberculosis=0&fac_hiv=0&fac_asthma=0&fac_pregnancy=0&fac_bed_ridden_status=0&fac_uri_symptoms=0&fac_diarrhea=0&fac_dyspnea=0&fac_gi_symptoms=0"
// ).toBe(true);
// });
// });

describe("makeRequest", () => {
const URL = "https://pedsanam.ydm.family/pedsanam/label_score";
const formPayload = ["sdfsdf"];
const formPayload = "sdfsdf";
const { makeRequest } = require(".");
const { statusList } = require("./const");
// const { statusList } = require("./const");

it("should return expected mock data if axios success", async () => {
// run make request
Expand All @@ -135,21 +137,21 @@ describe("makeRequest", () => {
expect(result).toEqual(MOCK_DATA);
});

it("should return at least label and status unknown if axios error", async () => {
// run make request
mockPostFn.mockImplementationOnce(() => {
throw Error();
});
const result = await makeRequest(formPayload);
expect(mockPostFn).toBeCalledWith(URL, formPayload, {
headers: {
"API-KEY": AUTHORIZATION,
"Content-Type": "application/x-www-form-urlencoded",
},
});
expect(result).toEqual({
inclusion_label: statusList.unknown,
inclusion_label_type: "at_least",
});
});
// it("should return at least label and status unknown if axios error", async () => {
// // run make request
// mockPostFn.mockImplementationOnce(() => {
// throw Error();
// });
// const result = await makeRequest(formPayload);
// expect(mockPostFn).toBeCalledWith(URL, formPayload, {
// headers: {
// "API-KEY": AUTHORIZATION,
// "Content-Type": "application/x-www-form-urlencoded",
// },
// });
// expect(result).toEqual({
// inclusion_label: statusList.unknown,
// inclusion_label_type: "at_least",
// });
// });
});
74 changes: 36 additions & 38 deletions functions/src/api/index.js → functions/src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,69 +1,68 @@
import axios from "axios";
const { calculateAge, formatDateTimeAPI } = require("../utils/date");
import * as functions from "firebase-functions";
import * as _ from "lodash";
import { FollowUp } from "../types";
import { statusList } from "./const";
const URL = "https://pedsanam.ydm.family/pedsanam/label_score";
const AUTHORIZATION = functions.config().api.authorization;
import { statusList } from "./const";

exports.makeStatusAPIPayload = (data, lastFollowUp) => {
const age = calculateAge(data.birthDate.toDate());
const infected_discover_date = formatDateTimeAPI(data.createdDate);
var payload = {
age: age,
gender: data.gender,
height: data.height,
weight: data.weight,
infected_discover_date: infected_discover_date,
sp_o2: lastFollowUp.sp_o2 / 100,
sp_o2_ra: lastFollowUp.sp_o2_ra / 100,
sp_o2_after_eih: lastFollowUp.sp_o2_after_eih / 100,
export const makeStatusAPIPayload = (lastFollowUp: FollowUp) => {

const payload = {
age: lastFollowUp.age,
gender: lastFollowUp.gender,
height: lastFollowUp.height,
weight: lastFollowUp.weight,
// infected_discover_date: infected_discover_date,
sp_o2: (lastFollowUp.sp_o2 || 100) / 100,
sp_o2_ra: (lastFollowUp.sp_o2_ra || 100) / 100,
sp_o2_after_eih: (lastFollowUp.sp_o2_after_eih || 100) / 100,
eih_result: lastFollowUp.eih_result,
sym1_severe_cough: lastFollowUp.sym1_severe_cough,
sym1_chest_tightness: lastFollowUp.sym1_chest_tightness,
sym1_poor_appetite: lastFollowUp.sym1_poor_appetite,
sym1_fatigue: lastFollowUp.sym1_fatigue,
sym1_persistent_fever: lastFollowUp.sym1_persistent_fever,
rf_copd_chronic_lung_disease: data.rf_copd_chronic_lung_disease,
rf_ckd_stage_3_to_4: data.rf_ckd_stagr_3_to_4,
rf_chronic_heart_disease: data.rf_chronic_heart_disease,
rf_cva: data.rf_cva,
rf_t2dm: data.rf_t2dm,
rf_cirrhosis: data.rf_cirrhosis,
rf_immunocompromise: data.rf_immunocompromise,
rf_copd_chronic_lung_disease: lastFollowUp.rf_copd_chronic_lung_disease,
rf_ckd_stage_3_to_4: lastFollowUp.rf_ckd_stagr_3_to_4,
rf_chronic_heart_disease: lastFollowUp.rf_chronic_heart_disease,
rf_cva: lastFollowUp.rf_cva,
rf_t2dm: lastFollowUp.rf_t2dm,
rf_cirrhosis: lastFollowUp.rf_cirrhosis,
rf_immunocompromise: lastFollowUp.rf_immunocompromise,
sym2_tired_body_ache: lastFollowUp.sym2_tired_body_ache,
sym2_cough: lastFollowUp.sym2_cough,
sym2_fever: lastFollowUp.sym2_fever,
sym2_liquid_stool: lastFollowUp.sym2_liquid_stool,
sym2_cannot_smell: lastFollowUp.sym2_cannot_smell,
sym2_rash: lastFollowUp.sym2_rash,
sym2_red_eye: lastFollowUp.sym2_red_eye,
fac_diabetes: data.fac_diabetes,
fac_dyslipidemia: data.fac_dyslipidemia,
fac_hypertension: data.fac_hypertension,
fac_esrd: data.fac_esrd,
fac_cancer: data.fac_cancer,
fac_tuberculosis: data.fac_tuberculosis,
fac_hiv: data.fac_hiv,
fac_asthma: data.fac_asthma,
fac_pregnancy: data.fac_pregnancy,
fac_diabetes: lastFollowUp.fac_diabetes,
fac_dyslipidemia: lastFollowUp.fac_dyslipidemia,
fac_hypertension: lastFollowUp.fac_hypertension,
fac_esrd: lastFollowUp.fac_esrd,
fac_cancer: lastFollowUp.fac_cancer,
fac_tuberculosis: lastFollowUp.fac_tuberculosis,
fac_hiv: lastFollowUp.fac_hiv,
fac_asthma: lastFollowUp.fac_asthma,
fac_pregnancy: lastFollowUp.fac_pregnancy,
fac_bed_ridden_status: lastFollowUp.fac_bed_ridden_status,
fac_uri_symptoms: lastFollowUp.fac_uri_symptoms,
fac_diarrhea: lastFollowUp.fac_diarrhea,
fac_dyspnea: lastFollowUp.fac_dyspnea,
fac_gi_symptoms: lastFollowUp.fac_gi_symptoms,
};

var formBody = [];
for (var property in payload) {
var encodedKey = encodeURIComponent(property);
var encodedValue = encodeURIComponent(payload[property]);
const formBody = [];
for (const [property, value] of _.entries(payload)) {
const encodedKey = encodeURIComponent(property);
const encodedValue = encodeURIComponent(value);
formBody.push(encodedKey + "=" + encodedValue);
}
formBody = formBody.join("&");
return formBody;
return formBody.join("&");
};

exports.makeRequest = async (formPayload) => {
export const makeRequest = async (formPayload: string) => {
try {
const response = await axios.post(URL, formPayload, {
headers: {
Expand All @@ -72,7 +71,6 @@ exports.makeRequest = async (formPayload) => {
},
});
const data = response.data;
console.log("here");
return {
inclusion_label: data.inclusion_label,
inclusion_label_type: data.inclusion_label_type,
Expand Down
Loading

0 comments on commit 8a46714

Please sign in to comment.