@@ -43,7 +43,7 @@ import {
43
43
} from 'camino-common/src/perimetre'
44
44
import { join } from 'node:path'
45
45
import { readFileSync } from 'node:fs'
46
- import shpjs from 'shpjs'
46
+ import { parseShp } from 'shpjs'
47
47
import { DeepReadonly , isNotNullNorUndefined , isNullOrUndefined , memoize } from 'camino-common/src/typescript-tools'
48
48
import { SDOMZoneId } from 'camino-common/src/static/sdom'
49
49
import { TitreSlug } from 'camino-common/src/validators/titres'
@@ -58,83 +58,85 @@ import { ZodUnparseable, callAndExit, zodParseEffect, zodParseEffectCallback } f
58
58
import { CaminoError } from 'camino-common/src/zod-tools'
59
59
import { RestNewPostCall } from '../../server/rest'
60
60
61
- export const getPerimetreInfos = ( pool : Pool ) => async ( req : CaminoRequest , res : CustomResponse < PerimetreInformations > ) => {
62
- const user = req . auth
61
+ export const getPerimetreInfos =
62
+ ( pool : Pool ) =>
63
+ async ( req : CaminoRequest , res : CustomResponse < PerimetreInformations > ) : Promise < void > => {
64
+ const user = req . auth
63
65
64
- if ( ! user ) {
65
- res . sendStatus ( HTTP_STATUS . FORBIDDEN )
66
- } else {
67
- const etapeIdOrSlugParsed = etapeIdOrSlugValidator . safeParse ( req . params . etapeId )
68
- const demarcheIdOrSlugParsed = demarcheIdOrSlugValidator . safeParse ( req . params . demarcheId )
69
-
70
- if ( ! etapeIdOrSlugParsed . success && ! demarcheIdOrSlugParsed . success ) {
71
- res . sendStatus ( HTTP_STATUS . BAD_REQUEST )
66
+ if ( ! user ) {
67
+ res . sendStatus ( HTTP_STATUS . FORBIDDEN )
72
68
} else {
73
- try {
74
- let etape : null | { demarche_id : DemarcheId ; geojson4326_perimetre : MultiPolygon | null ; sdom_zones : SDOMZoneId [ ] ; etape_type_id : EtapeTypeId ; communes : CommuneId [ ] } = null
75
- if ( etapeIdOrSlugParsed . success ) {
76
- const myEtape = await getEtapeById ( pool , etapeIdOrSlugParsed . data )
77
-
78
- etape = { demarche_id : myEtape . demarche_id , geojson4326_perimetre : myEtape . geojson4326_perimetre , sdom_zones : myEtape . sdom_zones ?? [ ] , etape_type_id : myEtape . etape_type_id , communes : [ ] }
79
- } else if ( demarcheIdOrSlugParsed . success ) {
80
- const demarche = await getDemarcheByIdOrSlug ( pool , demarcheIdOrSlugParsed . data )
81
- const etapes = await getEtapesByDemarcheId ( pool , demarche . demarche_id )
82
-
83
- const mostRecentEtapeFondamentale = getMostRecentEtapeFondamentaleValide ( [ { ordre : 1 , etapes } ] )
84
- if ( isNotNullNorUndefined ( mostRecentEtapeFondamentale ) ) {
85
- etape = {
86
- demarche_id : demarche . demarche_id ,
87
- geojson4326_perimetre : mostRecentEtapeFondamentale . geojson4326_perimetre ,
88
- sdom_zones : mostRecentEtapeFondamentale . sdom_zones ?? [ ] ,
89
- etape_type_id : mostRecentEtapeFondamentale . etape_type_id ,
90
- communes : mostRecentEtapeFondamentale . communes . map ( ( { id } ) => id ) ,
69
+ const etapeIdOrSlugParsed = etapeIdOrSlugValidator . safeParse ( req . params . etapeId )
70
+ const demarcheIdOrSlugParsed = demarcheIdOrSlugValidator . safeParse ( req . params . demarcheId )
71
+
72
+ if ( ! etapeIdOrSlugParsed . success && ! demarcheIdOrSlugParsed . success ) {
73
+ res . sendStatus ( HTTP_STATUS . BAD_REQUEST )
74
+ } else {
75
+ try {
76
+ let etape : null | { demarche_id : DemarcheId ; geojson4326_perimetre : MultiPolygon | null ; sdom_zones : SDOMZoneId [ ] ; etape_type_id : EtapeTypeId ; communes : CommuneId [ ] } = null
77
+ if ( etapeIdOrSlugParsed . success ) {
78
+ const myEtape = await getEtapeById ( pool , etapeIdOrSlugParsed . data )
79
+
80
+ etape = { demarche_id : myEtape . demarche_id , geojson4326_perimetre : myEtape . geojson4326_perimetre , sdom_zones : myEtape . sdom_zones ?? [ ] , etape_type_id : myEtape . etape_type_id , communes : [ ] }
81
+ } else if ( demarcheIdOrSlugParsed . success ) {
82
+ const demarche = await getDemarcheByIdOrSlug ( pool , demarcheIdOrSlugParsed . data )
83
+ const etapes = await getEtapesByDemarcheId ( pool , demarche . demarche_id )
84
+
85
+ const mostRecentEtapeFondamentale = getMostRecentEtapeFondamentaleValide ( [ { ordre : 1 , etapes } ] )
86
+ if ( isNotNullNorUndefined ( mostRecentEtapeFondamentale ) ) {
87
+ etape = {
88
+ demarche_id : demarche . demarche_id ,
89
+ geojson4326_perimetre : mostRecentEtapeFondamentale . geojson4326_perimetre ,
90
+ sdom_zones : mostRecentEtapeFondamentale . sdom_zones ?? [ ] ,
91
+ etape_type_id : mostRecentEtapeFondamentale . etape_type_id ,
92
+ communes : mostRecentEtapeFondamentale . communes . map ( ( { id } ) => id ) ,
93
+ }
91
94
}
95
+ } else {
96
+ res . sendStatus ( HTTP_STATUS . INTERNAL_SERVER_ERROR )
97
+ console . error ( "cas impossible où ni l'étape id ni la démarche Id n'est chargée" )
92
98
}
93
- } else {
94
- res . sendStatus ( HTTP_STATUS . INTERNAL_SERVER_ERROR )
95
- console . error ( "cas impossible où ni l'étape id ni la démarche Id n'est chargée" )
96
- }
97
99
98
- if ( isNullOrUndefined ( etape ) ) {
99
- res . json ( {
100
- superposition_alertes : [ ] ,
101
- sdomZoneIds : [ ] ,
102
- communes : [ ] ,
103
- } )
104
- } else {
105
- const demarche = await getDemarcheByIdOrSlug ( pool , etape . demarche_id )
106
- const titre = await getTitreByIdOrSlug ( pool , demarche . titre_id )
107
-
108
- const administrationsLocales = memoize ( ( ) => getAdministrationsLocalesByTitreId ( pool , demarche . titre_id ) )
109
-
110
- if (
111
- await canReadEtape (
112
- user ,
113
- memoize ( ( ) => Promise . resolve ( titre . titre_type_id ) ) ,
114
- administrationsLocales ,
115
- memoize ( ( ) => getTitulairesAmodiatairesByTitreId ( pool , demarche . titre_id ) ) ,
116
- etape . etape_type_id ,
117
- { ...demarche , titre_public_lecture : titre . public_lecture }
118
- )
119
- ) {
120
- await callAndExit ( getAlertesSuperposition ( etape . geojson4326_perimetre , titre . titre_type_id , titre . titre_slug , user , pool ) , async superpositionAlertes => {
121
- res . json ( {
122
- superposition_alertes : superpositionAlertes ,
123
- sdomZoneIds : etape . sdom_zones ,
124
- communes : etape . communes ,
125
- } )
100
+ if ( isNullOrUndefined ( etape ) ) {
101
+ res . json ( {
102
+ superposition_alertes : [ ] ,
103
+ sdomZoneIds : [ ] ,
104
+ communes : [ ] ,
126
105
} )
127
106
} else {
128
- res . sendStatus ( HTTP_STATUS . FORBIDDEN )
107
+ const demarche = await getDemarcheByIdOrSlug ( pool , etape . demarche_id )
108
+ const titre = await getTitreByIdOrSlug ( pool , demarche . titre_id )
109
+
110
+ const administrationsLocales = memoize ( ( ) => getAdministrationsLocalesByTitreId ( pool , demarche . titre_id ) )
111
+
112
+ if (
113
+ await canReadEtape (
114
+ user ,
115
+ memoize ( ( ) => Promise . resolve ( titre . titre_type_id ) ) ,
116
+ administrationsLocales ,
117
+ memoize ( ( ) => getTitulairesAmodiatairesByTitreId ( pool , demarche . titre_id ) ) ,
118
+ etape . etape_type_id ,
119
+ { ...demarche , titre_public_lecture : titre . public_lecture }
120
+ )
121
+ ) {
122
+ await callAndExit ( getAlertesSuperposition ( etape . geojson4326_perimetre , titre . titre_type_id , titre . titre_slug , user , pool ) , async superpositionAlertes => {
123
+ res . json ( {
124
+ superposition_alertes : superpositionAlertes ,
125
+ sdomZoneIds : etape . sdom_zones ,
126
+ communes : etape . communes ,
127
+ } )
128
+ } )
129
+ } else {
130
+ res . sendStatus ( HTTP_STATUS . FORBIDDEN )
131
+ }
129
132
}
133
+ } catch ( e ) {
134
+ res . status ( HTTP_STATUS . INTERNAL_SERVER_ERROR ) . send ( e )
135
+ console . error ( e )
130
136
}
131
- } catch ( e ) {
132
- res . status ( HTTP_STATUS . INTERNAL_SERVER_ERROR ) . send ( e )
133
- console . error ( e )
134
137
}
135
138
}
136
139
}
137
- }
138
140
139
141
const shapeValidator = z . array ( polygonValidator . or ( multiPolygonValidator ) ) . max ( 1 ) . min ( 1 )
140
142
const geojsonValidator = featureCollectionMultipolygonValidator . or ( featureCollectionPolygonValidator )
@@ -411,7 +413,7 @@ const fileNameToShape = <T extends ZodTypeAny>(pathFrom: string, validator: T):
411
413
try : ( ) => {
412
414
const fileContent = readFileSync ( pathFrom )
413
415
414
- return shpjs . parseShp ( fileContent )
416
+ return parseShp ( fileContent )
415
417
} ,
416
418
catch : ( ) => ( { message : ouvertureShapeError } ) ,
417
419
} ) ,
0 commit comments