Skip to content
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"ejs": "^2.6.1",
"express": "5.1.0",
"express-validator": "7.2.1",
"fast-csv": "^5.0.5",
"handlebars": "^4.7.8",
"jsonwebtoken": "9.0.2",
"knex": "2.5.1",
Expand Down
33 changes: 33 additions & 0 deletions src/database/migration/20250827115833_update_locais_coleta.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* eslint-disable */

import { parseFile } from 'fast-csv'
import { Knex } from 'knex'

export async function run(knex: Knex): Promise<void> {
const updates: Array<{
HCF: string;
LOCAL_COLETA: string;
DESCRICAO_COLETA: string;
}> = []

await new Promise<void>((resolve, reject) => {
parseFile(`${__dirname}/locais_coleta_corrigidos.csv`, { headers: true })
.on('error', error => reject(error))
.on('data', row => {
updates.push(row)
})
.on('end', () => resolve())
})

console.log(updates.length)

await knex.transaction(async trx => {
for (const update of updates) {
const { LOCAL_COLETA, DESCRICAO_COLETA } = update
await trx('locais_coleta').where({ id: LOCAL_COLETA })
.update({
descricao: DESCRICAO_COLETA
})
}
})
}
173 changes: 173 additions & 0 deletions src/database/migration/20250827122308_create_locais_coleta.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
/* eslint-disable */

import { Knex } from 'knex'

