@bitrix24/b24jssdk
Version:
Bitrix24 REST API JavaScript SDK
95 lines (92 loc) • 3.5 kB
JavaScript
/**
* @package @bitrix24/b24jssdk
* @version 2.0.0
* @copyright (c) 2026 Bitrix24
* @license MIT
* @see https://github.com/bitrix24/b24jssdk
* @see https://bitrix24.github.io/b24jssdk/
*/
import { AbstractAction } from '../abstract-action.mjs';
import { Result } from '../../result.mjs';
import { SdkError } from '../../sdk-error.mjs';
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
const AGGREGATE_FUNCTIONS = ["sum", "avg", "min", "max", "count", "countDistinct"];
class AggregateV3 extends AbstractAction {
static {
__name(this, "AggregateV3");
}
/**
* @param {ActionAggregateV3} options
* - `method: string` - an `*.aggregate` method name.
* - `select: AggregateSelectV3` - per-function field selection (`sum`/`avg`/`min`/`max`/`count`/`countDistinct`).
* - `params?: { filter }` - optional v3 filter (array-of-triples; use `FilterV3` to build it).
* - `requestId?: string` - tracking id.
*
* @returns {Promise<Result<AggregateResultV3>>} buckets keyed by function then field name.
*
* @example
* const response = await b24.actions.v3.aggregate.make({
* method: 'some.entity.aggregate',
* select: { sum: { amount: 'totalAmount' }, count: ['id'] },
* params: { filter: FilterV3.build(FilterV3.eq('status', 'NEW')) }
* })
* if (response.isSuccess) {
* const total = response.getData()?.sum?.amount
* }
*/
async make(options) {
const result = new Result();
const select = options?.select ?? {};
for (const fn of Object.keys(select)) {
if (!AGGREGATE_FUNCTIONS.includes(fn)) {
throw new SdkError({
code: "JSSDK_AGGREGATE_V3_INVALID_FUNCTION",
description: `AggregateV3: "${fn}" is not an aggregate function \u2014 use one of ${AGGREGATE_FUNCTIONS.join(" ")}.`,
status: 400
});
}
const fields = select[fn];
if (!Array.isArray(fields) && (typeof fields !== "object" || fields === null)) {
throw new SdkError({
code: "JSSDK_AGGREGATE_V3_INVALID_SELECT",
description: `AggregateV3: select.${fn} must be a string[] (default alias) or a { field: alias } map.`,
status: 400
});
}
}
const params = { select };
if (options?.params?.filter) {
params.filter = options.params.filter;
}
const response = await this._b24.actions.v3.call.make({
method: options.method,
params,
requestId: options.requestId
});
if (!response.isSuccess) {
this._logger.error("aggregateMethod", {
method: options.method,
requestId: options.requestId,
messages: response.getErrorMessages()
});
for (const [index, error] of response.errors) {
result.addError(error, index);
}
return result;
}
const payload = response.getData()?.result;
let buckets;
if (payload && typeof payload === "object" && "result" in payload) {
buckets = payload.result ?? {};
} else if (payload && typeof payload === "object") {
this._logger.warning(`aggregate.make: response has no nested 'result.result' envelope (the v3 reference \xA77 specifies double nesting); falling back to the top-level 'result'. method=${options.method}`);
buckets = payload;
} else {
buckets = {};
}
return result.setData(buckets);
}
}
export { AggregateV3 };
//# sourceMappingURL=aggregate.mjs.map