Skip to content

Commit

Permalink
feat(stats): add 'actifs' in dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
pYassine committed Jan 2, 2024
1 parent 7c0d494 commit d998e46
Show file tree
Hide file tree
Showing 36 changed files with 242 additions and 224 deletions.
1 change: 0 additions & 1 deletion packages/backend/src/_common/model/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export * from "./contact-support";
export * from "./interaction";
export * from "./jwt";
export * from "./message-sms";
export * from "./stats";
export * from "./structure";
export * from "./structure-doc";
export * from "./structure-stats";
Expand Down

This file was deleted.

This file was deleted.

3 changes: 0 additions & 3 deletions packages/backend/src/_common/model/stats/index.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @index('./*', f => `export * from '${f.path}'`)
export * from "./StructureStats.type";
export * from "./StructureStatsFull.type";
export * from "./StructureStatsQuestionsAtDateValidUsagers.type";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export class FixLastInteractionMigration1703676787763
) > 0
) {
const log = {
ref: usager.ref,
structureId: usager.structureId,
before: usager.lastInteraction.dateInteraction,
after: dateInteractionOut,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import { dataCompare } from "../../../util/dataCompare.service";
import { ExpressResponse } from "../../../util/express";
import {
AdminStructureListData,
AdminStructureStatsData,
Structure,
StructureAdmin,
UserStructureAuthenticated,
Expand All @@ -47,6 +46,7 @@ import { UsersController } from "../../../users/users.controller";
import { RegisterUserAdminDto } from "../../../users/dto";
import { format } from "date-fns";
import { structureCreatorService } from "../../../structures/services";
import { AdminStructureStatsData } from "@domifa/common";

@UseGuards(AuthGuard("jwt"), AppUserGuard)
@Controller("admin/structures")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,14 @@ import { usagerDocsRepository } from "../../../database/services/usager/usagerDo
import { StatsDeploiementExportModel } from "../../../excel/export-stats-deploiement";
import { StatsDeploiementStructureExportModel } from "../../../excel/export-stats-deploiement/StatsDeploiementStructureExportModel.type";
import { FranceRegion } from "../../../util/territoires";
import {
AdminStructureListData,
AdminStructureStatsData,
StructureAdmin,
} from "../../../_common/model";
import { AdminStructureListData, StructureAdmin } from "../../../_common/model";
import {
StatsByLocality,
StatsByMonth,
StructureType,
InteractionType,
UsagerDecisionStatut,
AdminStructureStatsData,
} from "@domifa/common";

@Injectable()
Expand Down Expand Up @@ -55,8 +52,10 @@ export class AdminStructuresService {

const structuresCountBySmsEnabled = await this.getStructuresWithSms();
const structuresCount = await structureRepository.count();
const usagersActifs = await usagerRepository.countTotalActifs();

const stats: AdminStructureStatsData = {
usagersActifs,
structuresCount,
usagersValidCountByStructureMap,
usagersAyantsDroitsCountByStructureMap,
Expand Down Expand Up @@ -345,10 +344,12 @@ export class AdminStructuresService {

const interactionsCountByStatut =
await this.getInteractionsCountByTypeMap();
const usagersActifs = await usagerRepository.countTotalActifs();

const stats: StatsDeploiementExportModel = {
exportDate: new Date(),
structures: structuresModels,
usagersActifs,
usagersAllCountByStructureId: usagersAllCountByStructureMap,
usagersValideCountByStructureId: usagersValidCountByStructureMap,
usagersAyantsDroitsByStructureId: usagersAyantsDroitsCountByStructureMap,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,18 +127,31 @@ async function countTotalUsagers(structuresId?: number[]): Promise<number> {
return usagers + ayantsDroits;
}

async function countTotalActifs(): Promise<number> {
const usagers: [{ actifs: string }] = await usagerRepository.query(`
async function countTotalActifs(): Promise<{
domicilies: number;
ayantsDroits: number;
actifs: number;
}> {
const usagers: {
domicilies: string;
ayantsDroits: string;
actifs: string;
}[] = await usagerRepository.query(`
SELECT
COUNT(DISTINCT uh."usagerUUID") as "domicilies",
COALESCE(SUM(jsonb_array_length(state->'ayantsDroits')), 0) as "ayantsDroits",
COUNT(DISTINCT uh."usagerUUID") + COALESCE(SUM(jsonb_array_length(state->'ayantsDroits')), 0) AS "actifs"
FROM "usager_history" uh JOIN usager u ON uh."usagerUUID" = u.uuid JOIN jsonb_array_elements(uh.states) AS state ON true
WHERE
(state->>'isActive')::boolean
AND (state->>'historyBeginDate')::timestamptz < CURRENT_DATE + INTERVAL '1 day'
AND (state->>'historyEndDate' is null OR (state->>'historyEndDate')::timestamptz >= CURRENT_DATE + INTERVAL '1 day' )
`);

return parseInt(usagers[0].actifs, 10);
return {
domicilies: parseInt(usagers[0].domicilies, 10),
ayantsDroits: parseInt(usagers[0].ayantsDroits, 10),
actifs: parseInt(usagers[0].actifs, 10),
};
}

async function countMigratedUsagers(): Promise<number> {
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ export type StatsDeploiementExportModel = {
region: string;
count: number;
}[];
usagersActifs: {
domicilies: number;
ayantsDroits: number;
actifs: number;
};
usersCount: number;
docsCount: number;
interactionsCountByStatut: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ function renderWorksheet({
renderExportDate();
renderUsagersCountByStatus();
renderStructuresCountByRegion();
renderTotalActifs();
renderStructuresCountByType();
renderUsersDocsCount();
renderInteractionsCountByType();
Expand Down Expand Up @@ -63,9 +64,21 @@ function renderWorksheet({
value: stats.usagersCountByStatut.AYANTS_DROITS,
});
}
function renderTotalActifs() {
let i = 13;
worksheetRendered.renderCell(i++, "b", {
value: stats.usagersActifs.actifs,
});
worksheetRendered.renderCell(i++, "b", {
value: stats.usagersActifs.domicilies,
});
worksheetRendered.renderCell(i++, "b", {
value: stats.usagersActifs.ayantsDroits,
});
}

function renderStructuresCountByType() {
let i = 13;
let i = 18;
worksheetRendered.renderCell(i++, "b", {
value: stats.structures.length,
});
Expand All @@ -82,8 +95,9 @@ function renderWorksheet({
value: stats.structuresCountByType["mairie"],
});
}

function renderInteractionsCountByType() {
let i = 23;
let i = 28;
worksheetRendered.renderCell(i++, "b", {
value: stats.interactionsCountByStatut["appel" as InteractionType],
});
Expand Down Expand Up @@ -127,10 +141,10 @@ function renderWorksheet({
}

function renderUsersDocsCount() {
worksheetRendered.renderCell(19, "b", {
worksheetRendered.renderCell(24, "b", {
value: stats.usersCount,
});
worksheetRendered.renderCell(20, "b", {
worksheetRendered.renderCell(25, "b", {
value: stats.docsCount,
});
}
Expand Down
2 changes: 1 addition & 1 deletion packages/backend/src/stats/services/publicStats.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class PublicStatsService implements OnModuleInit {
"courrierIn"
),
usagers: await usagerRepository.countTotalUsagers(),
actifs: await usagerRepository.countTotalActifs(),
actifs: (await usagerRepository.countTotalActifs()).actifs,
};

await this.cacheManager.set("home-stats", homeStats);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { type InteractionType } from "../../interactions";

export interface AdminStructureStatsData {
usersCount: number;
structuresCount: number;
usagersDocumentsCount: number;
usagersActifs: {
domicilies: number;
ayantsDroits: number;
actifs: number;
};
structuresCountByRegion: Array<{
region: string;
count: number;
}>;
structuresCountByTypeMap: Record<string, number>;
structuresCountBySmsEnabled: number;
interactionsCountByTypeMap: {
[statut in InteractionType]: number;
};
usagersCountByStatutMap: Record<string, number>;
usagersValidCountByStructureMap: Record<string, number>;
usagersAllCountByStructureMap: Record<string, number>;
usagersAyantsDroitsCountByStructureMap: Record<string, number>;
}
1 change: 1 addition & 0 deletions packages/common/src/stats/interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
// @index('./*', f => `export * from '${f.path}'`)
export * from "./AdminStructureStatsData.interface";
export * from "./HomeStats.interface";
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ <h3 class="mt-3 mb-1 py-0 fw-bold text-primary">
[options]="countOptions"
class="display-4 fw-bold text-primary"
></div>
<div class="counter-title">domiciliés</div>
<div class="counter-title">dossiers enregistrés</div>
</div>
<div class="my-2 col-12 col-md-3">
<div
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,12 @@ export class ManageUsagersTableComponent implements OnDestroy {
}

public goToProfil(usager: UsagerFormModel): void {
this.router.navigate([getUrlUsagerProfil(usager)]);
const url =
this.me.role === "facteur"
? `/profil/general/${usager.ref}`
: getUrlUsagerProfil(usager);

this.router.navigate([url]);
}

public refTrackBy(_index: number, item: UsagerFormModel) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,7 @@
</td>
<td class="clickable table-name td-name">
<a
[routerLink]="
usager.decision.statut === 'INSTRUCTION' || usager.decision.statut === 'ATTENTE_DECISION'
[routerLink]="(usager.decision.statut === 'INSTRUCTION' || usager.decision.statut === 'ATTENTE_DECISION') && me.role !== 'facteur'
? '/usager/'+usager.ref+'/edit/'+ETAPES_DEMANDE_URL[usager.etapeDemande]
: '/profil/general/'+usager.ref"
ariaCurrentWhenActive="page"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
<div class="stats-section-counter text-primary">
{{ publicStats.usagersCount | formatBigNumber }}
</div>
<div class="stats-section-label">domiciliés</div>
<div class="stats-section-label">dossiers enregistrés</div>
</button>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ <h2 class="form-title">Commentaires privés</h2>
</div>

<app-delete-usager-menu
*ngIf="usager"
*ngIf="usager && me.role !== 'facteur'"
class="my-4"
[usager]="usager"
[context]="'INSTRUCTION_FORM'"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import { Component, Input } from "@angular/core";

import { UsagerFormModel } from "./../../../usager-shared/interfaces/UsagerFormModel";
import { UserStructure } from "../../../../../_common/model";
import { AuthService } from "../../../shared/services";

@Component({
selector: "app-step-footer",
templateUrl: "./step-footer.component.html",
})
export class StepFooterComponent {
@Input() public usager!: UsagerFormModel;

public me!: UserStructure | null;

constructor(private readonly authService: AuthService) {
this.me = this.authService.currentUserValue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ <h3>Historique des décisions</h3>
<td>{{ histo.userName }}</td>
<td class="text-end d-print-none">
<app-delete-usager-menu
*ngIf="i === 0"
*ngIf="i === 0 && me.role !== 'facteur'"
[usager]="usager"
context="HISTORY"
[me]="me"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<app-delete-usager-menu
[usager]="usager"
context="PROFIL"
[me]="me"
*ngIf="me.role !== 'facteur'"
></app-delete-usager-menu>
</div>
</div>
Loading

0 comments on commit d998e46

Please sign in to comment.