export async function run(knex: Knex): Promise<void> {
await knex.transaction(async trx => {
let res

res = await trx('locais_coleta').insert({
descricao:
'Parque Estadual Serra da Baitaca-Caminho do Itupava. Trilha entre a entrada p/ o antes da entrada para o Morro Pão de Loth',
cidade_id: 4120804
})

await trx('tombos')
.update({
local_coleta_id: res[0]
})
.where({
hcf: 42136
})

res = await trx('locais_coleta').insert({
descricao:
'Parque Estadual Serra da Baitaca-Caminho do Itupava. Trilha entre a entrada p/ o Morro Pão de Loth até as Ruínas do Ipiranga',
cidade_id: 4120804
})

await trx('tombos')
.update({
local_coleta_id: res[0]
})
.where({
hcf: 41996
})

res = await trx('locais_coleta').insert({
descricao:
'APA Gama-Cabeça de Veado.ARIE do Córrego do Cedro. R.A. do Núcleo Bandeirante; SMPW. Q.26, conj. 3. Próximo à A.E.E. (EMBRAPA)',
cidade_id: 5300108
})

await trx('tombos')
.update({
local_coleta_id: res[0]
})
.where({
hcf: 30781
})

res = await trx('locais_coleta').insert({
descricao:
'APA Gama-Cabeça de Veado.ARIE do Córrego do Cedro. R.A. do Núcleo Bandeirante; Lagoa do Córrego do Cedro. Em frente ao SMPW Q.16 conj. 1',
cidade_id: 5300108
})

await trx('tombos')
.update({
local_coleta_id: res[0]
})
.where({
hcf: 30869
})

res = await trx('locais_coleta').insert({
descricao:
'APA Gama-Cabeça de Veado.ARIE do Córrego do Cedro. R.A. do Núcleo Bandeirante; Park Way. Clube da EMBRAPA. Q. 26. Conjunto 3 (área pública)',
cidade_id: 5300108
})

await trx('tombos')
.update({
local_coleta_id: res[0]
})
.where({
hcf: 30619
})

res = await trx('locais_coleta').insert({
descricao:
'APA Gama-Cabeça de Veado. ARIE Córrego do Cedro-R.A. Núcleo Bandeirante. SMPW Q 26 entre o conjunto 10 e a EMBRAPA (A.E.E)',
cidade_id: 5300108
})

await trx('tombos')
.update({
local_coleta_id: res[0]
})
.where({
hcf: 20082
})

await trx('tombos')
.update({
local_coleta_id: res[0]
})
.where({
hcf: 30868
})

res = await trx('locais_coleta').insert({
descricao:
'APA Gama-Cabeça de Veado. ARIE Córrego do Cedro-R.A. Núcleo Bandeirante. SMPW Lagoa do Córrego do Cedro. Q 16. Conj. 01. Lote 4',
cidade_id: 5300108
})

await trx('tombos')
.update({
local_coleta_id: res[0]
})
.where({
hcf: 30780
})

await trx('tombos')
.update({
local_coleta_id: res[0]
})
.where({
hcf: 30801
})

res = await trx('locais_coleta').insert({
descricao:
'APA Gama-Cabeça de Veado. ARIE Córrego do Cedro-R.A. Núcleo Bandeirante. SMPW Park Way. Clube da EMBRAPA. Q 26. Conjunto 3 (área pública)',
cidade_id: 5300108
})

await trx('tombos')
.update({
local_coleta_id: res[0]
})
.where({
hcf: 30620
})

await trx('tombos')
.update({
local_coleta_id: res[0]
})
.where({
hcf: 30621
})

res = await trx('locais_coleta').insert({
descricao:
'APA Gama e Cabeça de Veado, R.A. do Lago Sul. Fazenda Água Limpa (FAL-UnB). Área da "MOA"',
cidade_id: 5300108
})

await trx('tombos')
.update({
local_coleta_id: res[0]
})
.where({
hcf: 20083
})

res = await trx('locais_coleta').insert({
descricao:
'APA Gama e Cabeça de Veado, R.A. do Lago Sul. Fazenda Água Limpa (FAL-UnB). Área Borda de mata de galeria do Córrego do Macaco, afluente Gama',
cidade_id: 5300108
})

await trx('tombos')
.update({
local_coleta_id: res[0]
})
.where({
hcf: 38121
})
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* eslint-disable */

import { Knex } from 'knex'

export async function run(knex: Knex): Promise<void> {
await knex.transaction(async trx => {
await trx('cidades').where({ nome: 'Não Informado' })
.del()
await trx('estados').where({ nome: 'Não Informado' })
.del()
await trx('paises').where({ nome: 'Não Informado' })
.del()

await trx('paises').where({ sigla: 'OOO' })
.del()

await trx('paises').insert({
nome: 'Não Informado',
sigla: 'NI'
})

const paisesList = await trx('paises')

for (const pais of paisesList) {
await trx('estados').insert({
nome: 'Não Informado',
sigla: 'NI',
pais_id: pais.id
})
}

const estadosList = await trx('estados')

for (const estado of estadosList) {
await trx('cidades').insert({
nome: 'Não Informado',
estado_id: estado.id
})
}
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/* eslint-disable */

import { parseFile } from 'fast-csv'
import { Knex } from 'knex'

const getSinitizedState: { [key: string]: string } = {
PARANA: 'Paraná',
'SANTA CATARINA': 'Santa Catarina',
'RIO GRANDE DO SUL': 'Rio Grande do Sul',
'SAO PAULO': 'São Paulo',
'RIO DE JANEIRO': 'Rio de Janeiro',
'MINAS GERAIS': 'Minas Gerais',
'ESPIRITO SANTO': 'Espírito Santo',
'DISTRITO FEDERAL': 'Distrito Federal',
GOIAS: 'Goiás',
'MATO GROSSO DO SUL': 'Mato Grosso do Sul',
'MATO GROSSO': 'Mato Grosso',
TOCANTINS: 'Tocantins',
BAHIA: 'Bahia',
SERGIPE: 'Sergipe',
ALAGOAS: 'Alagoas',
PERNAMBUCO: 'Pernambuco',
CEARA: 'Ceará',
'RIO GRANDE DO NORTE': 'Rio Grande do Norte',
PARAIBA: 'Paraíba',
MARANHAO: 'Maranhão',
PIAUI: 'Piauí',
AMAPA: 'Amapá',
RORAIMA: 'Roraima',
AMAZONAS: 'Amazonas',
ACRE: 'Acre',
RONDONIA: 'Rondônia',
PARA: 'Pará',
CEARÁ: 'Ceará'
}

export async function run(knex: Knex): Promise<void> {
const updates: Array<{
CODIGO: string;
CIDADE: string;
ESTADO: string;
PAIS: string;
}> = []

await new Promise<void>((resolve, reject) => {
parseFile(`${__dirname}/20250827130059_adiciona_cidade_id_aos_locais_coleta/locais_traduzidos.csv`, { headers: true })
.on('error', error => reject(error))
.on('data', row => {
updates.push(row)
})
.on('end', () => resolve())
})

await knex.transaction(async trx => {
for (const update of updates) {
const { CODIGO, CIDADE, ESTADO, PAIS } = update

let cidadeId: number | null = null

if (CIDADE !== 'NI') {
// Já temos o ID final no CSV
cidadeId = Number(CIDADE)
} else {
if (ESTADO === 'NI') {
// Caso especial onde tanto cidade quanto estado são "Não Informado"
const paisRecord = await trx('paises').where({ nome: PAIS })
.first()

if (!paisRecord) {
continue
}

const estadoRecord = await trx('estados')
.where({ sigla: 'NI', pais_id: paisRecord.id })
.first()

if (!estadoRecord) {
continue
}

const cidadeRecord = await trx('cidades')
.where({ nome: 'Não Informado', estado_id: estadoRecord.id })
.first()

if (!cidadeRecord) {
continue
}

await trx('locais_coleta')
.where({ id: CODIGO })
.update({ cidade_id: cidadeRecord.id })

continue
}

const estadoRecord = await trx('estados')
.where({ nome: getSinitizedState[ESTADO] })
.first()

if (!estadoRecord) {
continue
}

const cidadeRecord = await trx('cidades')
.where({ nome: 'Não Informado', estado_id: estadoRecord.id })
.first()

if (!cidadeRecord) {
continue
}

cidadeId = cidadeRecord.id
}

if (cidadeId) {
await trx('locais_coleta')
.where({ id: CODIGO })
.update({ cidade_id: cidadeId })
}
}
})

console.log('Atualização concluída!')
}
Loading
Loading