From 99843061ab6598656ca7023e29e84588176bcf58 Mon Sep 17 00:00:00 2001 From: Daniel Gonzalez Date: Wed, 8 Oct 2025 20:20:11 -0400 Subject: [PATCH] feat(NFTs): Added pagination controls for NFTs --- src/use-cases/offer/index.js | 38 +++++++++++++++++++++- test/unit/mocks/adapters/index.js | 2 +- test/unit/use-cases/offer.use-case.unit.js | 8 +++-- 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/src/use-cases/offer/index.js b/src/use-cases/offer/index.js index d94afba..9fdbc71 100644 --- a/src/use-cases/offer/index.js +++ b/src/use-cases/offer/index.js @@ -267,7 +267,7 @@ class OfferUseCases { } } - async listNftOffers (page = 0, nsfw = false) { + /* async listNftOffers (page = 0, nsfw = false) { try { const data = await this.OfferModel.find({ displayCategory: { $ne: 'fungible' }, @@ -287,6 +287,42 @@ class OfferUseCases { console.error('Error in use-cases/offer/listNftOffers()') throw error } + } */ + + async listNftOffers (page = 0, nsfw = false) { + try { + const query = { + displayCategory: { $ne: 'fungible' }, + nsfw + } + // Total query offers + const totalOffers = await this.OfferModel.countDocuments(query) + // Total pages + const totalPages = Math.ceil(totalOffers / NFT_ENTRIES_PER_PAGE) + + const data = await this.OfferModel.find(query) + // Sort entries so newest entries show first. + .sort('-timestamp') + // Skip to the start of the selected page. + .skip(page * NFT_ENTRIES_PER_PAGE) + // Only return 20 results. + .limit(NFT_ENTRIES_PER_PAGE) + + // console.log('listNftOffers() returning this data: ', data) + + return { + data, + pagination: { + currentPage: page, + totalPages, + totalOffers, + pageSize: NFT_ENTRIES_PER_PAGE + } + } + } catch (error) { + console.error('Error in use-cases/offer/listNftOffers()') + throw error + } } async listFungibleOffers (page = 0) { diff --git a/test/unit/mocks/adapters/index.js b/test/unit/mocks/adapters/index.js index a8da981..a0f42b4 100644 --- a/test/unit/mocks/adapters/index.js +++ b/test/unit/mocks/adapters/index.js @@ -149,7 +149,7 @@ const localdb = { static findById () {} static find () {} static findOne () {} - + static countDocuments(){} async save () { return {} } diff --git a/test/unit/use-cases/offer.use-case.unit.js b/test/unit/use-cases/offer.use-case.unit.js index 35c67be..b7ca050 100644 --- a/test/unit/use-cases/offer.use-case.unit.js +++ b/test/unit/use-cases/offer.use-case.unit.js @@ -400,7 +400,7 @@ describe('#offer-use-case', () => { } }) - it('should list orders', async () => { + it('should list offers', async () => { const queryMock = { sort () { return this @@ -414,7 +414,11 @@ describe('#offer-use-case', () => { sandbox.stub(uut.OfferModel, 'find').returns(queryMock) const result = await uut.listNftOffers(1, true) - assert.isArray(result) + assert.isObject(result) + assert.property(result, 'data') + assert.property(result, 'pagination') + assert.isArray(result.data) + assert.isObject(result.pagination) }) })