Skip to content

Commit 65c192f

Browse files
committed
refactor: 💡 Changed deals.getAll API response
Added pagination parameters
1 parent 759ff3d commit 65c192f

File tree

5 files changed

+2552
-7207
lines changed

5 files changed

+2552
-7207
lines changed

‎examples/manager/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"@windingtree/sdk-storage": "workspace:*",
1414
"@windingtree/sdk-messages": "workspace:*",
1515
"@windingtree/sdk-node-api": "workspace:*",
16+
"@windingtree/sdk-db": "workspace:*",
1617
"wtmp-examples-shared-files": "workspace:*",
1718
"eslint": "^8.45.0",
1819
"eslint-config-react-app": "^7.0.1",

‎examples/manager/src/components/Deals.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
GenericQuery,
1010
} from '@windingtree/sdk-types';
1111
import { centerEllipsis, formatBalance } from '@windingtree/sdk-react/utils';
12+
import { PaginatedDealRecords } from '@windingtree/sdk-db';
1213

1314
/**
1415
* Deals table
@@ -30,7 +31,7 @@ export const Deals = () => {
3031
if (!node) {
3132
return;
3233
}
33-
const records = await node.deals.getAll.query({});
34+
const { records } = await node.deals.getAll.query({}) as PaginatedDealRecords;
3435
console.log('Deals:', records);
3536
setDeals(records);
3637
}, [node]);
@@ -88,6 +89,9 @@ export const Deals = () => {
8889
</tbody>
8990
</table>
9091
</div>
92+
93+
{message && <div style={{ marginTop: 20 }}>🚨 {message}</div>}
94+
{error && <div style={{ marginTop: 20 }}>🚨 {error}</div>}
9195
</div>
9296
);
9397
};

‎packages/db/src/deals.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ import { createLogger } from '@windingtree/sdk-logger';
55

66
const logger = createLogger('DealsDb');
77

8+
export interface PaginatedDealRecords extends Required<PaginationOptions> {
9+
total: number;
10+
records: DealRecord[];
11+
}
12+
813
/**
914
* Interface defining the properties of a Deal.
1015
*/
@@ -84,7 +89,7 @@ export class DealsDb {
8489
* @returns {Promise<DealRecord[]>} Deals records
8590
* @memberof DealsDb
8691
*/
87-
getAll(pagination?: PaginationOptions): Promise<DealRecord[]> {
92+
getAll(pagination?: PaginationOptions): Promise<PaginatedDealRecords> {
8893
// eslint-disable-next-line @typescript-eslint/no-misused-promises, no-async-promise-executor
8994
return new Promise(async (resolve, reject) => {
9095
try {
@@ -105,7 +110,11 @@ export class DealsDb {
105110
cursor++;
106111
}
107112

108-
resolve(records);
113+
resolve({
114+
...page,
115+
total: cursor,
116+
records,
117+
});
109118
} catch (error) {
110119
logger.error('getAll', error);
111120
reject(error);

‎packages/db/test/db.deals.spec.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,21 +77,24 @@ describe('DealsDb', () => {
7777
it('get all deal records from the storage', async () => {
7878
const pagination = { start: 0, skip: 10 };
7979
const deals = await dealsDb.getAll(pagination);
80-
expect(deals.length).toEqual(pagination.skip);
81-
deals.forEach((d) => {
80+
expect(deals.records.length).toEqual(pagination.skip);
81+
deals.records.forEach((d) => {
8282
const record = records.find((r) => r.offer.id === d.offer.id);
8383
expect(record).to.be.deep.eq(d);
8484
});
85+
expect(deals.total).to.be.equal(records.length);
86+
expect(deals.start).to.be.equal(pagination.start);
87+
expect(deals.skip).to.be.equal(pagination.skip);
8588
});
8689

8790
it('should return an empty array when no deals are found', async () => {
8891
const deals = await dealsDb.getAll({ start: 100, skip: 10 });
89-
expect(deals.length).toBe(0);
92+
expect(deals.records.length).toBe(0);
9093
});
9194

9295
it('should return all remaining deals when the skip count exceeds available deals', async () => {
9396
const deals = await dealsDb.getAll({ start: 5, skip: 10 });
94-
expect(deals.length).toBe(5);
97+
expect(deals.records.length).toBe(5);
9598
});
9699
});
97100
});

0 commit comments

Comments
 (0)