Skip to content

Commit 5114141

Browse files
added checkStatus method and its unit test to the RecordScanner object
1 parent c8e346e commit 5114141

File tree

4 files changed

+58
-1
lines changed

4 files changed

+58
-1
lines changed

sdk/src/models/record-scanner/registrationResponse.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* RegistrationResponse is a type that represents a response from a record scanning service.
2+
* RegistrationResponse is a type that represents a response from a record scanning service's registration endpoint.
33
*
44
* @example
55
* const registrationResponse: RegistrationResponse = {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* StatusResponse is a type that represents a response from a record scanning service's status endpoint.
3+
*
4+
* @example
5+
* const statusResponse: StatusResponse = {
6+
* synced: true,
7+
* percentage: 100,
8+
* }
9+
*/
10+
export interface StatusResponse {
11+
synced: boolean;
12+
percentage: number;
13+
}

sdk/src/record-scanner.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { RecordsFilter } from "./models/record-scanner/recordsFilter";
99
import { RecordsResponseFilter } from "./models/record-scanner/recordsResponseFilter";
1010
import { RegistrationRequest } from "./models/record-scanner/registrationRequest";
1111
import { RegistrationResponse } from "./models/record-scanner/registrationResponse";
12+
import { StatusResponse } from "./models/record-scanner/statusResponse";
1213

1314
type RecordScannerOptions = {
1415
url: string;
@@ -192,6 +193,29 @@ class RecordScanner implements RecordProvider {
192193
}
193194
}
194195

196+
/**
197+
* Check the status of a record scanner indexing job.
198+
*
199+
* @param {string} jobId The job id to check.
200+
* @returns {Promise<StatusResponse>} The status of the job.
201+
*/
202+
async checkStatus(jobId: string): Promise<StatusResponse> {
203+
try {
204+
const response = await this.request(
205+
new Request(`${this.url}/status/${jobId}`, {
206+
method: "POST",
207+
headers: { "Content-Type": "application/json" },
208+
body: JSON.stringify(jobId),
209+
}),
210+
);
211+
212+
return await response.json();
213+
} catch (error) {
214+
console.error(`Failed to check status of job: ${error}`);
215+
throw error;
216+
}
217+
}
218+
195219
/**
196220
* Find a record in the record scanner service.
197221
*

sdk/tests/record-scanner.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,26 @@ describe("RecordScanner", () => {
347347
expect(request.headers.get("Content-Type")).to.equal("application/json");
348348
});
349349

350+
it("should return StatusResponse after successfully checking status", async () => {
351+
recordScanner = new RecordScanner({ url: "https://record-scanner.aleo.org", account: defaultAccount });
352+
const mockResponse = {
353+
ok: true,
354+
status: 200,
355+
text: () => Promise.resolve(JSON.stringify({ synced: true, percentage: 100 })),
356+
json: () => Promise.resolve({ synced: true, percentage: 100 }),
357+
};
358+
fetchStub.resolves(mockResponse);
359+
const statusResponse = await recordScanner.checkStatus("test-job-id");
360+
expect(statusResponse).to.deep.equal({ synced: true, percentage: 100 });
361+
362+
const request = fetchStub.firstCall.args[0] as Request;
363+
const body = await request.text();
364+
const expectedBody = JSON.stringify("test-job-id");
365+
expect(body).to.equal(expectedBody);
366+
expect(request.method).to.equal("POST");
367+
expect(request.headers.get("Content-Type")).to.equal("application/json");
368+
});
369+
350370
it("should handle HTTP errors", async () => {
351371
recordScanner = new RecordScanner({ url: "https://record-scanner.aleo.org", account: defaultAccount });
352372
let mockResponse = {

0 commit comments

Comments
 (0)