Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

log: add winston #485

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions apps/api/src/lib/utils/csv.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Numero } from '@/shared/entities/numero.entity';
import { Toponyme } from '@/shared/entities/toponyme.entity';
import { Row } from '../types/validator.types';
import { ObjectId } from 'mongodb';
import { Logger } from '@/shared/utils/logger.utils';

export type FromCsvType = {
isValid?: boolean;
Expand Down Expand Up @@ -211,8 +212,11 @@ export async function extractFromCsv(
toponymes: communesData.toponymes,
};
} catch (error) {
console.log('ERROR extractFromCsv');
console.error(error);
Logger.error(
`Impossible d'extraire la BAL sur CSV`,
error,
extractFromCsv.name,
);
return { isValid: false, validationError: error.message };
}
}
3 changes: 3 additions & 0 deletions apps/api/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ import { ValidationPipe } from '@nestjs/common';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';

import { ApiModule } from './api.module';
import { WinstonLogger } from '@/shared/modules/logger/logger.service';
import { Logger } from '@/shared/utils/logger.utils';

async function bootstrap() {
const app = await NestFactory.create(ApiModule, {
cors: true,
logger: new WinstonLogger(Logger),
});

const config = new DocumentBuilder()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Module, MiddlewareConsumer, forwardRef } from '@nestjs/common';
import { Module, MiddlewareConsumer, forwardRef, Logger } from '@nestjs/common';

import { ApiDepotModule } from '@/shared/modules/api_depot/api_depot.module';

Expand All @@ -14,7 +14,7 @@ import { PublicationModule } from '@/shared/modules/publication/publication.modu
forwardRef(() => BaseLocaleModule),
PublicationModule,
],
providers: [HabilitationService, BaseLocaleMiddleware],
providers: [HabilitationService, BaseLocaleMiddleware, Logger],
controllers: [HabilitationController],
exports: [HabilitationService],
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,70 @@
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { HttpException, HttpStatus, Injectable, Logger } from '@nestjs/common';
import { ObjectId } from 'mongodb';

import {
Habilitation,
StatusHabiliation,
} from '@/shared/modules/api_depot/types/habilitation.type';
import { ApiDepotService } from '@/shared/modules/api_depot/api_depot.service';
import { BaseLocale } from '@/shared/entities/base_locale.entity';

import { BaseLocaleService } from '../../base_locale.service';

