Skip to content
This repository has been archived by the owner on Sep 3, 2019. It is now read-only.

Commit

Permalink
Merge pull request #249 from bodhiproject/withdraw-filter-or
Browse files Browse the repository at this point in the history
Make versions take multple
  • Loading branch information
BernardHan authored Jun 21, 2019
2 parents 009f85d + 4017a78 commit 5f71bb7
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 33 deletions.
50 changes: 33 additions & 17 deletions src/graphql/queries/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,11 @@ const { lowercaseFilters, runPaginatedQuery } = require('./utils');
const errCOracleFilterConflict =
Error('Cannot have both centralizedOracle and excludeCentralizedOracle filters');

const buildFilters = ({
OR,
txid,
txStatus,
address,
ownerAddress,
version,
centralizedOracle,
excludeCentralizedOracle,
currentRound,
currentResultIndex,
status,
language,
} = {}) => {
const buildFilters = (rawFilter = {}) => {
const {
OR,
versions,
} = rawFilter;
let filters = [];

// Handle OR array
Expand All @@ -29,6 +20,32 @@ const buildFilters = ({
}

// Handle other fields
if (versions) {
each(versions, (version) => {
const subfilter = buildFilter(rawFilter, version);
if (Object.keys(subfilter).length > 0) filters.push(subfilter);
});
} else {
const subfilter = buildFilter(rawFilter, undefined);
if (Object.keys(subfilter).length > 0) filters.push(subfilter);
}

return filters;
};

const buildFilter = (rawFilter, version) => {
const {
txid,
txStatus,
address,
ownerAddress,
centralizedOracle,
excludeCentralizedOracle,
currentRound,
currentResultIndex,
status,
language,
} = rawFilter;
const filter = {};
if (txid) filter.txid = txid;
if (txStatus) filter.txStatus = txStatus;
Expand All @@ -49,9 +66,8 @@ const buildFilters = ({
if (status) filter.status = status;
if (language) filter.language = language;

if (Object.keys(filter).length > 0) filters.push(filter);
return filters;
};
return filter;
}

module.exports = async (
parent,
Expand Down
43 changes: 31 additions & 12 deletions src/graphql/queries/withdrawable-events.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,45 @@ const getUniqueBets = async (withdrawerAddress, db) => {
* @return {array} Array of filters for the events query.
*/
const buildFilters = (bets, filter) => {
const { version, language, withdrawerAddress } = filter;
const { versions, language, withdrawerAddress } = filter;
const filters = [];

// for user created events
filters.push({ status: EVENT_STATUS.WITHDRAWING, ownerAddress: withdrawerAddress });
each(bets, (bet) => {
const currFilter = {
status: EVENT_STATUS.WITHDRAWING,
address: bet.eventAddress,
};
// final result invalid, betting round user should be able to see the events for withdraw
if (bet.eventRound == 0) {
currFilter.$or = [{ currentResultIndex: bet.resultIndex }, { currentResultIndex: 0 }];
if (versions) {
each(versions, (version) => {
const currFilter = {
status: EVENT_STATUS.WITHDRAWING,
address: bet.eventAddress,
};
// final result invalid, betting round user should be able to see the events for withdraw
if (bet.eventRound === 0) {
currFilter.$or = [{ currentResultIndex: bet.resultIndex }, { currentResultIndex: 0 }];
} else {
currFilter.currentResultIndex = bet.resultIndex;
}
if (version) currFilter.version = version;
if (language) currFilter.language = language;
filters.push(currFilter);
});
} else {
currFilter.currentResultIndex = bet.resultIndex;
const currFilter = {
status: EVENT_STATUS.WITHDRAWING,
address: bet.eventAddress,
};
// final result invalid, betting round user should be able to see the events for withdraw
if (bet.eventRound === 0) {
currFilter.$or = [{ currentResultIndex: bet.resultIndex }, { currentResultIndex: 0 }];
} else {
currFilter.currentResultIndex = bet.resultIndex;
}
if (language) currFilter.language = language;
filters.push(currFilter);
}
if (version) currFilter.version = version;
if (language) currFilter.language = language;
filters.push(currFilter);
});


return filters;
};

Expand Down
4 changes: 2 additions & 2 deletions src/graphql/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ input EventFilter {
txStatus: TransactionStatus
address: String
ownerAddress: String
version: Int
versions: [Int]
centralizedOracle: String
currentRound: Int
currentResultIndex: Int
Expand All @@ -268,7 +268,7 @@ input SearchEventsFilter {
}
input WithdrawableEventFilter {
version: Int
versions: [Int]
language: String
withdrawerAddress: String!
}
Expand Down
4 changes: 2 additions & 2 deletions test/graphql/queries.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('graphql/queries', () => {
{ txStatus: SUCCESS }
{ address: "0xd5d087daabc73fc6cc5d9c1131b93acbd53a2428" }
{ ownerAddress: "0xd5d087daabc73fc6cc5d9c1131b93acbd53a2428" }
{ version: 0 }
{ versions: [0] }
{ centralizedOracle: "0xd5d087daabc73fc6cc5d9c1131b93acbd53a2428" }
{ currentRound: 0 }
{ currentResultIndex: 0 }
Expand Down Expand Up @@ -93,7 +93,7 @@ describe('graphql/queries', () => {
it('should accept the version filter', async () => {
const valid = `
query {
events(filter: { version: 0 }) {
events(filter: { versions: [0] }) {
${PAGINATED_EVENTS}
}
}
Expand Down

0 comments on commit 5f71bb7

Please sign in to comment.