Skip to content

Commit

Permalink
allow filtering statistics by route type
Browse files Browse the repository at this point in the history
  • Loading branch information
demshy committed Feb 11, 2024
1 parent 23def55 commit 9ee5c4a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
4 changes: 4 additions & 0 deletions src/activities/dtos/find-activity-routes.input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ export class FindActivityRoutesInput {
@IsOptional()
ascentType?: AscentType[];

@Field(() => [String], { nullable: true })
@IsOptional()
routeTypes?: string[];

@Field(() => [String], { nullable: true })
@IsOptional()
publish?: PublishType[];
Expand Down
38 changes: 22 additions & 16 deletions src/activities/services/activity-routes.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,6 @@ export class ActivityRoutesService {
params: FindActivityRoutesInput = {},
currentUser: User = null,
): Promise<StatsActivities[]> {

const builder = this.activityRoutesRepository
.createQueryBuilder('ar')
.select('EXTRACT(YEAR FROM ar.date)', 'year')
Expand All @@ -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(
Expand Down

0 comments on commit 9ee5c4a

Please sign in to comment.