Skip to content

Commit

Permalink
fix(backend): fix search of some users
Browse files Browse the repository at this point in the history
  • Loading branch information
pYassine committed Aug 6, 2024
1 parent a00e4a1 commit 03dad5f
Show file tree
Hide file tree
Showing 18 changed files with 86 additions and 77 deletions.
3 changes: 0 additions & 3 deletions packages/backend/.env.preset/dist-prod.preset.env
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,3 @@ DOMIFA_CRON_EMAIL_USER_GUIDE_DELAY="7 days"
DOMIFA_CRON_EMAIL_IMPORT_GUIDE_CRONTIME="0 15 * * TUE"
DOMIFA_CRON_EMAIL_IMPORT_GUIDE_DELAY="7 days"


# 50 connexions max en prod: https://docs.microsoft.com/fr-fr/azure/postgresql/concepts-limits
POSTGRES_POOL_MAX_CONNEXIONS="100"
5 changes: 0 additions & 5 deletions packages/backend/.env.preset/dist.preset.env
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,3 @@ DOMIFA_SWAGGER_ENABLE=false

DOMIFA_SENTRY_ENABLED=true
DOMIFA_SENTRY_DEBUG_MODE_ENABLED=false


# 50 connexions en tout à partager entre la preprod + les environnements de dev :
# https://docs.microsoft.com/fr-fr/azure/postgresql/concepts-limits
POSTGRES_POOL_MAX_CONNEXIONS="100"
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,12 @@ export class AdminStructuresController {

@Get("last-update")
@AllowUserProfiles("super-admin-domifa")
public async getLastUpdate(): Promise<Date> {
public async getLastUpdate(): Promise<Date | null> {
const lastUsager = await structureRepository.findOne({
where: {},
order: { createdAt: "DESC" },
});
return lastUsager.createdAt;
return lastUsager?.createdAt ?? null;
}

@Get("")
Expand Down
6 changes: 6 additions & 0 deletions packages/backend/src/auth/guards/AppUserGuard.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ export class AppUserGuard implements CanActivate {
},
});

if (request?.body) {
addLogContext({
body: request?.body,
});
}

