Correção beneficiarios ativos
This commit is contained in:
@@ -40,10 +40,15 @@ export const load: PageServerLoad = async ({ url }) => {
|
||||
date: schema.deliveries.date
|
||||
})
|
||||
.from(schema.deliveries)
|
||||
.innerJoin(
|
||||
schema.beneficiaries,
|
||||
eq(schema.deliveries.beneficiaryId, schema.beneficiaries.id)
|
||||
)
|
||||
.where(
|
||||
and(
|
||||
gte(schema.deliveries.date, startDate),
|
||||
lte(schema.deliveries.date, endDate)
|
||||
lte(schema.deliveries.date, endDate),
|
||||
eq(schema.beneficiaries.status, 'ativo')
|
||||
)
|
||||
)
|
||||
.all();
|
||||
@@ -52,12 +57,23 @@ export const load: PageServerLoad = async ({ url }) => {
|
||||
|
||||
// 4. Fetch all deliveries in the selected range
|
||||
const recentDeliveries = db
|
||||
.select()
|
||||
.select({
|
||||
id: schema.deliveries.id,
|
||||
beneficiaryId: schema.deliveries.beneficiaryId,
|
||||
shiftId: schema.deliveries.shiftId,
|
||||
date: schema.deliveries.date,
|
||||
createdAt: schema.deliveries.createdAt
|
||||
})
|
||||
.from(schema.deliveries)
|
||||
.innerJoin(
|
||||
schema.beneficiaries,
|
||||
eq(schema.deliveries.beneficiaryId, schema.beneficiaries.id)
|
||||
)
|
||||
.where(
|
||||
and(
|
||||
gte(schema.deliveries.date, startDate),
|
||||
lte(schema.deliveries.date, endDate)
|
||||
lte(schema.deliveries.date, endDate),
|
||||
eq(schema.beneficiaries.status, 'ativo')
|
||||
)
|
||||
)
|
||||
.all();
|
||||
@@ -69,6 +85,11 @@ export const load: PageServerLoad = async ({ url }) => {
|
||||
lastDate: sql<string>`max(${schema.deliveries.date})`
|
||||
})
|
||||
.from(schema.deliveries)
|
||||
.innerJoin(
|
||||
schema.beneficiaries,
|
||||
eq(schema.deliveries.beneficiaryId, schema.beneficiaries.id)
|
||||
)
|
||||
.where(eq(schema.beneficiaries.status, 'ativo'))
|
||||
.groupBy(schema.deliveries.beneficiaryId)
|
||||
.all();
|
||||
|
||||
|
||||
@@ -1,30 +1,40 @@
|
||||
import { db } from '$lib/server/db';
|
||||
import * as schema from '$lib/server/db/schema';
|
||||
import { asc, eq, or, like, sql } from 'drizzle-orm';
|
||||
import { and, asc, eq, or, like, sql } from 'drizzle-orm';
|
||||
import type { PageServerLoad } from './$types';
|
||||
|
||||
export const load: PageServerLoad = async ({ url }) => {
|
||||
const search = url.searchParams.get('search') || '';
|
||||
const status = url.searchParams.get('status') || '';
|
||||
|
||||
try {
|
||||
const totalFamiliesResult = db
|
||||
.select({ count: sql<number>`count(*)` })
|
||||
.from(schema.beneficiaries)
|
||||
.where(eq(schema.beneficiaries.isParent, true))
|
||||
.where(
|
||||
and(
|
||||
eq(schema.beneficiaries.isParent, true),
|
||||
eq(schema.beneficiaries.status, 'ativo')
|
||||
)
|
||||
)
|
||||
.get();
|
||||
const totalFamilies = totalFamiliesResult?.count ?? 0;
|
||||
|
||||
const totalBeneficiariesResult = db
|
||||
.select({ count: sql<number>`count(*)` })
|
||||
.from(schema.beneficiaries)
|
||||
.where(eq(schema.beneficiaries.status, 'ativo'))
|
||||
.get();
|
||||
const totalBeneficiaries = totalBeneficiariesResult?.count ?? 0;
|
||||
|
||||
let query = db
|
||||
.select()
|
||||
.from(schema.beneficiaries)
|
||||
.where(eq(schema.beneficiaries.isParent, true));
|
||||
.where(
|
||||
and(
|
||||
eq(schema.beneficiaries.isParent, true),
|
||||
eq(schema.beneficiaries.status, 'ativo')
|
||||
)
|
||||
);
|
||||
|
||||
// We can fetch all and filter in memory since table size is guaranteed to be small (<20 tables, low concurrency).
|
||||
// This keeps SQLite query logic extremely simple and readable.
|
||||
@@ -40,16 +50,11 @@ export const load: PageServerLoad = async ({ url }) => {
|
||||
);
|
||||
}
|
||||
|
||||
if (status && status !== 'todos') {
|
||||
list = list.filter((b) => b.status === status);
|
||||
}
|
||||
|
||||
return {
|
||||
beneficiaries: list,
|
||||
totalFamilies,
|
||||
totalBeneficiaries,
|
||||
search,
|
||||
status: status || 'todos'
|
||||
search
|
||||
};
|
||||
} catch (err) {
|
||||
console.error('Error loading beneficiaries:', err);
|
||||
@@ -58,7 +63,6 @@ export const load: PageServerLoad = async ({ url }) => {
|
||||
totalFamilies: 0,
|
||||
totalBeneficiaries: 0,
|
||||
search,
|
||||
status: status || 'todos',
|
||||
error: 'Erro ao carregar a lista de beneficiários.'
|
||||
};
|
||||
}
|
||||
|
||||
@@ -234,7 +234,7 @@
|
||||
>
|
||||
<span
|
||||
class="text-muted small d-block mt-1"
|
||||
style="font-size: 0.75rem;">Número de famílias</span
|
||||
style="font-size: 0.75rem;">Número de famílias ativas</span
|
||||
>
|
||||
</div>
|
||||
</div>
|
||||
@@ -272,8 +272,8 @@
|
||||
>
|
||||
<span
|
||||
class="text-muted small d-block mt-1"
|
||||
style="font-size: 0.75rem;"
|
||||
>Todos os registos na base de dados</span
|
||||
style="font-size: 0.7rem;"
|
||||
>Registos ativos na base de dados</span
|
||||
>
|
||||
</div>
|
||||
</div>
|
||||
@@ -282,7 +282,7 @@
|
||||
|
||||
<div class="card border-0 shadow-sm rounded-4 p-3 mb-4 bg-white">
|
||||
<form method="GET" class="row g-3 align-items-end">
|
||||
<div class="col-md-6 col-lg-7">
|
||||
<div class="col-md-9 col-lg-10">
|
||||
<label
|
||||
for="search"
|
||||
class="form-label fw-semibold text-secondary small"
|
||||
@@ -298,24 +298,6 @@
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div class="col-md-3 col-lg-3">
|
||||
<label
|
||||
for="status"
|
||||
class="form-label fw-semibold text-secondary small"
|
||||
>Estado</label
|
||||
>
|
||||
<select
|
||||
name="status"
|
||||
id="status"
|
||||
value={data.status}
|
||||
class="form-select rounded-3 border-2"
|
||||
>
|
||||
<option value="todos">Todos</option>
|
||||
<option value="ativo">Ativo</option>
|
||||
<option value="inativo">Inativo</option>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div class="col-md-3 col-lg-2 d-grid">
|
||||
<button
|
||||
type="submit"
|
||||
@@ -386,7 +368,6 @@
|
||||
{/if}
|
||||
</div>
|
||||
</th>
|
||||
<th class="py-3" style="width: 120px;">Estado</th>
|
||||
<th class="px-4 py-3 text-end" style="width: 120px;"
|
||||
>Ações</th
|
||||
>
|
||||
@@ -395,7 +376,7 @@
|
||||
<tbody>
|
||||
{#if sortedBeneficiaries.length === 0}
|
||||
<tr>
|
||||
<td colspan="6" class="text-center py-5 text-muted">
|
||||
<td colspan="5" class="text-center py-5 text-muted">
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
width="40"
|
||||
@@ -428,21 +409,6 @@
|
||||
<td class="text-center fw-medium">
|
||||
{beneficiary.householdSize}
|
||||
</td>
|
||||
<td>
|
||||
{#if beneficiary.status === "ativo"}
|
||||
<span
|
||||
class="badge bg-success-subtle text-success px-2.5 py-1.5 rounded-pill border border-success-subtle text-uppercase fw-semibold"
|
||||
style="font-size: 0.7rem;"
|
||||
>Ativo</span
|
||||
>
|
||||
{:else}
|
||||
<span
|
||||
class="badge bg-danger-subtle text-danger px-2.5 py-1.5 rounded-pill border border-danger-subtle text-uppercase fw-semibold"
|
||||
style="font-size: 0.7rem;"
|
||||
>Inativo</span
|
||||
>
|
||||
{/if}
|
||||
</td>
|
||||
<td class="px-4 text-end">
|
||||
<a
|
||||
href="/admin/beneficiarios/{beneficiary.id}"
|
||||
|
||||
Reference in New Issue
Block a user