import { BadRequestException , Controller , Get , InternalServerErrorException , Logger , Param , NotFoundException , UseInterceptors , UseGuards } from "@nestjs/common" ; import { IReferenceLayer } from "../../../../shared/entities" ; import { FeatureCollection } from "geojson" ; import { Crud , CrudController , CrudRequest , CrudRequestInterceptor , Override , ParsedBody , ParsedRequest , CrudAuth } from "@nestjsx/crud" ; import isUUID from "validator/lib/isUUID" ; import { OptionalJwtAuthGuard } from "../../auth/guards/jwt-auth.guard" ; import { QueryFailedError } from "typeorm" ; import { ProjectId , ReferenceLayerId } from "../../../../shared/entities" ; import { User } from "../../users/entities/user.entity" ; import { JwtAuthGuard } from "../../auth/guards/jwt-auth.guard" ; import { ReferenceLayer } from "../entities/reference-layer.entity" ; import { ReferenceLayersService } from "../services/reference-layers.service" ; import { ProjectsService } from "../../projects/services/projects.service" ; import { CreateReferenceLayerDto } from "../entities/create-reference-layer.dto" ; import { ProjectVisibility } from "../../../../shared/constants" ; @ Crud ( { model : { type : ReferenceLayer } , params : { id : { type : "string" , primary : true , field : "id" } , projectId : { type : "string" , primary : true , field : "id" } } , query : { join : { project : { eager : true , select : false } } } , routes : { only : [ "createOneBase" , "deleteOneBase" , "getOneBase" ] } } ) @ CrudAuth ( { filter : ( req ) => { const user = req . user as User ; if ( req . method !== "GET" ) { return { "project.user_id" : user ? user . id : undefined } ; } else { const publicallyVisible = [ { "project.visibility" : ProjectVisibility . Published } , { "project.visibility" : ProjectVisibility . Visible } ] ; const visibleFilter = user ? [ { "project.user_id" : user . id } , ... publicallyVisible ] : publicallyVisible ; return { $and : [ { $or : visibleFilter } , { "project.archived" : false } ] } ; } } , persist : ( req ) => { const user = req . user as User ; return { userId : user ? user . id : undefined } ; } } ) @ Controller ( "api/reference-layer" ) export class ReferenceLayersController implements CrudController < ReferenceLayer > { get base ( ) : CrudController < ReferenceLayer > { return this ; } private readonly logger = new Logger ( ReferenceLayer . name ) ; constructor ( public service , public projectsService ) { } @ UseInterceptors ( CrudRequestInterceptor ) @ UseGuards ( OptionalJwtAuthGuard ) @ Get ( "project/:projectId" ) getProjectReferenceLayers ( @ ParsedRequest ( ) req , @ Param ( "projectId" ) projectId ) { const userId = typeof req . parsed . authPersist . userId === "string" ? req . parsed . authPersist . userId : undefined ; return this . service . getProjectReferenceLayers ( projectId , userId ) ; } @ UseInterceptors ( CrudRequestInterceptor ) @ UseGuards ( OptionalJwtAuthGuard ) @ Get ( ":id/geojson" ) async getGeojson ( @ Param ( "id" ) id , @ ParsedRequest ( ) req ) { const refLayer = await this . getOne ( id , req ) ; return refLayer . layer ; } @ Override ( ) @ UseGuards ( JwtAuthGuard ) async createOne ( @ ParsedRequest ( ) req , @ ParsedBody ( ) dto ) { if ( ! this . base . createOneBase ) { this . logger . error ( "Routes misconfigured. Missing `createOneBase` route" ) ; throw new InternalServerErrorException ( ) ; } try { return await this . service . createOne ( req , dto ) ; } catch ( error ) { if ( error instanceof QueryFailedError ) { throw new BadRequestException ( "The following fields are required: name, project, layer_type, layer" ) ; } else { this . logger . error ( `Error creating reference layer: ${ error }` ) ; throw new InternalServerErrorException ( ) ; } } } @ UseInterceptors ( CrudRequestInterceptor ) @ Override ( ) @ UseGuards ( OptionalJwtAuthGuard ) async getOne ( @ Param ( "id" ) id , @ ParsedRequest ( ) req ) { if ( ! this . base . getOneBase ) { this . logger . error ( "Routes misconfigured. Missing `getOneBase` route" ) ; throw new InternalServerErrorException ( ) ; } if ( ! isUUID ( id ) ) { throw new NotFoundException ( `Layer ID ${ id } is not a valid UUID` ) ; } const referenceLayer = await this . base . getOneBase ( req ) ; if ( ! referenceLayer ) { throw new NotFoundException ( `Layer ${ id } not found` ) ; } return referenceLayer ; } @ UseInterceptors ( CrudRequestInterceptor ) @ Override ( ) @ UseGuards ( OptionalJwtAuthGuard ) async deleteOne ( @ Param ( "id" ) id , @ ParsedRequest ( ) req ) { if ( ! this . base . deleteOneBase ) { this . logger . error ( "Routes misconfigured. Missing `deleteOneBase` route" ) ; throw new InternalServerErrorException ( ) ; } if ( ! isUUID ( id ) ) { throw new NotFoundException ( `Layer ID ${ id } is not a valid UUID` ) ; } return await this . base . deleteOneBase ( req ) ; } }