Riferimento API

Notifications

Riferimento API per il package Notifications - gestione notifiche SDI.

Package Notifications

Il package @fatturazione-elettronica-aruba/notifications fornisce metodi per recuperare e gestire le notifiche SDI (Sistema di Interscambio) per fatture inviate e ricevute.

Installazione

pnpm add @fatturazione-elettronica-aruba/notifications

NotificationsClient

Costruttore

import { NotificationsClient } from '@fatturazione-elettronica-aruba/notifications';

const notifications = new NotificationsClient({
  httpClient: client.http,
});

Opzioni:

NomeTipoRichiestoDescrizione
httpClientHttpClientIstanza HTTP client da ArubaClient

Metodi

getSentNotifications

Recupera le notifiche per le fatture inviate.

async getSentNotifications(params: GetNotificationsParams): Promise<NotificationsResponse>

Parametri:

NomeTipoRichiestoDescrizione
idstringNoIdentificativo SDI della fattura
filenamestringNoNome file della fattura
Almeno uno tra id o filename deve essere fornito.

Esempio:

const response = await notifications.getSentNotifications({
  filename: 'IT01234567890_00001.xml',
});

console.log(`Trovate ${response.count} notifiche`);
for (const notification of response.notifications) {
  console.log(`${notification.docType}: ${notification.notificationDate}`);
}

getSentNotificationDetail

Recupera il contenuto completo di una specifica notifica per una fattura inviata.

async getSentNotificationDetail(params: GetNotificationDetailParams): Promise<Notification>

Parametri:

NomeTipoRichiestoDescrizione
filenamestringNome file della notifica

Esempio:

const notification = await notifications.getSentNotificationDetail({
  filename: 'IT01234567890_00001_RC_001.xml',
});

console.log('Contenuto file notifica:', notification.file);

getReceivedNotifications

Recupera le notifiche per le fatture ricevute.

async getReceivedNotifications(params: GetNotificationsParams): Promise<NotificationsResponse>

Parametri: Come getSentNotifications

getReceivedNotificationDetail

Recupera il contenuto completo di una specifica notifica per una fattura ricevuta.

async getReceivedNotificationDetail(params: GetNotificationDetailParams): Promise<Notification>

Parametri: Come getSentNotificationDetail


Tipi

NotificationType

Tipi di notifica SDI:

CodiceNomeDescrizione
RCRicevuta di ConsegnaLa fattura è stata consegnata al destinatario
NSNotifica di ScartoLa fattura è stata scartata per errori di validazione
MCMancata ConsegnaLa fattura non è stata consegnata (destinatario irraggiungibile)
NENotifica EsitoEsito di accettazione/rifiuto del committente (EC01/EC02)
DTDecorrenza TerminiScaduti i 15 giorni senza risposta del committente
ATAttestazione di TrasmissioneCertificato di trasmissione (per fatture PA)
type NotificationType = 'RC' | 'NS' | 'MC' | 'NE' | 'DT' | 'AT';

EsitoResult

Codici esito committente (usati nelle notifiche NE):

CodiceDescrizione
EC01Fattura accettata
EC02Fattura rifiutata
type EsitoResult = 'EC01' | 'EC02';

Notification

interface Notification {
  date: string;              // Data fattura
  docType: NotificationType; // Tipo notifica (RC, NS, MC, ecc.)
  file: string;              // XML notifica codificato Base64
  filename: string;          // Nome file notifica
  invoiceId: string;         // Identificativo SDI
  notificationDate: string;  // Data ricezione notifica
  number: string | null;     // Numero fattura
  result: EsitoResult | null; // EC01/EC02 (solo per notifiche NE)
}

NotificationsResponse

interface NotificationsResponse {
  count: number;                 // Numero totale di notifiche
  notifications: Notification[]; // Array di notifiche
}

GetNotificationsParams

interface GetNotificationsParams {
  id?: string;       // Identificativo SDI
  filename?: string; // Nome file fattura
}

GetNotificationDetailParams

interface GetNotificationDetailParams {
  filename: string; // Nome file notifica
}

Flusso Notifiche

Fattura Inviata
     │
     ▼
┌─────────┐
│   NS    │──── Scartata (errori di validazione)
└────┬────┘
     │ Valida
     ▼
┌─────────┐
│ RC / MC │──── Consegnata (RC) o Mancata consegna (MC)
└────┬────┘
     │ Consegnata (RC)
     ▼
┌─────────┐
│   NE    │──── Esito committente (EC01: accetta, EC02: rifiuta)
└────┬────┘
     │ Nessuna risposta entro 15 giorni
     ▼
┌─────────┐
│   DT    │──── Decorrenza termini (accettazione tacita)
└─────────┘

Esempio di Polling

async function attendiConsegna(
  notifications: NotificationsClient,
  filename: string,
  maxTentativi = 10,
  intervalloMs = 30000
): Promise<Notification | null> {
  for (let i = 0; i < maxTentativi; i++) {
    const response = await notifications.getSentNotifications({ filename });

    const consegna = response.notifications.find(
      (n) => n.docType === 'RC' || n.docType === 'NS' || n.docType === 'MC'
    );

    if (consegna) {
      return consegna;
    }

    await new Promise((resolve) => setTimeout(resolve, intervalloMs));
  }

  return null;
}