import OrderappMerchantApi from './index' import apiOrderToAppOrder from '../../libs/apiOrderToAppOrder' import appBatchToApiBatch from '../../libs/appBatchToApiBatch' import appPaymentToApiPayment from '../../libs/appPaymentToApiPayment' export default class OrderApi { api: OrderappMerchantApi constructor (api: OrderappMerchantApi) { this.api = api } // * Order level api async getOrder (orderId: string): Promise { const endpoint = `/m/order?id=${orderId}` const response = await this.api.axiosGo.get(endpoint) return apiOrderToAppOrder(response.data.data[ 0 ]) } async getOrderByInfo (info: {serial?: string, code?: string}): Promise { const response = await this.api.axiosGo.get('/m/order/info', { params: info }) return apiOrderToAppOrder(response.data) } async getOrders (query: IOrderRequestQuery): Promise { const endpoint = '/m/order' const response = await this.api.axiosGo.get(endpoint, { params: query }) return response.data.data.filter((order: IOrder) => order.apiVersion === this.api.apiVersion).map(apiOrderToAppOrder) } async createOrder (data: IAddOrderRequestData): Promise { const endpoint = '/m/order' const response = await this.api.axiosGo.post(endpoint, data) return apiOrderToAppOrder(response.data) } async updateOrder (id: string, data: IUpdateOrderRequestData): Promise { const endpoint = `/m/order/${id}` const response = await this.api.axiosGo.put(endpoint, data) return apiOrderToAppOrder(response.data) } async surchargeOrder (orderId: string, surcharge: IOrderDiscount): Promise { const endpoint = `/m/order/${orderId}/surcharge` const response = await this.api.axiosGo.post(endpoint, surcharge) return apiOrderToAppOrder(response.data) } // 覆蓋掉 order.modifiers async updateModifiers (orderId: string, modifiers: IPriceModifier[]): Promise { const endpoint = `/m/order/${orderId}/modifier` const response = await this.api.axiosGo.post(endpoint, { modifiers }) return apiOrderToAppOrder(response.data) } // 更改人數 async updateCustomerCount (orderId: string, adults: number, children: number): Promise { const endpoint = `/m/order/${orderId}/customercount` const response = await this.api.axiosGo.post(endpoint, { count: adults + children, adults, children, }) return apiOrderToAppOrder(response.data) } // 移桌,給 from, to 桌號 async updateTable (orderId: string, from: string, to: string): Promise { const endpoint = `/m/order/${orderId}/table` const response = await this.api.axiosGo.post(endpoint, { from, to }) return apiOrderToAppOrder(response.data) } // 更改 tags async updateTags (orderId: string, tags: IMenuTag[]): Promise { const endpoint = `/m/order/${orderId}/tag` const response = await this.api.axiosGo.post(endpoint, { tags }) return apiOrderToAppOrder(response.data) } // 併單,從 fromOrderId 併到 orderId async mergeOrder (orderId: string, fromOrderId: string): Promise { const endpoint = `/m/order/${orderId}/merge` const response = await this.api.axiosGo.post(endpoint, { orderId: fromOrderId }) return apiOrderToAppOrder(response.data) } async payOrder (orderId: string, payment: IAppPayment): Promise { const apiPayment = appPaymentToApiPayment(payment) const endpoint = `/m/order/${orderId}/pay` const response = await this.api.axiosGo.put(endpoint, apiPayment) return apiOrderToAppOrder(response.data) } async updateOrderPayment (orderId: string, paymentId: string, data: IUpdatePaymentRequestData): Promise { const endpoint = `/m/order/${orderId}/payment/${paymentId}` const response = await this.api.axiosGo.put(endpoint, data) return apiOrderToAppOrder(response.data) } async updateOrderRemark (orderId: string, remark: string): Promise { const endpoint = `/m/order/${orderId}/remark` const response = await this.api.axiosGo.put(endpoint, { remark }) return apiOrderToAppOrder(response.data) } // 取消、退桌、作廢、外送拒接單 async cancelOrder (id: string, reason: string, identifier: string): Promise { const endpoint = `/m/order/${id}/cancel` const response = await this.api.axiosGo.put(endpoint, { reason, identifier, }) return apiOrderToAppOrder(response.data) } // 外送接單 async confirmOrder (id: string, deviceId: string, reconfirm: boolean = false): Promise { const endpoint = `/m/order/${id}/confirm` const response = await this.api.axiosGo.post(endpoint, { deviceId, batchIndices: [ 0 ], reconfirm, }) return apiOrderToAppOrder(response.data) } // 確認堂食單 async confirmBatch (id: string, deviceId: string, reconfirm: boolean = false, index: number): Promise { const endpoint = `/m/order/${id}/confirm` const response = await this.api.axiosGo.post(endpoint, { deviceId, batchIndices: [ index ], reconfirm, }) return apiOrderToAppOrder(response.data) } // 出餐 async readyOrder (id: string): Promise { const endpoint = `/m/order/${id}/ready` const response = await this.api.axiosGo.put(endpoint) return apiOrderToAppOrder(response.data) } // 取餐 async completeOrder (id: string, code: string): Promise { const endpoint = `/m/order/${id}/complete` const response = await this.api.axiosGo.put(endpoint, { code, }) return apiOrderToAppOrder(response.data) } // 手動送達 (店家自己送才要) async deliverOrder (id: string): Promise { const endpoint = `/m/order/${id}/deliver` const response = await this.api.axiosGo.put(endpoint) return apiOrderToAppOrder(response.data) } // * Batch level api async submitBatch (params: ISubmitBatchParams): Promise { const data = appBatchToApiBatch(params) const endpoint = `/m/order/${params.orderId}/submit` const response = await this.api.axiosGo.post(endpoint, data) return apiOrderToAppOrder(response.data) } // * Item level api // 更改價格 async updateItemPrice (orderId: string, itemId: string, price: number): Promise { const endpoint = `/m/order/${orderId}/item/${itemId}/price` const response = await this.api.axiosGo.post(endpoint, { price }) return apiOrderToAppOrder(response.data) } // 更改折扣 async updateItemModifiers (orderId: string, itemId: string, modifiers: IPriceModifier[]): Promise { const endpoint = `/m/order/${orderId}/item/${itemId}/modifier` const response = await this.api.axiosGo.post(endpoint, { modifiers }) return apiOrderToAppOrder(response.data) } // 更改 tags async updateItemTags (orderId: string, itemId: string, tags: IMenuTag[]): Promise { const endpoint = `/m/order/${orderId}/item/${itemId}/tag` const response = await this.api.axiosGo.post(endpoint, { tags }) return apiOrderToAppOrder(response.data) } // 取消 item async cancelOrderItem (orderId: string, itemId: string): Promise { const endpoint = `/m/order/${orderId}/item/${itemId}/cancel` const response = await this.api.axiosGo.put(endpoint) return apiOrderToAppOrder(response.data) } // * Reports async getOrderStatCategory ( from: string, // timestamp (seconds) to: string, // timestamp (seconds) cancelled: boolean = false, ): Promise { const api = cancelled ? this.api.axiosNode : this.api.axiosGo const response = await api.get('/m/order/stat/category', { params: { from, to, cancelled, }, }) return response.data } async getOrderStatSummary ( from: string, // timestamp (seconds) to: string, // timestamp (seconds) cancelled: boolean, deliveryType?: TDeliveryType, takeaway?: boolean, ): Promise { const params: IOrderStatSummaryRequestParams = { from, to, cancelled, } if (deliveryType != null) { params.deliveryType = deliveryType } if (takeaway != null) { params.takeaway = takeaway } const response = await this.api.axiosGo.get('/m/order/stat/summary', { params }) return response.data } async getOrderStatUnpaid ( from: string, // timestamp (seconds) to: string, // timestamp (seconds) ): Promise { const response = await this.api.axiosGo.get('/m/order/stat/unpaid', { params: { from, to, }, }) return response.data } // ? 不確定回應格式,後端專案未定義 response 且經測試 api 回應 [] async getOrderStatCancel ( from: string, // timestamp (seconds) to: string, // timestamp (seconds) takeaway: boolean, ): Promise { const params: IOrderStatCancelRequestParams = { from, to, } if (takeaway != null) { params.takeaway = takeaway } const response = await this.api.axiosGo.get('/m/order/stat/cancel', { params }) return response.data } }