Skip to content

Commit

Permalink
Merge pull request #175 from plezanje-net/log-routes
Browse files Browse the repository at this point in the history
Log routes
  • Loading branch information
demshy authored Sep 12, 2024
2 parents 7b392e3 + c103dbd commit c4ef3ec
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
ls ${{ github.workspace }}
- name: Install and prepare
run: |
docker-compose -f docker-compose.e2e.yml up -d
docker compose -f docker-compose.e2e.yml up -d
npm install
- name: Run tests
run: |
Expand Down
12 changes: 9 additions & 3 deletions src/activities/entities/activity-route.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
ManyToOne,
Index,
} from 'typeorm';
import { ObjectType, Field } from '@nestjs/graphql';
import { ObjectType, Field, registerEnumType } from '@nestjs/graphql';
import { Route } from '../../crags/entities/route.entity';
import { Activity } from './activity.entity';
import { Pitch } from '../../crags/entities/pitch.entity';
Expand All @@ -31,6 +31,9 @@ export enum AscentType {
T_ATTEMPT = 't_attempt',
TICK = 'tick',
}
registerEnumType(AscentType, {
name: 'AscentType',
});

export const tickAscentTypes = new Set([
AscentType.ONSIGHT,
Expand Down Expand Up @@ -64,6 +67,9 @@ export enum PublishType {
LOG = 'log',
PRIVATE = 'private',
}
registerEnumType(PublishType, {
name: 'PublishType',
});

/**
* Has Triggers:
Expand Down Expand Up @@ -111,15 +117,15 @@ export class ActivityRoute extends BaseEntity {
enum: AscentType,
default: AscentType.REDPOINT,
})
@Field()
@Field((type) => AscentType)
ascentType: AscentType;

@Column({
type: 'enum',
enum: PublishType,
default: PublishType.PRIVATE,
})
@Field()
@Field((type) => PublishType)
publish: PublishType;

@Column({ nullable: true })
Expand Down
1 change: 0 additions & 1 deletion src/activities/resolvers/activity-routes.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ export class ActivityRoutesResolver {
return this.activityRoutesService.getStats(input, currentUser);
}


@UseGuards(UserAuthGuard)
@Query(() => [ActivityRoute])
myCragSummary(
Expand Down
9 changes: 9 additions & 0 deletions src/crags/dtos/find-difficulty-votes.input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { InputType, Field } from '@nestjs/graphql';
import { IsOptional } from 'class-validator';

@InputType()
export class FindDifficultyVotesInput {
@Field({ nullable: true })
@IsOptional()
userId?: string;
}
9 changes: 9 additions & 0 deletions src/crags/dtos/find-star-rating-votes.input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { InputType, Field } from '@nestjs/graphql';
import { IsOptional } from 'class-validator';

@InputType()
export class FindStarRatingVotesInput {
@Field({ nullable: true })
@IsOptional()
userId?: string;
}
16 changes: 12 additions & 4 deletions src/crags/resolvers/routes.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ import { ActivityRoutesService } from '../../activities/services/activity-routes
import { FindActivityRoutesInput } from '../../activities/dtos/find-activity-routes.input';
import { StarRatingVotesService } from '../services/star-rating-votes.service';
import { StarRatingVote } from '../entities/star-rating-vote.entity';
import { FindDifficultyVotesInput } from '../dtos/find-difficulty-votes.input';
import { FindStarRatingVotesInput } from '../dtos/find-star-rating-votes.input';

@Resolver(() => Route)
@UseInterceptors(DataLoaderInterceptor)
Expand Down Expand Up @@ -256,13 +258,19 @@ export class RoutesResolver {
}

@ResolveField('difficultyVotes', () => [DifficultyVote])
async difficultyVotes(@Parent() route: Route): Promise<DifficultyVote[]> {
return this.difficultyVotesService.findByRouteId(route.id);
async difficultyVotes(
@Parent() route: Route,
@Args('input', { nullable: true }) input: FindDifficultyVotesInput = {},
): Promise<DifficultyVote[]> {
return this.difficultyVotesService.findByRouteId(route.id, input);
}

@ResolveField('starRatingVotes', () => [StarRatingVote])
async starRatingVotes(@Parent() route: Route): Promise<StarRatingVote[]> {
return this.starRatingVotesService.findByRouteId(route.id);
async starRatingVotes(
@Parent() route: Route,
@Args('input', { nullable: true }) input: FindStarRatingVotesInput = {},
): Promise<StarRatingVote[]> {
return this.starRatingVotesService.findByRouteId(route.id, input);
}

@ResolveField('crag', () => Crag)
Expand Down
13 changes: 11 additions & 2 deletions src/crags/services/difficulty-votes.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { setBuilderCache } from '../../core/utils/entity-cache/entity-cache-help
import { LatestDifficultyVotesInputServiceInput } from '../dtos/latest-difficulty-votes-service.input';
import { DifficultyVote } from '../entities/difficulty-vote.entity';
import { PaginatedDifficultyVotes } from '../utils/paginated-difficulty-votes';
import { FindDifficultyVotesInput } from '../dtos/find-difficulty-votes.input';

@Injectable()
export class DifficultyVotesService {
Expand All @@ -15,9 +16,17 @@ export class DifficultyVotesService {
private difficultyVoteRepository: Repository<DifficultyVote>,
) {}

async findByRouteId(routeId: string): Promise<DifficultyVote[]> {
async findByRouteId(
routeId: string,
input: FindDifficultyVotesInput = {},
): Promise<DifficultyVote[]> {
const where = {
...(input.userId && { userId: input.userId }),
...{ routeId },
};

const grades = this.difficultyVoteRepository.find({
where: { routeId: routeId },
where,
order: { difficulty: 'ASC' },
});

Expand Down
13 changes: 11 additions & 2 deletions src/crags/services/star-rating-votes.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { User } from '../../users/entities/user.entity';
import { StarRatingVote } from '../entities/star-rating-vote.entity';
import { FindStarRatingVotesInput } from '../dtos/find-star-rating-votes.input';

@Injectable()
export class StarRatingVotesService {
Expand All @@ -19,9 +20,17 @@ export class StarRatingVotesService {
.getMany();
}

async findByRouteId(routeId: string): Promise<StarRatingVote[]> {
async findByRouteId(
routeId: string,
input: FindStarRatingVotesInput = {},
): Promise<StarRatingVote[]> {
const where = {
...(input.userId && { userId: input.userId }),
...{ routeId },
};

return this.starRatingVoteRepository.find({
where: { routeId: routeId },
where,
order: { stars: 'ASC' },
});
}
Expand Down
14 changes: 11 additions & 3 deletions test/e2e/activity.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import { MailService } from '../../src/notification/services/mail.service';
import { initializeDbConn, prepareEnvironment, seedDatabase } from './helpers';
import { UsersModule } from '../../src/users/users.module';
import { CragsModule } from '../../src/crags/crags.module';
import { AscentType } from '../../src/activities/entities/activity-route.entity';
import {
AscentType,
PublishType,
} from '../../src/activities/entities/activity-route.entity';
import {
Activity,
ActivityType,
Expand Down Expand Up @@ -304,13 +307,18 @@ describe('Activity', () => {
const numOfNonPublicActivityRoutes =
response.body.data.activities.items.filter(
(a) =>
a.routes.filter((ar) => !['public'].includes(ar.publish)).length > 0,
a.routes.filter(
(ar) => ![PublishType.PUBLIC].includes(ar.publish.toLowerCase()),
).length > 0,
).length;
expect(numOfNonPublicActivityRoutes).toEqual(0);

const numOfActivitiesWithNoPublicActivityRoutes =
response.body.data.activities.items.filter(
(a) => !a.routes.some((ar) => ['public'].includes(ar.publish)),
(a) =>
!a.routes.some((ar) =>
[PublishType.PUBLIC].includes(ar.publish.toLowerCase()),
),
).length;
expect(numOfActivitiesWithNoPublicActivityRoutes).toEqual(0);

Expand Down
26 changes: 17 additions & 9 deletions test/e2e/activityRoutes.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ describe('Activity', () => {
queryResponse.body.data.activity.routes.filter(
(r) =>
r.route.id == mockRoutes[0].id &&
!(r.ascentType == AscentType.REPEAT),
!(r.ascentType.toLowerCase() == AscentType.REPEAT),
)[0].orderScore,
).toBe(mockRoutes[0].difficulty + 100);
expect(
Expand All @@ -207,27 +207,31 @@ describe('Activity', () => {
expect(
queryResponse.body.data.activity.routes.filter(
(r) =>
r.route.id == mockRoutes[2].id && r.AscentType != AscentType.REPEAT,
r.route.id == mockRoutes[2].id &&
r.ascentType.toLowerCase() != AscentType.REPEAT,
)[0].orderScore,
).toBe(mockRoutes[2].difficulty);
expect(
queryResponse.body.data.activity.routes.filter(
(r) =>
r.route.id == mockRoutes[2].id && r.AscentType != AscentType.REPEAT,
r.route.id == mockRoutes[2].id &&
r.ascentType.toLowerCase() != AscentType.REPEAT,
)[0].rankingScore,
).toBe(mockRoutes[2].difficulty);

// check repeat scores
expect(
queryResponse.body.data.activity.routes.filter(
(r) =>
r.route.id == mockRoutes[2].id && r.ascentType == AscentType.REPEAT,
r.route.id == mockRoutes[2].id &&
r.ascentType.toLowerCase() == AscentType.REPEAT,
)[0].orderScore,
).toBe(mockRoutes[2].difficulty - 10);
expect(
queryResponse.body.data.activity.routes.filter(
(r) =>
r.route.id == mockRoutes[2].id && r.ascentType == AscentType.REPEAT,
r.route.id == mockRoutes[2].id &&
r.ascentType.toLowerCase() == AscentType.REPEAT,
)[0].rankingScore,
).toBe(0);

Expand Down Expand Up @@ -295,27 +299,31 @@ describe('Activity', () => {
expect(
queryResponse.body.data.activity.routes.filter(
(r) =>
r.route.id == mockRoutes[8].id && r.AscentType != AscentType.T_REPEAT,
r.route.id == mockRoutes[8].id &&
r.ascentType.toLowerCase() != AscentType.T_REPEAT,
)[0].orderScore,
).toBe(mockRoutes[8].difficulty * 0.0001);
expect(
queryResponse.body.data.activity.routes.filter(
(r) =>
r.route.id == mockRoutes[8].id && r.AscentType != AscentType.T_REPEAT,
r.route.id == mockRoutes[8].id &&
r.ascentType.toLowerCase() != AscentType.T_REPEAT,
)[0].rankingScore,
).toBe(0);

// check tr repeat scores
expect(
queryResponse.body.data.activity.routes.filter(
(r) =>
r.route.id == mockRoutes[8].id && r.ascentType == AscentType.T_REPEAT,
r.route.id == mockRoutes[8].id &&
r.ascentType.toLowerCase() == AscentType.T_REPEAT,
)[0].orderScore,
).toBe((mockRoutes[8].difficulty - 10) * 0.0001);
expect(
queryResponse.body.data.activity.routes.filter(
(r) =>
r.route.id == mockRoutes[8].id && r.ascentType == AscentType.T_REPEAT,
r.route.id == mockRoutes[8].id &&
r.ascentType.toLowerCase() == AscentType.T_REPEAT,
)[0].rankingScore,
).toBe(0);

Expand Down

0 comments on commit c4ef3ec

Please sign in to comment.