Skip to content

Commit

Permalink
Add support for the 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 0be71ac
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = [
anchorID: { optional: true, type: 'string' },
limit: { optional: true, type: 'number' },
offset: { optional: true, type: 'number' },
winner: { optional: true, type: 'number' },
},
},
];
1 change: 1 addition & 0 deletions services/blockchain-indexer/shared/dataService/anchors.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const getAnchors = async params => {
// Store logs
if (params.anchorID) logger.debug(`Retrieved anchor with ID ${params.anchorID} from Lisk Core`);
else if (params.submitter) logger.debug(`Retrieved anchor with submitter: ${params.submitter} from Lisk Core`);
else if (params.winner) logger.debug('Retrieved winner anchors from Lisk Core');
else logger.debug(`Retrieved anchors with custom search: ${util.inspect(params)} from Lisk Core`);

// Get data from server
Expand Down
57 changes: 46 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,59 @@ 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 = [];

const { winner, ...restParams } = params;

if (params.search) {
const { search, ...remParams } = params;
params = remParams;
if (winner !== undefined) {
const response = await badgesTable.find(
{ ...restParams, limit: restParams.limit || 10 },
['anchorID'],
);

params.search = {
property: 'name',
pattern: search,
};
const anchorIDs = response.map((badge) => badge.anchorID).filter(badgeID => badgeID);

const res = await BluebirdPromise.map(
anchorIDs,
async (anchorID) => {
const anchorsData = await anchorsTable.find(
{ anchorID },
['anchorID', 'name', 'album', 'artists', 'spotifyId', 'appleMusicId', 'createdAt', 'submitter'],
);
return anchorsData.length ? anchorsData[0] : null;
},
{ concurrency: anchorIDs.length },
);
anchorData = res.filter(anchor => anchor);
} else {
if (restParams.search) {
const { search, ...remParams } = restParams;
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: 'number', min: 0, max: 1, default: 0 },
},
get schema() {
const anchorSchema = {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,15 @@
"format": "id",
"minLength": 1,
"maxLength": 64
},
"winner": {
"name": "winner",
"in": "query",
"description": "Filter anchors by winning status.",
"type": "integer",
"format": "int32",
"minimum": 0,
"maximum": 1,
"default": 0
}
}

0 comments on commit 0be71ac

Please sign in to comment.