From 9ee5c4ac5da45a41bcfad07b25ed58194fa66d85 Mon Sep 17 00:00:00 2001 From: Anze Demsar Date: Sun, 11 Feb 2024 16:49:36 +0100 Subject: [PATCH] allow filtering statistics by route type --- .../dtos/find-activity-routes.input.ts | 4 ++ .../services/activity-routes.service.ts | 38 +++++++++++-------- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/src/activities/dtos/find-activity-routes.input.ts b/src/activities/dtos/find-activity-routes.input.ts index 6fa24bf9..7510eb71 100644 --- a/src/activities/dtos/find-activity-routes.input.ts +++ b/src/activities/dtos/find-activity-routes.input.ts @@ -33,6 +33,10 @@ export class FindActivityRoutesInput { @IsOptional() ascentType?: AscentType[]; + @Field(() => [String], { nullable: true }) + @IsOptional() + routeTypes?: string[]; + @Field(() => [String], { nullable: true }) @IsOptional() publish?: PublishType[]; diff --git a/src/activities/services/activity-routes.service.ts b/src/activities/services/activity-routes.service.ts index a61b33e7..bcc2f960 100644 --- a/src/activities/services/activity-routes.service.ts +++ b/src/activities/services/activity-routes.service.ts @@ -511,7 +511,6 @@ export class ActivityRoutesService { params: FindActivityRoutesInput = {}, currentUser: User = null, ): Promise { - const builder = this.activityRoutesRepository .createQueryBuilder('ar') .select('EXTRACT(YEAR FROM ar.date)', 'year') @@ -532,25 +531,32 @@ export class ActivityRoutesService { "(r.publish_status IN ('published', 'in_review') OR (r.publish_status = 'draft' AND ar.user_id = :userId))", { userId: currentUser.id }, ) - .andWhere( - "coalesce(p.difficulty, r.difficulty) is not null" - ) - .groupBy("p.difficulty").addGroupBy(("r.difficulty")).addGroupBy("EXTRACT(YEAR FROM ar.date)").addGroupBy("ar.ascent_type") + .andWhere('coalesce(p.difficulty, r.difficulty) is not null') + .groupBy('p.difficulty') + .addGroupBy('r.difficulty') + .addGroupBy('EXTRACT(YEAR FROM ar.date)') + .addGroupBy('ar.ascent_type') .orderBy('coalesce(p.difficulty, r.difficulty)', 'ASC') .addOrderBy('year', 'ASC'); - setBuilderCache(builder, 'getRawAndEntities'); - - const raw = await builder.getRawMany() - const myStats = raw.map((element, index) => { - return { - year: element.year, - difficulty: element.difficulty, - ascent_type: element.ascent_type, - nr_routes: element.nr_routes, - }; + if (params.routeTypes != null) { + builder.andWhere('r.route_type_id IN(:...routeTypes)', { + routeTypes: params.routeTypes, }); - return myStats; + } + + setBuilderCache(builder, 'getRawAndEntities'); + + const raw = await builder.getRawMany(); + const myStats = raw.map((element, index) => { + return { + year: element.year, + difficulty: element.difficulty, + ascent_type: element.ascent_type, + nr_routes: element.nr_routes, + }; + }); + return myStats; } async find(