Skip to content

Commit 6718883

Browse files
author
Adrien Maret
authored
Ensure unix timestamp for measuredAt (#240)
Ensure the measuredAt timestamp is an Unix Timestamp (milliseconds)
1 parent 92343d3 commit 6718883

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

features/Decoder/PayloadController.feature

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ Feature: Payloads Controller
1414
| engineId | null |
1515
| assetId | null |
1616

17+
Scenario: Reject if measuredAt is not unix timestamp
18+
Given I try to send the following "dummy-temp" payloads:
19+
| deviceEUI | temperature | measuredAt |
20+
| "12345" | 21 | 1671007889 |
21+
Then I should receive an error matching:
22+
| message | "Invalid payload: \"measuredAt\" should be a timestamp in milliseconds" |
23+
1724
Scenario: Reject with error a DummyTemp payload
1825
Given I try to send the following "dummy-temp" payloads:
1926
| deviceEUI | temperature |
@@ -61,12 +68,12 @@ Feature: Payloads Controller
6168
And I should receive a result matching:
6269
| hits[0]._source.type | "temperature" |
6370
| hits[0]._source.measuredAt | "_DATE_NOW_" |
64-
| hits[0]._source.origin._id | "DummyTemp-linked1" |
71+
| hits[0]._source.origin._id | "DummyTemp-linked1" |
6572
| hits[0]._source.origin.type | "device" |
6673
| hits[0]._source.origin.measureName | "temperature" |
6774
| hits[0]._source.origin.deviceModel | "DummyTemp" |
6875
| hits[0]._source.origin.reference | "linked1" |
69-
| hits[0]._source.asset._id | "container-linked1" |
76+
| hits[0]._source.asset._id | "container-linked1" |
7077
| hits[0]._source.asset.measureName | "temperatureExt" |
7178
| hits[0]._source.asset.metadata.weight | 10 |
7279
| hits[0]._source.asset.metadata.height | 11 |

features/fixtures/application/decoders/DummyTempDecoder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export class DummyTempDecoder extends Decoder {
4242
payload.deviceEUI,
4343
"temperature",
4444
{
45-
measuredAt: Date.now(),
45+
measuredAt: payload.measuredAt || Date.now(),
4646
type: "temperature",
4747
values: {
4848
temperature: payload.temperature,

lib/modules/decoder/DecodedPayload.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,12 @@ export class DecodedPayload<TDecoder extends Decoder = Decoder> {
4848
);
4949
}
5050

51+
this.validateMeasurement(measurement);
52+
5153
if (!this.measurementsByDevice[deviceReference]) {
5254
this.measurementsByDevice[deviceReference] = [];
5355
}
56+
5457
const decodedMeasurement: DecodedMeasurement<TMeasureValues> = {
5558
measureName,
5659
...measurement,
@@ -91,4 +94,14 @@ export class DecodedPayload<TDecoder extends Decoder = Decoder> {
9194
getMetadata(deviceReference: string): JSONObject {
9295
return this.metadataByDevice[deviceReference];
9396
}
97+
98+
private validateMeasurement<TMeasureValues>(
99+
measurement: Omit<DecodedMeasurement<TMeasureValues>, "measureName">
100+
) {
101+
if (measurement.measuredAt.toString().length !== 13) {
102+
throw new BadRequestError(
103+
`Invalid payload: "measuredAt" should be a timestamp in milliseconds`
104+
);
105+
}
106+
}
94107
}

0 commit comments

Comments
 (0)