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

feat: add data subvention source #1263

Merged
merged 8 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
15 changes: 11 additions & 4 deletions app/(header-default)/donnees-financieres/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Metadata } from 'next';
import { HorizontalSeparator } from '#components-ui/horizontal-separator';
import { FinancesAssociationSection } from '#components/finances-section/association';
import { FinancesSocieteSection } from '#components/finances-section/societe';
import { SubventionsAssociationSection } from '#components/subventions-association-section';
import Title from '#components/title-section';
import { FICHE } from '#components/title-section/tabs';
import { isAssociation } from '#models/core/types';
Expand Down Expand Up @@ -45,10 +46,16 @@ const FinancePage = async (props: AppRouterProps) => {
session={session}
/>
{isAssociation(uniteLegale) ? (
<FinancesAssociationSection
session={session}
uniteLegale={uniteLegale}
/>
<>
<FinancesAssociationSection
session={session}
uniteLegale={uniteLegale}
/>
<SubventionsAssociationSection
session={session}
uniteLegale={uniteLegale}
/>
</>
) : (
<>
<FinancesSocieteSection uniteLegale={uniteLegale} />
Expand Down
2 changes: 2 additions & 0 deletions app/api/data-fetching/routes-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { getMandatairesRCS } from '#models/espace-agent/mandataires-rcs';
import { getDocumentsRNEProtected } from '#models/espace-agent/rne-protected/documents';
import { getDirigeantsRNE } from '#models/rne/dirigeants';
import { getRNEObservations } from '#models/rne/observations';
import { getSubventionsAssociationFromSlug } from '#models/subventions/association';
import { buildAndVerifyTVA } from '#models/tva/verify';
import { UnwrapPromise } from 'types';
import getBeneficiairesController, {
Expand All @@ -31,6 +32,7 @@ export const APIRoutesHandlers = {
association: getAssociationFromSlug,
'verify-tva': buildAndVerifyTVA,
'eori-validation': getEORIValidation,
'subventions-association': getSubventionsAssociationFromSlug,
} as const;

export type APIPath = keyof typeof APIRoutesHandlers;
Expand Down
1 change: 1 addition & 0 deletions app/api/data-fetching/routes-scopes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ export const APIRoutesScopes: Record<APIPath, AppScope> = {
association: AppScope.none,
'verify-tva': AppScope.none,
'eori-validation': AppScope.none,
'subventions-association': AppScope.subventionsAssociation,
};
55 changes: 55 additions & 0 deletions clients/api-data-subvention/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { HttpNotFound } from '#clients/exceptions';
import routes from '#clients/routes';
import constants from '#models/constants';
import { ISubvention, ISubventions } from '#models/subventions/association';
import { Siren } from '#utils/helpers';
import { httpGet } from '#utils/network';

/**
* Data Subvention
* https://api.datasubvention.beta.gouv.fr/
*/
export const clientDataSubvention = async (
siren: Siren
): Promise<ISubventions> => {
const route = routes.apiDataSubvention.grants.replace('{identifier}', siren);
const data = await httpGet<any>(route, {
headers: { 'x-access-token': process.env.DATA_SUBVENTION_API_KEY },
timeout: constants.timeout.XXL,
});
const msgNotFound = `No subvention data found for : ${siren}`;

if (!data.subventions || data.subventions.length === 0) {
throw new HttpNotFound(msgNotFound);
}

const subventions = mapToDomainObject(data.subventions);

if (subventions.length === 0) {
throw new HttpNotFound(msgNotFound);
}
return subventions;
};

const mapToDomainObject = (grantItems: IGrantItem[]): ISubvention[] => {
return grantItems
.filter((grantItem) => Boolean(grantItem.application))
.reduce((subventions: ISubvention[], grantItem) => {
const year = grantItem.application.annee_demande?.value;
const label = grantItem.application.statut_label?.value;
const status = grantItem.application.status?.value;
const description = grantItem.application.dispositif?.value;
const amount = grantItem.application.montants?.accorde?.value;

const newSubvention: ISubvention = {
year,
label,
status,
description,
amount,
};

return [...subventions, newSubvention];
}, [])
.sort((a, b) => b.year - a.year);
};
96 changes: 96 additions & 0 deletions clients/api-data-subvention/interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
type ApplicationField<T> = {
value: T;
provider: string;
last_update: string;
type: string;
};

type SubventionStatus = 'Accordé' | 'Refusé' | 'Prise en charge' | 'Recevable';
type SubventionLabel = 'Accordé' | 'Refusé' | 'En instruction';

type Contact = {
email: ApplicationField<string>;
telephone: ApplicationField<string>;
};

type Montants = {
total: ApplicationField<number>;
demande: ApplicationField<number>;
propose: ApplicationField<number>;
accorde: ApplicationField<number>;
};

type Versement = {
acompte: ApplicationField<number>;
solde: ApplicationField<number>;
realise: ApplicationField<number>;
compensation: {
'n-1': ApplicationField<number>;
reversement: ApplicationField<number>;
};
};

type Payment = {
activitee: ApplicationField<string>;
amount: ApplicationField<number>;
bop: ApplicationField<string>;
branche: ApplicationField<string>;
centreFinancier: ApplicationField<string>;
codeBranche: ApplicationField<string>;
dateOperation: ApplicationField<string>;
domaineFonctionnel: ApplicationField<string>;
ej: ApplicationField<string>;
libelleProgramme: ApplicationField<string>;
numeroDemandePayment: ApplicationField<string>;
numeroTier: ApplicationField<string>;
programme: ApplicationField<string>;
siret: ApplicationField<string>;
versementKey: ApplicationField<string>;
};

type ActionProposeeType = {
ej: ApplicationField<string>;
rang: ApplicationField<number>;
intitule: ApplicationField<string>;
objectifs: ApplicationField<string>;
objectifs_operationnels: {
provider: string;
last_update: string;
type: string;
};
description: ApplicationField<string>;
};

type TerritoireType = {
status: {
provider: string;
last_update: string;
type: string;
};
commentaire: ApplicationField<string>;
};

type Application = {
actions_proposee: ActionProposeeType[];
annee_demande: ApplicationField<number>;
contact: Contact;
// This is the name of the subvention
dispositif: ApplicationField<string>;
ej: ApplicationField<string>;
financeur_principal: ApplicationField<string>;
montants: Montants;
pluriannualite: ApplicationField<string>;
service_instructeur: ApplicationField<string>;
siret: ApplicationField<string>;
sous_dispositif: ApplicationField<string>;
status: ApplicationField<SubventionStatus>;
statut_label: ApplicationField<SubventionLabel>;
territoires: TerritoireType[];
versement: Versement;
versementKey: ApplicationField<string>;
};

type IGrantItem = {
application: Application;
payments: Payment[];
};
5 changes: 5 additions & 0 deletions clients/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ const routes = {
datagouv: {
ess: 'https://tabular-api.data.gouv.fr/api/resources/57bc99ca-0432-4b46-8fcc-e76a35c9efaf/data/',
},
apiDataSubvention: {
documentation: 'https://api.datasubvention.beta.gouv.fr/docs',
grants:
'https://api.datasubvention.beta.gouv.fr/association/{identifier}/grants',
},
conventionsCollectives: {
site: 'https://code.travail.gouv.fr/outils/convention-collective',
details: 'https://code.travail.gouv.fr/convention-collective/',
Expand Down
9 changes: 9 additions & 0 deletions components/administrations/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ export const DJEPVA = ({ queryString = '' }) => (
</a>
);

export const DataSubvention = ({ queryString = '' }) => (
<a
href={`/administration/data-subvention${queryString}`}
title="Data Subvention"
>
Data Subvention
</a>
);

export const MEF = ({ queryString = '' }) => (
<a
href={`/administration/mef${queryString}`}
Expand Down
79 changes: 79 additions & 0 deletions components/subventions-association-section/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
'use client';

import { Tag } from '#components-ui/tag';
import { DataSubvention } from '#components/administrations';
import { DataSectionClient } from '#components/section/data-section';
import { FullTable } from '#components/table/full';
import { EAdministration } from '#models/administrations/EAdministration';
import { IAssociation } from '#models/core/types';
import { ISession } from '#models/user/session';
import { formatCurrency } from '#utils/helpers';
import { useAPIRouteData } from 'hooks/fetch/use-API-route-data';

export const SubventionsAssociationSection: React.FC<{
uniteLegale: IAssociation;
session: ISession | null;
}> = ({ uniteLegale, session }) => {
const subventions = useAPIRouteData(
'subventions-association',
uniteLegale.siren,
session
);
if (!subventions) return null;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about this. Either the user has the rights to see it and he/she prefer an explanation like : Aucune demande de subvention n’a été trouvée pour cette association.. Or h/she is not agent connected and we could display the <AgentWall /> component

return (
<DataSectionClient
notFoundInfo="Aucune demande de subvention n’a été trouvée pour cette association."
title="Détail des subventions"
sources={[EAdministration.DATA_SUBVENTION]}
data={subventions}
>
{(subventions) =>
subventions.length === 0 ? (
<>
Aucune demande de subvention n’a été trouvée pour cette association.
</>
) : (
<>
<p>
Voici le détail des subventions demandées par l’association. Ces
données sont collectées par <DataSubvention />.
</p>
<FullTable
head={['Année', 'Dispositif', 'Montant', 'Status', 'Label']}
body={subventions.map((subvention) => [
<strong>{subvention.year}</strong>,
<strong>{subvention.description}</strong>,
formatCurrency(subvention.amount),
// TODO Component
<Tag
color={
subvention.status === 'Accordé'
? 'success'
: subvention.status === 'Refusé'
? 'error'
: 'new'
}
>
{subvention.status}
</Tag>,
// TODO Component
<Tag
color={
subvention.status === 'Accordé'
? 'success'
: subvention.status === 'Refusé'
? 'error'
: 'new'
}
>
{subvention.label}
</Tag>,
])}
/>
</>
)
}
</DataSectionClient>
);
};
14 changes: 14 additions & 0 deletions data/administrations/data-subvention.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
slug: data-subvention
short: Data.Subvention
site: https://datasubvention.beta.gouv.fr/
long: Data.Subvention
logoType: portrait
dataSources:
- label: Data.Subvention
apiSlug: data-subvention
data:
- label: Data.Subvention
targets:
- agent
contact: https://datasubvention.beta.gouv.fr/contact/
description: Un outil de consultation pour les agents de l'Etat porté par la DJEPVA et la DINUM pour éclaircir vos décisions et mieux connaître les associations
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am wondering wether we create an administration or we use the existing DJEPVA one

Copy link
Contributor Author

@rmonnier9 rmonnier9 Oct 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's see with the Data.Subvention team ?

1 change: 1 addition & 0 deletions models/administrations/EAdministration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export enum EAdministration {
MI = 'mi',
VIES = 'vies',
DJEPVA = 'djepva',
DATA_SUBVENTION = 'data-subvention',
ESSFRANCE = 'ess-france',
MARCHE_INCLUSION = 'marche-inclusion',
INFOGREFFE = 'infogreffe',
Expand Down
49 changes: 49 additions & 0 deletions models/subventions/association/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { clientDataSubvention } from '#clients/api-data-subvention';
import { HttpNotFound } from '#clients/exceptions';
import { EAdministration } from '#models/administrations/EAdministration';
import {
APINotRespondingFactory,
IAPINotRespondingError,
} from '#models/api-not-responding';
import { getUniteLegaleFromSlug } from '#models/core/unite-legale';
import { FetchRessourceException } from '#models/exceptions';
import logErrorInSentry from '#utils/sentry';

export type ISubventions = ISubvention[];

export interface ISubvention {
year: number;
label: string;
status: string;
description: string;
amount: number;
}

export const getSubventionsAssociationFromSlug = async (
slug: string
): Promise<ISubventions | IAPINotRespondingError | null> => {
const uniteLegale = await getUniteLegaleFromSlug(slug, {
isBot: false,
});

const { siren } = uniteLegale;

try {
return await clientDataSubvention(siren);
} catch (e: any) {
if (e instanceof HttpNotFound) {
return APINotRespondingFactory(EAdministration.DATA_SUBVENTION, 404);
}
logErrorInSentry(
new FetchRessourceException({
ressource: 'DataSubvention',
cause: e,
context: {
siren,
},
administration: EAdministration.DATA_SUBVENTION,
})
);
return APINotRespondingFactory(EAdministration.DATA_SUBVENTION, 500);
}
};
1 change: 1 addition & 0 deletions models/user/rights.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export enum AppScope {
carteProfessionnelleTravauxPublics = 'opendata',
nonDiffusible = 'nonDiffusible',
isAgent = 'isAgent',
subventionsAssociation = 'subventionsAssociation',
}

/**
Expand Down
50 changes: 50 additions & 0 deletions public/images/logos/data-subvention.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading