UNPKG

swipelime-client-node

Version:

swipelime-client-node is the official swipelime Node.js client library

248 lines (247 loc) 11.9 kB
import { EventEmitter } from 'events'; import DDPClient from 'simpleddp-node'; import { Client } from './index'; import TaskEvent from './models/TaskEvent'; import TaskCommand from './models/TaskCommand'; import { ServiceHandlerEventTypes, DataIdType, OrderItemsData, UniversalMenuItem, UniversalMenuItemData, UniversalMenuCategory, NativeTable, CustomOrderItem, UpsertUniversalMenuItemsReturn, UpsertTablesReturn, CancelOrderItemsReturn } from './types'; export declare class ServiceHandler { /** * The client instance used for communication. */ private readonly _client; /** * The DDP client instance used for subscribing to tasks. */ private readonly _ddpClient; /** * The ID of the tenant. */ private readonly _tenantId; /** * The event emitter for handling service handler events. */ private readonly _eventEmitter; /** * The subscription for tasks. */ private _tasksSubscription; /** * The cache for storing tasks. */ private _taskCache; /** * The latest tasks that have been received. * This is used to store the tasks that have been received while processing the previous batch of tasks. * We are only interested in the latest batch os tasks because that's the most up to date data. */ private _latestTasks; /** * A flag to indicate if we are processing tasks. * This is used to prevent multiple tasks processing at the same time. */ private _isProcessingTasks; /** * The timeout for tasks in milliseconds. * If a task is not processed within this time it will be deferred. * The check interval is used to check if there are any tasks that have timed out. */ private readonly _taskTimeout; private readonly _checkInterval; /** * Gets the event emitter for handling service handler events. */ get emitter(): EventEmitter<ServiceHandlerEventTypes>; /** * Gets the ID of the tenant. */ get tenantId(): string; /** * Creates a new instance of the ServiceHandler class. * @param client - The client instance used for communication. * @param ddpClient - The DDP client instance used for subscribing to tasks. * @param tenantId - The ID of the tenant. * @throws {Error} If the tenant ID is not provided. */ constructor(client: Client, ddpClient: DDPClient, _tenantId: string); isReady(): Promise<true>; private processTasksFinishes; private processTasks; private runTasksQueue; private init; private taskMapFunction; private getTaskIdFromTask; private checkOptionalIdValidity; /** * Starts the interval to check for long-running tasks. * If a task is not processed within the timeout, it will be deferred. */ private startTaskCheckInterval; private callTenantMethod; confirmTaskEvents(tasks: (TaskEvent | string)[]): Promise<void>; confirmTaskEvent(task: TaskEvent | string): Promise<void>; /** * Confirms a test command. * The test command can be fired from the test suite in the integration settings in swipelime. When the test command received, this method has to be called to confirm it. It's for testing purposes only. * * @param task - The task event, task command, or task ID. */ confirmTestCommand(task: TaskCommand | string): Promise<void>; /** * Refusing multiple tasks. * @param tasks - The tasks to refuse but they can also be the IDs of the tasks. */ refuseTasks(tasks: (TaskEvent | TaskCommand | string)[]): Promise<void>; /** * You can defer multiple tasks if you can't process them at the moment. * If tasks stay unprocessed for a long time, they will be deferred automatically. * @param tasks - The tasks to defer but they can also be the IDs of the tasks. * Deferred tasks will be re-sent to you later unless they were deferred too many times. */ deferTasks(tasks: (TaskEvent | TaskCommand | string)[]): Promise<void>; /** * Test method to make a DDP error. */ makeError(): Promise<void>; /** * Pings the server. * @returns A promise that resolves to 'pong'. * This method can be used to check if the server is reachable. * Valid login is required to use this method. */ ping(): Promise<'pong' | undefined>; /** * @deprecated Use the markOrderItemsPaymentStatus method instead. * It marks the payment request as done for a specific table. * When the payment is done for a table this method has to be called so our system can reflect to that. * @param tableIdData - The ID of the table. * @returns A promise that resolves to true if it's successful. * @throws {Error} If the table ID is not a valid ID or external ID. */ markPaymentDone(tableIdData: DataIdType): Promise<void>; /** * @deprecated Use the markOrderItemsPaymentStatus method instead. * It marks the payment request as cancelled for a specific table. * When the payment is cancelled for a table this method has to be called so our system can reflect to that. * @param tableIdData - The ID of the table. * @returns A promise that resolves to true if it's successful. * @throws {Error} If the table ID is not a valid ID or external ID. */ markPaymentCancelled(tableIdData: DataIdType): Promise<void>; /** * Finish all table sessions on the table. Users will not be able to order anymore. * When customers are leaving this method should be called to finish the table and lock their session so no more order can be made. * @param tableIdData - The ID of the table. * @returns A promise that resolves to true if it's successful. * @throws {Error} If the table ID is not a valid ID or external ID. */ finishTable(tableIdData: DataIdType): Promise<void>; /** * Retrieves the ordered items for a specific table. * * @param tableIdData - The ID of the table. * @returns A promise that resolves to the order event data. */ getOrderItems(tableIdData: DataIdType): Promise<OrderItemsData[] | undefined>; /** * Cancels the specified order items for a given table. * * @param tableIdData - The ID of the table. * @param orderItemIds - An array of order item IDs to be cancelled. * @returns A Promise that resolves to void. */ cancelOrderItems(tableIdData: DataIdType, orderItemIds: string[]): Promise<void>; /** * Retrieves the universal menu elements from the server. * * @returns A promise that resolves to an array of UniversalMenuItem or UniversalMenuCategory objects. */ getUniversalMenuElements(): Promise<(UniversalMenuItem | UniversalMenuCategory)[] | undefined>; /** * Retrieves the universal menu items. * @returns A promise that resolves to an array of UniversalMenuItem objects. */ getUniversalMenuItems(): Promise<UniversalMenuItem[] | undefined>; /** * Retrieves the universal menu categories. * @returns A promise that resolves to an array of UniversalMenuCategory objects. */ getUniversalMenuCategories(): Promise<UniversalMenuCategory[] | undefined>; /** * Retrieves all tables. * @returns A promise that resolves to an array of NativeTable objects. */ getTables(): Promise<NativeTable[] | undefined>; /** * Retrieves a table based on the provided table ID. * @param tableIdData - The ID of the table. * @returns A promise that resolves to the retrieved NativeTable object. */ getTable(tableIdData: DataIdType): Promise<NativeTable | undefined>; /** * Upserts universal menu items. * * @param universalMenuItemsData - An array of partial UniversalMenuItemData objects. * @param commandId - You can pass in the command id if this was a command from swipelime. * @returns A promise that resolves to an object containing the number of items updated and the number of new items. */ upsertUniversalMenuItems(universalMenuItemsData: Partial<UniversalMenuItemData>[], commandId?: string): Promise<UpsertUniversalMenuItemsReturn | undefined>; /** * Upserts tables. * * @param tableData - An array of partial NativeTable objects. * @param commandId - You can pass in the command id if this was a command from swipelime. * @returns A promise that resolves to an object containing the number of tables updated and the number of new items. */ upsertTables(tableData: Partial<NativeTable>[], commandId?: string): Promise<UpsertTablesReturn | undefined>; /** * Deletes menu elements (eg. items, categories) by their IDs. * @param ids - An array of IdOption objects representing the IDs of the menu elements to delete. * @returns A Promise that resolves to the number of menu elements deleted. */ deleteMenuElements(ids: DataIdType[]): Promise<number | undefined>; /** * Deletes tables by their IDs. * @param ids - An array of IdOption objects representing the IDs of the tables to delete. * @returns A Promise that resolves to the number of tables deleted. */ deleteTables(ids: DataIdType[]): Promise<number | undefined>; /** * Adds a custom order item to the table. It only requires a label (which can be from any language) a quantity and a price. * @param tableIdData - The ID of the table where the custom order item will be added. * @param customOrderItem - The custom order item to be added. * @returns A promise that resolves to an array of order item IDs either the original order item IDs or the newly generated ones. */ addCustomOrderItems(tableIdData: DataIdType, customOrderItem: CustomOrderItem[]): Promise<string[] | undefined>; /** * Changes the status of the order items. * @param tableIdData - The ID of the table where the custom order item will be added. * @param orderItemChanges - The order item changes eg. { orderItemId1: 'confirmed', orderItemId2: 'cancelled' } */ changeOrderItemsStatus(tableIdData: DataIdType, orderItemChanges: Record<string, 'confirmed' | 'cancelled'>): Promise<CancelOrderItemsReturn | undefined>; /** * Confirms a confirm universal menu elements command. * This command is fired when swipelime needs a confirmation that the elements are existing in your system. * * @param task - The task event, task command, or task ID. * @param elementsConfirmation - The confirmation of the elements eg. { elementId1: true, elementId2: false } */ confirmUniversalMenuElementsCommand(task: TaskCommand | string, elementsConfirmation: Record<string, boolean>): Promise<void>; /** * Marks the order items as paid or cancelled. * This is a feedback for us that the order items are paid and we can mark them as paid in our system. * @param tableIdData - The ID of the table where the order items are. * @param orderItemIds - An array of order item IDs to be marked as paid. * @param paymentStatus - The payment status to be set for the order items (paid or cancelled). * @returns A promise that resolves to the payment ID. */ markOrderItemsPaymentStatus(tableIdData: DataIdType, orderItemIds: string[], paymentStatus: 'paid' | 'cancelled'): Promise<string | undefined>; /** * Cancels the payment that was coming from the order-items-payment-confirmed * This can be used if the payment request was not successful and you want to cancel it. * This will mark the order items as unpaid and the customers can pay again. * @param tableIdData - The ID of the table where the payment was made. * @param paymentId - The ID of the payment to be cancelled. * @returns A promise that resolves to void. */ cancelPayment(tableIdData: DataIdType, paymentId: string): Promise<void>; }