Skip to content

Commit

Permalink
fix(Export): display address text for "location" fields in csv exports
Browse files Browse the repository at this point in the history
fixes #2802
  • Loading branch information
sleidig committed Jan 24, 2025
1 parent 4471d40 commit dce3d4d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ConfigurableEnumValue } from "../../../basic-datatypes/configurable-enum/configurable-enum.interface";
import moment from "moment/moment";
import { GeoLocation } from "../../../../features/location/location.datatype";

/**
* Transform a whole object into a readable format.
Expand Down Expand Up @@ -27,20 +28,31 @@ export function transformToReadableFormat(obj: any) {
* @param value the object for which a readable string should be returned
*/
export function getReadableValue(value: any): any {
if (isConfigurableEnum(value)) {
return value.label;
} else if (Array.isArray(value)) {
if (Array.isArray(value)) {
if (value.length > 0 && value.every((val) => value.indexOf(val) === 0)) {
// only return a single value if all elements in array are same
return getReadableValue(value[0]);
} else {
return value.map((v) => getReadableValue(v));
}
} else {
return value;
}

// TODO: refactor this into an extendable system where each Datatype defines their transformation instead of implementing it here

if (isConfigurableEnum(value)) {
return value.label;
}
if (isGeoLocation(value)) {
return value.locationString;
}

return value;
}

export function isConfigurableEnum(value: any): value is ConfigurableEnumValue {
return typeof value === "object" && value && "label" in value;
}

function isGeoLocation(value: any): value is GeoLocation {
return typeof value === "object" && value && "locationString" in value;
}
25 changes: 25 additions & 0 deletions src/app/core/export/download-service/download.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { EntityMapperService } from "app/core/entity/entity-mapper/entity-mapper
import { mockEntityMapper } from "app/core/entity/entity-mapper/mock-entity-mapper-service";
import { EntityDatatype } from "../../basic-datatypes/entity/entity.datatype";
import { TestEntity } from "../../../utils/test-utils/TestEntity";
import { GeoLocation } from "../../../features/location/location.datatype";

describe("DownloadService", () => {
let service: DownloadService;
Expand Down Expand Up @@ -130,6 +131,7 @@ describe("DownloadService", () => {
@DatabaseField({ dataType: "entity", label: "referenced entity 2" })
relatedEntity2: string;
}

const relatedEntity = testSchool;
const relatedEntity2 = testChild;

Expand Down Expand Up @@ -158,6 +160,7 @@ describe("DownloadService", () => {
})
relatedEntitiesArray: string[];
}

const testEntity = new EntityRefDownloadTestEntity();
testEntity.relatedEntitiesArray = [testSchool.getId(), testChild.getId()];

Expand All @@ -179,6 +182,7 @@ describe("DownloadService", () => {
})
relatedEntitiesArray: string[];
}

const testEntity = new EntityRefDownloadTestEntity();
testEntity.relatedEntitiesArray = ["undefined-id", testChild.getId()];

Expand Down Expand Up @@ -265,4 +269,25 @@ describe("DownloadService", () => {
`"${dateString}","10","someString"`,
]);
});

it("should export a location as its locationString only", async () => {
const locationObject: GeoLocation = {
locationString: "Test Location",
geoLookup: { lat: 0, lon: 0, display_name: "lookup location" },
};

const exportData = [
{
address: locationObject,
},
];

const csv = await service.createCsv(exportData);

const results = csv.split(DownloadService.SEPARATOR_ROW);
expect(results).toEqual([
'"address"',
`"${locationObject.locationString}"`,
]);
});
});

0 comments on commit dce3d4d

Please sign in to comment.