@@ -4,102 +4,65 @@ import { Timestamp, FieldValue } from 'firebase-admin/firestore';
44const getOrderById = async ( id ) => {
55 try {
66 const orderRef = fbDB . collection ( 'orders' ) . doc ( id ) ;
7- const ordersnapshot = await orderRef . get ( ) ;
8- const orderMenuIdList = ordersnapshot . data ( ) . menu_id_list ; //주문메뉴 id 리스트
9- let orderMenuList = [ ] ;
10- for ( let orderMenuId of orderMenuIdList ) { //async/await은 forEach문 안에서 사용할 수 없다.
11- const orderMenuRef = fbDB . collection ( 'order-menus' ) . doc ( orderMenuId ) ;
12- const orderMenusnapshot = await orderMenuRef . get ( ) ;
13- const menuId = orderMenusnapshot . data ( ) . menu_id ; //메뉴 id
14- const menuRef = fbDB . collection ( 'menus' ) . doc ( menuId ) ;
15- const menusnapshot = await menuRef . get ( ) ; //메뉴 이름 확인용
16- let optionList = [ ] ;
17- for ( let op of orderMenusnapshot . data ( ) . options ) { //주문한 메뉴의 옵션 리스트
18- const optionRef = fbDB . collection ( 'menu-options' ) . doc ( op . ops_id ) ; //옵션 id
19- const optionsnapshot = await optionRef . get ( ) ;
20- let contentList = [ ] ;
21- for ( let cid of op . content_id ) { //["1", "2"]
22- for ( let i of optionsnapshot . data ( ) . content ) {
23- if ( i . id == cid ) {
24- contentList . push ( { id : cid , name : i . name , price : i . price , container : i . container } ) ;
25- }
26- }
27- }
28- optionList . push ( { id : op . ops_id , option_name : optionsnapshot . data ( ) . option_name , content : contentList } ) //옵션 아이디, 옵션 이름, 콘텐츠 id
29- } ;
30- orderMenuList . push ( { orderMenuId : orderMenuId , name : menusnapshot . data ( ) . name , amount : orderMenusnapshot . data ( ) . amount , price : menusnapshot . data ( ) . price ,
31- menu_option : optionList , container : menusnapshot . data ( ) . container } ) ;
32- } ;
33- const restrtRef = fbDB . collection ( 'restaurants' ) . doc ( ordersnapshot . data ( ) . restrt_id ) ; //옵션 id
34- const restrtsnapshot = await restrtRef . get ( ) ;
35- const order = { id :ordersnapshot . id , user_id : ordersnapshot . data ( ) . user_id , state : ordersnapshot . data ( ) . state , restrt_id : ordersnapshot . data ( ) . restrt_id , orderMenuList : orderMenuList ,
36- total_price : ordersnapshot . data ( ) . total_price , requirements : ordersnapshot . data ( ) . requirements , payment_method : ordersnapshot . data ( ) . payment_method ,
37- order_number : ordersnapshot . data ( ) . order_number , created_at : ordersnapshot . data ( ) . created_at ,
38- restrt_name : restrtsnapshot . data ( ) . name , restrt_img : restrtsnapshot . data ( ) . img , check_review : ordersnapshot . data ( ) . check_review
39- } ;
40- return order ;
7+ const orderDoc = await orderRef . get ( ) ;
8+ const resData = orderDoc . data ( ) ;
9+ console . log ( resData ) ;
10+ resData . id = id ;
11+ return resData ;
4112 } catch ( error ) {
4213 console . log ( error ) ;
4314 throw error ;
4415 }
4516} ;
4617
47- //for test
48- const changeOrderState = async ( id ) => {
49- try {
50- const orderRef = fbDB . collection ( 'orders' ) . doc ( id ) ;
51- setTimeout ( ( ) => orderRef . update ( { state : "accepted" } ) , 5000 ) ;
52- setTimeout ( ( ) => orderRef . update ( { state : "cooked" } ) , 10000 ) ;
53- setTimeout ( ( ) => {
54- orderRef . update ( { state : "packed" } ) ;
55- orderRef . update ( { packed_time : FieldValue . serverTimestamp ( ) } ) ;
56- } , 15000 ) ;
57-
58- } catch ( error ) {
59- console . log ( error ) ;
60- throw error ;
61- }
62-
63- }
64-
6518const postOrder = async ( data ) => {
6619 try {
67- let menu_list = [ ] ;
68- for ( let menu of data . orderMenuList ) {
69- const res = await fbDB . collection ( 'order-menus' ) . add ( menu ) ;
70- menu_list . push ( res . id ) ;
71- }
20+ //식당 DB의 order_num 갱신
7221 const restrtRef = fbDB . collection ( 'restaurants' ) . doc ( data . restrt_id ) ;
7322 const updateOrdernum = await restrtRef . update ( { order_number :FieldValue . increment ( 1 ) } ) ;
23+ //식당DB revenues 컬렉션 문서 갱신
24+ const now = new Date ( ) ;
25+ const utcNow = now . getTime ( ) + ( now . getTimezoneOffset ( ) * 60 * 1000 ) ; // 현재 시간을 utc로 변환한 밀리세컨드값
26+ const koreaTimeDiff = 9 * 60 * 60 * 1000 ; // 한국 시간은 UTC보다 9시간 빠름
27+ const koreaNow = new Date ( utcNow + koreaTimeDiff ) ; // utc로 변환된 값을 한국 시간으로 변환시키기 위해 9시간(밀리세컨드)를 더함
28+ const nowYear = koreaNow . getFullYear ( ) ; //요일 일월화수목금토
29+ const nowMonth = ( "0" + ( 1 + koreaNow . getMonth ( ) ) ) . slice ( - 2 ) ;
30+ const nowDay = koreaNow . getDate ( ) + "" ;
31+ const revenueId = nowYear + nowMonth ;
32+ const revenueRef = restrtRef . collection ( 'revenues' ) . doc ( revenueId ) ;
33+ const updateRevenue = await revenueRef . update ( {
34+ total_revenue :FieldValue . increment ( Number ( data . total_price ) ) , //달 매출 갱신
35+ [ nowDay ] : FieldValue . increment ( Number ( data . total_price ) ) , //하루 매출 갱신
36+ } ) ;
37+ //order 정보 생성
7438 const refsnapshot = await restrtRef . get ( ) ;
75- const order = {
39+ const orderData = {
7640 restrt_id : data . restrt_id ,
7741 user_id : data . user_id ,
7842 requirements : data . requirements ,
7943 payment_method : data . payment_method ,
8044 total_price : Number ( data . total_price ) ,
8145 created_at : FieldValue . serverTimestamp ( ) ,
82- menu_id_list : menu_list ,
8346 order_number : Number ( refsnapshot . data ( ) . order_number ) ,
8447 state : "checking" ,
8548 packed_time : null ,
8649 check_review : false ,
50+ preview_text : data . preview_text ,
51+ orderMenuList : data . orderMenuList
8752 } ;
88- const res = await fbDB . collection ( 'orders' ) . add ( order ) ;
89- const ref = fbDB . collection ( 'orders' ) . doc ( res . id ) ;
53+ const orderRes = await fbDB . collection ( 'orders' ) . add ( orderData ) ;
54+ const ref = fbDB . collection ( 'orders' ) . doc ( orderRes . id ) ;
9055 const snapshot = await ref . get ( ) ;
9156 const resData = snapshot . data ( ) ;
92- resData . id = res . id ;
93- //for test
94- changeOrderState ( res . id ) ;
57+ resData . id = orderRes . id ;
9558 return resData ;
9659 } catch ( error ) {
9760 console . log ( error ) ;
9861 throw error ;
9962 }
10063} ;
10164
102- const getOrderListByUserId = async ( id ) => {
65+ const getOrderListByUserId = async ( id ) => { //비홀성화
10366 try {
10467 const orderRef = fbDB . collection ( 'orders' ) . where ( 'user_id' , '==' , id ) . orderBy ( 'created_at' , 'desc' ) ;
10568 const ordersnapshot = await orderRef . get ( ) ;
0 commit comments