Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Route page support #174

Merged
merged 3 commits into from
Sep 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/activities/services/activities.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,6 @@ export class ActivitiesService {
params: FindActivitiesInput = {},
currentUser: User = null,
): Promise<StatsActivities[]> {

const builder = this.activitiesRepository
.createQueryBuilder('ac')
.select('EXTRACT(YEAR FROM ac.date)', 'year')
Expand Down Expand Up @@ -247,7 +246,6 @@ export class ActivitiesService {
} as StatsActivities;
});
return myStats;

}

async find(params: FindActivitiesInput = {}): Promise<Activity[]> {
Expand Down Expand Up @@ -326,7 +324,7 @@ export class ActivitiesService {
ActivityRoute,
'ar',
'ar.activity_id = a.id AND (ar."publish" IN (:...publish))',
{ publish: ['log', 'public'] },
{ publish: ['public'] },
);

// Allow/disallow based on publishStatus of contained activity routes
Expand Down
4 changes: 2 additions & 2 deletions src/activities/services/activity-routes.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ export class ActivityRoutesService {
if (!currentUser) {
// Allow showing only public ascents to guests
builder.andWhere('ar."publish" IN (:...publish)', {
publish: ['log', 'public'],
publish: ['public'],
});

// Allow showing only published routes (no drafts or in_reviews)
Expand All @@ -657,7 +657,7 @@ export class ActivityRoutesService {
'(ar.user_id = :userId OR ar."publish" IN (:...publish))',
{
userId: currentUser.id,
publish: ['log', 'public'],
publish: ['public'],
},
);
// TODO: should also allow showing club ascents
Expand Down
8 changes: 8 additions & 0 deletions src/crags/crags.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ import { RouteEvent } from './entities/route-event.entity';
import { Parking } from './entities/parking.entity';
import { ParkingsService } from './services/parkings.service';
import { AreaLoader } from './loaders/area.loader';
import { ActivityRoutesService } from '../activities/services/activity-routes.service';
import { ActivitiesModule } from '../activities/activities.module';
import { ClubMember } from '../users/entities/club-member.entity';
import { Club } from '../users/entities/club.entity';

@Module({
imports: [
Expand Down Expand Up @@ -98,7 +102,10 @@ import { AreaLoader } from './loaders/area.loader';
IceFallProperty,
StarRatingVote,
Parking,
Club,
ClubMember,
]),
forwardRef(() => ActivitiesModule),
forwardRef(() => AuditModule),
BullModule.registerQueue({
name: 'summary',
Expand Down Expand Up @@ -148,6 +155,7 @@ import { AreaLoader } from './loaders/area.loader';
CragLoader,
RouteLoader,
ParkingsService,
ActivityRoutesService,
],
controllers: [UploadController],
exports: [
Expand Down
27 changes: 27 additions & 0 deletions src/crags/resolvers/routes.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ import { LatestDifficultyVotesInput } from '../dtos/latest-difficulty-votes.inpu
import { PaginatedDifficultyVotes } from '../utils/paginated-difficulty-votes';
import { MoveRouteToSectorInput } from '../dtos/move-route-to-sector.input';
import { SectorsService } from '../services/sectors.service';
import { PaginatedActivityRoutes } from '../../activities/utils/paginated-activity-routes.class';
import { ActivityRoutesService } from '../../activities/services/activity-routes.service';
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';

@Resolver(() => Route)
@UseInterceptors(DataLoaderInterceptor)
Expand All @@ -57,14 +62,18 @@ export class RoutesResolver {
private routesService: RoutesService,
private sectorsService: SectorsService,
private difficultyVotesService: DifficultyVotesService,
private starRatingVotesService: StarRatingVotesService,
private entityPropertiesService: EntityPropertiesService,
private notificationService: NotificationService,
private activityRoutesService: ActivityRoutesService,
) {}

/* QUERIES */

@Query(() => Route)
@UseFilters(NotFoundFilter)
@AllowAny()
@UseGuards(UserAuthGuard)
async route(@Args('id') id: string): Promise<Route> {
return this.routesService.findOneById(id);
}
Expand Down Expand Up @@ -251,6 +260,11 @@ export class RoutesResolver {
return this.difficultyVotesService.findByRouteId(route.id);
}

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

@ResolveField('crag', () => Crag)
async getCrag(
@Parent() route: Route,
Expand Down Expand Up @@ -286,4 +300,17 @@ export class RoutesResolver {
): Promise<RouteType> {
return loader.load(route.routeTypeId);
}

@ResolveField('activityRoutes', () => PaginatedActivityRoutes)
@UseGuards(UserAuthGuard)
async activityRoutes(
@Parent() route: Route,
@Args('input', { nullable: true }) input: FindActivityRoutesInput = {},
@CurrentUser() currentUser: User,
): Promise<PaginatedActivityRoutes> {
return this.activityRoutesService.paginate(
{ ...input, routeId: route.id },
currentUser,
);
}
}
7 changes: 7 additions & 0 deletions src/crags/services/star-rating-votes.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,11 @@ export class StarRatingVotesService {
.andWhere('srv.route_id IN (:...routeIds)', { routeIds })
.getMany();
}

async findByRouteId(routeId: string): Promise<StarRatingVote[]> {
return this.starRatingVoteRepository.find({
where: { routeId: routeId },
order: { stars: 'ASC' },
});
}
}
13 changes: 6 additions & 7 deletions test/e2e/activity.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ describe('Activity', () => {
expect(activitiesIds).not.toContain(mockData.activities.nonCragActivity.id);
});

it('should get only public (or log) activity routes and only activities containing at least one public (or log) activity route when fetching as a guest', async () => {
it('should get only public activity routes and only activities containing at least one public activity route when fetching as a guest', async () => {
const response = await request(app.getHttpServer())
.post('/graphql')
.send({
Expand Down Expand Up @@ -304,14 +304,13 @@ describe('Activity', () => {
const numOfNonPublicActivityRoutes =
response.body.data.activities.items.filter(
(a) =>
a.routes.filter((ar) => !['log', 'public'].includes(ar.publish))
.length > 0,
a.routes.filter((ar) => !['public'].includes(ar.publish)).length > 0,
).length;
expect(numOfNonPublicActivityRoutes).toEqual(0);

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

Expand All @@ -320,7 +319,7 @@ describe('Activity', () => {
const returnedActivitiesIds = response.body.data.activities.items.map(
(a) => a.id,
);
expect(returnedActivitiesIds).toContain(
expect(returnedActivitiesIds).not.toContain(
mockData.activities.activityWithLogRoutes.id,
);
expect(returnedActivitiesIds).toContain(
Expand All @@ -340,15 +339,15 @@ describe('Activity', () => {
mockData.activities.activityWithPublicRoutes.activityRoutes
.publicActivityRoute.id,
);
expect(returnedActivityRoutesIds).toContain(
expect(returnedActivityRoutesIds).not.toContain(
mockData.activities.activityWithLogRoutes.activityRoutes.logActivityRoute
.id,
);
expect(returnedActivityRoutesIds).toContain(
mockData.activities.activityWithMixedRoutes.activityRoutes
.publicActivityRoute.id,
);
expect(returnedActivityRoutesIds).toContain(
expect(returnedActivityRoutesIds).not.toContain(
mockData.activities.activityWithMixedRoutes.activityRoutes
.logActivityRoute.id,
);
Expand Down
Loading