Skip to content

Commit

Permalink
feat: Return all devices
Browse files Browse the repository at this point in the history
  • Loading branch information
d-beezee committed May 20, 2024
1 parent 92861fc commit 58ebe74
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 27 deletions.
12 changes: 12 additions & 0 deletions src/routes/device/device_type/operating_systems/_get/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,16 @@ describe("GET /devices/{type}/operating_systems", () => {
expect(response.body[0]).toHaveProperty("id", 1);
expect(response.body[0]).toHaveProperty("name", "Android");
});

it("Should return 406 if device type = all and filterBy is present", async () => {
const responseModel = await request(app)
.get("/devices/all/operating_systems?filterBy[model]=Galaxy S10")
.set("authorization", "Bearer tester");
expect(responseModel.status).toBe(406);

const responseManufacturer = await request(app)
.get("/devices/all/operating_systems?filterBy[manufacturer]=Samsung")
.set("authorization", "Bearer tester");
expect(responseManufacturer.status).toBe(406);
});
});
57 changes: 30 additions & 27 deletions src/routes/device/device_type/operating_systems/_get/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import OpenapiError from "@src/features/OpenapiError";
import { tryber } from "@src/features/database";
import UserRoute from "@src/features/routes/UserRoute";

Expand Down Expand Up @@ -41,49 +42,54 @@ export default class Route extends UserRoute<{
}
}

protected async filter() {
if (!(await super.filter())) return false;
if (this.deviceType === "all" && this.filterBy) {
this.setError(
406,
new OpenapiError("Filtering is not allowed for all devices")
);
return false;
}
return true;
}

protected async prepare(): Promise<void> {
try {
const results = await this.getData();

if (results.length)
return this.setSuccess(
200,
results.map((row) => ({
id: row.id,
name: row.name,
type: row.type,
}))
);

const fallbackResults = await this.getFallback();
if (!fallbackResults.length) throw Error("Error on finding devices");
this.setSuccess(
const results = await this.getOperativeSystems();

return this.setSuccess(
200,
fallbackResults.map((row) => ({
results.map((row) => ({
id: row.id,
name: row.name,
type: row.type,
type: this.mapDeviceTypeToFormFactor(row.form_factor),
}))
);
} catch (error) {
this.setError(404, error as OpenapiError);
}
}

private async getData() {
private async getOperativeSystems() {
if (this.deviceType === "all") {
return await tryber.tables.WpAppqEvdPlatform.do()
.distinct("id")
.select("name")
.select("form_factor");
}

const osFromFilters = await this.getOsFromFilters();
if (!osFromFilters.length) return [];
if (!osFromFilters.length) return await this.getFallback();

const results = await tryber.tables.WpAppqEvdPlatform.do()
.distinct("id")
.select("name")
.select("form_factor")
.whereIn("id", osFromFilters);

return results.map((row) => ({
...row,
type: this.mapDeviceTypeToFormFactor(row.form_factor),
}));
if (!results.length) await this.getFallback();
return results;
}

private async getOsFromFilters() {
Expand Down Expand Up @@ -121,10 +127,7 @@ export default class Route extends UserRoute<{

const results = await query;

return results.map((row) => ({
...row,
type: this.mapDeviceTypeToFormFactor(row.form_factor),
}));
return results;
}

private mapDeviceTypeToFormFactor(deviceType: number) {
Expand Down

0 comments on commit 58ebe74

Please sign in to comment.