Skip to content

Commit

Permalink
Merge pull request #105 from MTES-MCT/develop
Browse files Browse the repository at this point in the history
Ressources influencées 🚀
  • Loading branch information
vincentlaine authored Nov 25, 2024
2 parents 743dd32 + b233d59 commit 0b9b0c1
Show file tree
Hide file tree
Showing 28 changed files with 403 additions and 163 deletions.
91 changes: 41 additions & 50 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,73 +1,64 @@
<p align="center">
<a href="http://nestjs.com/" target="blank"><img src="https://nestjs.com/img/logo-small.svg" width="200" alt="Nest Logo" /></a>
</p>

[circleci-image]: https://img.shields.io/circleci/build/github/nestjs/nest/master?token=abc123def456
[circleci-url]: https://circleci.com/gh/nestjs/nest

<p align="center">A progressive <a href="http://nodejs.org" target="_blank">Node.js</a> framework for building efficient and scalable server-side applications.</p>
<p align="center">
<a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/v/@nestjs/core.svg" alt="NPM Version" /></a>
<a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/l/@nestjs/core.svg" alt="Package License" /></a>
<a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/dm/@nestjs/common.svg" alt="NPM Downloads" /></a>
<a href="https://circleci.com/gh/nestjs/nest" target="_blank"><img src="https://img.shields.io/circleci/build/github/nestjs/nest/master" alt="CircleCI" /></a>
<a href="https://coveralls.io/github/nestjs/nest?branch=master" target="_blank"><img src="https://coveralls.io/repos/github/nestjs/nest/badge.svg?branch=master#9" alt="Coverage" /></a>
<a href="https://discord.gg/G7Qnnhy" target="_blank"><img src="https://img.shields.io/badge/discord-online-brightgreen.svg" alt="Discord"/></a>
<a href="https://opencollective.com/nest#backer" target="_blank"><img src="https://opencollective.com/nest/backers/badge.svg" alt="Backers on Open Collective" /></a>
<a href="https://opencollective.com/nest#sponsor" target="_blank"><img src="https://opencollective.com/nest/sponsors/badge.svg" alt="Sponsors on Open Collective" /></a>
<a href="https://paypal.me/kamilmysliwiec" target="_blank"><img src="https://img.shields.io/badge/Donate-PayPal-ff3f59.svg"/></a>
<a href="https://opencollective.com/nest#sponsor" target="_blank"><img src="https://img.shields.io/badge/Support%20us-Open%20Collective-41B883.svg" alt="Support us"></a>
<a href="https://twitter.com/nestframework" target="_blank"><img src="https://img.shields.io/twitter/follow/nestframework.svg?style=social&label=Follow"></a>
</p>
<!--[![Backers on Open Collective](https://opencollective.com/nest/backers/badge.svg)](https://opencollective.com/nest#backer)
[![Sponsors on Open Collective](https://opencollective.com/nest/sponsors/badge.svg)](https://opencollective.com/nest#sponsor)-->
# VigiEau Admin - Backend

## Description

[Nest](https://github.com/nestjs/nest) framework TypeScript starter repository.

## Installation

```bash
$ yarn install
```
### Pré-requis

## Running the app
Vous aurez besoin de [NodeJS](https://nodejs.org/) v18+ et [Yarn](https://yarnpkg.com/) pour lancer ce projet.

```bash
# development
$ yarn run start
Nous vous recommandons de regarder la [documentation de NestJS](https://nestjs.com/).

# watch mode
$ yarn run start:dev
### Variables d'environnement

# production mode
$ yarn run start:prod
```bash
cp env.example .env
```

## Test
- NODE_ENV : local / dev / prod
- PORT : Port sur lequel tournera le serveur
- OAUTH2_CLIENT : Se référer à la documentation de [ProConnect](https://www.proconnect.gouv.fr/)
- SESSION_SECRET : Token JWT
- DATABASE : Informations pour se connecter à la DB (Postgres)
- WEBSITE_URL : Site web du frontend (http://localhost:3000 en local)
- DOMAIN : Domaine sur lequel tourne le serveur (localhost en local)
- API_DATAGOUV : Informations pour se connecter à Datagouv et pouvoir upload automatiquement les données de VigiEau
- ADMINJS : User / password pour accéder au backend AdminJS
- S3 : Informations pour se connecter aux buckets S3
- MAIL : Informations pour se connecter à la boite mail
- MAIL_MTE : Mail générique à renseigner pour l'envoi de mail systématique à une adresse
- DOMAIN_NAME : Domaine du frontend (localhost:3000 en local)
- PATH_TO_WRITE_FILE : Dossier pour stocker les fichiers temporaires ou le serveur peut lire / écrire

### Installation des dépendances

```bash
# unit tests
$ yarn run test
yarn install
```

# e2e tests
$ yarn run test:e2e
### Lancer nuxt en mode développement

# test coverage
$ yarn run test:cov
Démarre le serveur sur http://localhost:3001

```bash
yarn start:dev
```

## Support
### Générer le code de production

Génère le code de production de l’application:

```bash
yarn build
```

Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please [read more here](https://docs.nestjs.com/support).
## Contribution

## Stay in touch
Les Pull Requests sont les bienvenues. Pour des changements majeurs merci d'ouvrir auparavant une issue pour en discuter.

- Author - [Kamil Myśliwiec](https://kamilmysliwiec.com)
- Website - [https://nestjs.com](https://nestjs.com/)
- Twitter - [@nestframework](https://twitter.com/nestframework)
Assurez-vous de mettre à jour les tests en conséquence.

## License

Nest is [MIT licensed](LICENSE).
[MIT](https://choosealicense.com/licenses/mit/)
12 changes: 9 additions & 3 deletions env.example
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ DATABASE_NAME=regleau

WEBSITE_URL=http://localhost:3000
DOMAIN=localhost
API_SANDRE=
API_GEO=
API_DATAGOUV=
API_SANDRE=https://services.sandre.eaufrance.fr/
API_GEO=https://geo.api.gouv.fr
API_DATAGOUV=https://www.data.gouv.fr/api/1/
API_DATAGOUV_KEY=
API_DATAGOUV_DATASET=

Expand All @@ -39,4 +39,10 @@ MAIL_PASSWORD=
MAIL_HOST=
MAIL_PORT=

MAIL_MTE=

DOMAIN_NAME=

PATH_TO_WRITE_FILE=

NODE_TLS_REJECT_UNAUTHORIZED=0
5 changes: 5 additions & 0 deletions src/admin.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import { Departement } from './departement/entities/departement.entity';
import { BassinVersant } from './bassin_versant/entities/bassin_versant.entity';
import { Region } from './core/entities/region.entity';

/**
* Désactivé pour l'instant car au final pas utilisé
* Pour le réactiver il suffit de réimporter AdminModule dans AppModule
*/

const DEFAULT_ADMIN = {
email: process.env.ADMINJS_USER,
password: process.env.ADMINJS_PASSWORD,
Expand Down
13 changes: 10 additions & 3 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { ScheduleModule } from '@nestjs/schedule';
import { DepartementModule } from './departement/departement.module';
import { UsageModule } from './usage/usage.module';
import { ThematiqueModule } from './thematique/thematique.module';
import { AdminModule } from './admin.module';
import { ArreteRestrictionModule } from './arrete_restriction/arrete_restriction.module';
import { AppController } from './app.controller';
import { LoggerModule } from './logger/logger.module';
Expand All @@ -36,6 +35,7 @@ import { UsageFeedbackModule } from './usage_feedback/usage_feedback.module';
import { StatisticModule } from './statistic/statistic.module';
import { ArreteMunicipalModule } from './arrete_municipal/arrete_municipal.module';
import { AbonnementMailModule } from './abonnement_mail/abonnement_mail.module';
import { isArray, isObject } from './mail_templates/helpers/handlebars_helpers';

// @ts-ignore
@Module({
Expand Down Expand Up @@ -101,13 +101,20 @@ import { AbonnementMailModule } from './abonnement_mail/abonnement_mail.module';
preview: process.env.NODE_ENV === 'local',
template: {
dir: __dirname + '/mail_templates',
adapter: new HandlebarsAdapter(),
adapter: new HandlebarsAdapter({'isObject': isObject, 'isArray': isArray}),
options: {
strict: true,
},
},
options: {
partials: {
dir: __dirname + '/mail_templates/partials',
options: {
strict: true,
},
},
},
}),
AdminModule,
HealthModule,
ArreteCadreModule,
AuthModule,
Expand Down
4 changes: 4 additions & 0 deletions src/arrete_cadre/arrete_cadre.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import { UserModule } from '../user/user.module';
import { FichierModule } from '../fichier/fichier.module';
import { RestrictionModule } from '../restriction/restriction.module';
import { UsageModule } from '../usage/usage.module';
import {
ArreteCadreZoneAlerteCommunesModule
} from '../arrete_cadre_zone_alerte_communes/arrete_cadre_zone_alerte_communes.module';

@Module({
imports: [
Expand All @@ -23,6 +26,7 @@ import { UsageModule } from '../usage/usage.module';
FichierModule,
RestrictionModule,
UsageModule,
ArreteCadreZoneAlerteCommunesModule,
],
controllers: [ArreteCadreController],
providers: [ArreteCadreService],
Expand Down
111 changes: 45 additions & 66 deletions src/arrete_cadre/arrete_cadre.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ import { FichierService } from '../fichier/fichier.service';
import { RestrictionService } from '../restriction/restriction.service';
import { UsageService } from '../usage/usage.service';
import moment from 'moment/moment';
import {
ArreteCadreZoneAlerteCommunesService,
} from '../arrete_cadre_zone_alerte_communes/arrete_cadre_zone_alerte_communes.service';

@Injectable()
export class ArreteCadreService {
Expand All @@ -46,6 +49,7 @@ export class ArreteCadreService {
private readonly fichierService: FichierService,
private readonly restrictionService: RestrictionService,
private readonly usageService: UsageService,
private readonly arreteCadreZoneAlerteCommunesService: ArreteCadreZoneAlerteCommunesService,
) {
}

Expand Down Expand Up @@ -80,7 +84,7 @@ export class ArreteCadreService {
: In(currentUser.role_departements),
},
};
const acToReturn = await this.arreteCadreRepository.find(<FindManyOptions> {
const acToReturn = await this.arreteCadreRepository.find(<FindManyOptions>{
select: {
id: true,
numero: true,
Expand Down Expand Up @@ -157,63 +161,30 @@ export class ArreteCadreService {
code: In(currentUser.role_departements),
},
};
const [arreteCadre, usagesArreteCadre, departements]: any = await Promise.all(<any> [
this.arreteCadreRepository.findOne(<FindOneOptions> {
select: {
id: true,
numero: true,
dateDebut: true,
dateFin: true,
statut: true,
fichier: {
id: true,
nom: true,
url: true,
size: true,
},
departementPilote: {
id: true,
code: true,
nom: true,
},
zonesAlerte: {
id: true,
code: true,
nom: true,
type: true,
disabled: true,
departement: {
id: true,
code: true,
},
},
arretesRestriction: {
id: true,
numero: true,
statut: true,
},
arreteCadreAbroge: {
id: true,
numero: true,
dateDebut: true,
dateFin: true,
},
},
relations: [
'departementPilote',
'zonesAlerte',
'zonesAlerte.departement',
'arretesRestriction',
'fichier',
'arreteCadreAbroge',
],
where: whereClause,
order: {
zonesAlerte: {
code: 'ASC',
},
},
}),

const qb = this.arreteCadreRepository.createQueryBuilder('arreteCadre')
.select([
'arreteCadre.id', 'arreteCadre.numero', 'arreteCadre.dateDebut', 'arreteCadre.dateFin', 'arreteCadre.statut',
'fichier.id', 'fichier.nom', 'fichier.url', 'fichier.size',
'departementPilote.id', 'departementPilote.code', 'departementPilote.nom',
'zonesAlerte.id', 'zonesAlerte.code', 'zonesAlerte.nom', 'zonesAlerte.type', 'zonesAlerte.disabled', 'zonesAlerte.ressourceInfluencee',
'departement.id', 'departement.code',
'arretesRestriction.id', 'arretesRestriction.numero', 'arretesRestriction.statut',
'arreteCadreAbroge.id', 'arreteCadreAbroge.numero', 'arreteCadreAbroge.dateDebut', 'arreteCadreAbroge.dateFin',
'aczac.id', 'communes.id', 'communes.code', 'communes.nom',
])
.leftJoin('arreteCadre.departementPilote', 'departementPilote')
.leftJoin('arreteCadre.zonesAlerte', 'zonesAlerte')
.leftJoin('zonesAlerte.departement', 'departement')
.leftJoin('arreteCadre.arretesRestriction', 'arretesRestriction')
.leftJoin('arreteCadre.fichier', 'fichier')
.leftJoin('arreteCadre.arreteCadreAbroge', 'arreteCadreAbroge')
.leftJoin('zonesAlerte.arreteCadreZoneAlerteCommunes', 'aczac', 'aczac.arreteCadreId = arreteCadre.id')
.leftJoin('aczac.communes', 'communes')
.where(whereClause)
.orderBy('zonesAlerte.code', 'ASC');
const [arreteCadre, usagesArreteCadre, departements]: any = await Promise.all(<any>[
qb.getOne(),
this.usageService.findByArreteCadre(id),
this.departementService.findByArreteCadreId(id),
]);
Expand All @@ -224,14 +195,21 @@ export class ArreteCadreService {
);
}
arreteCadre.usages = usagesArreteCadre;
arreteCadre.zonesAlerte.map(za => {
if (za.arreteCadreZoneAlerteCommunes[0] && za.arreteCadreZoneAlerteCommunes[0].communes?.length > 0) {
za.communes = structuredClone(za.arreteCadreZoneAlerteCommunes[0].communes);
}
delete za.arreteCadreZoneAlerteCommunes;
return za;
});
if (departements) {
arreteCadre.departements = departements;
}
return arreteCadre;
}

async findDatagouv(): Promise<ArreteCadre[]> {
return this.arreteCadreRepository.find(<FindManyOptions> {
return this.arreteCadreRepository.find(<FindManyOptions>{
select: {
id: true,
numero: true,
Expand Down Expand Up @@ -271,7 +249,7 @@ export class ArreteCadreService {
}

findByArreteRestrictionId(id: number): Promise<ArreteCadre[]> {
return this.arreteCadreRepository.find(<FindManyOptions> {
return this.arreteCadreRepository.find(<FindManyOptions>{
select: {
id: true,
numero: true,
Expand Down Expand Up @@ -326,7 +304,7 @@ export class ArreteCadreService {
}

findByDepartement(depCode: string): Promise<ArreteCadre[]> {
return this.arreteCadreRepository.find(<FindManyOptions> {
return this.arreteCadreRepository.find(<FindManyOptions>{
select: {
id: true,
numero: true,
Expand Down Expand Up @@ -355,8 +333,9 @@ export class ArreteCadreService {
await this.checkAci(createArreteCadreDto, false, currentUser);
const arreteCadre =
await this.arreteCadreRepository.save(createArreteCadreDto);
arreteCadre.usages =
await this.usageService.updateAllByArreteCadre(arreteCadre);
arreteCadre.usages = await this.usageService.updateAllByArreteCadre(arreteCadre);
await this.arreteCadreZoneAlerteCommunesService.updateAllByArreteCadre(arreteCadre.id, createArreteCadreDto);

this.sendAciMails(null, arreteCadre, currentUser);
return arreteCadre;
}
Expand All @@ -378,8 +357,8 @@ export class ArreteCadreService {
id,
...updateArreteCadreDto,
});
arreteCadre.usages =
await this.usageService.updateAllByArreteCadre(arreteCadre);
arreteCadre.usages = await this.usageService.updateAllByArreteCadre(arreteCadre);
await this.arreteCadreZoneAlerteCommunesService.updateAllByArreteCadre(arreteCadre.id, updateArreteCadreDto);

await this.repercussionOnAr(oldAc, arreteCadre);
this.sendAciMails(oldAc, arreteCadre, currentUser);
Expand Down Expand Up @@ -558,7 +537,7 @@ export class ArreteCadreService {
},
);
const oldUsagesUpdates = oldAc.usages.filter(u => usagesUpdated.some(uu => uu.id === u.id));
await Promise.all(<any> [
await Promise.all(<any>[
this.restrictionService.deleteZonesByArreteCadreId(
zonesDeleted.map((z) => z.id),
oldAc.id,
Expand Down
Loading

0 comments on commit 0b9b0c1

Please sign in to comment.