From ce9ffd3478625381bee907294a027f39b025f13d Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Wed, 13 Mar 2024 14:32:37 +0100 Subject: [PATCH] add countEntries; add offset to sortEntries; --- src/collection.ts | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/collection.ts b/src/collection.ts index a25b368..e6265c3 100644 --- a/src/collection.ts +++ b/src/collection.ts @@ -393,14 +393,18 @@ export class Collection implements IChatGroup { } public async sortEntries( - fn: (a: T, b: T) => number + fn: (a: T, b: T) => number, + offset = 0 ): Promise { - await this.populate(); + if (this._entries.length === 0) { + const num = await this.countEntries(); + await this.populate(num); + } this._entries.sort((a, b) => fn(a as T, b as T)); for (let i = 0; i < this._entries.length; ++i) { const entry = this._entries[i]; - if (entry.sortOrder !== i.toString()) { - await this._entries[i].setSortOrder(i); + if (entry.sortOrder !== (i + offset).toString()) { + await entry.setSortOrder(i + offset); } } } @@ -412,6 +416,17 @@ export class Collection implements IChatGroup { return this._elements; } + public async countEntries(filter = {}): Promise { + const res: { + status: number; + data: { total: number; filteredTotal: number }; + } = await axios.post(`${BASE_URL}/lists/${this.id}/entries/filter/total`, { + filter: filter, + }); + assertReturnCode(res, 200); + return res.data.total; + } + public async populate( limit = 100, orderBy: Array<{