@Injectable()
export class HabilitationService {
constructor(
private readonly baseLocaleService: BaseLocaleService,
private readonly apiDepotService: ApiDepotService,
private readonly logger: Logger,
) {}

async findOne(habilitationId: string): Promise<Habilitation> {
return this.apiDepotService.findOneHabiliation(habilitationId);
if (!ObjectId.isValid(habilitationId)) {
throw new HttpException(
'L’identifiant de l’habilitation est invalide',
HttpStatus.BAD_REQUEST,
);
}

try {
return await this.apiDepotService.findOneHabiliation(habilitationId);
} catch (error) {
this.logger.error(
`Impossible de trouver l'habilitation ${habilitationId}`,
error.response?.data || 'No server response',
HabilitationService.name,
);
throw new HttpException(
(error.response?.data as any).message || 'No server response',
HttpStatus.BAD_GATEWAY,
);
}
}

async isValid(habilitationId: string): Promise<boolean> {
const habilitation: Habilitation =
await this.apiDepotService.findOneHabiliation(habilitationId);
let habilitation: Habilitation;

if (!ObjectId.isValid(habilitationId)) {
throw new HttpException(
'L’identifiant de l’habilitation est invalide',
HttpStatus.NOT_FOUND,
);
}

try {
habilitation =
await this.apiDepotService.findOneHabiliation(habilitationId);
} catch (error) {
this.logger.error(
`Impossible de trouver l'habilitation ${habilitationId}`,
error.response?.data || 'No server response',
HabilitationService.name,
);
throw new HttpException(
(error.response?.data as any).message || 'No server response',
HttpStatus.BAD_GATEWAY,
);
}

// On verifie que l'habilitation est valide
if (habilitation.status !== StatusHabiliation.ACCEPTED) {
return false;
Expand Down Expand Up @@ -51,9 +93,21 @@ export class HabilitationService {
);
}
}

const habilitation: Habilitation =
await this.apiDepotService.createOneHabiliation(baseLocale);
let habilitation: Habilitation;
try {
habilitation =
await this.apiDepotService.createOneHabiliation(baseLocale);
} catch (error) {
this.logger.error(
`Impossible de créer une habilitation pour la commune ${baseLocale.commune}`,
error.response?.data || 'No server response',
HabilitationService.name,
);
throw new HttpException(
(error.response?.data as any).message || 'No server response',
HttpStatus.BAD_GATEWAY,
);
}

await this.baseLocaleService.updateHabilitation(baseLocale, habilitation);

Expand All @@ -72,8 +126,15 @@ export class HabilitationService {
try {
await this.apiDepotService.sendPinCodeHabiliation(habilitationId);
} catch (error) {
const { statusCode, message } = error.response.data;
throw new HttpException(message, statusCode);
this.logger.error(
`Impossible d'envoyer le code pour l'habilitation ${habilitationId}`,
error.response?.data || 'No server response',
HabilitationService.name,
);
throw new HttpException(
(error.response?.data as any).message || 'No server response',
HttpStatus.BAD_GATEWAY,
);
}
}

Expand All @@ -94,8 +155,15 @@ export class HabilitationService {
);
return data;
} catch (error) {
const { statusCode, message } = error.response.data;
throw new HttpException(message, statusCode);
this.logger.error(
`Impossible de valider le code pour l'habilitation ${habilitationId}`,
error.response?.data || 'No server response',
HabilitationService.name,
);
throw new HttpException(
(error.response?.data as any).message || 'No server response',
HttpStatus.BAD_GATEWAY,
);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Module, forwardRef } from '@nestjs/common';
import { Logger, Module, forwardRef } from '@nestjs/common';
import { PopulateService } from './populate.service';
import { BaseLocaleModule } from '../../base_locale.module';
import { ApiDepotModule } from '@/shared/modules/api_depot/api_depot.module';
Expand All @@ -10,7 +10,7 @@ import { BanPlateformModule } from '@/shared/modules/ban_plateform/ban_plateform
forwardRef(() => ApiDepotModule),
forwardRef(() => BanPlateformModule),
],
providers: [PopulateService],
providers: [PopulateService, Logger],
exports: [PopulateService],
})
export class PopulateModule {}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { FromCsvType, extractFromCsv } from '@/lib/utils/csv.utils';
import { ApiDepotService } from '@/shared/modules/api_depot/api_depot.service';
import { BanPlateformService } from '@/shared/modules/ban_plateform/ban_plateform.service';
import { Inject, Injectable, forwardRef } from '@nestjs/common';
import { Inject, Injectable, forwardRef, Logger } from '@nestjs/common';

