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

Add device:mUnlink API action #60

Merged
merged 5 commits into from
Apr 7, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
42 changes: 27 additions & 15 deletions features/DeviceController.feature
Original file line number Diff line number Diff line change
Expand Up @@ -179,19 +179,6 @@ Feature: Device Manager device controller
Then I should receive an error matching:
| message | "Assets \"PERFO-non-existing\" do not exist" |

Scenario: Unlink device from an asset
Given I successfully execute the action "device-manager/device":"linkAsset" with args:
| _id | "DummyTemp_attached-ayse-unlinked" |
| assetId | "PERFO-unlinked" |
When I successfully execute the action "device-manager/device":"unlink" with args:
| _id | "DummyTemp_attached-ayse-unlinked" |
Then The document "device-manager":"devices":"DummyTemp_attached-ayse-unlinked" content match:
| assetId | null |
Then The document "tenant-ayse":"devices":"DummyTemp_attached-ayse-unlinked" content match:
| assetId | null |
And The document "tenant-ayse":"assets":"PERFO-unlinked" content match:
| measures | {} |

Scenario: Error when unlinking from an asset
When I execute the action "device-manager/device":"unlink" with args:
| _id | "DummyTemp_attached-ayse-unlinked" |
Expand All @@ -210,7 +197,7 @@ Feature: Device Manager device controller
| assetId | null |
And The document "tenant-ayse":"assets":"PERFO-unlinked" content match:
| measures | {} |

Scenario: Unlink multiple devices from multiple assets using CSV
Given I successfully execute the action "device-manager/device":"linkAsset" with args:
| _id | "DummyTemp_attached-ayse-unlinked" |
Expand All @@ -224,6 +211,31 @@ Feature: Device Manager device controller
And The document "tenant-ayse":"assets":"PERFO-unlinked" content match:
| measures | {} |

Scenario: Unlink device from an asset
And I successfully execute the action "device-manager/device":"attachTenant" with args:
| _id | "DummyTemp_detached" |
| index | "tenant-ayse" |
When I successfully execute the action "device-manager/device":"mLink" with args:
| body.records.0.deviceId | "DummyTemp_attached-ayse-unlinked" |
| body.records.0.assetId | "PERFO-unlinked" |
| body.records.1.deviceId | "DummyTemp_detached" |
| body.records.1.assetId | "PERFO-unlinked" |
When I successfully execute the action "device-manager/device":"unlink" with args:
| _id | "DummyTemp_attached-ayse-unlinked" |
Then The document "device-manager":"devices":"DummyTemp_attached-ayse-unlinked" content match:
| assetId | null |
Then The document "tenant-ayse":"devices":"DummyTemp_attached-ayse-unlinked" content match:
| assetId | null |
And The document "tenant-ayse":"assets":"PERFO-unlinked" content match:
| measures.position.reference | "detached" |
| measures.position.payloadUuid | "some-uuid" |
| measures.position.accuracy | 42 |
| measures.position.model | "_STRING_" |
| measures.position.id | "_STRING_" |
| measures.position.point.lon | 3.876716 |
| measures.position.point.lat | 43.610767 |
| measures.position.updatedAt | 1610793427950 |

Scenario: Clean payloads collection
Given I successfully execute the action "collection":"truncate" with args:
| index | "device-manager" |
Expand All @@ -247,7 +259,7 @@ Feature: Device Manager device controller
Then I should receive a result matching:
| total | 2 |
And I successfully execute the action "device-manager/device":"prunePayloads" with args:
| body.days | 0 |
| body.days | 0 |
| body.deviceModel | "DummyTemp" |
And I successfully execute the action "collection":"refresh" with args:
| index | "device-manager" |
Expand Down
12 changes: 11 additions & 1 deletion features/fixtures/fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,17 @@ module.exports = {
{
reference: 'detached',
model: 'DummyTemp',
measures: {},
measures: {
position: {
updatedAt: 1610793427950,
payloadUuid: 'some-uuid',
point: {
lat: 43.610767,
lon: 3.876716,
},
accuracy: 42,
}
},
metadata: {},
tenantId: null,
assetId: null
Expand Down
18 changes: 11 additions & 7 deletions lib/services/DeviceService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ export class DeviceService {
}

async mUnlink (devices: Device[], { strict }) {
console.log('devices', devices);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Debug leftover

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, i was somehow sure i deleted them ... 👎

const unlinkedDevices = devices.filter(device => !device._source.assetId);

if (strict && unlinkedDevices.length > 0) {
Expand Down Expand Up @@ -270,7 +271,7 @@ export class DeviceService {

assetDocuments.push({ _id: device._source.assetId, body: { measures } });
}

const updatedDevice = await this.writeToDatabase(
deviceDocuments,
async (deviceDocuments: DeviceMRequestContent[]): Promise<JSONObject> => {
Expand All @@ -294,12 +295,16 @@ export class DeviceService {
const updatedAssets = await this.writeToDatabase(
assetDocuments,
async (assetDocuments: DeviceMRequestContent[]): Promise<JSONObject> => {

console.log(JSON.stringify(assetDocuments));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DItto


const updated = await this.sdk.document.mUpdate(
document.tenantId,
'assets',
assetDocuments);

console.log(updated);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DItto


return {
successes: results.successes.concat(updated.successes),
errors: results.errors.concat(updated.errors)
Expand All @@ -315,17 +320,16 @@ export class DeviceService {
}

private async eraseAssetMeasure (tenantId: string, device: Device) {
const asset = await this.sdk.document.get(tenantId, 'assets', device._source.assetId);
const { _source: { measures } } = await this.sdk.document.get(tenantId, 'assets', device._source.assetId);

const measure = {};

for (const [key, value] of Object.entries(device._source.measures)) {
if (!asset._source.measures[key]) {
measure[key] = value;
for (const [measureName] of Object.entries(device._source.measures)) {
if (measures[measureName]) {
delete measures[measureName];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid the usage of delete who always trigger deoptimization from turbofan because the object internal shape is changing.
You can set the value to undefined, it will be ignored by the JSON parsers

}
}

return measure;
return measures;
}

private async tenantExists (tenantId: string) {
Expand Down