From 3920f6b54fd1a45c78f8a17ffff18d184b85da2d Mon Sep 17 00:00:00 2001 From: jackneer Date: Sun, 27 Oct 2024 11:27:06 +0800 Subject: [PATCH] allow pass customized filename for exporting csv --- lib/modules/device/DevicesController.ts | 3 +- lib/modules/device/types/DeviceApi.ts | 2 + .../devices/action-export-measures.test.ts | 47 +++++++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) diff --git a/lib/modules/device/DevicesController.ts b/lib/modules/device/DevicesController.ts index dd2acdf3..7d78211c 100644 --- a/lib/modules/device/DevicesController.ts +++ b/lib/modules/device/DevicesController.ts @@ -514,6 +514,7 @@ export class DevicesController { const exportId = request.getString("exportId"); const { id } = await this.measureExporter.getExport(engineId, exportId); + const filename = request.getString("filename") || `device-${id}.csv`; const stream = await this.measureExporter.sendExport( engineId, exportId, @@ -521,7 +522,7 @@ export class DevicesController { request.response.configure({ headers: { - "Content-Disposition": `attachment; filename="device-${id}.csv"`, + "Content-Disposition": `attachment; filename="${filename}"`, "Content-Type": "text/csv", }, }); diff --git a/lib/modules/device/types/DeviceApi.ts b/lib/modules/device/types/DeviceApi.ts index 948eb210..d4659b3d 100644 --- a/lib/modules/device/types/DeviceApi.ts +++ b/lib/modules/device/types/DeviceApi.ts @@ -245,6 +245,8 @@ export interface ApiDeviceExportMeasuresRequest query?: JSONObject; sort?: JSONObject; }; + + filename?: string; } export type ApiDeviceExportMeasuresResult = { link: string; diff --git a/tests/scenario/modules/devices/action-export-measures.test.ts b/tests/scenario/modules/devices/action-export-measures.test.ts index b706859f..f114ba4a 100644 --- a/tests/scenario/modules/devices/action-export-measures.test.ts +++ b/tests/scenario/modules/devices/action-export-measures.test.ts @@ -209,4 +209,51 @@ describe("DevicesController:exportMeasures", () => { "Measure Id,Measured At,Measured At ISO,Measure Type,Device Id,Device Model,Asset Id,Asset Model,temperature.temperature,accelerationSensor.acceleration.x,accelerationSensor.acceleration.y,accelerationSensor.acceleration.z,accelerationSensor.accuracy,battery.battery\n", ); }); + + it("should export a file with a customized filename", async () => { + const filename = "my-customized-filename.csv"; + await sendPayloads(sdk, "dummy-temp", [ + { deviceEUI: "linked1", temperature: 37 }, + ]); + await sdk.collection.refresh("engine-ayse", "measures"); + + const { result } = await sdk.query({ + controller: "device-manager/devices", + action: "exportMeasures", + engineId: "engine-ayse", + _id: "DummyTemp-linked1", + lang: "koncorde", + filename, + }); + + const response = await axios.get("http://localhost:7512" + result.link, { + responseType: "stream", + }); + + expect(response.headers).toHaveProperty( + "Content-Disposition", + `attachment; filename="${filename}"`, + ); + }); + + it("should export a file with a default filename", async () => { + await sendPayloads(sdk, "dummy-temp", [ + { deviceEUI: "linked1", temperature: 37 }, + ]); + await sdk.collection.refresh("engine-ayse", "measures"); + + const { result } = await sdk.query({ + controller: "device-manager/devices", + action: "exportMeasures", + engineId: "engine-ayse", + _id: "DummyTemp-linked1", + lang: "koncorde", + }); + + const response = await axios.get("http://localhost:7512" + result.link, { + responseType: "stream", + }); + + expect(response.headers).toHaveProperty("Content-Disposition"); + }); });