Skip to content

Commit

Permalink
feat: add search function to storage service (#84)
Browse files Browse the repository at this point in the history
remove search function from device service
  • Loading branch information
sirily11 authored Apr 22, 2022
1 parent 0172b5b commit 57dd2a4
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -227,20 +227,4 @@ export class DeviceRegistrationService extends BaseMongoDBService<schema.IDevice
return [false, undefined];
}
}

/**
* Search devices matched using localIpAddress, remoteIpAddress
* @param key
*/
async search(key: string): Promise<schema.IDevice[]> {
const query = this.model
.find({
$or: [
{ "networkSettings.localIpAddress": key },
{ "networkSettings.remoteIpAddress": key },
],
})
.limit(configs.Configurations.numberPerPage);
return query.exec();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,49 @@ export class StorageManagementService extends BaseMongoDBService<schema.IStorage
* Search items by key
* @param key
*/
async search(key: string): Promise<schema.IStorageItem[]> {
async searchById(key: string): Promise<schema.IStorageItem[]> {
const query = this.model
.find({
$or: [{ qr_code: { $regex: ".*" + key + ".*" } }],
})
.limit(configs.Configurations.numberPerPage);
return query.exec();
}

/**
* Search devices matched using qr_code, localIpAddress, remoteIpAddress
* @param key
*/
async search(
key: string
): Promise<interfaces.db.StorageItemWithStatusDBInterface[]> {
const pipelines = [
{
$lookup: {
from: `${enums.ModelName.device}s`,
localField: "qr_code",
foreignField: "id",
as: "status",
},
},
{
$unwind: {
path: "$status",
},
},
{
$match: {
$or: [
{ "status.networkSettings.localIpAddress": key },
{ "status.networkSettings.remoteIpAddress": key },
],
},
},
];

const query = this.model
.aggregate(pipelines)
.limit(configs.Configurations.numberPerPage);
return query.exec();
}
}
46 changes: 0 additions & 46 deletions packages/etd-services/src/tests/mongodb/device/device.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,50 +151,4 @@ describe("Given a device plugin", () => {
const result = await plugin.auth(mockData.MockDeviceID, token);
expect(result[0]).toBeTruthy();
});

test("When search device by local ip address", async () => {
await schema.StorageItemModel.create(mockData.MockStorageItem);
await schema.StorageItemModel.create(mockData.MockStorageItem2);
await schema.StorageItemModel.create(mockData.MockStorageItem3);
await schema.DeviceModel.create(mockData.MockDeviceStatus);
await schema.DeviceModel.create(mockData.MockDeviceStatus2);
await schema.DeviceModel.create(mockData.MockDeviceStatus3);

const plugin = new DeviceRegistrationService();
const result = await plugin.search(
mockData.MockDeviceStatus.networkSettings.localIpAddress
);

expect(result[0].networkSettings.localIpAddress).toBe(
mockData.MockDeviceStatus.networkSettings.localIpAddress
);

expect(result[1].networkSettings.localIpAddress).toBe(
mockData.MockDeviceStatus3.networkSettings.localIpAddress
);
expect(result).toHaveLength(2);
});

test("When search device by local remote address", async () => {
await schema.StorageItemModel.create(mockData.MockStorageItem);
await schema.StorageItemModel.create(mockData.MockStorageItem2);
await schema.StorageItemModel.create(mockData.MockStorageItem3);
await schema.DeviceModel.create(mockData.MockDeviceStatus);
await schema.DeviceModel.create(mockData.MockDeviceStatus2);
await schema.DeviceModel.create(mockData.MockDeviceStatus3);

const plugin = new DeviceRegistrationService();
const result = await plugin.search(
mockData.MockDeviceStatus.networkSettings.remoteIpAddress
);

expect(result[0].networkSettings.localIpAddress).toBe(
mockData.MockDeviceStatus.networkSettings.localIpAddress
);

expect(result[1].networkSettings.localIpAddress).toBe(
mockData.MockDeviceStatus2.networkSettings.localIpAddress
);
expect(result).toHaveLength(2);
});
});
53 changes: 51 additions & 2 deletions packages/etd-services/src/tests/mongodb/storage/item.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import { mockData } from "@etherdata-blockchain/common";
import { schema } from "@etherdata-blockchain/storage-model";
import { MongoMemoryServer } from "mongodb-memory-server";
import mongoose from "mongoose";
import { StorageManagementService } from "../../../mongodb";
import {
DeviceRegistrationService,
StorageManagementService,
} from "../../../mongodb";

describe("Given a storage item", () => {
let dbServer: MongoMemoryServer;
Expand Down Expand Up @@ -69,7 +72,7 @@ describe("Given a storage item", () => {
await schema.DeviceModel.create(mockData.MockDeviceStatus2);

const plugin = new StorageManagementService();
const result = await plugin.search(mockData.MockStorageItem.qr_code);
const result = await plugin.searchById(mockData.MockStorageItem.qr_code);

expect(result[0].qr_code).toBe(mockData.MockStorageItem.qr_code);
expect(result).toHaveLength(1);
Expand All @@ -82,4 +85,50 @@ describe("Given a storage item", () => {
const authorized = plugin.auth(mockData.MockStorageItem.qr_code);
expect(authorized).toBeTruthy();
});

test("When search device by local ip address", async () => {
await schema.StorageItemModel.create(mockData.MockStorageItem);
await schema.StorageItemModel.create(mockData.MockStorageItem2);
await schema.StorageItemModel.create(mockData.MockStorageItem3);
await schema.DeviceModel.create(mockData.MockDeviceStatus);
await schema.DeviceModel.create(mockData.MockDeviceStatus2);
await schema.DeviceModel.create(mockData.MockDeviceStatus3);

const plugin = new StorageManagementService();
const result = await plugin.search(
mockData.MockDeviceStatus.networkSettings.localIpAddress
);

expect(result[0].status!.networkSettings.localIpAddress).toBe(
mockData.MockDeviceStatus.networkSettings.localIpAddress
);

expect(result[1].status!.networkSettings.localIpAddress).toBe(
mockData.MockDeviceStatus3.networkSettings.localIpAddress
);
expect(result).toHaveLength(2);
});

test("When search device by local remote address", async () => {
await schema.StorageItemModel.create(mockData.MockStorageItem);
await schema.StorageItemModel.create(mockData.MockStorageItem2);
await schema.StorageItemModel.create(mockData.MockStorageItem3);
await schema.DeviceModel.create(mockData.MockDeviceStatus);
await schema.DeviceModel.create(mockData.MockDeviceStatus2);
await schema.DeviceModel.create(mockData.MockDeviceStatus3);

const plugin = new StorageManagementService();
const result = await plugin.search(
mockData.MockDeviceStatus.networkSettings.remoteIpAddress
);

expect(result[0].status!.networkSettings.localIpAddress).toBe(
mockData.MockDeviceStatus.networkSettings.localIpAddress
);

expect(result[1].status!.networkSettings.localIpAddress).toBe(
mockData.MockDeviceStatus2.networkSettings.localIpAddress
);
expect(result).toHaveLength(2);
});
});

0 comments on commit 57dd2a4

Please sign in to comment.