박권수

Merge branch 'server' into web

......@@ -4,7 +4,6 @@ const Profile = require('../../models/profile');
const DoctorInfo = require('../../models/doctorInfo');
const Hub = require('../../models/hub');
const Bottle = require('../../models/bottle');
const BottleMedicine = require('../../models/bottleMedicine');
const PatientInfo = require('../../models/patientInfo');
const { uploadDoctorLicense } = require('../../util/GoogleCloudStorage');
const Joi = require('joi');
......
......@@ -27,7 +27,7 @@ exports.bottleConnect = async(ctx) => {
return;
}
const { bottleId, hubId } = ctx.request.body;
const { bottleId, hubId, bottleNm } = ctx.request.body;
const isExistBottle = await Bottle.findByBottleId(bottleId);
if(isExistBottle) {
......@@ -54,7 +54,8 @@ exports.bottleConnect = async(ctx) => {
const newBottle = new Bottle({
bottleId,
hubId
hubId,
bottleNm,
});
const client = await Mqtt.mqttOn(hosting);
......@@ -363,6 +364,50 @@ exports.setMedicineWeight = async ctx => {
};
//약병 이름 변경
exports.setBottleName = async ctx => {
const token = ctx.req.headers.authorization;
if(!token || !token.length) {
ctx.status = 401;
return;
}
// eslint-disable-next-line no-undef
const { userId } = jwt.verify(token, process.env.JWT_SECRET);
const user = await User.findByUserId(userId);
if(!user || !user.userTypeCd || user.useYn !== 'Y') {
ctx.status = 403;
return;
}
const { bottleId } = ctx.params;
const { bottleNm } = ctx.request.body;
const bottle = await Bottle.findByBottleId(bottleId);
if(!bottle) {
ctx.status = 404;
ctx.body = {
error : '약병 찾을 수 없음.',
}
return;
}
const hub = await Hub.findByHubId(bottle.getHubId());
if(hub.getHub_UserId() !== userId) {
ctx.status = 403;
ctx.body = {
error : '해당 허브 권한 없음',
}
return;
}
await bottle.setBottleNm(bottleNm);
await bottle.save();
ctx.status = 200;
};
// //비어있는 약병에 의사를 등록한다.
// exports.registerDoctorToBottle = async ctx => {
// const token = ctx.req.headers.authorization;
......
......@@ -52,6 +52,14 @@ bottle.patch('/:bottleId', bottleCtrl.setMedicine);
bottle.patch('/weight/:bottleId', bottleCtrl.setMedicineWeight);
/**
* 약병 이름 변경
* request parameter : bottleid, bottleNm
* url : http://localhost:4000/api/bottle/name/:bottleId
* return : null
*/
bottle.patch('/name/:bottleId', bottleCtrl.setBottleName);
/**
* 비어있는 약병에 전담의 등록
* request parameter : bottleId, doctorId
* url : http://localhost:4000/api/bottle/doctor/:bottleId
......
......@@ -6,6 +6,7 @@ const Mqtt = require('../../util/MqttModule');
const DataProcess = require('../../util/DataProcess');
const jwt = require('jsonwebtoken');
//허브 연결
exports.hubConnect = async (ctx) => {
const token = ctx.req.headers.authorization;
if(!token || !token.length) {
......@@ -21,7 +22,7 @@ exports.hubConnect = async (ctx) => {
return;
}
const { hubId, host } = ctx.request.body;
const { hubId, host, hubNm, } = ctx.request.body;
const isExistHub = await Hub.findByHubId(hubId);
if(isExistHub) {
......@@ -39,7 +40,8 @@ exports.hubConnect = async (ctx) => {
const hub = new Hub({
hubId,
hosting,
userId
userId,
hubNm,
});
await hub.save();
......@@ -48,6 +50,44 @@ exports.hubConnect = async (ctx) => {
};
//허브 연결 해제
exports.hubDisconnect = async(ctx) => {
const token = ctx.req.headers.authorization;
if(!token || !token.length) {
ctx.status = 401;
return;
}
// eslint-disable-next-line no-undef
const { userId } = jwt.verify(token, process.env.JWT_SECRET);
const user = await User.findByUserId(userId);
if(!user || !user.userTypeCd || user.useYn !== 'Y') {
ctx.status = 403;
return;
}
const { hubId } = ctx.params;
const hub = await Hub.findByHubId(hubId);
if(!hub) {
ctx.status = 404;
return;
}
if(hub.getHub_UserId() !== userId) {
ctx.status = 403;
return;
}
const hosting = await hub.getHubHost();
Mqtt.mqttOff(hosting);
await Bottle.deleteMany({ hubId });
await Hub.deleteOne({ hubId });
ctx.status = 204;
};
//허브 정보 조회
exports.getHubList = async(ctx) => {
const token = ctx.req.headers.authorization;
if(!token || !token.length) {
......@@ -75,7 +115,8 @@ exports.getHubList = async(ctx) => {
};
};
exports.hubDisconnect = async(ctx) => {
//허브 이름 변경
exports.setHubName = async ctx => {
const token = ctx.req.headers.authorization;
if(!token || !token.length) {
ctx.status = 401;
......@@ -91,22 +132,9 @@ exports.hubDisconnect = async(ctx) => {
}
const { hubId } = ctx.params;
const { hubNm } = ctx.request.body;
const hub = await Hub.findByHubId(hubId);
if(!hub) {
ctx.status = 404;
return;
}
if(hub.getHub_UserId() !== userId) {
ctx.status = 403;
return;
}
const hosting = await hub.getHubHost();
Mqtt.mqttOff(hosting);
await Hub.updateOne({ hubId }, { hubNm });
await Bottle.deleteMany({ hubId });
await Hub.deleteOne({ hubId });
ctx.status = 204;
};
\ No newline at end of file
ctx.status = 200;
};
......
......@@ -12,6 +12,14 @@ const hub = new Router();
hub.post('/', hubCtrl.hubConnect);
/**
* 허브 등록 해제
* request parameter : x
* url : http://localhost:4000/api/hub/:hubId
* return : null
*/
hub.delete('/:hubId', hubCtrl.hubDisconnect);
/**
* 로그인한 유저의 허브 목록 가져오기
* request parameter : X
* url : http://localhost:4000/api/hub
......@@ -20,11 +28,13 @@ hub.post('/', hubCtrl.hubConnect);
hub.get('/', hubCtrl.getHubList);
/**
* 허브 등록 해제
* request parameter : x
* 로그인한 유저의 특정 허브 이름 변경
* request parameter : hubId, hubNm
* url : http://localhost:4000/api/hub/:hubId
* return : null
*/
hub.delete('/:hubId', hubCtrl.hubDisconnect);
hub.patch('/:hubId', hubCtrl.setHubName);
module.exports = hub;
\ No newline at end of file
......
......@@ -5,6 +5,7 @@ const Schema = mongoose.Schema;
const BottleSchema = new Schema ({
bottleId : { type : Number, required : true, unique : true },
hubId : { type : Number, required : true, ref : 'Hub', },
bottleNm : { type : String, required : true, maxlength : 10, },
});
BottleSchema.statics.findByBottleId = function(bottleId) {
......@@ -23,5 +24,9 @@ BottleSchema.methods.getHubId = function() {
return this.hubId;
};
BottleSchema.methods.setBottleNm = function(bottleNm) {
this.bottleNm = bottleNm;
}
module.exports = mongoose.model('Bottle', BottleSchema);
\ No newline at end of file
......
......@@ -6,6 +6,7 @@ const HubSchema = new Schema ({
hubId : { type : Number, required : true, unique : true },
hosting : { type : Object, default : null },
userId : { type : String, default : null, ref : 'User', lowercase : true, },
hubNm : { type : String, required : true, maxlength : 10, },
});
HubSchema.statics.findByHubId = function(hubId) {
......