From 64809570e10051ea82b0f04a5032e2bf5a42b7e6 Mon Sep 17 00:00:00 2001 From: salamca Date: Sun, 31 Dec 2023 17:16:59 +0100 Subject: [PATCH 1/8] Fix missing area slugs and set area slug as non-nullable. --- src/crags/entities/area.entity.ts | 2 +- .../1703958939270-areaSlugNonNullable.ts | 51 +++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 src/migration/1703958939270-areaSlugNonNullable.ts diff --git a/src/crags/entities/area.entity.ts b/src/crags/entities/area.entity.ts index bbd7d48..2b2ccd9 100644 --- a/src/crags/entities/area.entity.ts +++ b/src/crags/entities/area.entity.ts @@ -35,7 +35,7 @@ export class Area extends BaseEntity { @Field() name: string; - @Column({ nullable: true }) + @Column() @Field() slug: string; diff --git a/src/migration/1703958939270-areaSlugNonNullable.ts b/src/migration/1703958939270-areaSlugNonNullable.ts new file mode 100644 index 0000000..4c6ebb2 --- /dev/null +++ b/src/migration/1703958939270-areaSlugNonNullable.ts @@ -0,0 +1,51 @@ +import slugify from 'slugify'; +import { MigrationInterface, QueryRunner } from 'typeorm'; + +export class areaSlugNonNullable1703958939270 implements MigrationInterface { + name = 'areaSlugNonNullable1703958939270'; + + public async up(queryRunner: QueryRunner): Promise { + // Some areas that were added by users have slugs missing. Add slugs before making slug non-nullable. + const missingSlugAreas = await queryRunner.query( + `SELECT a.name, a.id FROM "area" a WHERE a.slug IS NULL`, + ); + + for (let i = 0; i < missingSlugAreas.length; i++) { + const area = missingSlugAreas[i]; + const slug = await this.generateAreaSlug(area.name, queryRunner); + await queryRunner.query( + `UPDATE "area" a SET "slug" = '${slug}' WHERE a.id = '${area.id}'`, + ); + } + + await queryRunner.query( + `ALTER TABLE "area" ALTER COLUMN "slug" SET NOT NULL`, + ); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `ALTER TABLE "area" ALTER COLUMN "slug" DROP NOT NULL`, + ); + } + + private async generateAreaSlug(areaName: string, queryRunner: QueryRunner) { + let slug = slugify(areaName, { lower: true }); + let suffixCounter = 0; + let suffix = ''; + + while ( + ( + await queryRunner.query( + `SELECT * FROM "area" a WHERE a.slug = '${slug}${suffix}'`, + ) + ).length > 0 + ) { + suffixCounter++; + suffix = '-' + suffixCounter; + } + slug += suffix; + + return slug; + } +} From f5696c8eca84b6a5d9cf9a0469178c5b694d4c06 Mon Sep 17 00:00:00 2001 From: salamca Date: Sun, 31 Dec 2023 17:27:43 +0100 Subject: [PATCH 2/8] Add slug generation for new areas. --- src/crags/services/areas.service.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/crags/services/areas.service.ts b/src/crags/services/areas.service.ts index 630f01b..c04e26f 100644 --- a/src/crags/services/areas.service.ts +++ b/src/crags/services/areas.service.ts @@ -6,6 +6,7 @@ import { InjectRepository } from '@nestjs/typeorm'; import { FindManyOptions, IsNull, MoreThan, Repository } from 'typeorm'; import { Country } from '../entities/country.entity'; import { FindAreasInput } from '../dtos/find-areas.input'; +import slugify from 'slugify'; @Injectable() export class AreasService { @@ -61,6 +62,8 @@ export class AreasService { await this.countryRepository.findOneByOrFail({ id: data.countryId }), ); + area.slug = await this.generateAreaSlug(area.name); + return this.areasRepository.save(area); } @@ -81,4 +84,22 @@ export class AreasService { return this.areasRepository.remove(area).then(() => true); } + + private async generateAreaSlug(areaName: string) { + let slug = slugify(areaName, { lower: true }); + let suffixCounter = 0; + let suffix = ''; + + while ( + await this.areasRepository.findOne({ + where: { slug: slug + suffix }, + }) + ) { + suffixCounter++; + suffix = '-' + suffixCounter; + } + slug += suffix; + + return slug; + } } From d9b2883df30840181d56e9d7840ece9fdbfb29b6 Mon Sep 17 00:00:00 2001 From: salamca Date: Sun, 31 Dec 2023 17:30:45 +0100 Subject: [PATCH 3/8] Add all crags query. --- src/crags/resolvers/crags.resolver.ts | 7 +++++++ src/crags/services/crags.service.ts | 8 +++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/crags/resolvers/crags.resolver.ts b/src/crags/resolvers/crags.resolver.ts index 24146c4..0a40969 100644 --- a/src/crags/resolvers/crags.resolver.ts +++ b/src/crags/resolvers/crags.resolver.ts @@ -84,6 +84,13 @@ export class CragsResolver { }); } + @UseGuards(UserAuthGuard) + // TODO: add conditions for showing 'public' crags to everyone (in service) + @Query(() => [Crag]) + async crags(): Promise { + return this.cragsService.findAll(); + } + /* MUTATIONS */ @UseGuards(UserAuthGuard) diff --git a/src/crags/services/crags.service.ts b/src/crags/services/crags.service.ts index 848f0db..2c6ba9d 100644 --- a/src/crags/services/crags.service.ts +++ b/src/crags/services/crags.service.ts @@ -67,6 +67,12 @@ export class CragsService { return crags; } + async findAll(): Promise { + const qb = this.cragsRepository.createQueryBuilder('c'); + const result = qb.getMany(); + return result; + } + async processAllCrags() { ( await this.cragsRepository.find({ @@ -257,7 +263,7 @@ export class CragsService { routeTypeId: params.routeTypeId, }); } - + console.log(builder.getSql()); setBuilderCache(builder); return builder; From afa244fbe81321e3f42cf550ff605b9749686933 Mon Sep 17 00:00:00 2001 From: salamca Date: Thu, 11 Jan 2024 20:20:32 +0100 Subject: [PATCH 4/8] Rename column rainProof to rainproof. --- src/crags/entities/crag.entity.ts | 2 +- .../1705000587590-fixRainproofFieldName.ts | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 src/migration/1705000587590-fixRainproofFieldName.ts diff --git a/src/crags/entities/crag.entity.ts b/src/crags/entities/crag.entity.ts index 27664b4..a1e776b 100644 --- a/src/crags/entities/crag.entity.ts +++ b/src/crags/entities/crag.entity.ts @@ -257,7 +257,7 @@ export class Crag extends BaseEntity { @Column({ nullable: true }) @Field({ nullable: true }) - rainProof: boolean; + rainproof: boolean; @OneToOne(() => Image, { nullable: true }) @JoinColumn() diff --git a/src/migration/1705000587590-fixRainproofFieldName.ts b/src/migration/1705000587590-fixRainproofFieldName.ts new file mode 100644 index 0000000..e11793e --- /dev/null +++ b/src/migration/1705000587590-fixRainproofFieldName.ts @@ -0,0 +1,17 @@ +import { MigrationInterface, QueryRunner } from 'typeorm'; + +export class fixRainproofFieldName1705000587590 implements MigrationInterface { + name = 'fixRainproofFieldName1705000587590'; + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `ALTER TABLE "crag" RENAME COLUMN "rain_proof" TO "rainproof"`, + ); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `ALTER TABLE "crag" RENAME COLUMN "rainproof" TO "rain_proof"`, + ); + } +} From 04f48cca11a39f421086ef6ec4d351ca3ad81ffd Mon Sep 17 00:00:00 2001 From: Anze Demsar Date: Sun, 21 Jan 2024 12:11:36 +0100 Subject: [PATCH 5/8] add area loader --- src/crags/crags.module.ts | 2 ++ src/crags/loaders/area.loader.ts | 24 ++++++++++++++++++++++++ src/crags/resolvers/areas.resolver.ts | 23 ++++++++++++++++++++++- src/crags/resolvers/crags.resolver.ts | 11 +++++++++++ src/crags/services/areas.service.ts | 5 +++++ 5 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 src/crags/loaders/area.loader.ts diff --git a/src/crags/crags.module.ts b/src/crags/crags.module.ts index 9260e05..1bcb273 100644 --- a/src/crags/crags.module.ts +++ b/src/crags/crags.module.ts @@ -70,6 +70,7 @@ import { RouteLoader } from './loaders/route.loader'; 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'; @Module({ imports: [ @@ -143,6 +144,7 @@ import { ParkingsService } from './services/parkings.service'; MailService, SummaryQueueConsumer, ConfigService, + AreaLoader, CragLoader, RouteLoader, ParkingsService, diff --git a/src/crags/loaders/area.loader.ts b/src/crags/loaders/area.loader.ts new file mode 100644 index 0000000..5792e1c --- /dev/null +++ b/src/crags/loaders/area.loader.ts @@ -0,0 +1,24 @@ +import DataLoader from 'dataloader'; +import { Injectable } from '@nestjs/common'; +import { NestDataLoader } from '../../core/interceptors/data-loader.interceptor'; +import { Area } from '../entities/area.entity'; +import { AreasService } from '../services/areas.service'; + +@Injectable() +export class AreaLoader implements NestDataLoader { + constructor(private readonly areasService: AreasService) {} + + generateDataLoader(): DataLoader { + return new DataLoader(async (keys) => { + const areas = await this.areasService.findByIds(keys.map((k) => k)); + + const areasMap: { [key: string]: Area } = {}; + + areas.forEach((area) => { + areasMap[area.id] = area; + }); + + return keys.map((areaId) => areasMap[areaId] ?? null); + }); + } +} diff --git a/src/crags/resolvers/areas.resolver.ts b/src/crags/resolvers/areas.resolver.ts index 10dc5a9..6721ed1 100644 --- a/src/crags/resolvers/areas.resolver.ts +++ b/src/crags/resolvers/areas.resolver.ts @@ -1,4 +1,11 @@ -import { Resolver, Mutation, Args, Query } from '@nestjs/graphql'; +import { + Resolver, + Mutation, + Args, + Query, + ResolveField, + Parent, +} from '@nestjs/graphql'; import { Area } from '../entities/area.entity'; import { Roles } from '../../auth/decorators/roles.decorator'; import { UseFilters, UseInterceptors } from '@nestjs/common'; @@ -8,6 +15,11 @@ import { CreateAreaInput } from '../dtos/create-area.input'; import { AreasService } from '../services/areas.service'; import { AllowAny } from '../../auth/decorators/allow-any.decorator'; import { NotFoundFilter } from '../filters/not-found.filter'; +import { Country } from '../entities/country.entity'; +import { Loader } from '../../core/interceptors/data-loader.interceptor'; +import { CountryLoader } from '../loaders/country.loader'; +import { Crag } from '../entities/crag.entity'; +import DataLoader from 'dataloader'; @Resolver(() => Area) export class AreasResolver { @@ -44,4 +56,13 @@ export class AreasResolver { async deleteArea(@Args('id') id: string): Promise { return this.areasService.delete(id); } + + @ResolveField('country', () => Country) + async getCountry( + @Parent() crag: Crag, + @Loader(CountryLoader) + loader: DataLoader, + ): Promise { + return loader.load(crag.countryId); + } } diff --git a/src/crags/resolvers/crags.resolver.ts b/src/crags/resolvers/crags.resolver.ts index 0a40969..bc567ad 100644 --- a/src/crags/resolvers/crags.resolver.ts +++ b/src/crags/resolvers/crags.resolver.ts @@ -44,6 +44,8 @@ import { NotificationService } from '../../notification/services/notification.se import { ForeignKeyConstraintFilter } from '../filters/foreign-key-constraint.filter'; import { ImagesService } from '../services/images.service'; import { Roles } from '../../auth/decorators/roles.decorator'; +import { Area } from '../entities/area.entity'; +import { AreaLoader } from '../loaders/area.loader'; @Resolver(() => Crag) @UseInterceptors(DataLoaderInterceptor) @@ -218,6 +220,15 @@ export class CragsResolver { return loader.load(crag.countryId); } + @ResolveField('area', () => Area, { nullable: true }) + async getArea( + @Parent() crag: Area, + @Loader(AreaLoader) + loader: DataLoader, + ): Promise { + return crag.areaId ? loader.load(crag.areaId) : null; + } + @Mutation(() => Boolean) @Roles('admin') @UseGuards(UserAuthGuard) diff --git a/src/crags/services/areas.service.ts b/src/crags/services/areas.service.ts index c04e26f..2f14331 100644 --- a/src/crags/services/areas.service.ts +++ b/src/crags/services/areas.service.ts @@ -25,6 +25,11 @@ export class AreasService { return this.areasRepository.findOneByOrFail({ slug }); } + findByIds(ids: string[]): Promise { + const qb = this.areasRepository.createQueryBuilder('area'); + return qb.whereInIds(ids).getMany(); + } + find(params: FindAreasInput = {}): Promise { const options: FindManyOptions = { order: {}, From 642fa1ed67a75b6901a85f1f93f9b7cf7fcabcd1 Mon Sep 17 00:00:00 2001 From: salamca Date: Sun, 21 Jan 2024 16:06:25 +0100 Subject: [PATCH 6/8] Add grade distros json field (nrRoutesByGrade) to crag entity. --- package-lock.json | 19 +++- package.json | 1 + src/crags/entities/crag.entity.ts | 5 + src/crags/services/crags.service.ts | 2 +- .../1705839051844-addNrRoutesByGradeToCrag.ts | 104 ++++++++++++++++++ 5 files changed, 128 insertions(+), 3 deletions(-) create mode 100644 src/migration/1705839051844-addNrRoutesByGradeToCrag.ts diff --git a/package-lock.json b/package-lock.json index 7cab869..3de9242 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "plezanje-graphql", - "version": "0.0.1", + "version": "0.0.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "plezanje-graphql", - "version": "0.0.1", + "version": "0.0.2", "license": "UNLICENSED", "dependencies": { "@nestjs/apollo": "^10.1.3", @@ -32,6 +32,7 @@ "dotenv": "^16.0.3", "fs": "0.0.1-security", "graphql": "^16.6.0", + "graphql-type-json": "^0.3.2", "handlebars": "^4.7.7", "hbs": "^4.2.0", "ioredis": "^5.2.3", @@ -6161,6 +6162,14 @@ "graphql": "^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0 || ^16.0.0" } }, + "node_modules/graphql-type-json": { + "version": "0.3.2", + "resolved": "https://registry.npmjs.org/graphql-type-json/-/graphql-type-json-0.3.2.tgz", + "integrity": "sha512-J+vjof74oMlCWXSvt0DOf2APEdZOCdubEvGDUAlqH//VBYcOYsGgRW7Xzorr44LvkjiuvecWc8fChxuZZbChtg==", + "peerDependencies": { + "graphql": ">=0.8.0" + } + }, "node_modules/graphql-ws": { "version": "5.5.5", "resolved": "https://registry.npmjs.org/graphql-ws/-/graphql-ws-5.5.5.tgz", @@ -16270,6 +16279,12 @@ "tslib": "^2.1.0" } }, + "graphql-type-json": { + "version": "0.3.2", + "resolved": "https://registry.npmjs.org/graphql-type-json/-/graphql-type-json-0.3.2.tgz", + "integrity": "sha512-J+vjof74oMlCWXSvt0DOf2APEdZOCdubEvGDUAlqH//VBYcOYsGgRW7Xzorr44LvkjiuvecWc8fChxuZZbChtg==", + "requires": {} + }, "graphql-ws": { "version": "5.5.5", "resolved": "https://registry.npmjs.org/graphql-ws/-/graphql-ws-5.5.5.tgz", diff --git a/package.json b/package.json index 4e24fe4..9969559 100644 --- a/package.json +++ b/package.json @@ -51,6 +51,7 @@ "dotenv": "^16.0.3", "fs": "0.0.1-security", "graphql": "^16.6.0", + "graphql-type-json": "^0.3.2", "handlebars": "^4.7.7", "hbs": "^4.2.0", "ioredis": "^5.2.3", diff --git a/src/crags/entities/crag.entity.ts b/src/crags/entities/crag.entity.ts index a1e776b..a7fd89f 100644 --- a/src/crags/entities/crag.entity.ts +++ b/src/crags/entities/crag.entity.ts @@ -33,6 +33,7 @@ import { GradingSystem } from './grading-system.entity'; import { User } from '../../users/entities/user.entity'; import { EntityStatus } from './enums/entity-status.enum'; import { PublishStatus } from './enums/publish-status.enum'; +import GraphQLJSON from 'graphql-type-json'; export enum CragType { SPORT = 'sport', @@ -263,4 +264,8 @@ export class Crag extends BaseEntity { @JoinColumn() @Field(() => Image, { nullable: true }) coverImage: Promise; + + @Column({ type: 'jsonb', nullable: true }) + @Field((type) => GraphQLJSON, { nullable: true }) + nrRoutesByGrade: JSON; } diff --git a/src/crags/services/crags.service.ts b/src/crags/services/crags.service.ts index 2c6ba9d..db4957c 100644 --- a/src/crags/services/crags.service.ts +++ b/src/crags/services/crags.service.ts @@ -263,7 +263,7 @@ export class CragsService { routeTypeId: params.routeTypeId, }); } - console.log(builder.getSql()); + setBuilderCache(builder); return builder; diff --git a/src/migration/1705839051844-addNrRoutesByGradeToCrag.ts b/src/migration/1705839051844-addNrRoutesByGradeToCrag.ts new file mode 100644 index 0000000..322e634 --- /dev/null +++ b/src/migration/1705839051844-addNrRoutesByGradeToCrag.ts @@ -0,0 +1,104 @@ +import { MigrationInterface, QueryRunner } from 'typeorm'; + +type TGrade = { + id: string; + difficulty: number; + name: string; + gradingSystemId: string; +}; + +export class addNrRoutesByGradeToCrag1705839051844 + implements MigrationInterface +{ + name = 'addNrRoutesByGradeToCrag1705839051844'; + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `ALTER TABLE "crag" ADD "nr_routes_by_grade" jsonb`, + ); + + // Get all grading systems and grades + const gradingSystems = await queryRunner.query( + `SELECT * FROM grading_system gs ORDER BY gs.position ASC`, + ); + + for (const gradingSystem of gradingSystems) { + const grades = await queryRunner.query( + `SELECT * FROM grade g WHERE grading_system_id = '${gradingSystem.id}' ORDER BY g.difficulty ASC`, + ); + + gradingSystem.grades = grades; + } + + // Calculate all grade distros for all crags + const crags = await queryRunner.query('SELECT id FROM crag'); + for (const crag of crags) { + const routes = await queryRunner.query( + `SELECT difficulty FROM route WHERE crag_id = '${crag.id}'`, + ); + + const roundedDiffCountsByGradingSystem = {}; + + for (const gradingSystem of gradingSystems) { + const grades = gradingSystem.grades; + roundedDiffCountsByGradingSystem[gradingSystem.id] = {}; + + for (let route of routes) { + if (route.isProject) continue; + + const grade = await this.difficultyToGrade(route.difficulty, grades); + + if ( + roundedDiffCountsByGradingSystem[gradingSystem.id][ + grade.difficulty + ] === undefined + ) { + roundedDiffCountsByGradingSystem[gradingSystem.id][ + grade.difficulty + ] = 1; + } else { + roundedDiffCountsByGradingSystem[gradingSystem.id][ + grade.difficulty + ]++; + } + } + } + + await queryRunner.query( + `UPDATE crag SET nr_routes_by_grade = '${JSON.stringify( + roundedDiffCountsByGradingSystem, + )}' WHERE id = '${crag.id}'`, + ); + } + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `ALTER TABLE "crag" DROP COLUMN "nr_routes_by_grade"`, + ); + } + + private async difficultyToGrade(difficulty: number, grades: TGrade[]) { + let prevGrade: TGrade; + let currGrade: TGrade; + // assuming grades are ordered by difficulty low to high + for (let i = 1; i < grades.length; i++) { + prevGrade = grades[i - 1]; + currGrade = grades[i]; + + if (currGrade.difficulty >= difficulty) { + // are we closer to left or right 'whole' grade + if ( + difficulty - prevGrade.difficulty <= + currGrade.difficulty - difficulty + ) { + return prevGrade; + } else { + return currGrade; + } + } + } + // fallback to last (max) grade if this grading system does not have a grade of such difficulty + return currGrade; + } +} From 62272c10b917a98887266518519dd6db7b876ec0 Mon Sep 17 00:00:00 2001 From: salamca Date: Fri, 16 Feb 2024 14:02:35 +0100 Subject: [PATCH 7/8] Add hasSport, hasBoulder, hasMultipitch fields to crag. --- src/crags/entities/crag.entity.ts | 12 ++++++ ...ddHasSportHasBoulderHasMultipitchToCrag.ts | 40 +++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 src/migration/1708077643082-addHasSportHasBoulderHasMultipitchToCrag.ts diff --git a/src/crags/entities/crag.entity.ts b/src/crags/entities/crag.entity.ts index a7fd89f..ada4b81 100644 --- a/src/crags/entities/crag.entity.ts +++ b/src/crags/entities/crag.entity.ts @@ -268,4 +268,16 @@ export class Crag extends BaseEntity { @Column({ type: 'jsonb', nullable: true }) @Field((type) => GraphQLJSON, { nullable: true }) nrRoutesByGrade: JSON; + + @Column({ default: false }) + @Field() + hasSport: boolean; + + @Column({ default: false }) + @Field() + hasBoulder: boolean; + + @Column({ default: false }) + @Field() + hasMultipitch: boolean; } diff --git a/src/migration/1708077643082-addHasSportHasBoulderHasMultipitchToCrag.ts b/src/migration/1708077643082-addHasSportHasBoulderHasMultipitchToCrag.ts new file mode 100644 index 0000000..6ac5bd1 --- /dev/null +++ b/src/migration/1708077643082-addHasSportHasBoulderHasMultipitchToCrag.ts @@ -0,0 +1,40 @@ +import { MigrationInterface, QueryRunner } from 'typeorm'; + +export class addHasSportHasBoulderHasMultipitchToCrag1708077643082 + implements MigrationInterface +{ + name = 'addHasSportHasBoulderHasMultipitchToCrag1708077643082'; + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `ALTER TABLE "crag" ADD "has_sport" boolean NOT NULL DEFAULT false`, + ); + + await queryRunner.query( + `ALTER TABLE "crag" ADD "has_boulder" boolean NOT NULL DEFAULT false`, + ); + await queryRunner.query( + `ALTER TABLE "crag" ADD "has_multipitch" boolean NOT NULL DEFAULT false`, + ); + + const crags = await queryRunner.query( + `SELECT c.id, r.route_type_id FROM crag c + INNER JOIN route r on r.crag_id = c.id + GROUP BY r.route_type_id, c.id`, + ); + + for (const crag of crags) { + if (['sport', 'boulder', 'multipitch'].includes(crag.route_type_id)) { + await queryRunner.query( + `UPDATE crag SET has_${crag.route_type_id} = true WHERE id = '${crag.id}'`, + ); + } + } + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE "crag" DROP COLUMN "has_multipitch"`); + await queryRunner.query(`ALTER TABLE "crag" DROP COLUMN "has_boulder"`); + await queryRunner.query(`ALTER TABLE "crag" DROP COLUMN "has_sport"`); + } +} From 606c42b72dde1583328425dfc8fa04ddeb8389ea Mon Sep 17 00:00:00 2001 From: Anze Demsar Date: Sat, 24 Feb 2024 18:00:56 +0100 Subject: [PATCH 8/8] add auth conditions to crags resolver --- src/crags/resolvers/crags.resolver.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/crags/resolvers/crags.resolver.ts b/src/crags/resolvers/crags.resolver.ts index bc567ad..989f1d9 100644 --- a/src/crags/resolvers/crags.resolver.ts +++ b/src/crags/resolvers/crags.resolver.ts @@ -46,6 +46,7 @@ import { ImagesService } from '../services/images.service'; import { Roles } from '../../auth/decorators/roles.decorator'; import { Area } from '../entities/area.entity'; import { AreaLoader } from '../loaders/area.loader'; +import { FindCragsServiceInput } from '../dtos/find-crags-service.input'; @Resolver(() => Crag) @UseInterceptors(DataLoaderInterceptor) @@ -86,11 +87,17 @@ export class CragsResolver { }); } + @AllowAny() @UseGuards(UserAuthGuard) - // TODO: add conditions for showing 'public' crags to everyone (in service) @Query(() => [Crag]) - async crags(): Promise { - return this.cragsService.findAll(); + async crags( + @CurrentUser() user: User, + @Args('input', { nullable: true }) input: FindCragsServiceInput = {}, + ): Promise { + return this.cragsService.find({ + ...input, + user, + }); } /* MUTATIONS */