Skip to content

Commit

Permalink
feat(core-api): search transactions by asset (#2618)
Browse files Browse the repository at this point in the history
  • Loading branch information
spkjp authored and faustbrian committed May 25, 2019
1 parent b94e419 commit 6f810ca
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 1 deletion.
36 changes: 36 additions & 0 deletions __tests__/integration/core-api/handlers/transactions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,42 @@ describe("API 2.0 - Transactions", () => {
expect(response.data.data).toBeArray();
utils.expectTransaction(response.data.data[0]);
});

it("should POST a search for transactions with an asset matching any delegate", async () => {
const response = await utils.request("POST", "transactions/search", {
asset: {
delegate: {},
},
});
expect(response).toBeSuccessfulResponse();
expect(response.data.data).toBeArray();
expect(response.data.data).toHaveLength(51);
utils.expectTransaction(response.data.data[0]);
});

it("should POST a search for transactions with an asset matching any delegate and sender public key", async () => {
const response = await utils.request("POST", "transactions/search", {
asset: {
delegate: {},
},
senderPublicKey: "0377f81a18d25d77b100cb17e829a72259f08334d064f6c887298917a04df8f647",
});
expect(response).toBeSuccessfulResponse();
expect(response.data.data).toBeArray();
expect(response.data.data).toHaveLength(1);
utils.expectTransaction(response.data.data[0]);
});

it("should POST a search for transactions with an wrong asset", async () => {
const response = await utils.request("POST", "transactions/search", {
asset: {
garbage: {},
},
});
expect(response).toBeSuccessfulResponse();
expect(response.data.data).toBeArray();
expect(response.data.data).toHaveLength(0);
});
});

describe("POST /transactions", () => {
Expand Down
1 change: 1 addition & 0 deletions packages/core-api/src/handlers/transactions/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,5 +145,6 @@ export const search: object = {
.integer()
.min(0),
}),
asset: Joi.object(),
},
};
2 changes: 1 addition & 1 deletion packages/core-database-postgres/src/models/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export class Transaction extends Model {
init: col => {
return col.value;
},
supportedOperators: [Database.SearchOperator.OP_EQ],
supportedOperators: [Database.SearchOperator.OP_CONTAINS],
},
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,17 @@ export class SearchParameterConverter implements Database.IISearchParameterConve
});
continue;
}

// if the field supports CONTAINS (@>), then ignore any others.
if (fieldDescriptor.supportedOperators.includes(Database.SearchOperator.OP_CONTAINS)) {
searchParameters.parameters.push({
field: fieldName,
operator: Database.SearchOperator.OP_CONTAINS,
value: params[fieldName],
});

continue;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export enum SearchOperator {
OP_GTE = "gte",
OP_LTE = "lte",
OP_LIKE = "like",
OP_CONTAINS = "contains",
// placeholder. For parameters that require custom(not a 1-to-1 field to column mapping) filtering logic on the data-layer repo
OP_CUSTOM = "custom_operator",
}
Expand Down

0 comments on commit 6f810ca

Please sign in to comment.