Skip to content

Commit

Permalink
Affichage arrêtes municipaux temps réel ✨
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentlaine committed Oct 1, 2024
1 parent bd669bf commit 4c32611
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
12 changes: 10 additions & 2 deletions src/zones/zones.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,19 @@ import { CommunesModule } from '../communes/communes.module';

@Module({
imports: [
TypeOrmModule.forFeature([ZoneAlerteComputed, Restriction, Usage, Thematique, ArreteCadre, Fichier, ArreteMunicipal]),
TypeOrmModule.forFeature([
ZoneAlerteComputed,
Restriction,
Usage,
Thematique,
ArreteCadre,
Fichier,
ArreteMunicipal
]),
DepartementsModule,
DataModule,
StatisticsModule,
CommunesModule
CommunesModule,
],
controllers: [ZonesController],
providers: [ZonesService],
Expand Down
31 changes: 29 additions & 2 deletions src/zones/zones.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,17 @@ export class ZonesService {
zonesCommunesIndex: any = {};
zoneTree;
lastUpdate = null;
lastUpdateAm = null;
loading = false;

constructor(@InjectRepository(ZoneAlerteComputed)
private readonly zoneAlerteComputedRepository: Repository<ZoneAlerteComputed>,
private readonly departementsService: DepartementsService,
private readonly statisticsService: StatisticsService,
private readonly dataService: DataService,
private readonly communesService: CommunesService) {
private readonly communesService: CommunesService,
@InjectRepository(ArreteMunicipal)
private readonly arreteMunicipalRepository: Repository<ArreteMunicipal>,) {
this.loadAllZones(true);
}

Expand Down Expand Up @@ -284,7 +287,7 @@ export class ZonesService {
this.zonesIndex = keyBy(this.allZonesWithRestrictions, 'id');
this.zoneTree.finish();

this.communeArretesMunicipaux = await this.communesService.findArretesMunicipaux();
await this.updateArreteMunicipaux();

this.loading = false;
this.logger.log('LOADING ALL ZONES & COMMUNES - END');
Expand Down Expand Up @@ -370,6 +373,11 @@ export class ZonesService {
};
}

async updateArreteMunicipaux() {
this.communeArretesMunicipaux = await this.communesService.findArretesMunicipaux();
this.lastUpdateAm = new Date();
}

/**
* Vérification régulière s'il n'y a pas de nouvelles zones
*/
Expand All @@ -391,4 +399,23 @@ export class ZonesService {
this.loadAllZones();
}
}

/**
* Vérification régulière s'il n'y a pas de nouveaaux arrêtés municipaux
*/
@Cron(CronExpression.EVERY_30_SECONDS)
async updateArretesMunicipaux() {
if (!this.lastUpdateAm) {
return;
}
const count = await this.arreteMunicipalRepository
.createQueryBuilder('arrete_municipal')
.where({
updatedAt: MoreThan(this.lastUpdateAm.toLocaleString('sv')),
})
.getCount();
if (count > 0) {
this.updateArreteMunicipaux();
}
}
}

0 comments on commit 4c32611

Please sign in to comment.