{"version":3,"file":"call-list.mjs","sources":["../../../../../src/core/actions/v2/call-list.ts"],"sourcesContent":["import type { ActionOptions } from '../abstract-action'\nimport type { TypeCallParams } from '../../../types/http'\nimport type { AjaxResult } from '../../http/ajax-result'\nimport { AbstractAction } from '../abstract-action'\nimport { Result } from '../../result'\n\nexport type ActionCallListV2 = ActionOptions & {\n  method: string\n  params?: Omit<TypeCallParams, 'start'>\n  idKey?: string\n  customKeyForResult?: string\n  requestId?: string\n}\n\n/**\n * Fast data retrieval without counting the total number of records. `restApi:v2`\n *\n * @todo add docs\n */\nexport class CallListV2 extends AbstractAction {\n  /**\n   * Fast data retrieval without counting the total number of records.\n   *\n   * @template T - The type of the elements of the returned array (default is `unknown`).\n   *\n   * @param {ActionCallListV2} options - parameters for executing the request.\n   *     - `method: string` - The name of the REST API method that returns a list of data (for example: `crm.item.list`, `tasks.task.list`)\n   *     - `params?: Omit<TypeCallParams, 'start'>` - Request parameters, excluding the `start` parameter,\n   *         since the method is designed to obtain all data in one call.\n   *         Note: Use `filter`, `order`, and `select` to control the selection.\n   *     - `idKey?: string` - The name of the field containing the unique identifier of the element.\n   *         Default is 'ID' (uppercase). Alternatively, it can be 'id' (lowercase).\n   *         or another field, depending on the REST API data structure.\n   *     - `customKeyForResult?: string` - A custom key indicating that the response REST API will be\n   *        grouped by this field.\n   *        Example: `items` to group a list of CRM items.\n   *    - `requestId?: string` - Unique request identifier for tracking. Used for query deduplication and debugging.\n   *\n   * @returns {Promise<Result<T[]>>} A promise that resolves to the result of an REST API call.\n   *\n   * @example\n   * import { EnumCrmEntityTypeId, Text } from '@bitrix24/b24jssdk'\n   *\n   * interface CrmItem { id: number, title: string }\n   * const sixMonthAgo = new Date()\n   * sixMonthAgo.setMonth((new Date()).getMonth() - 6)\n   * sixMonthAgo.setHours(0, 0, 0)\n   * const response = await b24.actions.v2.callList.make<CrmItem>({\n   *   method: 'crm.item.list',\n   *   params: {\n   *     entityTypeId:  EnumCrmEntityTypeId.company,\n   *     filter: {\n   *       '=%title': 'A%',\n   *       '>=createdTime': Text.toB24Format(sixMonthAgo) // created at least 6 months ago\n   *     },\n   *     select: ['id', 'title']\n   *   },\n   *   idKey: 'id',\n   *   customKeyForResult: 'items',\n   *   requestId: 'list-123'\n   * })\n   * if (!response.isSuccess) {\n   *   throw new Error(`Problem: ${response.getErrorMessages().join('; ')}`)\n   * }\n   * const list = response.getData()\n   * console.log(`Result: ${list?.length}`) // Number of items received\n   */\n  public override async make<T = unknown>(options: ActionCallListV2): Promise<Result<T[]>> {\n    const batchSize = 50\n    const result: Result<T[]> = new Result()\n\n    const idKey = options?.idKey ?? 'ID'\n    const customKeyForResult = options?.customKeyForResult ?? null\n    const params = options?.params ?? {}\n\n    const moreIdKey = `>${idKey}`\n    const requestParams: TypeCallParams = {\n      ...params,\n      order: { ...(params['order'] || {}), [idKey]: 'ASC' },\n      filter: { ...(params['filter'] || {}), [moreIdKey]: 0 },\n      start: -1\n    }\n\n    let allItems: T[] = []\n    let isContinue = true\n\n    do {\n      const response: AjaxResult<T> = await this._b24.actions.v2.call.make<T>({\n        method: options.method,\n        params: requestParams,\n        requestId: options.requestId\n      })\n\n      if (!response.isSuccess) {\n        this._logger.error('callFastListMethod', {\n          method: options.method,\n          requestId: options.requestId,\n          messages: response.getErrorMessages()\n        })\n        for (const [index, error] of response.errors) {\n          result.addError(error, index)\n        }\n        isContinue = false\n        break\n      }\n      const responseData = response.getData()\n      if (!responseData) {\n        isContinue = false\n        break\n      }\n\n      let resultData: T[] = []\n      if (null === customKeyForResult) {\n        resultData = responseData.result as T[]\n      } else {\n        resultData = (responseData.result as any)[customKeyForResult] as T[]\n      }\n\n      if (resultData.length === 0) {\n        isContinue = false\n        break\n      }\n\n      allItems = [...allItems, ...resultData]\n\n      if (resultData.length < batchSize) {\n        isContinue = false\n        break\n      }\n\n      // Update the filter for the next iteration\n      const lastItem = resultData[resultData.length - 1] as Record<string, any>\n      if (\n        lastItem\n        && typeof lastItem[idKey] !== 'undefined'\n      ) {\n        requestParams.filter[moreIdKey] = Number.parseInt(lastItem[idKey])\n      } else {\n        isContinue = false\n        break\n      }\n    } while (isContinue)\n\n    return result.setData(allItems)\n  }\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;AAmBO,MAAM,mBAAmB,cAAA,CAAe;AAAA,EAnB/C;AAmB+C,IAAA,MAAA,CAAA,IAAA,EAAA,YAAA,CAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAgD7C,MAAsB,KAAkB,OAAA,EAAiD;AACvF,IAAA,MAAM,SAAA,GAAY,EAAA;AAClB,IAAA,MAAM,MAAA,GAAsB,IAAI,MAAA,EAAO;AAEvC,IAAA,MAAM,KAAA,GAAQ,SAAS,KAAA,IAAS,IAAA;AAChC,IAAA,MAAM,kBAAA,GAAqB,SAAS,kBAAA,IAAsB,IAAA;AAC1D,IAAA,MAAM,MAAA,GAAS,OAAA,EAAS,MAAA,IAAU,EAAC;AAEnC,IAAA,MAAM,SAAA,GAAY,IAAI,KAAK,CAAA,CAAA;AAC3B,IAAA,MAAM,aAAA,GAAgC;AAAA,MACpC,GAAG,MAAA;AAAA,MACH,KAAA,EAAO,EAAE,GAAI,MAAA,CAAO,OAAO,CAAA,IAAK,EAAC,EAAI,CAAC,KAAK,GAAG,KAAA,EAAM;AAAA,MACpD,MAAA,EAAQ,EAAE,GAAI,MAAA,CAAO,QAAQ,CAAA,IAAK,EAAC,EAAI,CAAC,SAAS,GAAG,CAAA,EAAE;AAAA,MACtD,KAAA,EAAO;AAAA,KACT;AAEA,IAAA,IAAI,WAAgB,EAA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