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

testing release #16

Merged
merged 4 commits into from
May 13, 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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ Study project - Yet another personal finance app
## Overview

- Keep track of your personal expenses and income
- Draw nice charts
- [Draw nice charts](https://vue-data-ui.graphieros.com/)
- [Use nice UI components](https://vuetifyjs.com/)
- Import and export data
- [_Don't take this code too serious_](./docs/THINGS_TO_FIX.md)
- <https://sombriks.atlassian.net/jira/software/projects/RED/boards/2>
Expand Down
8 changes: 8 additions & 0 deletions service-node-koa/app/controllers/dashboard.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { getDashboard } from '../services/index.mjs'

export const getDashboardRequest = async ctx => {
const { usuario_id } = ctx.request.params
const { inicio, fim } = ctx.request.query
// more validations
ctx.body = await getDashboard({usuario_id, inicio, fim})
}
13 changes: 7 additions & 6 deletions service-node-koa/app/controllers/index.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// reexportação dos módulos de rota
export * from "./categoria.mjs"
export * from "./conta.mjs"
export * from "./movimentacao.mjs"
export * from "./planejamento.mjs"
export * from "./recorrencia.mjs"
export * from "./user.mjs"
export * from './categoria.mjs'
export * from './conta.mjs'
export * from './dashboard.mjs'
export * from './movimentacao.mjs'
export * from './planejamento.mjs'
export * from './recorrencia.mjs'
export * from './user.mjs'
4 changes: 3 additions & 1 deletion service-node-koa/app/main.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
findCategoriaRequest,
findContaRequest,
findMovimentacaoRequest,
findRecorrenciaRequest, geraLancamentosRequest,
findRecorrenciaRequest, geraLancamentosRequest, getDashboardRequest,
insertCategoriaRequest,
insertContaRequest,
insertMovimentacaoRequest,
Expand Down Expand Up @@ -109,6 +109,8 @@ new ApiBuilder({router}).path(b => {
b.get("/lancamentos", geraLancamentosRequest)
})
});

b.get("/dashboard", getDashboardRequest)
});
}).build();

Expand Down
179 changes: 179 additions & 0 deletions service-node-koa/app/services/dashboard.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
import { knex } from '../config/db/index.mjs'

/**
*
* @param usuario_id
* @param inicio
* @param fim
*/
export const getDashboard = async ({ usuario_id, inicio, fim }) => {
inicio = new Date(inicio).toISOString()
fim = new Date(fim).toISOString()
return {
receitaDespesaTotalPeriodo: await receitaDespesaTotalPeriodo({ usuario_id, inicio, fim }),
receitaDespesaEfetivadaPeriodo: await receitaDespesaEfetivadaPeriodo({ usuario_id, inicio, fim }),
despesaConta: await despesaConta({ usuario_id, inicio, fim }),
despesaCategoria: await despesaCategoria({ usuario_id, inicio, fim }),
receitaConta: await receitaConta({ usuario_id, inicio, fim }),
receitaCategoria: await receitaCategoria({ usuario_id, inicio, fim }),
composicaoDespesas: await composicaoDespesas({ usuario_id, inicio, fim }),
composicaoReceitas: await composicaoReceitas({ usuario_id, inicio, fim }),
saldos: {
// Saldos relativos ao período
anteriorGeral: 0,
anterior1Ano: -10,
anterior6Meses: 0,
anterior1Mes: 0,
periodo: 0,
projetado1Mes: 0,
projetado6Meses: 10,
projetado1Ano: 0
},
vencimentos: {
quitadas: 7,
aVencer: 3,
emAtraso: 0
},
limites: [],
planejamentos: []
}
}

async function receitaDespesaTotalPeriodo({ usuario_id, inicio, fim }) {
return knex.raw(`
with data_frame as (select *
from movimentacao
where conta_id in (select id from conta where usuario_id = :usuario_id)
and vencimento between :inicio and :fim)
select 'Receita total' as label, sum(valor) as value, 'lightgreen' as color
from data_frame
where tipo_movimentacao_id = 1
union
select 'Despesa total' as label, sum(valor) as value, 'red' as color
from data_frame
where tipo_movimentacao_id = 2
`, { usuario_id, inicio, fim })
}