@Injectable()
export class PopulateService {
constructor(
private apiDepotService: ApiDepotService,
@Inject(forwardRef(() => BanPlateformService))
private banPlateformService: BanPlateformService,
private readonly logger: Logger,
) {}

private async extractFromApiDepot(codeCommune: string): Promise<FromCsvType> {
Expand All @@ -17,7 +18,6 @@ export class PopulateService {
await this.apiDepotService.downloadCurrentRevisionFile(codeCommune);

const result: FromCsvType = await extractFromCsv(fileData, codeCommune);

if (!result.isValid) {
throw new Error('Invalid CSV file');
}
Expand All @@ -32,7 +32,6 @@ export class PopulateService {
await this.banPlateformService.getBanAssemblage(codeCommune);

const result = await extractFromCsv(file, codeCommune);

if (!result.isValid) {
throw new Error('Invalid CSV file');
}
Expand All @@ -50,8 +49,10 @@ export class PopulateService {
return data;
}

console.error(
this.logger.error(
`Aucune adresse n’a pu être extraite avec le code commune: ${codeCommune}`,
null,
PopulateService.name,
);

return { voies: [], numeros: [], toponymes: [] };
Expand Down
8 changes: 4 additions & 4 deletions apps/api/test/habilitation.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,10 @@ describe('HABILITATION MODULE', () => {
const response = await request(app.getHttpServer())
.get(`/bases-locales/${balId}/habilitation`)
.set('Authorization', `Bearer ${token}`)
.expect(404);
.expect(502);

const responseExpected = {
statusCode: 404,
statusCode: 502,
message: 'L’identifiant de l’habilitation demandé n’existe pas',
};

Expand Down Expand Up @@ -434,10 +434,10 @@ describe('HABILITATION MODULE', () => {
.post(`/bases-locales/${balId}/habilitation/email/validate-pin-code`)
.set('Authorization', `Bearer ${token}`)
.send({ code: '123456' })
.expect(412);
.expect(502);

expect(response.body).toEqual({
statusCode: 412,
statusCode: 502,
message: 'Code non valide, 9 tentatives restantes',
});
});
Expand Down
3 changes: 2 additions & 1 deletion apps/cron/src/cron.module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Module } from '@nestjs/common';
import { Logger, Module } from '@nestjs/common';
import { ScheduleModule } from '@nestjs/schedule';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { MailerModule } from '@nestjs-modules/mailer';
Expand Down Expand Up @@ -51,6 +51,7 @@ import { CacheModule } from '@/shared/modules/cache/cache.module';
DetectConflictTask,
RemoveSoftDeleteBalTask,
RemoveDemoBalTask,
Logger,
],
})
export class CronModule {}
7 changes: 6 additions & 1 deletion apps/cron/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { NestFactory } from '@nestjs/core';
import { CronModule } from './cron.module';

import { WinstonLogger } from '@/shared/modules/logger/logger.service';
import { Logger } from '@/shared/utils/logger.utils';

async function bootstrap() {
await NestFactory.createApplicationContext(CronModule);
await NestFactory.createApplicationContext(CronModule, {
logger: new WinstonLogger(Logger),
});
}
bootstrap();
16 changes: 12 additions & 4 deletions apps/cron/src/task_queue.class.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Logger } from '@/shared/utils/logger.utils';

export type Task = {
title: string;
run(): Promise<void>;
Expand All @@ -19,14 +21,20 @@ export class TaskQueue {

while (this.queue.length > 0) {
const task = this.queue.shift();
console.log(`TASK START ${task.title}`);
Logger.info(
`[${TaskQueue.name}] TASK START ${task.title}`,
TaskQueue.name,
);
try {
await task.run();
} catch (error) {
console.error(`TASK ERROR ${task.title}`);
console.error(error);
Logger.error(
`[${TaskQueue.name}] TASK ERROR ${task.title}`,
error,
TaskQueue.name,
);
}
console.log(`TASK END ${task.title}`);
Logger.info(`[${TaskQueue.name}] TASK END ${task.title}`, TaskQueue.name);
}

this.isTaskRunning = false;
Expand Down
25 changes: 17 additions & 8 deletions apps/cron/src/tasks/detect_conflict.task.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable } from '@nestjs/common';
import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { In, Repository } from 'typeorm';

Expand All @@ -25,6 +25,7 @@ export class DetectConflictTask implements Task {
@InjectRepository(BaseLocale)
private basesLocalesRepository: Repository<BaseLocale>,
private cacheService: CacheService,
private readonly logger: Logger,
) {}

public async run() {
Expand All @@ -33,14 +34,17 @@ export class DetectConflictTask implements Task {
KEY_DETECT_CONFLICT_PUBLISHED_SINCE,
);
const detectConflictPublishedSince = new Date(cache?.value || '1970-01-01');
console.log('Detect conflict since : ', detectConflictPublishedSince);
this.logger.log(
`Detect conflict since : ${detectConflictPublishedSince}`,
DetectConflictTask.name,
);
const currentRevisions: Revision[] =
await this.apiDepotService.getCurrentRevisions(
detectConflictPublishedSince,
);
console.log(
'Number of current revisions processed : ',
currentRevisions.length,
this.logger.log(
`Number of current revisions processed : ${currentRevisions.length}`,
DetectConflictTask.name,
);
const revisedCommunes = currentRevisions.map((r) => r.codeCommune);

Expand All @@ -53,8 +57,11 @@ export class DetectConflictTask implements Task {
try {
await this.updateConflictStatus(codeCommune);
} catch (error) {
console.error(`Unable to detect conflict for ${codeCommune}`);
console.error(error);
this.logger.error(
`Unable to detect conflict for ${codeCommune}`,
error,
DetectConflictTask.name,
);
}
}
}
Expand All @@ -73,8 +80,10 @@ export class DetectConflictTask implements Task {
await this.apiDepotService.getCurrentRevision(codeCommune);

if (!currentRevision) {
console.error(
this.logger.error(
`Comportement inattendu : pas de révision courante pour la commune ${codeCommune}`,
null,
DetectConflictTask.name,
);
return;
}
Expand Down
Loading
Loading