diff --git a/Server/src/controllers/orderController.js b/Server/src/controllers/orderController.js index 083574f..99759d2 100644 --- a/Server/src/controllers/orderController.js +++ b/Server/src/controllers/orderController.js @@ -133,5 +133,5 @@ const getOrderListByUserIdWithPage = async (req, res) => { export default { getOrderById, postOrder, - getOrderListByUserIdWithPage + getOrderListByUserIdWithPage, }; \ No newline at end of file diff --git a/Server/src/controllers/restrtController.js b/Server/src/controllers/restrtController.js index 0cf9bf8..4d216fe 100644 --- a/Server/src/controllers/restrtController.js +++ b/Server/src/controllers/restrtController.js @@ -60,9 +60,10 @@ const postMenu = async (req, res) => { */ const postOption = async (req, res) => { const data = req.body; - const { id } = req.params; + const restrtId = req.params.restrtId; + const menuId = req.params.menuId; try { - const postOption = await restrtService.postOption(id, data); + const postOption = await restrtService.postOption(restrtId, menuId, data); return res.status(200).json({ status: 200, success: true, @@ -179,9 +180,10 @@ const getMenuList = async (req, res) => { }; const getMenuWithOptions = async (req, res) => { - const { id } = req.params; + const restrtId = req.params.restrtId; + const menuId = req.params.menuId; try { - const getMenuWithOptions = await restrtService.getMenuWithOptions(id); + const getMenuWithOptions = await restrtService.getMenuWithOptions(restrtId, menuId); return res.status(200).json({ status: 200, success: true, @@ -254,9 +256,9 @@ const deleteMenu = async (req, res) => { * @access Private */ const deleteOption = async (req, res) => { - const { menuId, optionId} = req.params; + const { restrtId, menuId, optionId} = req.params; try { - const deleteOption = await restrtService.deleteOption(menuId, optionId); + const deleteOption = await restrtService.deleteOption(restrtId, menuId, optionId); return res.status(200).json({ status: 200, success: true, @@ -299,6 +301,31 @@ const searchRestrt = async (req, res) => { } }; +/** + * @route GET /restrt/search/:data + * @desc searching restrt data in restrtDB + * @access Private + */ +const getRevenue = async (req, res) => { + const { id } = req.params; + try { + const getRevenue = await restrtService.getRevenue(id); + return res.status(200).json({ + status: 200, + success: true, + message: "식당 매출 읽기 성공", + data: getRevenue, + }); + }catch (error){ + console.log(error); + res.status(400).json({ + status: 400, + success: false, + message: error.message, + }); + } +}; + export default { postRestrt, postMenu, @@ -312,4 +339,5 @@ export default { deleteOption, searchRestrt, getRestrtListWithCategory, + getRevenue, }; \ No newline at end of file diff --git a/Server/src/controllers/reviewController.js b/Server/src/controllers/reviewController.js index b8cae4a..0704701 100644 --- a/Server/src/controllers/reviewController.js +++ b/Server/src/controllers/reviewController.js @@ -27,6 +27,32 @@ const postReview = async (req, res) => { } }; +/** + * @route POST /review/emoji + * @desc post emoji data + * @access Private + */ +const postEmoji = async (req, res) => { + const { id } = req.params; + const data = req.body; + try { + const postEmoji = await reviewService.postEmoji(id, data); + return res.status(200).json({ + status: 200, + success: true, + message: "리뷰 이모지 생성 성공", + data: postEmoji, + }); + }catch (error){ + console.log(error); + res.status(400).json({ + status: 400, + success: false, + message: error.message, + }); + } +}; + /** * @route GET /review/restrt/:id * @desc get restaurant reviews data @@ -148,6 +174,7 @@ const deleteReview = async (req, res) => { export default { postReview, + postEmoji, getReviewListByRestrtIdWithPage, getReviewListByUserIdWithPage, deleteReview, diff --git a/Server/src/middleware/changeState.js b/Server/src/middleware/changeState.js index aef4812..81ea691 100644 --- a/Server/src/middleware/changeState.js +++ b/Server/src/middleware/changeState.js @@ -1,4 +1,4 @@ -import schedule from 'node-schedule-tz'; +import schedule from 'node-schedule'; import { fbDB } from '../config/firebase.js'; import { Timestamp, FieldValue } from 'firebase-admin/firestore'; @@ -47,7 +47,6 @@ const changeState = async (id) =>{ const nowDay = koreaNow.getDay(); //요일 일월화수목금토 const nowHour = koreaNow.getHours(); //시 const nowMinutes = koreaNow.getMinutes(); //분 - const dayTime = timeList[(nowDay + 6) % 7]; //가게 특정요일 운영시간 if(dayTime == "휴무"){ @@ -58,12 +57,10 @@ const changeState = async (id) =>{ const openMinute = Number(dayTime.slice(3, 5)); const closeHour = Number(dayTime.slice(-5, -3)); const closeMinute = Number(dayTime.slice(-2)); - console.log(id); console.log('현재', nowHour, nowMinutes, nowDay); console.log(openHour, openMinute, closeHour, closeMinute); - if (openHour > nowHour){ changeClosed(id); console.log('끝1'); @@ -96,14 +93,71 @@ const changeState = async (id) =>{ } }; +const makeMonthRevenueDoc = async (id) => { + try{ + const revenueRef = fbDB.collection('restaurants').doc(id).collection('revenues'); + const now = new Date(); + const utcNow = now.getTime() + (now.getTimezoneOffset() * 60 * 1000); // 현재 시간을 utc로 변환한 밀리세컨드값 + const koreaTimeDiff = 9 * 60 * 60 * 1000; // 한국 시간은 UTC보다 9시간 빠름 + const koreaNow = new Date(utcNow + koreaTimeDiff); // utc로 변환된 값을 한국 시간으로 변환시키기 위해 9시간(밀리세컨드)를 더함 + const nowYear = koreaNow.getFullYear(); //요일 일월화수목금토 + const nowMonth = ("0" + (1 + koreaNow.getMonth())).slice(-2); + const revenueId = nowYear + nowMonth; + const data = {total_revenue: 0}; + const newDoc = await revenueRef.doc(revenueId).set(data); + }catch (error){ + console.log(error); + throw error; + } +}; + +const makeDayRevenueDoc = async (id) => { + try{ + const revenueRef = fbDB.collection('restaurants').doc(id).collection('revenues'); + const now = new Date(); + const utcNow = now.getTime() + (now.getTimezoneOffset() * 60 * 1000); // 현재 시간을 utc로 변환한 밀리세컨드값 + const koreaTimeDiff = 9 * 60 * 60 * 1000; // 한국 시간은 UTC보다 9시간 빠름 + const koreaNow = new Date(utcNow + koreaTimeDiff); // utc로 변환된 값을 한국 시간으로 변환시키기 위해 9시간(밀리세컨드)를 더함 + const nowYear = koreaNow.getFullYear(); //요일 일월화수목금토 + const nowMonth = ("0" + (1 + koreaNow.getMonth())).slice(-2); + const nowDay = koreaNow.getDate() + ""; + const revenueId = nowYear + nowMonth; + const update = await revenueRef.doc(revenueId).update({[nowDay]: 0}); + }catch (error){ + console.log(error); + throw error; + } +}; + const makeSchedule = async (id) => { try{ const restrtRef = fbDB.collection('restaurants').doc(id); const restrtSnapshot = await restrtRef.get(); const timeList = restrtSnapshot.data().opening_hours; //월화수목금토일 - //자정마다 order number 초기화 - const initOrderNumber = schedule.scheduleJob(`00 00 15 * * *`, function () { + //매월 초마다 매출 문서 생성하는 스케줄 + const monthRule = new schedule.RecurrenceRule(); + monthRule.date = 1; + monthRule.hour = 0; + monthRule.minute = 0; + monthRule.second = 1; + monthRule.tz = 'Asia/Seoul'; + const initMonthRevenue = schedule.scheduleJob(monthRule, function () { + makeMonthRevenueDoc(id); + console.log(`${id}식당 Month Revenu init`); + }); + //자정마다 매출 문서 생성하는 스케줄 + const DayRule = new schedule.RecurrenceRule(); + DayRule.hour = 0; + DayRule.minute = 0; + DayRule.second = 10; + DayRule.tz = 'Asia/Seoul'; + const initDayRevenue = schedule.scheduleJob(DayRule, function () { //-9시간 + makeDayRevenueDoc(id); + console.log(`${id}식당 Day Revenu init`); + }); + //자정마다 order number 초기화하는 스케줄 + const initOrderNumber = schedule.scheduleJob(`0 0 15 * * *`, function () { changeOrderNumber(id); console.log(`${id}식당 order number init`); }); @@ -182,6 +236,8 @@ export { changeClosed, changeState, changeOrderNumber, + makeDayRevenueDoc, + makeMonthRevenueDoc, makeSchedule, init, } \ No newline at end of file diff --git a/Server/src/routes/restrtRouter.js b/Server/src/routes/restrtRouter.js index 0700a9a..870d561 100644 --- a/Server/src/routes/restrtRouter.js +++ b/Server/src/routes/restrtRouter.js @@ -6,15 +6,16 @@ const router = Router(); router.post('/', multer.single('img'), restrtController.postRestrt); router.post('/menus', multer.array('img'), restrtController.postMenu); -router.post('/options/:id', restrtController.postOption); +router.post('/options/:restrtId/:menuId', restrtController.postOption); router.get('/', restrtController.getRestrtList); router.get('/category/:c', restrtController.getRestrtListWithCategory); router.get('/menu-list/:id', restrtController.getMenuList); router.get('/:id', restrtController.getRestrt); -router.get('/menu/:id', restrtController.getMenuWithOptions); +router.get('/menu/:restrtId/:menuId', restrtController.getMenuWithOptions); router.delete('/:id', restrtController.deleteRestrt); router.delete('/:restrtId/:menuId', restrtController.deleteMenu); -router.delete('/menu/:menuId/:optionId', restrtController.deleteOption); +router.delete('/menu/:restrtId/:menuId/:optionId', restrtController.deleteOption); router.get('/search/:data', restrtController.searchRestrt); +router.get('/revenue/:id', restrtController.getRevenue); export default router; diff --git a/Server/src/routes/reviewRouter.js b/Server/src/routes/reviewRouter.js index 2747e98..f171cb8 100644 --- a/Server/src/routes/reviewRouter.js +++ b/Server/src/routes/reviewRouter.js @@ -5,6 +5,7 @@ import {multer} from '../middleware/image.js'; const router = Router(); router.post('', multer.single('img'), reviewController.postReview); +router.post('/emoji/:id', reviewController.postEmoji); router.get('/restrt/:id', reviewController.getReviewListByRestrtIdWithPage); //?page=2&size=3 router.get('/user/:id', reviewController.getReviewListByUserIdWithPage); //?page=2&size=3 router.delete('/:id', reviewController.deleteReview); diff --git a/Server/src/service/orderService.js b/Server/src/service/orderService.js index cc270c3..4a16fe9 100644 --- a/Server/src/service/orderService.js +++ b/Server/src/service/orderService.js @@ -4,94 +4,57 @@ import { Timestamp, FieldValue } from 'firebase-admin/firestore'; const getOrderById = async (id) => { try{ const orderRef = fbDB.collection('orders').doc(id); - const ordersnapshot = await orderRef.get(); - const orderMenuIdList = ordersnapshot.data().menu_id_list; //주문메뉴 id 리스트 - let orderMenuList = []; - for (let orderMenuId of orderMenuIdList){ //async/await은 forEach문 안에서 사용할 수 없다. - const orderMenuRef = fbDB.collection('order-menus').doc(orderMenuId); - const orderMenusnapshot = await orderMenuRef.get(); - const menuId = orderMenusnapshot.data().menu_id; //메뉴 id - const menuRef = fbDB.collection('menus').doc(menuId); - const menusnapshot = await menuRef.get(); //메뉴 이름 확인용 - let optionList = []; - for (let op of orderMenusnapshot.data().options){ //주문한 메뉴의 옵션 리스트 - const optionRef = fbDB.collection('menu-options').doc(op.ops_id); //옵션 id - const optionsnapshot = await optionRef.get(); - let contentList = []; - for (let cid of op.content_id){ //["1", "2"] - for (let i of optionsnapshot.data().content){ - if (i.id == cid){ - contentList.push({id: cid, name: i.name, price: i.price, container: i.container}); - } - } - } - optionList.push({id: op.ops_id, option_name: optionsnapshot.data().option_name, content: contentList}) //옵션 아이디, 옵션 이름, 콘텐츠 id - }; - orderMenuList.push({orderMenuId: orderMenuId, name: menusnapshot.data().name, amount: orderMenusnapshot.data().amount, price: menusnapshot.data().price, - menu_option: optionList, container: menusnapshot.data().container }); - }; - const restrtRef = fbDB.collection('restaurants').doc(ordersnapshot.data().restrt_id); //옵션 id - const restrtsnapshot = await restrtRef.get(); - const order = {id:ordersnapshot.id, user_id: ordersnapshot.data().user_id, state: ordersnapshot.data().state, restrt_id: ordersnapshot.data().restrt_id, orderMenuList: orderMenuList, - total_price: ordersnapshot.data().total_price, requirements: ordersnapshot.data().requirements, payment_method: ordersnapshot.data().payment_method, - order_number: ordersnapshot.data().order_number, created_at: ordersnapshot.data().created_at, - restrt_name: restrtsnapshot.data().name, restrt_img: restrtsnapshot.data().img, check_review: ordersnapshot.data().check_review - }; - return order; + const orderDoc = await orderRef.get(); + const resData = orderDoc.data(); + console.log(resData); + resData.id = id; + return resData; }catch (error){ console.log(error); throw error; } }; -//for test -const changeOrderState = async (id) => { - try{ - const orderRef = fbDB.collection('orders').doc(id); - setTimeout(() => orderRef.update({state: "accepted"}), 5000); - setTimeout(() => orderRef.update({state: "cooked"}), 10000); - setTimeout(() => { - orderRef.update({state: "packed"}); - orderRef.update({packed_time: FieldValue.serverTimestamp()}); - }, 15000); - - }catch (error){ - console.log(error); - throw error; - } - -} - const postOrder = async (data) => { try{ - let menu_list = []; - for (let menu of data.orderMenuList){ - const res = await fbDB.collection('order-menus').add(menu); - menu_list.push(res.id); - } + //식당 DB의 order_num 갱신 const restrtRef = fbDB.collection('restaurants').doc(data.restrt_id); const updateOrdernum = await restrtRef.update({order_number:FieldValue.increment(1)}); + //식당DB revenues 컬렉션 문서 갱신 + const now = new Date(); + const utcNow = now.getTime() + (now.getTimezoneOffset() * 60 * 1000); // 현재 시간을 utc로 변환한 밀리세컨드값 + const koreaTimeDiff = 9 * 60 * 60 * 1000; // 한국 시간은 UTC보다 9시간 빠름 + const koreaNow = new Date(utcNow + koreaTimeDiff); // utc로 변환된 값을 한국 시간으로 변환시키기 위해 9시간(밀리세컨드)를 더함 + const nowYear = koreaNow.getFullYear(); //요일 일월화수목금토 + const nowMonth = ("0" + (1 + koreaNow.getMonth())).slice(-2); + const nowDay = koreaNow.getDate() + ""; + const revenueId = nowYear + nowMonth; + const revenueRef = restrtRef.collection('revenues').doc(revenueId); + const updateRevenue = await revenueRef.update({ + total_revenue:FieldValue.increment(Number(data.total_price)), //달 매출 갱신 + [nowDay]: FieldValue.increment(Number(data.total_price)), //하루 매출 갱신 + }); + //order 정보 생성 const refsnapshot = await restrtRef.get(); - const order = { + const orderData = { restrt_id : data.restrt_id, user_id : data.user_id, requirements : data.requirements, payment_method : data.payment_method, total_price : Number(data.total_price), created_at: FieldValue.serverTimestamp(), - menu_id_list: menu_list, order_number: Number(refsnapshot.data().order_number), state: "checking", packed_time: null, check_review: false, + preview_text: data.preview_text, + orderMenuList: data.orderMenuList }; - const res = await fbDB.collection('orders').add(order); - const ref = fbDB.collection('orders').doc(res.id); + const orderRes = await fbDB.collection('orders').add(orderData); + const ref = fbDB.collection('orders').doc(orderRes.id); const snapshot = await ref.get(); const resData = snapshot.data(); - resData.id = res.id; - //for test - changeOrderState(res.id); + resData.id = orderRes.id; return resData; }catch (error){ console.log(error); @@ -99,7 +62,7 @@ const postOrder = async (data) => { } }; -const getOrderListByUserId = async (id) => { +const getOrderListByUserId = async (id) => { //비홀성화 try{ const orderRef = fbDB.collection('orders').where('user_id', '==', id).orderBy('created_at', 'desc'); const ordersnapshot = await orderRef.get(); diff --git a/Server/src/service/restrtService.js b/Server/src/service/restrtService.js index 732a7df..4280e6a 100644 --- a/Server/src/service/restrtService.js +++ b/Server/src/service/restrtService.js @@ -1,7 +1,7 @@ import { fbDB } from '../config/firebase.js'; import {uploadFile} from '../middleware/image.js'; import {v4 as uuidv4} from 'uuid'; -import {changeState, makeSchedule} from '../middleware/changeState.js'; +import {changeState, makeSchedule, makeMonthRevenueDoc, makeDayRevenueDoc} from '../middleware/changeState.js'; import { Timestamp, FieldValue } from 'firebase-admin/firestore'; const postRestrt = async (file, data) => { @@ -12,16 +12,18 @@ const postRestrt = async (file, data) => { }else{ data.img = null; } + //자료형 변환 data.category = Number(data.category); data.star_rating = Number(data.star_rating); data.review_count = Number(data.review_count); data.order_number = Number(data.order_number); data.opening_hours = JSON.parse(data.opening_hours); data.geo_point = JSON.parse(data.geo_point); - const res = await fbDB.collection('restaurants').add(data); changeState(res.id); makeSchedule(res.id); + makeMonthRevenueDoc(res.id); + makeDayRevenueDoc(res.id); const restId = {id: res.id}; return restId; }catch (error){ @@ -39,14 +41,19 @@ const postMenu = async (files, data) => { //❗데이터 객체 개수와 이미 }else{ data[i].img = null; } - const res = await fbDB.collection('menus').add(data[i]); - menuIdList.push({id: res.id}); - + const menuData = { + name: data[i].name, + price: data[i].price, + img: data[i].img, + detail_category: data[i].detail_category, + container: Number(data[i].container), + description: data[i].description, + disabled: data[i].disabled + } const restrtId = data[i].restrt_id; - const restrtRef = fbDB.collection('restaurants').doc(restrtId); - const restrtRes = await restrtRef.update({ - menu_list: FieldValue.arrayUnion(res.id) - }); + const menuCollection = fbDB.collection('restaurants').doc(restrtId).collection('menus'); + const res = await menuCollection.add(menuData); + menuIdList.push({id: res.id}); }; return menuIdList; }catch (error){ @@ -55,24 +62,20 @@ const postMenu = async (files, data) => { //❗데이터 객체 개수와 이미 } }; -const postOption = async (id, data) => { +const postOption = async (restrtId, menuId, data) => { try{ const options = []; for (let doc of data){ - for (let contdoc of doc.content){ + for (let contdoc of doc.contents){ contdoc.id = uuidv4(); } - const resOption = await fbDB.collection('menu-options').add(doc); - const optRef = fbDB.collection('menu-options').doc(resOption.id); - const optSnapshot = await optRef.get(); - let optData = optSnapshot.data(); + console.log(restrtId); + console.log(menuId); + const optionCollection = fbDB.collection('restaurants').doc(restrtId).collection('menus').doc(menuId).collection('menu-options'); + const resOption = await optionCollection.add(doc); + const optData = doc; optData.id = resOption.id; options.push(optData); - //메뉴 디비에 해당 메뉴의 옵션id를 리스트에 추가 - const menuRef = fbDB.collection('menus').doc(id); - const resMenu = await menuRef.update({ - option_id: FieldValue.arrayUnion(resOption.id) - }); }; return options;//optionIdList; }catch (error){ @@ -81,35 +84,34 @@ const postOption = async (id, data) => { } }; - const getRestrt = async (id) => { try{ const restRef = fbDB.collection('restaurants').doc(id); - const restrtSnapshot = await restRef.get(); - if (!restrtSnapshot.exists){ + const restrtDoc = await restRef.get(); + if (!restrtDoc.exists){ throw {message: "There is no such restrt corresponding to the provided identifier."}; }else{ - let restrtData = restrtSnapshot.data(); + const restrtData = restrtDoc.data(); restrtData.id = id; - const menuIdList = restrtData.menu_list; - const menuList = []; - const categoryList=[]; - for (let menuId of menuIdList){ - const menuRef = fbDB.collection('menus').doc(menuId); - const menuSnapshot = await menuRef.get(); - const menuData = menuSnapshot.data(); - menuData.id = menuSnapshot.id; - if (categoryList.indexOf(menuData.detail_category) > -1){ - const index = categoryList.indexOf(menuData.detail_category); - menuList[index].menuList.push(menuData); + + const menusRef = fbDB.collection('restaurants').doc(id).collection('menus'); + const menusSnapshot = await menusRef.get(); + restrtData.menu_list = []; + const categoryList = []; + menusSnapshot.forEach(doc => { + const menuData = doc.data(); + menuData.restrt_id = id; + menuData.id = doc.id; + const index = categoryList.indexOf(doc.data().detail_category); + if(index > -1){ + restrtData.menu_list[index].menuList.push(menuData); }else{ - categoryList.push(menuData.detail_category); - let x = {detail_category: menuData.detail_category, menuList:[menuData]}; - menuList.push(x); + categoryList.push(doc.data().detail_category); + const x = {detail_category: doc.data().detail_category, menuList:[menuData]}; + restrtData.menu_list.push(x); } - } - const sortedMenuList = menuList.sort((a, b) => a.detail_category < b.detail_category ? -1 : 1); - restrtData.menu_list = sortedMenuList; + }); + restrtData.menu_list.sort((a, b) => a.detail_category < b.detail_category ? -1 : 1); return restrtData; } }catch (error){ @@ -165,17 +167,15 @@ const getRestrtListWithCategory = async (c) => { //🔺 위치에 따라 리스 const getMenuList = async (id) => { try{ - const restRef = fbDB.collection('restaurants').doc(id); - const snapshot = await restRef.get(); - const menuIdList = snapshot.data().menu_list; + const menusCollection = fbDB.collection('restaurants').doc(id).collection('menus'); + const menuSnapshot = await menusCollection.get(); let menuList = []; - for (let menuId of menuIdList){ //async/await은 forEach문 안에서 사용할 수 없다. - const menuRef = fbDB.collection('menus').doc(menuId); - const menusnapshot = await menuRef.get(); - const menuData = menusnapshot.data(); - menuData.menuId = menuId + menuSnapshot.forEach(doc => { + const menuData = doc.data(); + menuData.id = doc.id; + menuData.restrtId = id; menuList.push(menuData); - } + }); if (menuList == 0){ throw {message: "empty error"}; }else{ @@ -187,22 +187,21 @@ const getMenuList = async (id) => { } }; -const getMenuWithOptions = async (id) =>{ +const getMenuWithOptions = async (restrtId, menuId) =>{ try{ - const menuRef = fbDB.collection('menus').doc(id); - const menusnapshot = await menuRef.get(); - const optionIdList = menusnapshot.data().option_id; - const menuData = menusnapshot.data(); - menuData.id = menusnapshot.id; - let optionList = []; + const menuRef = fbDB.collection('restaurants').doc(restrtId).collection('menus').doc(menuId); + const menuSnapshot = await menuRef.get(); + const menuData = menuSnapshot.data(); + menuData.id = menuSnapshot.id; //메뉴 아이디 - for (let optionId of optionIdList){ //async/await은 forEach문 안에서 사용할 수 없다. - const optionRef = fbDB.collection('menu-options').doc(optionId); - const optionsnapshot = await optionRef.get(); - const optionData = optionsnapshot.data(); - optionData.id = optionId; + const optionList = []; + const optionRef = menuRef.collection('menu-options'); + const optionSnapshot = await optionRef.get(); + optionSnapshot.forEach(doc => { + const optionData = doc.data(); + optionData.id = doc.id; optionList.push(optionData); - }; + }); const menuWithOption = {menu: menuData, option: optionList}; return menuWithOption; }catch (error){ @@ -225,10 +224,9 @@ const deleteRestrt = async (id) => { const deleteMenu = async (restrtId, menuId) => { try{ - const restrtRef = fbDB.collection('restaurants').doc(restrtId); - const res = await restrtRef.update({ - menu_list: FieldValue.arrayRemove(menuId) - }); + const menusRef = fbDB.collection('restaurants').doc(restrtId).collection('menus'); + const deleteMenu = await menusRef.doc(menuId).delete(); + //하위 컬렉션인 옵션 삭제 const data = {restrtId: restrtId, menuId: menuId}; return data; }catch (error){ @@ -237,12 +235,10 @@ const deleteMenu = async (restrtId, menuId) => { } }; -const deleteOption = async (menuId, optionId) => { +const deleteOption = async (restrtId, menuId, optionId) => { try{ - const menuRef = fbDB.collection('menus').doc(menuId); - const res = await menuRef.update({ - option_id: FieldValue.arrayRemove(optionId) - }); + const optionsRef = fbDB.collection('restaurants').doc(restrtId).collection('menus').doc(menuId).collection('menu-options'); + const deleteOption = await optionsRef.doc(optionId).delete(); const data = {menuId: menuId, optionId: optionId}; return data; }catch (error){ @@ -299,6 +295,65 @@ const searchRestrt = async (data) => { //🔺 위치에 따라 리스트 선정 } }; +const getRevenue = async (id) => { + try{ + const now = new Date(); + const utcNow = now.getTime() + (now.getTimezoneOffset() * 60 * 1000); // 현재 시간을 utc로 변환한 밀리세컨드값 + const koreaTimeDiff = 9 * 60 * 60 * 1000; // 한국 시간은 UTC보다 9시간 빠름 + const koreaNow = new Date(utcNow + koreaTimeDiff); // utc로 변환된 값을 한국 시간으로 변환시키기 위해 9시간(밀리세컨드)를 더함 + const nowYear = koreaNow.getFullYear(); //요일 일월화수목금토 + const nowMonth = ("0" + (1 + koreaNow.getMonth())).slice(-2); + const nowDay = koreaNow.getDate() + ""; + const revenueId = nowYear + nowMonth; + //오늘 매출, 이번달 매출 + const revenueRef = fbDB.collection('restaurants').doc(id).collection('revenues').doc(revenueId); + const revenueSnapshot = await revenueRef.get(); + const todayRevenue = revenueSnapshot.data()[nowDay]; + const monthRevenue = revenueSnapshot.data().total_revenue; + //최근 6달의 매출 + const lastSixRevenueList = []; + const idList = []; + const Monthnum = Number(nowMonth); + for (let i=5 ; i>=0 ; i--){ + if(Monthnum - i > 0){ + const month = ("0"+(Monthnum - i)).slice(-2); + const id = nowYear + month; + idList.push(id); + }else{ + const month = ("0"+(Monthnum - i + 12)).slice(-2); + const year = nowYear - 1; + const id = year + month; + idList.push(id); + } + } + for (let revenueid of idList){ + const ref = fbDB.collection('restaurants').doc(id).collection('revenues').doc(revenueid); + const doc = await ref.get(); + let data; + if (doc.data() == undefined ){ + data = { + id: revenueid, + total_revenue: 0 + }; + }else{ + data = { + id: revenueid, + total_revenue: doc.data().total_revenue + }; + } + lastSixRevenueList.push(data); + } + const resData = { + todayRevenue: todayRevenue, + monthRevenue: monthRevenue, + lastSixRevenueList: lastSixRevenueList + } + return resData; + }catch (error){ + console.log(error); + throw error; + } +}; export default { postRestrt, @@ -313,4 +368,5 @@ export default { deleteOption, searchRestrt, getRestrtListWithCategory, + getRevenue, }; \ No newline at end of file diff --git a/Server/src/service/reviewService.js b/Server/src/service/reviewService.js index a54a6a6..591bc80 100644 --- a/Server/src/service/reviewService.js +++ b/Server/src/service/reviewService.js @@ -37,6 +37,24 @@ const postReview = async (file, data) => { } }; +const postEmoji = async (id, data) => { + try{ + const emojiData = data.emoji; + const reviewRef = fbDB.collection('reviews').doc(id); + const updateEmoji = await reviewRef.update({ + emoji: emojiData + }); + const reviewDoc = await reviewRef.get(); + const resData = reviewDoc.data(); + resData.id = id; + console.log(resData); + return resData; + }catch(error){ + console.log(error); + throw error; + } +}; + const getReviewListByRestrtId = async (id) => { try{ const reviewRef = fbDB.collection('reviews'); @@ -168,6 +186,7 @@ const deleteReview = async (id) => { export default { postReview, + postEmoji, getReviewListByRestrtId, getReviewListByRestrtIdWithPage, getReviewListByUserId,