async function receitaDespesaEfetivadaPeriodo({ usuario_id, inicio, fim }) {
return knex.raw(`
with data_frame as (select *
from movimentacao
where conta_id in (select id from conta where usuario_id = :usuario_id)
and vencimento between :inicio and :fim
and efetivada is not null)
select 'Receita efetivada' as label, sum(valor) as value, 'lightgreen' as color
from data_frame
where tipo_movimentacao_id = 1
union
select 'Despesa efetivada' as label, sum(valor) as value, 'red' as color
from data_frame
where tipo_movimentacao_id = 2
`, { usuario_id, inicio, fim })

}

async function despesaConta({ usuario_id, inicio, fim }) {
return knex.raw(`
with data_frame as (select *
from conta
join movimentacao on conta.id = movimentacao.conta_id
where usuario_id = :usuario_id
and tipo_movimentacao_id = 2
and vencimento between :inicio and :fim)
select descricao as label,
cor as color,
sum(valor) as value
from data_frame
group by descricao, cor
`, { usuario_id, inicio, fim })
}

async function despesaCategoria({ usuario_id, inicio, fim }) {
return knex.raw(`
with data_frame as (select categoria.*, movimentacao.*
from movimentacao
left join categoria on categoria.id = movimentacao.categoria_id
where usuario_id = :usuario_id
and tipo_movimentacao_id = 2
and vencimento between :inicio and :fim)
select descricao as label,
cor as color,
sum(valor) as value
from data_frame
group by descricao, cor
`, { usuario_id, inicio, fim })
}


async function receitaConta({ usuario_id, inicio, fim }) {
return knex.raw(`
with data_frame as (select *
from conta
join movimentacao on conta.id = movimentacao.conta_id
where usuario_id = :usuario_id
and tipo_movimentacao_id = 1
and vencimento between :inicio and :fim)
select descricao as label,
cor as color,
sum(valor) as value
from data_frame
group by descricao, cor
`, { usuario_id, inicio, fim })
}

async function receitaCategoria({ usuario_id, inicio, fim }) {
return knex.raw(`
with data_frame as (select categoria.*, movimentacao.*
from movimentacao
left join categoria on categoria.id = movimentacao.categoria_id
where usuario_id = :usuario_id
and tipo_movimentacao_id = 1
and vencimento between :inicio and :fim)
select descricao as label,
cor as color,
sum(valor) as value
from data_frame
group by descricao, cor
`, { usuario_id, inicio, fim })
}

async function composicaoDespesas({ usuario_id, inicio, fim }){
const contas = await knex("conta").where({usuario_id})
for await (const conta of contas) {
const conta_id = conta.id
conta.color = conta.cor
conta.data = await knex.raw(`
with data_frame as (select categoria.*, movimentacao.*
from movimentacao
left join categoria on categoria.id = movimentacao.categoria_id
where conta_id = :conta_id
and vencimento between :inicio and :fim
and tipo_movimentacao_id = 2)
select descricao as label, cor as color, sum(valor) as value
from data_frame
group by descricao, cor
`,{conta_id, inicio, fim})
}
return contas.filter(c => c.data.length)
}

async function composicaoReceitas({ usuario_id, inicio, fim }){
const contas = await knex("conta").where({usuario_id})
for await (const conta of contas) {
const conta_id = conta.id
conta.color = conta.cor
conta.data = await knex.raw(`
with data_frame as (select categoria.*, movimentacao.*
from movimentacao
left join categoria on categoria.id = movimentacao.categoria_id
where conta_id = :conta_id
and vencimento between :inicio and :fim
and tipo_movimentacao_id = 1)
select descricao as label, cor as color, sum(valor) as value
from data_frame
group by descricao, cor
`,{conta_id, inicio, fim})
}
return contas.filter(c => c.data.length)
}
23 changes: 12 additions & 11 deletions service-node-koa/app/services/index.mjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// reexportação dos módulos de serviço
export * from "./categoria.mjs";
export * from "./conta.mjs";
export * from "./modelocategoria.mjs";
export * from "./movimentacao.mjs";
export * from "./planejamento.mjs";
export * from "./recorrencia.mjs";
export * from "./tipo_conta.mjs";
export * from "./tipo_movimentacao.mjs";
export * from "./tipo_recorrencia.mjs";
export * from "./usuario.mjs";
export * from "./invite.mjs";
export * from './categoria.mjs'
export * from './conta.mjs'
export * from './dashboard.mjs'
export * from './modelocategoria.mjs'
export * from './movimentacao.mjs'
export * from './planejamento.mjs'
export * from './recorrencia.mjs'
export * from './tipo_conta.mjs'
export * from './tipo_movimentacao.mjs'
export * from './tipo_recorrencia.mjs'
export * from './usuario.mjs'
export * from './invite.mjs'
Loading
Loading