getCurrentScope().setUser({
email: user.email,
username:
Expand Down
9 changes: 0 additions & 9 deletions packages/backend/src/config/domifaConfig.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,6 @@ export function loadConfig(x: Partial<DomifaEnv>): DomifaConfig {
required: false,
defaultValue: false,
}),

poolMaxConnections: configParser.parseInteger(
x,
"POSTGRES_POOL_MAX_CONNEXIONS",
{
defaultValue: 10, // 10 is also driver default: https://node-postgres.com/api/pool#constructor
}
),
},
typeorm: {
runOnStartup: configParser.parseBoolean(
Expand Down Expand Up @@ -272,7 +264,6 @@ export function loadConfig(x: Partial<DomifaEnv>): DomifaConfig {
defaultValue: CronExpression.EVERY_DAY_AT_11PM,
}
),

delay: configParser.parseDelay(
x,
"DOMIFA_CRON_MONITORING_CLEANER_DELAY",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ export type DomifaConfigPostgres = {
password: string; // POSTGRES_PASSWORD
database: string; // POSTGRES_DATABASE
ssl: boolean; // POSTGRES_SSL
poolMaxConnections: number; // POSTGRES_POOL_MAX_CONNEXIONS
};
2 changes: 0 additions & 2 deletions packages/backend/src/config/model/DomifaEnv.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,13 @@ export type DomifaEnv = {
DOMIFA_CRON_EMAIL_CONSUMER_CRONTIME: string;
DOMIFA_CRON_SMS_CONSUMER_CRONTIME: string;
DOMIFA_CRON_FETCH_END_DOM_CRONTIME: string;

DOMIFA_ANONYMIZER_PASSWORD: string;
POSTGRES_HOST: string;
POSTGRES_PORT: string;
POSTGRES_USERNAME: string;
POSTGRES_PASSWORD: string;
POSTGRES_DATABASE: string;
POSTGRES_SSL: boolean;
POSTGRES_POOL_MAX_CONNEXIONS: string;
DOMIFA_TYPEORM_RUN_ON_STARTUP: string;
DOMIFA_MAIL_SMTP_ID: string;
DOMIFA_MAIL_SMTP_MAILTRAP_HOST: string;
Expand Down
5 changes: 3 additions & 2 deletions packages/backend/src/config/services/configParser.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
DomifaEnv,
DomifaEnvKey,
DOMIFA_CONFIG_DELAY_UNITS,
DomifaConfigDelay,
} from "../model";

export const configParser = {
Expand Down Expand Up @@ -153,7 +154,7 @@ function parseDelay<T extends string>(
} = {
required: true,
}
) {
): DomifaConfigDelay {
const value = parseString(envConfig, key, {
defaultValue,
required,
Expand All @@ -178,7 +179,7 @@ function parseDelay<T extends string>(
}
return { amount, unit };
}
return undefined;
return defaultValue as unknown as DomifaConfigDelay;
}

function parseIntegerFromString(value: string): number | undefined {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const PG_CONNECT_OPTIONS: PostgresConnectionOptions = {
username: domifaConfig().postgres.username,
password: domifaConfig().postgres.password,
database: domifaConfig().postgres.database,
poolSize: 100,
poolSize: domifaConfig().envId === "test" ? 1 : 100,
ssl: domifaConfig().postgres.ssl
? {
rejectUnauthorized: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ async function connect(

if (domifaConfig().envId !== "test") {
appLogger.warn(
`[appTypeormManager] Connecting to postgres database "${pgConfig.database}" at ${pgConfig.host}:${pgConfig.port} (max poolMaxConnections=${pgConfig.poolMaxConnections}")`
`[appTypeormManager] Connecting to postgres database "${pgConfig.database}" at ${pgConfig.host}:${pgConfig.port}`
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ describe("interactionsCreator", () => {
});

expect(resultat.usager.lastInteraction.dateInteraction).toEqual(
MOCKED_NEW_DATE
new Date(MOCKED_NEW_DATE)
);
expect(resultat.interaction.nbCourrier).toEqual(0);
});
Expand All @@ -273,7 +273,7 @@ describe("interactionsCreator", () => {
});

expect(resultat.usager.lastInteraction.dateInteraction).toEqual(
MOCKED_NEW_DATE
new Date(MOCKED_NEW_DATE)
);

expect(resultat.interaction.nbCourrier).toEqual(0);
Expand Down Expand Up @@ -514,7 +514,7 @@ describe("interactionsCreator", () => {
interaction,
});
expect(resultat.usager.lastInteraction.dateInteraction).toEqual(
MOCKED_NEW_DATE
new Date(MOCKED_NEW_DATE)
);

expect(resultat.interaction.nbCourrier).toEqual(0);
Expand Down
4 changes: 2 additions & 2 deletions packages/backend/src/open-data-places/load-mss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const loadMssData = async () => {
!domifaConfig().openDataApps.mssUrl ||
!domifaConfig().openDataApps.mssToken
) {
appLogger.log("[IMPORT DATA MSS] Fail, token or url is not in env");
appLogger.info("[IMPORT DATA MSS] Fail, token or url is not in env");
return;
}
appLogger.info("Import MSS start 🏃‍♂️... ");
Expand All @@ -44,7 +44,7 @@ const getFromMss = async () => {
const position = await getLocation(address);

if (!position) {
appLogger.log("Adresse not found " + address);
appLogger.warn("Adresse not found " + address);
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export class UsagersController {
.createQueryBuilder()
.select(joinSelectFields(USAGER_LIGHT_ATTRIBUTES))
.where(
`"structureId" = :structureId and statut = :statut AND nom_prenom ILIKE :search`,
`"structureId" = :structureId and statut = :statut AND nom_prenom ILIKE :search`,
{
statut: "RADIE",
structureId: user.structureId,
Expand Down
52 changes: 35 additions & 17 deletions packages/backend/src/util/AppLogger.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pino, Logger, SerializedRequest } from "pino";
import { pino, Logger, SerializedRequest, LoggerOptions } from "pino";
import * as pinoSerializers from "pino-std-serializers";
import traceCaller from "./traceCaller";
import { randomUUID } from "crypto";
Expand All @@ -19,8 +19,30 @@ class Store {
}

const requestContextStorage = new AsyncLocalStorage<Store>();
const pinoOptions: LoggerOptions = {
redact: {
paths: [
"password",
"token",
"secret",
"ssn",
"body.password",
"body.token",
"body.secret",
],
censor: "[REDACTED]",
},
serializers: {
req: (request: IncomingMessage) =>
redactAuthorizationHeader(pinoSerializers.req(request)),
res: pinoSerializers.res,
body: (body) => {
return body;
},
},
};

const rootLogger = traceCaller(pino());
const rootLogger: Logger = traceCaller(pino(pinoOptions));

function log(
logger: Logger,
Expand Down Expand Up @@ -60,7 +82,7 @@ function getLogger(_target: any, name: string) {
}

// Main app logger, using either rootLogger if not inside a request, or a child logger stored in requestContextStorage
export const appLogger = new Proxy(rootLogger, { get: getLogger });
export const appLogger: Logger = new Proxy(rootLogger, { get: getLogger });

export function addLogContext(fields: pino.Bindings) {
const store = requestContextStorage.getStore();
Expand All @@ -79,25 +101,18 @@ function redactAuthorizationHeader(req: SerializedRequest): SerializedRequest {
authorization: `${authorization.slice(0, 10)}-REDACTED`,
};
}

return req;
}

function httpLogger(req: RequestWithId, res: Response, next: NextFunction) {
req.id = req.headers["X-Request-Id"] || randomUUID();
const startTime = Date.now();

const requestLogger = rootLogger.child(
{
req,
},
{
serializers: {
req: (request: IncomingMessage) =>
redactAuthorizationHeader(pinoSerializers.req(request)),
res: pinoSerializers.res,
},
}
);
const requestLogger = rootLogger.child({
req,
body: req.body,
});

function onResFinished() {
res.removeListener("close", onResFinished);
Expand All @@ -109,9 +124,12 @@ function httpLogger(req: RequestWithId, res: Response, next: NextFunction) {
const store = requestContextStorage.getStore();

if (store) {
store.logger.info({ res, responseTime }, "http_request");
store.logger.info({ res, responseTime, body: req.body }, "http_request");
} else {
rootLogger.warn({ responseTime }, "http_request NO CONTEXT");
rootLogger.warn(
{ responseTime, body: req.body },
"http_request NO CONTEXT"
);
}
}

Expand Down
17 changes: 6 additions & 11 deletions packages/backend/src/util/sentry/AppSentryInterceptor.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import {
InternalServerErrorException,
NestInterceptor,
} from "@nestjs/common";
import { addRequestDataToEvent } from "@sentry/node";
import { addRequestDataToEvent, Request as SentryRequest } from "@sentry/node";

import { Observable, throwError } from "rxjs";
import { catchError } from "rxjs/operators";
import { UserStructureAuthenticated } from "../../_common/model";
import { appLogger } from "../AppLogger.service";
import { Request } from "express";

@Injectable()
export class AppSentryInterceptor implements NestInterceptor {
Expand All @@ -31,9 +32,6 @@ export class AppSentryInterceptor implements NestInterceptor {
if (user) {
logContext.user = logSentryUser(user);
}
if (req?.body) {
logContext.payload = req.body;
}
} else {
prefix = "[core]";
}
Expand All @@ -57,12 +55,11 @@ export class AppSentryInterceptor implements NestInterceptor {
}

function parseRequest(context: ExecutionContext): {
req: any;
req: SentryRequest;
user: UserStructureAuthenticated;
body: any;
} {
const httpContext = context.switchToHttp();
const expressRequest: any = httpContext.getRequest();
const expressRequest: Request = httpContext.getRequest();

if (!expressRequest) {
return null;
Expand All @@ -71,26 +68,24 @@ function parseRequest(context: ExecutionContext): {
include: {
request: true,
user: false,
transaction: true,
},
});

const req = data.request;
const user = expressRequest.user as UserStructureAuthenticated;
const body = expressRequest.body;

return {
req,
user,
body,
};
}

function logSentryRequest(req: any): Record<string, any> {
const headers = req.headers ?? {};

return {
method: req.method,
url: req.url,
data: req?.data,
headers: {
host: headers.host,
origin: headers.origin,
Expand Down
3 changes: 0 additions & 3 deletions packages/backend/src/util/test/AppTestHelper.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,6 @@ async function tearDownTestApp({ module }: AppTestContext): Promise<void> {
async function bootstrapTestConnection(): Promise<DataSource> {
return await appTypeormManager.connect({
reuseConnexion: true,
overrideConfig: {
poolMaxConnections: 1,
},
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import { captureException } from "@sentry/angular";
import { CustomToastService } from "../modules/shared/services";
import { Router } from "@angular/router";

const MAX_RETRIES = 2;
const RETRY_DELAY = 1000;
const MAX_RETRIES = 1;
const RETRY_DELAY = 800;
const ERROR_STATUS_CODES_TO_RETRY = [0, 408, 500, 502, 503, 504];

@Injectable({
Expand All @@ -38,6 +38,7 @@ export class ServerErrorInterceptor implements HttpInterceptor {
count: MAX_RETRIES,
delay: (error, retryCount) => {
if (this.isRetryable(error)) {
console.log(error);
console.log(`Tentative de nouvelle requête ${retryCount}`);
return timer(RETRY_DELAY);
}
Expand Down
Loading

0 comments on commit 03dad5f

Please sign in to comment.