1+ import { AxiosInstance } from "axios" ;
2+ import { Environment , Service , } from ".." ;
3+ import { getBaseUrl } from "../utils" ;
4+
5+ interface SigleRaccomandata {
6+ mittente : Mittente ;
7+ creation_timestamp : number ;
8+ update_timestamp : number ;
9+ confirmed : boolean ;
10+ state : string ;
11+ id : string ;
12+ }
13+
14+ interface Mittente {
15+ nome : string ;
16+ cognome : string ;
17+ email ?: string ;
18+ }
19+
20+ interface Opzioni {
21+ fronteretro ?: boolean ;
22+ colori ?: boolean ;
23+ autoconfirm ?: boolean ;
24+ ar ?: boolean ;
25+ timestamp_invio ?: any ;
26+ callback_url ?: any ;
27+ callback_field ?: any ;
28+ custom ?: any ;
29+ }
30+
31+ interface Destinatari {
32+ nome ?: string ;
33+ cognome ?: string ;
34+ comune ?: string ;
35+ cap ?: string ;
36+ provincia ?: string ;
37+ nazione ?: string ;
38+ email ?: string ;
39+ dug ?: string ;
40+ indirizzo ?: string ;
41+ civico ?: string ;
42+ co ?: string ;
43+ casella_postale ?: string ;
44+ ufficio_postale ?: string ;
45+ ragione_sociale ?: string ;
46+ }
47+
48+ interface RaccomandataResponse {
49+ mittente ?: Mittente ;
50+ destinatari ?: Destinatari [ ] ;
51+ documento ?: string [ ] ;
52+ opzioni ?: Opzioni ;
53+ prodotto ?: string ;
54+ creation_timestamp ?: number ;
55+ update_timestamp ?: number ;
56+ confirmed ?: boolean ;
57+ state ?: string ;
58+ documento_validato ?: Documentovalidato ;
59+ pricing ?: Pricing ;
60+ lock ?: boolean ;
61+ confirmed_timestamp ?: number ;
62+ id : string ;
63+ }
64+
65+ interface Pricing {
66+ dettaglio : Dettaglio [ ] ;
67+ totale : Totale ;
68+ }
69+
70+ interface Totale {
71+ importo_totale_netto : number ;
72+ importo_totale_iva : number ;
73+ importo_totale : number ;
74+ }
75+
76+ interface Dettaglio {
77+ codice_servizio : string ;
78+ descrizione_servizio : string ;
79+ percentuale_iva : number ;
80+ quantita : number ;
81+ importo_unitario_totale : number ;
82+ importo_unitario_netto : number ;
83+ importo_unitario_iva : number ;
84+ }
85+
86+ interface Documentovalidato {
87+ pdf : string ;
88+ jpg : string ;
89+ pagine : number ;
90+ size : number ;
91+ }
92+
93+ interface Tracking {
94+ timestamp : number ;
95+ descrizione : string ;
96+ type : string ;
97+ definitivo : boolean ;
98+ }
99+
100+ export class UfficioPostale implements Service {
101+ client : AxiosInstance ;
102+ readonly service = 'ufficioPostale' ;
103+ readonly baseUrl = 'ws.ufficiopostale.com' ;
104+ environment : Environment ;
105+
106+ constructor ( client : AxiosInstance , environment : Environment ) {
107+ this . client = client ;
108+ this . environment = environment ;
109+ }
110+
111+ async listRaccomandate ( state ?: 'NEW' | 'CONFIRMED' | 'SENDING' | 'SENT' | 'ERROR' ) : Promise < SigleRaccomandata [ ] > {
112+ return await ( await this . client . get ( this . url + '/raccomandate' + ( state ? `/${ state } ` : '' ) ) ) . data . data ;
113+ }
114+
115+ async getRaccomandata ( id : string ) {
116+ return await ( await this . client . get ( this . url + '/raccomandate' + `/${ id } ` ) ) . data . data ;
117+ }
118+
119+ async createRaccomandataRequest ( mittente : Mittente , destinatari : Destinatari , documento : string [ ] , opzioni : Opzioni = { } , autoconfirm = false ) : Promise < RaccomandataResponse [ ] | any [ ] > {
120+ if ( ! ( 'autoconfirm' in opzioni ) ) opzioni = { ...opzioni , autoconfirm }
121+ return await ( await this . client . post ( this . url + '/raccomandate' , JSON . stringify ( { mittente, destinatari, documento, opzioni} ) ) ) . data . data ;
122+ }
123+
124+ async confirmRequest ( request_id : string | RaccomandataResponse ) {
125+ const id : string = ( typeof request_id === 'object' && 'id' in request_id ) ? request_id . id : request_id ;
126+ return await ( await this . client . patch ( this . url + '/raccomandate/' + id , JSON . stringify ( { 'confirmed' : true } ) ) ) . data . data ;
127+ }
128+
129+ async track ( tracking_number : string ) : Promise < Tracking [ ] > {
130+ return await ( await this . client . get ( this . url + '/traking/' + tracking_number ) ) . data . data ;
131+ }
132+
133+ async listDug ( ) : Promise < { codice_dug : string ; dug : string } [ ] > {
134+ return await ( await this . client . get ( this . url + '/dug' ) ) . data . data ;
135+ }
136+
137+ get url ( ) {
138+ return getBaseUrl ( this . environment , this . baseUrl )
139+ }
140+ }
0 commit comments