박권수

feat. 허브 이름 변경, 약병 이름 변경 api

...@@ -4,7 +4,6 @@ const Profile = require('../../models/profile'); ...@@ -4,7 +4,6 @@ const Profile = require('../../models/profile');
4 const DoctorInfo = require('../../models/doctorInfo'); 4 const DoctorInfo = require('../../models/doctorInfo');
5 const Hub = require('../../models/hub'); 5 const Hub = require('../../models/hub');
6 const Bottle = require('../../models/bottle'); 6 const Bottle = require('../../models/bottle');
7 -const BottleMedicine = require('../../models/bottleMedicine');
8 const PatientInfo = require('../../models/patientInfo'); 7 const PatientInfo = require('../../models/patientInfo');
9 const { uploadDoctorLicense } = require('../../util/GoogleCloudStorage'); 8 const { uploadDoctorLicense } = require('../../util/GoogleCloudStorage');
10 const Joi = require('joi'); 9 const Joi = require('joi');
......
...@@ -27,7 +27,7 @@ exports.bottleConnect = async(ctx) => { ...@@ -27,7 +27,7 @@ exports.bottleConnect = async(ctx) => {
27 return; 27 return;
28 } 28 }
29 29
30 - const { bottleId, hubId } = ctx.request.body; 30 + const { bottleId, hubId, bottleNm } = ctx.request.body;
31 31
32 const isExistBottle = await Bottle.findByBottleId(bottleId); 32 const isExistBottle = await Bottle.findByBottleId(bottleId);
33 if(isExistBottle) { 33 if(isExistBottle) {
...@@ -54,7 +54,8 @@ exports.bottleConnect = async(ctx) => { ...@@ -54,7 +54,8 @@ exports.bottleConnect = async(ctx) => {
54 54
55 const newBottle = new Bottle({ 55 const newBottle = new Bottle({
56 bottleId, 56 bottleId,
57 - hubId 57 + hubId,
58 + bottleNm,
58 }); 59 });
59 60
60 const client = await Mqtt.mqttOn(hosting); 61 const client = await Mqtt.mqttOn(hosting);
...@@ -363,6 +364,50 @@ exports.setMedicineWeight = async ctx => { ...@@ -363,6 +364,50 @@ exports.setMedicineWeight = async ctx => {
363 364
364 }; 365 };
365 366
367 +//약병 이름 변경
368 +exports.setBottleName = async ctx => {
369 + const token = ctx.req.headers.authorization;
370 + if(!token || !token.length) {
371 + ctx.status = 401;
372 + return;
373 + }
374 +
375 + // eslint-disable-next-line no-undef
376 + const { userId } = jwt.verify(token, process.env.JWT_SECRET);
377 + const user = await User.findByUserId(userId);
378 + if(!user || !user.userTypeCd || user.useYn !== 'Y') {
379 + ctx.status = 403;
380 + return;
381 + }
382 +
383 + const { bottleId } = ctx.params;
384 + const { bottleNm } = ctx.request.body;
385 +
386 + const bottle = await Bottle.findByBottleId(bottleId);
387 + if(!bottle) {
388 + ctx.status = 404;
389 + ctx.body = {
390 + error : '약병 찾을 수 없음.',
391 + }
392 + return;
393 + }
394 +
395 + const hub = await Hub.findByHubId(bottle.getHubId());
396 + if(hub.getHub_UserId() !== userId) {
397 + ctx.status = 403;
398 + ctx.body = {
399 + error : '해당 허브 권한 없음',
400 + }
401 + return;
402 + }
403 +
404 + await bottle.setBottleNm(bottleNm);
405 + await bottle.save();
406 +
407 + ctx.status = 200;
408 +
409 +};
410 +
366 // //비어있는 약병에 의사를 등록한다. 411 // //비어있는 약병에 의사를 등록한다.
367 // exports.registerDoctorToBottle = async ctx => { 412 // exports.registerDoctorToBottle = async ctx => {
368 // const token = ctx.req.headers.authorization; 413 // const token = ctx.req.headers.authorization;
......
...@@ -52,6 +52,14 @@ bottle.patch('/:bottleId', bottleCtrl.setMedicine); ...@@ -52,6 +52,14 @@ bottle.patch('/:bottleId', bottleCtrl.setMedicine);
52 bottle.patch('/weight/:bottleId', bottleCtrl.setMedicineWeight); 52 bottle.patch('/weight/:bottleId', bottleCtrl.setMedicineWeight);
53 53
54 /** 54 /**
55 + * 약병 이름 변경
56 + * request parameter : bottleid, bottleNm
57 + * url : http://localhost:4000/api/bottle/name/:bottleId
58 + * return : null
59 + */
60 + bottle.patch('/name/:bottleId', bottleCtrl.setBottleName);
61 +
62 +/**
55 * 비어있는 약병에 전담의 등록 63 * 비어있는 약병에 전담의 등록
56 * request parameter : bottleId, doctorId 64 * request parameter : bottleId, doctorId
57 * url : http://localhost:4000/api/bottle/doctor/:bottleId 65 * url : http://localhost:4000/api/bottle/doctor/:bottleId
......
...@@ -6,6 +6,7 @@ const Mqtt = require('../../util/MqttModule'); ...@@ -6,6 +6,7 @@ const Mqtt = require('../../util/MqttModule');
6 const DataProcess = require('../../util/DataProcess'); 6 const DataProcess = require('../../util/DataProcess');
7 const jwt = require('jsonwebtoken'); 7 const jwt = require('jsonwebtoken');
8 8
9 +//허브 연결
9 exports.hubConnect = async (ctx) => { 10 exports.hubConnect = async (ctx) => {
10 const token = ctx.req.headers.authorization; 11 const token = ctx.req.headers.authorization;
11 if(!token || !token.length) { 12 if(!token || !token.length) {
...@@ -21,7 +22,7 @@ exports.hubConnect = async (ctx) => { ...@@ -21,7 +22,7 @@ exports.hubConnect = async (ctx) => {
21 return; 22 return;
22 } 23 }
23 24
24 - const { hubId, host } = ctx.request.body; 25 + const { hubId, host, hubNm, } = ctx.request.body;
25 26
26 const isExistHub = await Hub.findByHubId(hubId); 27 const isExistHub = await Hub.findByHubId(hubId);
27 if(isExistHub) { 28 if(isExistHub) {
...@@ -39,7 +40,8 @@ exports.hubConnect = async (ctx) => { ...@@ -39,7 +40,8 @@ exports.hubConnect = async (ctx) => {
39 const hub = new Hub({ 40 const hub = new Hub({
40 hubId, 41 hubId,
41 hosting, 42 hosting,
42 - userId 43 + userId,
44 + hubNm,
43 }); 45 });
44 46
45 await hub.save(); 47 await hub.save();
...@@ -48,6 +50,44 @@ exports.hubConnect = async (ctx) => { ...@@ -48,6 +50,44 @@ exports.hubConnect = async (ctx) => {
48 50
49 }; 51 };
50 52
53 +//허브 연결 해제
54 +exports.hubDisconnect = async(ctx) => {
55 + const token = ctx.req.headers.authorization;
56 + if(!token || !token.length) {
57 + ctx.status = 401;
58 + return;
59 + }
60 +
61 + // eslint-disable-next-line no-undef
62 + const { userId } = jwt.verify(token, process.env.JWT_SECRET);
63 + const user = await User.findByUserId(userId);
64 + if(!user || !user.userTypeCd || user.useYn !== 'Y') {
65 + ctx.status = 403;
66 + return;
67 + }
68 +
69 + const { hubId } = ctx.params;
70 +
71 + const hub = await Hub.findByHubId(hubId);
72 + if(!hub) {
73 + ctx.status = 404;
74 + return;
75 + }
76 + if(hub.getHub_UserId() !== userId) {
77 + ctx.status = 403;
78 + return;
79 + }
80 +
81 + const hosting = await hub.getHubHost();
82 + Mqtt.mqttOff(hosting);
83 +
84 + await Bottle.deleteMany({ hubId });
85 + await Hub.deleteOne({ hubId });
86 +
87 + ctx.status = 204;
88 +};
89 +
90 +//허브 정보 조회
51 exports.getHubList = async(ctx) => { 91 exports.getHubList = async(ctx) => {
52 const token = ctx.req.headers.authorization; 92 const token = ctx.req.headers.authorization;
53 if(!token || !token.length) { 93 if(!token || !token.length) {
...@@ -75,7 +115,8 @@ exports.getHubList = async(ctx) => { ...@@ -75,7 +115,8 @@ exports.getHubList = async(ctx) => {
75 }; 115 };
76 }; 116 };
77 117
78 -exports.hubDisconnect = async(ctx) => { 118 +//허브 이름 변경
119 +exports.setHubName = async ctx => {
79 const token = ctx.req.headers.authorization; 120 const token = ctx.req.headers.authorization;
80 if(!token || !token.length) { 121 if(!token || !token.length) {
81 ctx.status = 401; 122 ctx.status = 401;
...@@ -91,22 +132,9 @@ exports.hubDisconnect = async(ctx) => { ...@@ -91,22 +132,9 @@ exports.hubDisconnect = async(ctx) => {
91 } 132 }
92 133
93 const { hubId } = ctx.params; 134 const { hubId } = ctx.params;
135 + const { hubNm } = ctx.request.body;
94 136
95 - const hub = await Hub.findByHubId(hubId); 137 + await Hub.updateOne({ hubId }, { hubNm });
96 - if(!hub) {
97 - ctx.status = 404;
98 - return;
99 - }
100 - if(hub.getHub_UserId() !== userId) {
101 - ctx.status = 403;
102 - return;
103 - }
104 138
105 - const hosting = await hub.getHubHost(); 139 + ctx.status = 200;
106 - Mqtt.mqttOff(hosting);
107 -
108 - await Bottle.deleteMany({ hubId });
109 - await Hub.deleteOne({ hubId });
110 -
111 - ctx.status = 204;
112 }; 140 };
......
...@@ -12,6 +12,14 @@ const hub = new Router(); ...@@ -12,6 +12,14 @@ const hub = new Router();
12 hub.post('/', hubCtrl.hubConnect); 12 hub.post('/', hubCtrl.hubConnect);
13 13
14 /** 14 /**
15 + * 허브 등록 해제
16 + * request parameter : x
17 + * url : http://localhost:4000/api/hub/:hubId
18 + * return : null
19 + */
20 + hub.delete('/:hubId', hubCtrl.hubDisconnect);
21 +
22 +/**
15 * 로그인한 유저의 허브 목록 가져오기 23 * 로그인한 유저의 허브 목록 가져오기
16 * request parameter : X 24 * request parameter : X
17 * url : http://localhost:4000/api/hub 25 * url : http://localhost:4000/api/hub
...@@ -20,11 +28,13 @@ hub.post('/', hubCtrl.hubConnect); ...@@ -20,11 +28,13 @@ hub.post('/', hubCtrl.hubConnect);
20 hub.get('/', hubCtrl.getHubList); 28 hub.get('/', hubCtrl.getHubList);
21 29
22 /** 30 /**
23 - * 허브 등록 해제 31 + * 로그인한 유저의 특정 허브 이름 변경
24 - * request parameter : x 32 + * request parameter : hubId, hubNm
25 * url : http://localhost:4000/api/hub/:hubId 33 * url : http://localhost:4000/api/hub/:hubId
26 * return : null 34 * return : null
27 */ 35 */
28 -hub.delete('/:hubId', hubCtrl.hubDisconnect); 36 +hub.patch('/:hubId', hubCtrl.setHubName);
37 +
38 +
29 39
30 module.exports = hub; 40 module.exports = hub;
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -5,6 +5,7 @@ const Schema = mongoose.Schema; ...@@ -5,6 +5,7 @@ const Schema = mongoose.Schema;
5 const BottleSchema = new Schema ({ 5 const BottleSchema = new Schema ({
6 bottleId : { type : Number, required : true, unique : true }, 6 bottleId : { type : Number, required : true, unique : true },
7 hubId : { type : Number, required : true, ref : 'Hub', }, 7 hubId : { type : Number, required : true, ref : 'Hub', },
8 + bottleNm : { type : String, required : true, maxlength : 10, },
8 }); 9 });
9 10
10 BottleSchema.statics.findByBottleId = function(bottleId) { 11 BottleSchema.statics.findByBottleId = function(bottleId) {
...@@ -23,5 +24,9 @@ BottleSchema.methods.getHubId = function() { ...@@ -23,5 +24,9 @@ BottleSchema.methods.getHubId = function() {
23 return this.hubId; 24 return this.hubId;
24 }; 25 };
25 26
27 +BottleSchema.methods.setBottleNm = function(bottleNm) {
28 + this.bottleNm = bottleNm;
29 +}
30 +
26 31
27 module.exports = mongoose.model('Bottle', BottleSchema); 32 module.exports = mongoose.model('Bottle', BottleSchema);
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -6,6 +6,7 @@ const HubSchema = new Schema ({ ...@@ -6,6 +6,7 @@ const HubSchema = new Schema ({
6 hubId : { type : Number, required : true, unique : true }, 6 hubId : { type : Number, required : true, unique : true },
7 hosting : { type : Object, default : null }, 7 hosting : { type : Object, default : null },
8 userId : { type : String, default : null, ref : 'User', lowercase : true, }, 8 userId : { type : String, default : null, ref : 'User', lowercase : true, },
9 + hubNm : { type : String, required : true, maxlength : 10, },
9 }); 10 });
10 11
11 HubSchema.statics.findByHubId = function(hubId) { 12 HubSchema.statics.findByHubId = function(hubId) {
......