Guest User

Untitled

a guest
Dec 12th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.53 KB | None | 0 0
  1. var mysql = require("mysql");
  2. const uuidv4 = require("uuid/v4");
  3.  
  4. var pool = mysql.createPool({
  5. connectionLimit: 10,
  6. host: "128.199.251.92",
  7. user: "root",
  8. password: "testing",
  9. database: "dashboard_doctor"
  10. });
  11.  
  12. exports.getAllPatients = (event, context, callback) => {
  13. context.callbackWaitsForEmptyEventLoop = false;
  14. pool.query("SELECT patients.uuid AS _id, patients.name, ins_policy.policy_no, contacts.contact FROM ((patients INNER JOIN ins_policy ON patients.id = ins_policy.patient_id INNER JOIN contacts ON patients.id = contacts.patient_id ))", function (error, results, fields) {
  15. if (error) {
  16. throw error;
  17. } else {
  18. const res = results.map(result => (
  19.  
  20. {
  21. _id: result._id,
  22. name: result.name,
  23. policy_no: result.policy_no,
  24. phone_number: result.contact
  25. // dob: result.dob,
  26. // job: result.job,
  27. }
  28.  
  29. ))
  30. callback(null, res);
  31.  
  32. }
  33.  
  34. });
  35. };
  36.  
  37. exports.getPatient = (event, context, callback) => {
  38. context.callbackWaitsForEmptyEventLoop = false;
  39. let getPatients = new Promise((resolve, reject) => {
  40. pool.query(
  41. `SELECT p.uuid AS _id, name, policy_no, gender, contact, dob, weight,height, blood_type, job, food_allergy, drug_allergy, lifestyle, genetic_disorders FROM patients p
  42. LEFT JOIN
  43. ins_policy i ON p.id = i.patient_id
  44. LEFT JOIN
  45. contacts c ON p.id = c.patient_id
  46. LEFT JOIN
  47. health_general h ON p.id = h.patient_id WHERE p.uuid = '` +
  48. event.uuid +
  49. "'",
  50. (err, res, field) => {
  51. if (err) {
  52. reject(err);
  53. }
  54. resolve(res);
  55. }
  56. );
  57. });
  58.  
  59. let getSessionHistory = new Promise((resolve, reject) => {
  60. pool.query(`SELECT s.uuid as _id, json_unquote(json_extract(s.document, '$.complaint')) AS complaint, s.end_time AS timetamp from patients p LEFT JOIN sessions s ON p.id = s.patient_id WHERE p.uuid ='` + event.uuid +
  61. "'", (err, res, field) => {
  62. if (err) { reject(err) };
  63. resolve(res);
  64. });
  65. });
  66.  
  67. let getHealthHistory = new Promise((resolve, reject) => {
  68. pool.query(`SELECT h.info, h.when, h.type FROM patients p
  69. LEFT JOIN
  70. health_history h ON p.id = h.id
  71. WHERE p.uuid ='` + event.uuid +
  72. "'", (err, res, field) => {
  73. if (err) { reject(err) };
  74. resolve(res);
  75. });
  76. });
  77.  
  78. Promise.all([getPatients, getSessionHistory, getHealthHistory]).then(results => {
  79. const response = {
  80. _id: results[0][0]._id,
  81. name: results[0][0].name,
  82. policy_no: results[0][0].policy_no,
  83. gender: results[0][0].gender,
  84. phone_number: results[0][0].contact,
  85. dob: results[0][0].dob,
  86. weight: results[0][0].weight,
  87. height: results[0][0].height,
  88. blood_type: results[0][0].blood_type,
  89. job: results[0][0].job,
  90. session_history: results[1],
  91. food_allergy: results[0][0].food_allergy,
  92. drug_allergy: results[0][0].drug_allergy,
  93. lifestyle: results[0][0].lifestyle,
  94. genetic_disorders: results[0][0].genetic_disorders,
  95. health_history: results[2]
  96. }
  97.  
  98.  
  99. callback(null, response);
  100. });
  101. };
  102.  
  103. exports.getHealthHistory = (event, context, callback) => {
  104. context.callbackWaitsForEmptyEventLoop = false;
  105. pool.query(
  106. `SELECT h.info, h.when, h.type FROM patients p LEFT JOIN health_history h ON p.id = h.id WHERE p.uuid ='` +
  107. event.uuid +
  108. "'",
  109. (err, res, field) => {
  110. if (err) {
  111. throw err;
  112. }
  113.  
  114. callback(null, res);
  115. }
  116. );
  117. }
  118.  
  119. exports.updatePatient = (event, context, callback) => {
  120. context.callbackWaitsForEmptyEventLoop = false;
  121. let healthGeneral = [
  122. "weight",
  123. "height",
  124. "food_allergies",
  125. "drug_allergies",
  126. "lifestyle",
  127. "genetic_disorders"
  128. ];
  129.  
  130. let setQuery = [];
  131.  
  132. for (const key in JSON.parse(JSON.stringify(event.body))) {
  133. if (healthGeneral.includes(key)) {
  134. if (typeof event[key] == Number) {
  135. setQuery.push(`health_general.${key} = ${event.body[key]}`);
  136. } else {
  137. setQuery.push(`health_general.${key} = '${event.body[key]}'`);
  138. }
  139. }
  140. }
  141.  
  142. let updateQuery = new Promise((resolve, reject) => {
  143. pool.query(
  144. `UPDATE health_general LEFT JOIN patients p ON health_general.patient_id = p.id SET ${setQuery.join(
  145. ", "
  146. )} WHERE p.uuid = '${event.uuid}'`,
  147. (err, res, field) => {
  148. if (err) reject(err);
  149. resolve(res);
  150. }
  151. );
  152. });
  153.  
  154. let getPatients = new Promise((resolve, reject) => {
  155. pool.query(
  156. `SELECT p.uuid AS _id, name, policy_no, gender, contact, dob, weight,height, blood_type, job, food_allergy, drug_allergy, lifestyle, genetic_disorders FROM patients p
  157. LEFT JOIN
  158. ins_policy i ON p.id = i.patient_id
  159. LEFT JOIN
  160. contacts c ON p.id = c.patient_id
  161. LEFT JOIN
  162. health_general h ON p.id = h.patient_id WHERE p.uuid = '` +
  163. event.uuid +
  164. "'",
  165. (err, res, field) => {
  166. if (err) {
  167. reject(err);
  168. }
  169. resolve(res);
  170. }
  171. );
  172. });
  173.  
  174.  
  175. Promise.all([getPatients]).then(results => {
  176. const response = {
  177. _id: results[0][0]._id,
  178. name: results[0][0].name,
  179. policy_no: results[0][0].policy_no,
  180. gender: results[0][0].gender,
  181. phone_number: results[0][0].contact,
  182. dob: results[0][0].dob,
  183. weight: results[0][0].weight,
  184. height: results[0][0].height,
  185. blood_type: results[0][0].blood_type,
  186. job: results[0][0].job,
  187. food_allergy: results[0][0].food_allergy,
  188. drug_allergy: results[0][0].drug_allergy,
  189. lifestyle: results[0][0].lifestyle,
  190. genetic_disorders: results[0][0].genetic_disorders,
  191. }
  192.  
  193.  
  194. if (setQuery.length !== 0) {
  195. updateQuery.then(() => {
  196. callback(null, response);
  197. });
  198. } else {
  199. callback(null, "No update field");
  200. }
  201. });
  202.  
  203.  
  204. }
  205.  
  206. exports.addSessions = (event, context, callback) => {
  207. context.callbackWaitsForEmptyEventLoop = false;
  208. const qr = "INSERT INTO sessions SET ?";
  209. const post = {
  210. uuid: uuidv4(),
  211. patient_id: event.patient_id,
  212. doctor_id: event.doctor_id,
  213. document: JSON.stringify(event.document),
  214. chat_history: event.chat_history,
  215. start_time: event.start_time,
  216. end_time: event.end_time
  217. };
  218. pool.query(qr, post, function (error, results, fields) {
  219. if (error) {
  220. throw error;
  221. } else {
  222. callback(null, results);
  223. }
  224. });
  225. };
  226.  
  227. exports.getListSessions = (event, context, callback) => {
  228. context.callbackWaitsForEmptyEventLoop = false;
  229. pool.query("SELECT * FROM sessions WHERE uuid='" + event.uuid + "'", function (
  230. error,
  231. results,
  232. fields
  233. ) {
  234. if (error) {
  235. throw error;
  236. } else {
  237. callback(null, results);
  238. }
  239. });
  240. };
  241.  
  242. exports.getSessions = (event, context, callback) => {
  243. context.callbackWaitsForEmptyEventLoop = false;
  244. pool.query(`SELECT s.uuid AS _id, JSON_UNQUOTE(JSON_OBJECT('_id', p.uuid, 'name', name, 'policy_no', policy_no, 'npk', npk, 'gender', gender, 'phone_number', contact, 'birth', dob, 'weight', weight, 'height', height, 'blood_type', blood_type, 'job', job, 'food_allergies', food_allergy, 'drug_allergies', drug_allergy, 'lifestyle', lifestyle, 'genetic_disorders', genetic_disorders )) As patient, JSON_UNQUOTE(s.document) AS document, s.start_time AS started_at, s.end_time AS finished_at, s.chat_history AS chat_history, GROUP_CONCAT(JSON_UNQUOTE(JSON_OBJECT('_id', s.uuid, 'complaint', json_unquote(json_extract(s.document, '$.complaint')), 'timestamp', s.end_time))) AS session_history, GROUP_CONCAT(JSON_UNQUOTE(JSON_OBJECT('info',hh.info, 'when', hh.when, 'type', hh.type))) AS health_history
  245. FROM sessions s
  246. LEFT JOIN
  247. patients p ON s.patient_id = p.id
  248. LEFT JOIN
  249. ins_policy i ON s.patient_id = i.patient_id
  250. LEFT JOIN
  251. contacts c ON s.patient_id = c.patient_id
  252. LEFT JOIN
  253. health_general h ON s.patient_id = h.patient_id
  254. LEFT JOIN
  255. health_history hh ON s.patient_id = hh.patient_id
  256. WHERE p.uuid = '` + event.uuid + "'", function (error, results, fields) {
  257. if (error) {
  258. throw error;
  259. } else {
  260. var sBad = results[0].session_history
  261. var sGood = "[" + sBad.replace(/\n/g, ",") + "]";
  262. var aObjs = JSON.parse(sGood);
  263.  
  264. var hsBad = results[0].health_history
  265. var hsGood = "[" + hsBad.replace(/\n/g, ",") + "]";
  266. var hsObjs = JSON.parse(hsGood);
  267.  
  268. const response = {
  269. _id: results[0]._id,
  270. patient: JSON.parse(results[0].patient),
  271. document: JSON.parse(results[0].document),
  272. started_at: results[0].started_at,
  273. finished_at: results[0].finished_at,
  274. chat_history: results[0].chat_history,
  275. session_history: aObjs,
  276. health_history: hsObjs
  277. }
  278. callback(null, response);
  279. }
  280. });
  281. }
  282.  
  283. exports.createSession = (event, context, callback) => {
  284. context.callbackWaitsForEmptyEventLoop = false;
  285. const dr = "INSERT INTO doctors SET ?";
  286. const post = {
  287. uuid: uuidv4(),
  288. name: event.doctorName
  289. };
  290.  
  291. const getDoctor = () => new Promise((resolve, reject) => {
  292. pool.query(`SELECT doctors.name FROM doctors WHERE name = '` + event.doctorName + `'`,
  293. (err, res, field) => {
  294. if (err) {
  295. reject(err);
  296. }
  297. resolve(res);
  298. })
  299. })
  300.  
  301. const createDoctor = () => new Promise((resolve, reject) => {
  302. pool.query(`INSERT INTO doctors SET uuid = '${post.uuid}', name = '${post.name}'`, (err, res, field) => {
  303. if (err) {
  304. reject(err);
  305. }
  306. resolve(res);
  307. });
  308. });
  309.  
  310. const getPatient = () => new Promise((resolve, reject) => {
  311. pool.query(`SELECT id FROM patients WHERE uuid = '${event.uuid}'`, (err, res, field) => {
  312. if (err) {
  313. reject(err);
  314. }
  315. resolve(res)
  316. })
  317. })
  318.  
  319. const createSession = () => new Promise((resolve, reject) => {
  320. pool.query(`INSERT INTO `sessions` (`id`, `uuid`, `patient_id`, `doctor_id`, `document`, `chat_history`, `start_time`, `end_time`) VALUES (NULL, '89ujiokm,mjhygt', '4', '9', '', '', current_timestamp(), current_timestamp());`)
  321. })
  322.  
  323. // getDoctor().then(res => {
  324. // if (res.length === 0) {
  325. // createDoctor()
  326. // .then(resp => {
  327. // callback(null, resp)
  328. // })
  329. // } else {
  330. // callback(null, res)
  331. // }
  332. // })
  333. }
Add Comment
Please, Sign In to add comment