diff --git a/services/blockchain-indexer/shared/dataService/business/anchors.js b/services/blockchain-indexer/shared/dataService/business/anchors.js index fdcfb0a90..53203416e 100644 --- a/services/blockchain-indexer/shared/dataService/business/anchors.js +++ b/services/blockchain-indexer/shared/dataService/business/anchors.js @@ -6,6 +6,7 @@ const BluebirdPromise = require('bluebird'); const transactionsIndexSchema = require('../../database/schema/anchors'); const imagesIndexSchema = require('../../database/schema/images'); const votesIndexSchema = require('../../database/schema/votes'); +const badgesIndexSchema = require('../../database/schema/badges'); const config = require('../../../config'); const MYSQL_ENDPOINT = config.endpoints.mysql; @@ -28,25 +29,54 @@ const getVotesIndex = () => getTableInstance( MYSQL_ENDPOINT, ); +const getBadgesIndex = () => getTableInstance( + badgesIndexSchema.tableName, + badgesIndexSchema, + MYSQL_ENDPOINT, +); + const getAnchors = async (params = {}) => { const anchorsTable = await getAnchorsIndex(); const imagesTable = await getImagesIndex(); const votesTable = await getVotesIndex(); + const badgesTable = await getBadgesIndex(); + + let anchorData = []; - if (params.search) { - const { search, ...remParams } = params; - params = remParams; + if (params.winner) { + const anchorIDs = await badgesTable.find( + { offset: params.offset || 0, limit: params.limit || 10 }, + ['anchorID'], + ); - params.search = { - property: 'name', - pattern: search, - }; + anchorData = await BluebirdPromise.map( + anchorIDs, + async (anchorID) => { + const anchorsData = await anchorsTable.find( + { anchorID }, + ['anchorID', 'name', 'album', 'artists', 'spotifyId', 'appleMusicId', 'createdAt', 'submitter'], + ); + return anchorsData; + }, + { concurrency: anchorIDs.length }, + ); + } else { + if (params.search) { + const { search, ...remParams } = params; + params = remParams; + + params.search = { + property: 'name', + pattern: search, + }; + } + + anchorData = await anchorsTable.find( + { ...params, limit: params.limit || 10 }, + ['anchorID', 'name', 'album', 'artists', 'spotifyId', 'appleMusicId', 'createdAt', 'submitter'], + ); } - const anchorData = await anchorsTable.find( - { ...params, limit: params.limit || 10 }, - ['anchorID', 'name', 'album', 'artists', 'spotifyId', 'appleMusicId', 'createdAt', 'submitter'], - ); const total = anchorData.length; const data = await BluebirdPromise.map( diff --git a/services/gateway/apis/http-version3/methods/anchors.js b/services/gateway/apis/http-version3/methods/anchors.js index aa9555cfb..230c4c20f 100644 --- a/services/gateway/apis/http-version3/methods/anchors.js +++ b/services/gateway/apis/http-version3/methods/anchors.js @@ -14,6 +14,7 @@ module.exports = { search: { optional: true, type: 'string' }, limit: { optional: true, type: 'number', min: 1, max: 100, default: 10 }, offset: { optional: true, type: 'number', min: 0, default: 0 }, + winner: { optional: true, type: 'boolean' }, }, get schema() { const anchorSchema = {}; diff --git a/services/gateway/sources/version3/anchors.js b/services/gateway/sources/version3/anchors.js index c142f735c..833aaa712 100644 --- a/services/gateway/sources/version3/anchors.js +++ b/services/gateway/sources/version3/anchors.js @@ -17,6 +17,7 @@ module.exports = { senderAddress: '=,string', }], search: '=,string', + winner: '=,boolean', limit: '=,number', offset: '=,number', sort: '=,string',