Skip to content

Commit

Permalink
feat(api-http): add /receipts/{id} endpoint (#769)
Browse files Browse the repository at this point in the history
* add /receipts/{id}

* test

* style: resolve style guide violations

* ci: fix foundry nightly

---------

Co-authored-by: oXtxNt9U <[email protected]>
  • Loading branch information
oXtxNt9U and oXtxNt9U authored Nov 19, 2024
1 parent aef58dc commit 274c428
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 2 deletions.
34 changes: 34 additions & 0 deletions packages/api-http/integration/routes/receipts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,40 @@ describe<{
}
});

it("/receipts/{id}", async () => {
await apiContext.transactionRepository.save(receiptTransactions);
await apiContext.receiptsRepository.save(receipts);
await apiContext.walletRepository.save(receiptWallets);

const testCases = [
{
id: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
statusCode: 404,
result: null,
},
{
id: receipts[0].id,
result: receipts[0],
},
{
id: receipts[receipts.length - 1].id,
result: receipts[receipts.length - 1],
},
];

for (const { id, statusCode: expectedStatusCode = 200, result } of testCases) {
try {
const { statusCode, data } = await request(`/receipts/${id}`, options);

assert.equal(statusCode, expectedStatusCode);
assert.equal(data, result);
} catch (ex) {
assert.equal(expectedStatusCode, 404);
assert.equal(ex.message, "Response code 404 (Not Found)");
}
}
});

it("/receipts/contracts", async () => {
let { statusCode, data } = await request("/receipts", options);
assert.equal(statusCode, 200);
Expand Down
15 changes: 15 additions & 0 deletions packages/api-http/source/controllers/receipts.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Boom from "@hapi/boom";
import Hapi from "@hapi/hapi";
import {
Contracts as ApiDatabaseContracts,
Expand Down Expand Up @@ -63,6 +64,20 @@ export class ReceiptsController extends Controller {
);
}

public async show(request: Hapi.Request) {
const receipt = await this.receiptRepositoryFactory()
.createQueryBuilder()
.select()
.where("id = :id", { id: request.params.id })
.getOne();

if (!receipt) {
return Boom.notFound();
}

return this.toResource(receipt, ReceiptResource, false);
}

public async contracts(request: Hapi.Request) {
const criteria: Search.Criteria.ReceiptCriteria = request.query;
const pagination = this.getQueryPagination(request.query);
Expand Down
13 changes: 13 additions & 0 deletions packages/api-http/source/routes/receipts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ export const register = (server: Contracts.Api.ApiServer): void => {
path: "/receipts",
});

server.route({
handler: (request: Hapi.Request) => controller.show(request),
method: "GET",
options: {
validate: {
params: Joi.object({
id: Joi.string().hex().length(64),
}),
},
},
path: "/receipts/{id}",
});

server.route({
handler: (request: Hapi.Request) => controller.contracts(request),
method: "GET",
Expand Down
3 changes: 1 addition & 2 deletions packages/api-http/source/routes/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ export const register = (server: Contracts.Api.ApiServer): void => {
options: {
validate: {
params: Joi.object({
// TODO: length depends on hash size...
id: Joi.string().hex() /* .length(64), */,
id: Joi.string().hex().length(64),
}),
query: Joi.object({
transform: Joi.bool().default(true),
Expand Down

0 comments on commit 274c428

Please sign in to comment.