Skip to content

Commit

Permalink
allow pass customized filename for exporting csv
Browse files Browse the repository at this point in the history
  • Loading branch information
jackneer committed Oct 27, 2024
1 parent be8cfc4 commit 3920f6b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/modules/device/DevicesController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -514,14 +514,15 @@ 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,
);

request.response.configure({
headers: {
"Content-Disposition": `attachment; filename="device-${id}.csv"`,
"Content-Disposition": `attachment; filename="${filename}"`,
"Content-Type": "text/csv",
},
});
Expand Down
2 changes: 2 additions & 0 deletions lib/modules/device/types/DeviceApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ export interface ApiDeviceExportMeasuresRequest
query?: JSONObject;
sort?: JSONObject;
};

filename?: string;
}
export type ApiDeviceExportMeasuresResult = {
link: string;
Expand Down
47 changes: 47 additions & 0 deletions tests/scenario/modules/devices/action-export-measures.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ApiDeviceExportMeasuresRequest>({
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<ApiDeviceExportMeasuresRequest>({
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");
});
});

0 comments on commit 3920f6b

Please sign in to comment.