Skip to content

Commit

Permalink
Merge pull request #428 from dnum-mi/tech/harmonize-api-logs
Browse files Browse the repository at this point in the history
refactor: 🔊 harmonize api logs
  • Loading branch information
ArnaudTA authored May 23, 2023
2 parents 2e84326 + 02bcbd6 commit 98541f2
Show file tree
Hide file tree
Showing 23 changed files with 798 additions and 534 deletions.
1 change: 0 additions & 1 deletion apps/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
"dotenv": "^16.0.3",
"fastify": "^4.17.0",
"fastify-keycloak-adapter": "^2.0.1",
"lodash.kebabcase": "^4.1.1",
"mustache": "^4.2.0",
"nanoid": "4.0.2",
"node-vault-client": "^0.6.1",
Expand Down
5 changes: 4 additions & 1 deletion apps/server/src/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,10 @@ export const synchroniseModels = async () => {
} catch (error) {
app.log.error({
message: 'Models synchronisation with database failed.',
error: JSON.stringify(error),
error: {
message: error?.message,
trace: error?.trace,
},
})
}
}
72 changes: 39 additions & 33 deletions apps/server/src/controllers/admin/organizations.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,36 @@ import {
} from '../../models/queries/organization-queries.js'
import { organizationSchema } from 'shared/src/schemas/organization.js'
import { adminGroupPath } from 'shared/src/utils/const.js'
import { getLogInfos } from '../../utils/logger.js'
import { addReqLogs } from '../../utils/logger.js'
import { sendOk, sendCreated, sendNotFound, sendBadRequest, sendForbidden } from '../../utils/response.js'

// GET
export const getAllOrganizationsController = async (req, res) => {
if (!req.session.user.groups?.includes(adminGroupPath)) sendForbidden(res, 'Vous n\'avez pas les droits administrateurs')
if (!req.session.user.groups?.includes(adminGroupPath)) sendForbidden(res, 'Vous n\'avez pas les droits administrateur')

try {
const organizations = await getOrganizations()
req.log.info({
...getLogInfos(),
addReqLogs({
req,
description: 'Organisations récupérées avec succès',
})
sendOk(res, organizations)
} catch (error) {
const message = 'Echec de récupération des organisations'
req.log.error({
...getLogInfos(),
description: message,
error: error.message,
trace: error.trace,
const description = 'Echec de la récupération des organisations'
addReqLogs({
req,
description,
error,
})
sendNotFound(res, message)
sendNotFound(res, description)
}
}

// POST
export const createOrganizationController = async (req, res) => {
const data = req.body

if (!req.session.user.groups?.includes(adminGroupPath)) sendForbidden(res, 'Vous n\'avez pas les droits administrateurs')
if (!req.session.user.groups?.includes(adminGroupPath)) sendForbidden(res, 'Vous n\'avez pas les droits administrateur')

try {
await organizationSchema.validateAsync(data)
Expand All @@ -46,21 +45,23 @@ export const createOrganizationController = async (req, res) => {
if (isNameTaken) throw new Error('Cette organisation existe déjà')

const organization = await createOrganization(data)
req.log.info({
...getLogInfos({

addReqLogs({
req,
description: 'Organisation créée avec succès',
extras: {
organizationId: organization.id,
}),
description: 'L\'organisation a bien été enregistrée en base',
},
})
sendCreated(res, organization)
} catch (error) {
req.log.error({
...getLogInfos(),
description: 'Echec d\'enregistrement de l\'organisation',
error: error.message,
trace: error.trace,
const description = 'Echec de la création de l\'organisation'
addReqLogs({
req,
description,
error,
})
sendBadRequest(res, error.message)
sendBadRequest(res, description)
}
}

Expand All @@ -69,7 +70,7 @@ export const updateOrganizationController = async (req, res) => {
const name = req.params.orgName
const { active, label } = req.body

if (!req.session.user.groups?.includes(adminGroupPath)) sendForbidden(res, 'Vous n\'avez pas les droits administrateurs')
if (!req.session.user.groups?.includes(adminGroupPath)) sendForbidden(res, 'Vous n\'avez pas les droits administrateur')
try {
if (active !== undefined) {
await updateActiveOrganization({ name, active })
Expand All @@ -78,20 +79,25 @@ export const updateOrganizationController = async (req, res) => {
await updateLabelOrganization({ name, label })
}
const organization = await getOrganizationByName(name)
req.log.info({
...getLogInfos({

addReqLogs({
req,
description: 'Organisation mise à jour avec succès',
extras: {
organizationId: organization.id,
}),
description: 'L\'organisation a bien été mise à jour',
},
})
sendCreated(res, organization)
} catch (error) {
req.log.error({
...getLogInfos(),
description: 'Echec de mise à jour de l\'organisation',
error: error.message,
trace: error.trace,
const description = 'Echec de la mise à jour de l\'organisation'
addReqLogs({
req,
description,
extras: {
organizationName: name,
},
error,
})
sendBadRequest(res, error.message)
sendBadRequest(res, description)
}
}
26 changes: 13 additions & 13 deletions apps/server/src/controllers/admin/project.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import { getAllProjects } from '../models/queries/project-queries.js'
import { getLogInfos } from '../utils/logger.js'
import { sendOk, sendNotFound } from '../utils/response.js'
import { addReqLogs } from '../../utils/logger.js'
import { getAllProjects } from '../../models/queries/project-queries.js'
import { sendOk, sendNotFound } from '../../utils/response.js'
import { adminGroupPath } from 'shared/src/utils/const.js'

export const getAllProjectsController = async (req, res) => {
try {
if (!req.session.user.groups?.includes(adminGroupPath)) throw new Error('Vous n\'avez pas les droits administrateurs')
if (!req.session.user.groups?.includes(adminGroupPath)) throw new Error('Vous n\'avez pas les droits administrateur')
const projects = await getAllProjects()
req.log.info({
...getLogInfos(),
description: 'Projects récupérés avec succès',
addReqLogs({
req,
description: 'Ensemble des projets récupérés avec succès',
})
return sendOk(res, projects)
} catch (error) {
const message = 'Projets non trouvés'
req.log.error({
...getLogInfos(),
description: message,
error: error.message,
const description = 'Echec de la récupération de l\'ensemble des projets'
addReqLogs({
req,
description,
error,
})
sendNotFound(res, message)
sendNotFound(res, description)
}
}
20 changes: 11 additions & 9 deletions apps/server/src/controllers/admin/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,24 @@ import {
import { adminGroupPath } from 'shared/src/utils/const.js'

import { sendNotFound, sendOk } from '../../utils/response.js'
import { getLogInfos } from '../../utils/logger.js'
import { addReqLogs } from '../../utils/logger.js'

export const getUsersController = async (req, res) => {
try {
if (!req.session.user.groups?.includes(adminGroupPath)) throw new Error('Vous n\'avez pas les droits administrateurs')
if (!req.session.user.groups?.includes(adminGroupPath)) throw new Error('Vous n\'avez pas les droits administrateur')
const users = await getUsers()
req.log.info({
...getLogInfos(),
description: 'Utilisateurs récupérés avec succès',

addReqLogs({
req,
description: 'Ensemble des utilisateurs récupérés avec succès',
})
sendOk(res, users)
} catch (error) {
req.log.error({
...getLogInfos(),
description: 'Echec de récupération des utilisateurs',
error: error.message,
const description = 'Echec de la récupération de l\'ensemble des utilisateurs'
addReqLogs({
req,
description,
error,
})
sendNotFound(res, error.message)
}
Expand Down
Loading

0 comments on commit 98541f2

Please sign in to comment.