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

RED-15 / RED-82 quitadas / vencidas / a vencer #18

Merged
merged 1 commit into from
May 16, 2024
Merged
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
42 changes: 29 additions & 13 deletions service-node-koa/app/services/dashboard.mjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { knex } from '../config/db/index.mjs'
import { addMonths, addYears } from 'date-fns'

/**
*
* @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()
Expand All @@ -20,11 +15,7 @@ export const getDashboard = async ({ usuario_id, inicio, fim }) => {
composicaoDespesas: unwrap(await composicaoDespesas({ usuario_id, inicio, fim })),
composicaoReceitas: unwrap(await composicaoReceitas({ usuario_id, inicio, fim })),
saldos: await saldos({ usuario_id, inicio, fim }),
vencimentos: {
quitadas: 7,
aVencer: 3,
emAtraso: 0
},
vencimentos: unwrap(await vencimentos({ usuario_id, inicio, fim })),
limites: [],
planejamentos: []
}
Expand Down Expand Up @@ -189,7 +180,7 @@ async function saldos({ usuario_id, inicio, fim }) {
usuario_id,
inicio: addMonths(inicio, 1).toISOString(),
fim: addYears(fim, 1).toISOString()
})),
}))
}
}

Expand All @@ -211,8 +202,33 @@ async function saldo({ usuario_id, inicio, fim }) {
`, { usuario_id, inicio, fim })
}

async function vencimentos({ usuario_id, inicio, fim }) {
const [result] = unwrap(await knex.raw(`
with data_frame as (select vencimento, efetivada
from movimentacao
where conta_id in (select id from conta where usuario_id = :usuario_id)
and vencimento between :inicio and :fim),
emAtraso as (select count(*) as contas
from data_frame
where efetivada is null
and vencimento <= CURRENT_TIMESTAMP),
aVencer as (select count(*) as contas
from data_frame
where efetivada is null
and vencimento >= CURRENT_TIMESTAMP),
quitadas as (select count(*) as contas
from data_frame
where efetivada is not null)
select emAtraso.contas as emAtraso, aVencer.contas as aVencer, quitadas.contas as quitadas
from emAtraso,
aVencer,
quitadas;
`, { usuario_id, inicio, fim }))
return result
}

/**
* knex.raw behaves differently on sqlite and postgres
* knex.raw behaves differently when on sqlite or postgres
*
* @param result
*/
Expand Down
Loading