박권수

feat. Google Cloud Storage Refactoring

...@@ -3,12 +3,11 @@ ...@@ -3,12 +3,11 @@
3 const User = require('../../models/user'); 3 const User = require('../../models/user');
4 const Profile = require('../../models/profile'); 4 const Profile = require('../../models/profile');
5 const DoctorInfo = require('../../models/doctorInfo'); 5 const DoctorInfo = require('../../models/doctorInfo');
6 +const { uploadDoctorLicense } = require('../../util/GoogleCloudStorage');
6 const Joi = require('joi'); 7 const Joi = require('joi');
7 const jwt = require('jsonwebtoken'); 8 const jwt = require('jsonwebtoken');
8 const axios = require('axios'); 9 const axios = require('axios');
9 10
10 -const { Storage } = require('@google-cloud/storage');
11 -const GoogleStorageUrl = 'https://storage.googleapis.com/';
12 11
13 exports.register = async(ctx) => { 12 exports.register = async(ctx) => {
14 const { 13 const {
...@@ -107,14 +106,6 @@ exports.doctorRegister = async ctx => { ...@@ -107,14 +106,6 @@ exports.doctorRegister = async ctx => {
107 106
108 const { doctorInfoFile } = ctx.request.files; 107 const { doctorInfoFile } = ctx.request.files;
109 108
110 - const info = {
111 - contact,
112 - hospitalAddr,
113 - hospitalNm,
114 - doctorType,
115 - doctorNm,
116 - doctorLicense : '',
117 - };
118 109
119 const schema = Joi.object().keys({ 110 const schema = Joi.object().keys({
120 userId : Joi.string().email().max(50).required(), 111 userId : Joi.string().email().max(50).required(),
...@@ -161,6 +152,24 @@ exports.doctorRegister = async ctx => { ...@@ -161,6 +152,24 @@ exports.doctorRegister = async ctx => {
161 return; 152 return;
162 } 153 }
163 154
155 +
156 + const [fileName, filePath] = [doctorInfoFile.name, doctorInfoFile.path];
157 + const doctorLicense = await uploadDoctorLicense({
158 + userId,
159 + fileName,
160 + filePath,
161 + });
162 +
163 + const info = {
164 + contact,
165 + hospitalAddr,
166 + hospitalNm,
167 + doctorType,
168 + doctorNm,
169 + doctorLicense,
170 + };
171 +
172 +
164 const doctor = new User({ 173 const doctor = new User({
165 userId, 174 userId,
166 userTypeCd : 'DOCTOR', 175 userTypeCd : 'DOCTOR',
...@@ -168,19 +177,6 @@ exports.doctorRegister = async ctx => { ...@@ -168,19 +177,6 @@ exports.doctorRegister = async ctx => {
168 }); 177 });
169 178
170 await doctor.setPassword(password); 179 await doctor.setPassword(password);
171 - doctor.save();
172 -
173 -
174 - const destination = userId + '_' + doctorInfoFile.name;
175 - const storage = new Storage();
176 - storage.bucket('doctor-info').upload(doctorInfoFile.path, {
177 - destination,
178 - }, (err, file, res) => {
179 - if(err) console.log('Fail to upload Doctor License');
180 - else {
181 - info.doctorLicense = GoogleStorageUrl + `${res.bucket}/${res.name}`;
182 - console.log('Success to Upload Doctor License!');
183 - }
184 180
185 const doctorInfo = new DoctorInfo({ 181 const doctorInfo = new DoctorInfo({
186 doctorId : userId, 182 doctorId : userId,
...@@ -188,8 +184,8 @@ exports.doctorRegister = async ctx => { ...@@ -188,8 +184,8 @@ exports.doctorRegister = async ctx => {
188 useYn : 'W', 184 useYn : 'W',
189 }); 185 });
190 186
187 + doctor.save();
191 doctorInfo.save(); 188 doctorInfo.save();
192 - });
193 189
194 ctx.status = 201; 190 ctx.status = 201;
195 191
......
1 const User = require('../../models/user'); 1 const User = require('../../models/user');
2 const DoctorInfo = require('../../models/doctorInfo'); 2 const DoctorInfo = require('../../models/doctorInfo');
3 const jwt = require('jsonwebtoken'); 3 const jwt = require('jsonwebtoken');
4 - 4 +const { viewDoctorLicense } = require('../../util/GoogleCloudStorage');
5 -const { Storage } = require('@google-cloud/storage');
6 5
7 6
8 /** 7 /**
...@@ -110,15 +109,10 @@ exports.getDoctorRegReqDetail = async ctx => { ...@@ -110,15 +109,10 @@ exports.getDoctorRegReqDetail = async ctx => {
110 return; 109 return;
111 } 110 }
112 111
113 - const fileName = doctorInfo.info.doctorLicense.split('/').pop();
114 - const file = new Storage().bucket('doctor-info').file(fileName);
115 - const option = {
116 - version : 'v4',
117 - expires : Date.now() + 1000 * 60 * 15,
118 - action : 'read',
119 - };
120 112
121 - const [signedUrl] = file ? await file.getSignedUrl(option) : ['']; 113 + const doctorLicense = await viewDoctorLicense({
114 + doctorInfo
115 + });
122 116
123 ctx.status = 200; 117 ctx.status = 200;
124 ctx.body = { 118 ctx.body = {
...@@ -126,7 +120,7 @@ exports.getDoctorRegReqDetail = async ctx => { ...@@ -126,7 +120,7 @@ exports.getDoctorRegReqDetail = async ctx => {
126 ...doctorInfo._doc, 120 ...doctorInfo._doc,
127 info : { 121 info : {
128 ...doctorInfo.info, 122 ...doctorInfo.info,
129 - doctorLicense : signedUrl, 123 + doctorLicense,
130 }, 124 },
131 }, 125 },
132 }; 126 };
......
1 -const Bottle = require('../models/bottle');
2 const BottleMedicine = require('../models/bottleMedicine'); 1 const BottleMedicine = require('../models/bottleMedicine');
3 const TakeMedicineHist = require('../models/takeMedicineHistory'); 2 const TakeMedicineHist = require('../models/takeMedicineHistory');
4 3
......
1 +const { Storage } = require('@google-cloud/storage');
2 +
3 +const storage = new Storage();
4 +const GoogleStorageUrl = 'https://storage.googleapis.com/';
5 +
6 +
7 +//의사 아이디, 업로드할 파일명, 업로드할 파일 경로를 인자로 받아, File을 GCS에 업로드 후 GCS 주소를 반환
8 +exports.uploadDoctorLicense = async ({ userId, fileName, filePath }) => {
9 + const destination = userId + '_' + fileName;
10 + const result = await storage.bucket('doctor-info').upload(filePath, {
11 + destination,
12 + });
13 +
14 + const doctorLicenseUrl = GoogleStorageUrl + `${result[0].bucket.id}/${result[0].name}`;
15 +
16 + return doctorLicenseUrl;
17 +};
18 +
19 +//의사 정보를 인자로 받아 해당 Doctor License의 Signed URL을 반환
20 +exports.viewDoctorLicense = async ({ doctorInfo }) => {
21 + const fileName = doctorInfo.info.doctorLicense.split('/').pop();
22 + const file = new Storage().bucket('doctor-info').file(fileName);
23 + const option = {
24 + version : 'v4',
25 + expires : Date.now() + 1000 * 60 * 15,
26 + action : 'read',
27 + };
28 +
29 + const [signedUrl] = file ? await file.getSignedUrl(option) : [null];
30 +
31 + return signedUrl;
32 +};
33 +
34 +//의사 ID, 약 ID, 복용량을 인자로 받아, QR Code를 생성
35 +exports.prescribeMedicine = async ({ doctorId, medicineId, dosage }) => {
36 + //toDo
37 +};
...\ No newline at end of file ...\ No newline at end of file