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 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
6 changes: 2 additions & 4 deletions .env.dev
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ SENTRY_AUTH_TOKEN=
MATOMO_ENABLED=disabled
MATOMO_SITE_ID=
MATOMO_API_SITE_ID=
UPTIME_ROBOT_API_KEY=
ALTERNATIVE_SEARCH_ROUTE=
INSEE_DIRIGEANT_CLIENT_ID=
INSEE_DIRIGEANT_CLIENT_SECRET=
PROXY_API_KEY=
REDIS_URL=redis://127.0.0.1:6379
REDIS_ENABLED=false
Expand All @@ -39,4 +36,5 @@ FRANCECONNECT_REDIRECT_URI=http://localhost:3000/api/auth/france-connect/callbac
FRANCECONNECT_POST_LOGOUT_REDIRECT_URI=http://localhost:3000/api/auth/france-connect/logout-callback
CRISP_TOKEN_ID=
CRISP_TOKEN_KEY=
CRISP_WEBSITE_ID="064fca1b-bdd6-4a81-af56-9f38e40953ad"
CRISP_WEBSITE_ID="064fca1b-bdd6-4a81-af56-9f38e40953ad"
DATA_SUBVENTION_API_KEY=
17 changes: 12 additions & 5 deletions app/(header-default)/donnees-financieres/[slug]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,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 All @@ -13,6 +13,7 @@ import extractParamsAppRouter, {
import getSession from '#utils/server-side-helper/app/get-session';
import ComptesBodacc from 'app/(header-default)/donnees-financieres/[slug]/_components/bodacc';
import { ComptesAssociationSection } from 'app/(header-default)/donnees-financieres/[slug]/_components/dca';
import { Metadata } from 'next';
import BilansSection from './_components';

export const generateMetadata = async (
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,
};
56 changes: 56 additions & 0 deletions clients/api-data-subvention/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
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.XXXL,
});

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
Loading
Loading