Skip to content

Commit

Permalink
fix: exports support a count query
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSlimvReal authored Mar 25, 2022
1 parent b43ab66 commit 9216351
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
38 changes: 31 additions & 7 deletions src/app/core/export/export-service/export.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,13 +398,6 @@ describe("ExportService", () => {
const todayNote = await createNoteInDB("today", [child]);
todayNote.date = new Date();
await entityMapper.save(todayNote);
const query = [
{ query: "name" },
{
query: ":getRelated(Note, children)[* date > ?]",
subQueries: [{ query: "subject" }],
},
];

let result = await service.createCsv(
undefined,
Expand All @@ -419,6 +412,13 @@ describe("ExportService", () => {
let resultRows = result.split(ExportService.SEPARATOR_ROW);
expect(resultRows).toEqual(['"subject"', '"yesterday"', '"today"']);

const query = [
{ query: "name" },
{
query: ":getRelated(Note, children)[* date > ?]",
subQueries: [{ query: "subject" }],
},
];
result = await service.createCsv(
[child],
query,
Expand Down Expand Up @@ -459,6 +459,30 @@ describe("ExportService", () => {
);
});

it("should work when using the count function", async () => {
await createNoteInDB("first", [new Child(), new Child()]);
await createNoteInDB("second", [new Child()]);

const result = await service.createCsv(undefined, [
{
query: `${Note.ENTITY_TYPE}:toArray`,
subQueries: [
{ query: "subject" },
{ query: ".children:count", label: "Children" },
],
},
]);

const resultRows = result.split(ExportService.SEPARATOR_ROW);
expect(resultRows).toEqual(
jasmine.arrayWithExactContents([
'"subject","Children"',
'"first","2"',
'"second","1"',
])
);
});

async function createChildInDB(name: string): Promise<Child> {
const child = new Child();
child.name = name;
Expand Down
8 changes: 5 additions & 3 deletions src/app/core/export/export-service/export.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,13 @@ export class ExportService {
[object]
);

if (!exportColumnConfig.subQueries && value.length === 1) {
// queryData() always returns an array, simple queries should be a direct value however
if (!Array.isArray(value)) {
return value;
} else if (!exportColumnConfig.subQueries && value.length === 1) {
return value[0];
} else {
return value.filter((val) => val !== undefined);
}
return value.filter((val) => val !== undefined);
}

/**
Expand Down

0 comments on commit 9216351

Please sign in to comment.