From 92163515ddb7ef2d5cb7d04f6f2f14029cdc565c Mon Sep 17 00:00:00 2001 From: Simon <33730997+TheSlimvReal@users.noreply.github.com> Date: Fri, 25 Mar 2022 10:51:22 +0100 Subject: [PATCH] fix: exports support a count query --- .../export-service/export.service.spec.ts | 38 +++++++++++++++---- .../export/export-service/export.service.ts | 8 ++-- 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/src/app/core/export/export-service/export.service.spec.ts b/src/app/core/export/export-service/export.service.spec.ts index 160ca881b8..5ee783094a 100644 --- a/src/app/core/export/export-service/export.service.spec.ts +++ b/src/app/core/export/export-service/export.service.spec.ts @@ -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, @@ -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, @@ -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 { const child = new Child(); child.name = name; diff --git a/src/app/core/export/export-service/export.service.ts b/src/app/core/export/export-service/export.service.ts index 0d8c53f4d2..76f55aa7d2 100644 --- a/src/app/core/export/export-service/export.service.ts +++ b/src/app/core/export/export-service/export.service.ts @@ -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); } /**