Skip to content

Commit

Permalink
add countEntries; add offset to sortEntries;
Browse files Browse the repository at this point in the history
  • Loading branch information
aleneum committed Mar 13, 2024
1 parent 1f53aaa commit ce9ffd3
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,14 +393,18 @@ export class Collection implements IChatGroup {
}

public async sortEntries<T extends Entry>(
fn: (a: T, b: T) => number
fn: (a: T, b: T) => number,
offset = 0
): Promise<void> {
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);
}
}
}
Expand All @@ -412,6 +416,17 @@ export class Collection implements IChatGroup {
return this._elements;
}

public async countEntries(filter = {}): Promise<number> {
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<{
Expand Down

0 comments on commit ce9ffd3

Please sign in to comment.