UNPKG

@open-tender/store

Version:

A library of hooks, reducers, utility functions, and types for use with Open Tender applications that utilize our in-store POS API

391 lines (390 loc) 17.5 kB
import { __awaiter, __generator } from "tslib"; import { errorsApi } from './errors'; var parseResponse = function (response, contentType) { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) { if (response.status === 204) { return [2 /*return*/, new Promise(function (resolve) { resolve(null); })]; } else if (response.status === 202) { return [2 /*return*/, new Promise(function (resolve) { resolve(response.body); })]; } switch (contentType) { case 'text/csv': case 'text/plain': case 'text/html': case 'text/xml': return [2 /*return*/, response.text()]; case 'application/pdf': return [2 /*return*/, response.blob()]; default: return [2 /*return*/, response.json()]; } return [2 /*return*/]; }); }); }; var handleError = function (status, parsed) { switch (status) { case 405: return errorsApi.methodNotAllowed; case 500: return errorsApi.internalServerError; case 501: return errorsApi.notImplemented; case 502: return errorsApi.badGateway; case 503: return errorsApi.tempUnavailable; case 504: return errorsApi.gatewayTimeout; default: return parsed; } }; var makeHeaders = function (posTerminalId, kdsTerminalId) { var headers = undefined; if (posTerminalId) { headers = { 'Content-Type': 'application/json', 'pos-terminal-id': "".concat(posTerminalId) }; } else if (kdsTerminalId) { headers = { 'Content-Type': 'application/json', 'kds-terminal-id': "".concat(kdsTerminalId) }; } else { headers = { 'Content-Type': 'application/json' }; } return headers; }; var PosAPI = /** @class */ (function () { function PosAPI(init) { var _this = this; this.request = function (endpoint, method, data) { return __awaiter(_this, void 0, void 0, function () { var _this = this; return __generator(this, function (_a) { // eslint-disable-next-line no-async-promise-executor return [2 /*return*/, new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () { var url, res, contentType, parsed, error, err_1; var _a; return __generator(this, function (_b) { switch (_b.label) { case 0: _b.trys.push([0, 3, , 4]); url = "".concat(this.apiUrl).concat(endpoint); return [4 /*yield*/, fetch(url, { method: method, headers: makeHeaders(this.posTerminalId, this.kdsTerminalId), body: data ? JSON.stringify(data) : null })]; case 1: res = _b.sent(); contentType = (_a = res.headers.get('Content-Type')) === null || _a === void 0 ? void 0 : _a.split(';')[0]; if (!contentType) { return [2 /*return*/, reject(errorsApi.internalServerError)]; } return [4 /*yield*/, parseResponse(res, contentType)]; case 2: parsed = _b.sent(); if (!res.ok) { error = handleError(res.status, parsed); return [2 /*return*/, reject(error)]; } return [2 /*return*/, resolve(parsed)]; case 3: err_1 = _b.sent(); return [2 /*return*/, reject(errorsApi.internalServerError)]; case 4: return [2 /*return*/]; } }); }); })]; }); }); }; this.apiUrl = init.apiUrl; this.posTerminalId = init.posTerminalId; this.kdsTerminalId = init.kdsTerminalId; } PosAPI.prototype.post = function (endpoint, data) { return this.request("/".concat(endpoint), 'POST', data); }; PosAPI.prototype.getKioskConfig = function () { return this.request("/kiosk"); }; PosAPI.prototype.getStore = function () { return this.request("/store"); }; PosAPI.prototype.getSelectOptions = function () { return this.request("/select-options"); }; PosAPI.prototype.getItemTypes = function () { return this.request("/item-types"); }; PosAPI.prototype.getCardRead = function () { return this.request("/devices/card/read"); }; PosAPI.prototype.postCardCancel = function () { return this.request("/devices/card/cancel", 'POST', {}); }; PosAPI.prototype.getBarcodeRead = function () { return this.request("/devices/qrcode/read"); }; PosAPI.prototype.postBarcodeCancel = function () { return this.request("/devices/qrcode/cancel", 'POST', {}); }; PosAPI.prototype.getSettings = function (posSettingType) { var query = "?pos_setting_type=".concat(posSettingType); return this.request("/settings".concat(query)); }; PosAPI.prototype.postSettings = function (posSettingType) { var query = posSettingType ? "&pos_setting_type=".concat(posSettingType) : ''; return this.request("/settings/pull?force=true".concat(query), 'POST', {}); }; PosAPI.prototype.getInternalSettings = function () { return this.request("/internal-settings"); }; PosAPI.prototype.postInternalSettings = function (data) { return this.request("/internal-settings", 'POST', data); }; PosAPI.prototype.postGiftCardCredit = function (data, checkOnly) { var query = checkOnly ? '?check_only=true' : ''; return this.request("/gift-cards/credit".concat(query), 'POST', data); }; PosAPI.prototype.getGiftCardBalance = function (code, cardNumber) { var params = code ? "code=".concat(code) : "card_number=".concat(cardNumber || ''); return this.request("/gift-cards/balance?".concat(params)); }; // identifier can be either a timeclock ID as an integer // or a swipe card code as a string PosAPI.prototype.getEmployee = function (identifier) { return this.request("/employees/".concat(identifier)); }; PosAPI.prototype.getCashier = function () { return this.request("/cashier"); }; PosAPI.prototype.postCardAssign = function (employeeId, code) { var data = { employee_id: employeeId, code: code }; return this.request("/employee-cards/assign", 'POST', data); }; PosAPI.prototype.postCardUnassign = function (code) { return this.request("/employee-cards/unassign", 'POST', { code: code }); }; PosAPI.prototype.postTimePunch = function (data) { return this.request("/time-punches", 'POST', data); }; PosAPI.prototype.getTimePunchesReport = function (businessDate, employeeId) { var params = []; if (businessDate) params.push("business_date=".concat(businessDate)); if (employeeId) params.push("employee_id=".concat(employeeId)); var query = params.length ? "?".concat(params.join('&')) : ''; return this.request("/time-punches-report".concat(query)); }; PosAPI.prototype.postPrintShiftSummary = function (employeeId) { return this.request("/shift-summary/".concat(employeeId, "/print"), 'POST', {}); }; PosAPI.prototype.postCashEvent = function (data) { return this.request("/cash-events", 'POST', data); }; PosAPI.prototype.getCashSummary = function (employeeId) { return this.request("/cash-summary/".concat(employeeId)); }; PosAPI.prototype.postPrintCashSummary = function (employeeId) { return this.request("/cash-summary/".concat(employeeId, "/print"), 'POST', {}); }; PosAPI.prototype.getAlerts = function () { return this.request("/alerts"); }; PosAPI.prototype.getAlert = function (alertId) { return this.request("/alerts/".concat(alertId)); }; PosAPI.prototype.putAlert = function (alertId, data) { return this.request("/alerts/".concat(alertId), 'PUT', data); }; PosAPI.prototype.deleteAlert = function (alertId) { return this.request("/alerts/".concat(alertId), 'DELETE'); }; PosAPI.prototype.getOfflineAuths = function () { return this.request("/credit/offline-transactions"); }; PosAPI.prototype.postOfflineAuths = function () { return this.request("/credit/offline-transactions/process", 'POST', {}); }; PosAPI.prototype.getMenu = function (revenueCenterId, serviceType, requestedAt) { var params = "revenue_center_id=".concat(revenueCenterId, "&service_type=").concat(serviceType, "&requested_at=").concat(requestedAt); return this.request("/menus?".concat(params)); }; PosAPI.prototype.getMenuColors = function () { return this.request("/menu-colors"); }; PosAPI.prototype.getMenuPages = function () { return this.request("/menu-pages"); }; PosAPI.prototype.getDeals = function () { return this.request("/deals"); }; PosAPI.prototype.getSurcharges = function (serviceType) { var query = serviceType ? "?service_type=".concat(serviceType) : ''; return this.request("/surcharges".concat(query)); }; PosAPI.prototype.getDiscounts = function (serviceType, orderType) { var params = []; if (serviceType) params.push("service_type=".concat(serviceType)); if (orderType) params.push("order_type=".concat(orderType)); var query = params.length ? "?".concat(params.join('&')) : ''; return this.request("/discounts".concat(query)); }; PosAPI.prototype.getDiscount = function (name) { return this.request("/discounts/".concat(name)); }; PosAPI.prototype.getDiscountViaQRCode = function (code) { return this.request("/qrcodes/".concat(code)); }; PosAPI.prototype.getTaxes = function (serviceType, orderType) { var params = []; if (serviceType) params.push("service_type=".concat(serviceType)); if (orderType) params.push("order_type=".concat(orderType)); var query = params.length ? "?".concat(params.join('&')) : ''; return this.request("/taxes".concat(query)); }; PosAPI.prototype.getRevenueCenter = function (revenueCenterId) { return this.request("/revenue-centers/".concat(revenueCenterId)); }; PosAPI.prototype.postOpenCashDrawer = function () { return this.request("/devices/drawer/open", 'POST', {}); }; PosAPI.prototype.postTender = function (orderId, data) { return this.request("/orders/".concat(orderId, "/tenders"), 'POST', data); }; PosAPI.prototype.patchTender = function (orderId, index, data) { return this.request("/orders/".concat(orderId, "/tenders/").concat(index), 'PATCH', data); }; PosAPI.prototype.postTenderVoid = function (orderId, index) { return this.request("/orders/".concat(orderId, "/tenders/").concat(index, "/void"), 'POST', {}); }; PosAPI.prototype.postCreditTender = function (orderId, data) { return this.request("/orders/".concat(orderId, "/tenders/credit"), 'POST', data); }; PosAPI.prototype.postChipDNATender = function (orderId, data) { return this.request("/orders/".concat(orderId, "/tenders/chipdna"), 'POST', data); }; PosAPI.prototype.postChipDNACancel = function () { return this.request("/chipdna/cancel", 'POST', {}); }; PosAPI.prototype.postChipDNATmsUpdate = function () { return this.request("/chipdna/tms-update", 'POST', {}); }; PosAPI.prototype.postIdentifyCustomer = function (data) { return this.request("/identify-customer", 'POST', data); }; PosAPI.prototype.postSendReceipt = function (orderUuid, data) { return this.request("/orders/".concat(orderUuid, "/send-receipt"), 'POST', data); }; PosAPI.prototype.getCustomer = function (customerId, endpoints) { var query = "endpoints=".concat(endpoints.join(',')); return this.request("/customers/".concat(customerId, "?").concat(query)); }; PosAPI.prototype.postOrderValidate = function (order) { return this.request("/orders/validate", 'POST', order); }; PosAPI.prototype.postOrder = function (order) { return this.request("/orders", 'POST', order); }; PosAPI.prototype.deleteOrder = function (order) { return this.request("/orders", 'DELETE', order); }; PosAPI.prototype.getOrders = function (args) { var params = []; var business_date = args.business_date, channel_type = args.channel_type, parent_receipt_uuid = args.parent_receipt_uuid, prep_status = args.prep_status, receipt_type = args.receipt_type, search = args.search, sort_by = args.sort_by, sort_direction = args.sort_direction; if (business_date) params.push("business_date=".concat(business_date)); if (channel_type) params.push("channel_type=".concat(channel_type)); if (prep_status) params.push("prep_status=".concat(prep_status)); if (receipt_type) params.push("receipt_type=".concat(receipt_type)); if (parent_receipt_uuid) params.push("parent_receipt_uuid=".concat(parent_receipt_uuid)); if (search) params.push("search=".concat(search)); if (sort_by) params.push("sort_by=".concat(sort_by)); if (sort_direction) params.push("sort_direction=".concat(sort_direction)); var query = params.length ? "?".concat(params.join('&')) : ''; return this.request("/orders".concat(query)); }; PosAPI.prototype.patchOrder = function (orderId, data) { return this.request("/orders/".concat(orderId), 'PATCH', data); }; PosAPI.prototype.postReceipt = function (orderId) { return this.request("/orders/".concat(orderId, "/print"), 'POST', {}); }; PosAPI.prototype.putTicketsStatus = function (orderId, data) { var endpoint = "/orders/".concat(orderId, "/tickets"); return this.request(endpoint, 'PUT', data); }; PosAPI.prototype.postTicketsPrint = function (orderId, data) { if (data === void 0) { data = {}; } var endpoint = "/orders/".concat(orderId, "/tickets/print"); return this.request(endpoint, 'POST', data); }; PosAPI.prototype.postTicketsReset = function (orderId) { var endpoint = "/orders/".concat(orderId, "/tickets/reset"); return this.request(endpoint, 'POST', {}); }; PosAPI.prototype.putTicketStatus = function (orderId, ticketNo, data) { var endpoint = "/orders/".concat(orderId, "/tickets/").concat(ticketNo); return this.request(endpoint, 'PUT', data); }; PosAPI.prototype.postTicketPrint = function (orderId, ticketNo, data) { var endpoint = "/orders/".concat(orderId, "/tickets/").concat(ticketNo, "/print"); return this.request(endpoint, 'POST', data); }; PosAPI.prototype.postTicketStatus = function (orderId, ticketNo, status) { var endpoint = "/orders/".concat(orderId, "/tickets/").concat(ticketNo, "/").concat(status); return this.request(endpoint, 'POST', {}); }; PosAPI.prototype.getArrivals = function () { return this.request("/arrivals"); }; PosAPI.prototype.postAcknowledgeArrival = function (orderId) { return this.request("/orders/".concat(orderId, "/ack-arrival"), 'POST', {}); }; PosAPI.prototype.postRefundValidate = function (orderId, refund) { return this.request("/orders/".concat(orderId, "/refund/validate"), 'POST', refund); }; PosAPI.prototype.postRefund = function (orderId, refund) { return this.request("/orders/".concat(orderId, "/refund"), 'POST', refund); }; PosAPI.prototype.getTags = function () { return this.request("/tags"); }; PosAPI.prototype.getAllergens = function () { return this.request("/allergens"); }; PosAPI.prototype.testPrint = function () { return this.request('/test-printer', 'POST', {}); }; PosAPI.prototype.rateOrder = function (orderId, data) { return this.request("/orders/".concat(orderId, "/rating"), 'POST', data); }; PosAPI.prototype.fetchPaymentConfigs = function () { return this.request("/kiosk/payment-config"); }; return PosAPI; }()); export default PosAPI;