Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core-api): search transactions by asset #2618

Merged
merged 1 commit into from
May 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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