Skip to content

Commit

Permalink
feat: add tva proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
XavierJp committed Dec 18, 2023
1 parent 1626e9a commit 5ad45ea
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 6 deletions.
6 changes: 6 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
rneActeDownloadController,
rneBilanDownloadController,
} from './src/controllers/rne-download';
import { tvaController } from './src/controllers/tva';

dotenv.config();

Expand Down Expand Up @@ -86,6 +87,11 @@ app.use('/status', statusRouter);
*/
app.use('/association/:rna', associationController);

/**
* Association
*/
app.use('/tva/:tvaNumber', tvaController);

/**
* Error handling
*/
Expand Down
10 changes: 10 additions & 0 deletions src/clients/tva/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import routes from '../urls';
import { httpGet } from '../../utils/network';
import constants from '../../constants';

export const clientTVA = async (rna: string): Promise<string> => {
const url = `${routes.tva}${rna}`;
const response = await httpGet(url, { timeout: constants.timeout.L });
const { data } = response;
return data;
};
1 change: 1 addition & 0 deletions src/clients/urls.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const routes = {
association: 'https://siva-integ1.cegedim.cloud/apim/api-asso/api/structure/',
tva: 'https://ec.europa.eu/taxation_customs/vies/rest-api/ms/FR/vat/',
inpi: {
portail: {
entreprise: 'https://data.inpi.fr/entreprises/',
Expand Down
17 changes: 17 additions & 0 deletions src/controllers/tva.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Request, Response, NextFunction } from 'express';
import { verifyTVANumber } from '../models/siren-and-siret';
import { clientTVA } from '../clients/tva';

export const tvaController = async (
req: Request,
res: Response,
next: NextFunction
) => {
try {
const tvaNumber = verifyTVANumber(req.params?.tvaNumber);
const tva = await clientTVA(tvaNumber);
res.status(200).json(tva);
} catch (e) {
next(e);
}
};
29 changes: 23 additions & 6 deletions src/models/siren-and-siret/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,32 @@ import {
NotASiretError,
NotLuhnValidSirenError,
NotLuhnValidSiretError,
} from "./siren-and-siret-exceptions";
} from './siren-and-siret-exceptions';

/**
* Siren and siret types
*/
type Brand<K, T> = K & { __brand: T };

export type Siren = Brand<string, "Siren">;
export type Siret = Brand<string, "Siren">;
export type TVANumber = Brand<string, 'TVANumber'>;

export const isTVANumber = (slug: string): slug is TVANumber => {
return !!slug.match(/^\d{11}$/g);
};

/**
* throw an exception if a string is not a TVA Number
* */
export const verifyTVANumber = (slug: string): TVANumber => {
if (!isTVANumber(slug)) {
throw new Error('Not a valid TVANumber');
} else {
return slug;
}
};

export type Siren = Brand<string, 'Siren'>;
export type Siret = Brand<string, 'Siren'>;

export const isSiren = (slug: string): slug is Siren => {
if (!hasSirenFormat(slug) || !isLuhnValid(slug)) {
Expand Down Expand Up @@ -79,7 +96,7 @@ const luhnChecksum = (str: string) => {

export const isLuhnValid = (str: string) => {
// La poste siren and siret are the only exceptions to Luhn's formula
if (str.indexOf("356000000") === 0) {
if (str.indexOf('356000000') === 0) {
return true;
}
return luhnChecksum(str) % 10 == 0;
Expand All @@ -93,8 +110,8 @@ export const hasSirenFormat = (str: string) => !!str.match(/^\d{9}$/g);

export const hasSiretFormat = (str: string) => !!str.match(/^\d{14}$/g);

export const formatSiret = (siret = "") => {
return siret.replace(/(\d{3})/g, "$1 ").replace(/(\s)(?=(\d{2})$)/g, "");
export const formatSiret = (siret = '') => {
return siret.replace(/(\d{3})/g, '$1 ').replace(/(\s)(?=(\d{2})$)/g, '');
};

export const extractSirenFromSiret = (siret: string) => {
Expand Down

0 comments on commit 5ad45ea

Please sign in to comment.