Skip to content

Commit

Permalink
feat(structure-milo): ajouter region et departement au referentiel
Browse files Browse the repository at this point in the history
  • Loading branch information
Mzem committed Jun 23, 2023
1 parent 9ea1e58 commit 7551d24
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 20 deletions.
68 changes: 49 additions & 19 deletions scripts/data-migrations/remplir_les_structures_milo.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,64 @@
require('dotenv').config()
const { Sequelize } = require('sequelize')
const sequelize = new Sequelize(process.env.DATABASE_URL)
const sequelize = new Sequelize(process.env.DATABASE_URL, {
dialect: 'postgres'
})

const conseillersMiloDuCEJ = require('./extract_conseillers_milo_250523.json')
const conseillersMiloDuCEJEtLeurStructureDeReference = require('./structures_conseillers_milo_250523.json')
const referentielStructuresMilo = require('./referentiel_structures_milo.json')
const conseillersMiloDuCEJ = require('./conseillers_milo_cej.json')
const conseillersMiloDuCEJEtLeurStructureDeReference = require('./structures_conseillers_milo_cej.json')

sequelize.transaction(async transaction => {
// Alimentation referentiel structures Milo
for (const structureMilo of referentielStructuresMilo) {
await sequelize.query(
`INSERT INTO structure_milo (id, nom_officiel, nom_usuel, nom_region, code_region, nom_departement, code_departement) VALUES (?, ?, ?, ?, ?, ?, ?) ON CONFLICT (id) DO UPDATE SET nom_officiel = ?, nom_usuel = ?, nom_region = ?, code_region = ?, nom_departement = ?, code_departement = ?`,
{
replacements: [
structureMilo.id,
structureMilo.nom_officiel,
structureMilo.nom_usuel ?? null,
structureMilo.region,
structureMilo.code_region,
structureMilo.departement,
structureMilo.code_departement,
structureMilo.nom_officiel,
structureMilo.nom_usuel ?? null,
structureMilo.region,
structureMilo.code_region,
structureMilo.departement,
structureMilo.code_departement
],
transaction
}
)
}

for (const conseillerEtStructure of conseillersMiloDuCEJEtLeurStructureDeReference) {
// si le conseiller a une structure, on rajoute cette structure au referentiel
// si le conseiller a une structure Milo, on la rajoute au référentiel si elle n'existe pas
if (conseillerEtStructure.id_ml_principale) {
const nomUsuelOuNull = conseillerEtStructure.nom_usuel
? conseillerEtStructure.nom_usuel
: null
await sequelize.query(
`INSERT INTO structure_milo (id, nom_officiel, nom_usuel) VALUES (?, ?, ?) ON CONFLICT (id) DO UPDATE SET nom_officiel = ?, nom_usuel = ?`,
{
replacements: [
conseillerEtStructure.id_ml_principale,
conseillerEtStructure.nom_officiel,
nomUsuelOuNull,
conseillerEtStructure.nom_officiel,
nomUsuelOuNull
],
transaction
}
)

if (conseillerEtStructure.nom_officiel) {
await sequelize.query(
`INSERT INTO structure_milo (id, nom_officiel, nom_usuel) VALUES (?, ?, ?) ON CONFLICT DO NOTHING`,
{
replacements: [
conseillerEtStructure.id_ml_principale,
conseillerEtStructure.nom_officiel,
nomUsuelOuNull
],
transaction
}
)
}

// rattachement entre conseiller Milo dans le CEJ et structure Milo
conseillerCEJ = conseillersMiloDuCEJ.find(
conseillerMiloDuCEJ =>
conseillerMiloDuCEJ.username === conseillerEtStructure.username
conseillerMiloDuCEJ.login === conseillerEtStructure.username
)

if (conseillerCEJ) {
Expand All @@ -37,7 +67,7 @@ sequelize.transaction(async transaction => {
{
replacements: [
conseillerEtStructure.id_ml_principale,
conseillerCEJ.id_bdd_cej
conseillerCEJ.id_cej
],
transaction
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
'use strict'

module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.sequelize.transaction(async transaction => {
await queryInterface.addColumn(
'structure_milo',
'nom_region',
{
type: Sequelize.STRING,
allowNull: true
},
{ transaction }
)
await queryInterface.addColumn(
'structure_milo',
'code_region',
{
type: Sequelize.STRING,
allowNull: true
},
{ transaction }
)
await queryInterface.addColumn(
'structure_milo',
'nom_departement',
{
type: Sequelize.STRING,
allowNull: true
},
{ transaction }
)
await queryInterface.addColumn(
'structure_milo',
'code_departement',
{
type: Sequelize.STRING,
allowNull: true
},
{ transaction }
)
})
},

async down(queryInterface, Sequelize) {
await queryInterface.sequelize.transaction(async transaction => {
await queryInterface.removeColumn('structure_milo', 'nom_region', {
transaction
})
await queryInterface.removeColumn('structure_milo', 'code_region', {
transaction
})
await queryInterface.removeColumn('structure_milo', 'nom_departement', {
transaction
})
await queryInterface.removeColumn('structure_milo', 'code_departement', {
transaction
})
})
}
}
24 changes: 24 additions & 0 deletions src/infrastructure/sequelize/models/structure-milo.sql-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,30 @@ export class StructureMiloDto extends Model {
type: DataType.STRING
})
nomUsuel: string | null

@Column({
field: 'nom_region',
type: DataType.STRING
})
nomRegion?: string | null

@Column({
field: 'code_region',
type: DataType.STRING
})
codeRegion?: string | null

@Column({
field: 'nom_departement',
type: DataType.STRING
})
nomDepartement?: string | null

@Column({
field: 'code_departement',
type: DataType.STRING
})
codeDepartement?: string | null
}

@Table({
Expand Down
6 changes: 5 additions & 1 deletion test/fixtures/sql-models/structureMilo.sql-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ export function uneStructureMiloDto(
const defaults: AsSql<StructureMiloDto> = {
id: '1',
nomOfficiel: '06-Nice',
nomUsuel: 'Nice'
nomUsuel: 'Nice',
nomDepartement: undefined,
codeDepartement: undefined,
nomRegion: undefined,
codeRegion: undefined
}

return { ...defaults, ...args }
Expand Down

0 comments on commit 7551d24

Please sign in to comment.