Skip to content

Commit

Permalink
Add support for winner parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
reyraa committed Nov 12, 2023
1 parent 1a4cf83 commit 20763a0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 11 deletions.
52 changes: 41 additions & 11 deletions services/blockchain-indexer/shared/dataService/business/anchors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(
Expand Down
1 change: 1 addition & 0 deletions services/gateway/apis/http-version3/methods/anchors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand Down
1 change: 1 addition & 0 deletions services/gateway/sources/version3/anchors.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module.exports = {
senderAddress: '=,string',
}],
search: '=,string',
winner: '=,boolean',
limit: '=,number',
offset: '=,number',
sort: '=,string',
Expand Down

0 comments on commit 20763a0

Please sign in to comment.