-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFirmaDigitale.ts
More file actions
207 lines (185 loc) · 6.5 KB
/
FirmaDigitale.ts
File metadata and controls
207 lines (185 loc) · 6.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import axios, { AxiosInstance } from 'axios';
import { Environment, Service, } from '../index';
import { Callback } from '../types';
import { getBaseUrl } from "../utils";
export interface Prodotto {
nome?: string;
tipo?: string;
codice_prodotto?: string;
prezzo?: number;
}
export interface Anagrafica {
nome?: string;
cognome?: string;
email?: string;
cellulare?: string;
codice_fiscale?: string;
data_nascita?: string;
sesso?: string;
comune_nascita?: string;
provincia_nascita?: string;
nazione_nascita?: string;
indirizzo_residenza?: string;
comune_residenza?: string;
provincia_residenza?: string;
cap_residenza?: string;
nazione_residenza?: string;
destinatario?: string;
indirizzo_spedizione?: string;
comune_spedizione?: string;
provincia_spedizione?: string;
cap_spedizione?: string;
tipo_documento?: string;
numero_documento?: string;
soggetto_emittente?: string;
data_emissione?: string;
data_scadenza?: string;
note?: string
}
export interface FirmaElettronica {
id: string;
filename: string;
title: string;
description: string;
members: FesMemberResponse[];
status: string;
download_link: string;
callback_status: string;
callback: FesCallback;
}
interface FesCallback {
method: string;
field: string;
url: string;
}
interface FesMemberResponse {
firstname: string;
lastname: string;
email: string;
phone: string;
status: string;
createdAt: number;
updatedAt: number;
sign_link: string;
}
interface FesMember {
firstname: string;
lastname: string;
email: string;
phone: string;
signs: Sign[];
}
interface Sign {
page: number;
position: string;
}
/**
* Service for managing digital signatures and electronic signature requests
*/
export class FirmaDigitale implements Service {
client: AxiosInstance;
readonly service = 'firmaDigitale';
readonly baseUrl = 'ws.firmadigitale.com';
environment: Environment;
constructor(client: AxiosInstance, environment: Environment) {
this.client = client;
this.environment = environment;
}
/**
* Retrieves the list of available digital signature products
*/
async getProducts(): Promise<Array<Prodotto>> {
// TODO: Add error handling for failed API requests with user-friendly messages
return await (await this.client.get(this.url + '/prodotti')).data.data
}
/**
* Gets details of a specific digital signature request
* @param id - The request ID
*/
async getRequest(id: string) {
// TODO: Add validation for empty/invalid ID and graceful error message
return await (await this.client.get(this.url + '/richiesta/' + id)).data.data
}
/**
* Lists all digital signature requests
*/
async listRequests(): Promise<Array<any>> {
// TODO: Add error handling for API failures
return await (await this.client.get(this.url + '/richiesta/')).data.data
}
/**
* Downloads the request module/form for a specific request
* @param id - The request ID
*/
async getRequestModule(id: string): Promise<Buffer> {
// TODO: Add validation and error handling for invalid ID or missing module
return await this.client.get(this.url + '/richiesta/' + id + '/modulo');
}
/**
* Requests a digital signature product
* @param codProdotto - Product code to request (see https://developers.openapi.it/services/firmadigitale)
* @param data - Additional data required by the specific product
* @param assistenza - Whether to request assistance
* @param callback - Optional callback configuration
*/
async requestProduct(codProdotto: string, data: any, assistenza?: boolean, callback?: Callback) {
// TODO: Add validation for codProdotto and data parameters with clear error messages
const body = { ...data };
if (assistenza) body.assistenza = assistenza;
if (callback) body.callback = callback;
// TODO: Handle API errors with descriptive messages (invalid product code, missing required fields, etc.)
return await (await this.client.post(this.url + '/richiesta/' + codProdotto, JSON.stringify(body))).data.data
}
/**
* Gets details of a specific electronic signature request
* @param id - The electronic signature ID
*/
async getFirmaElettronica(id: string): Promise<FirmaElettronica> {
// TODO: Add error handling for invalid ID or not found cases
return await (await this.client.get(this.url + '/firma_elettronica/' + id )).data.data;
}
/**
* Lists all electronic signature requests
*/
async listFirmaElettronica(): Promise<FirmaElettronica[]> {
// TODO: Add error handling for API failures
return await (await this.client.get(this.url + '/firma_elettronica/')).data.data;
}
/**
* Creates a new electronic signature request
* @param filename - Name of the file to be signed
* @param content - Base64 encoded file content
* @param members - Array of signers with their details and signature positions
* @param callback - Optional callback configuration for status updates
* @param title - Optional title for the signature request
* @param description - Optional description
*/
async createFirmaElettronica(filename: string, content: string, members: FesMember[], callback?: FesCallback, title?: string, description?: string): Promise<FirmaElettronica> {
// TODO: Validate required fields (filename, content, members) and provide clear error messages
let body: any = {
filename,
content,
members,
callback
};
if (title) body.title = title;
if (description) body.description = description;
// TODO: Handle API errors (invalid file format, missing member info, etc.) with user-friendly messages
return await (await this.client.post(this.url + '/firma_elettronica/base', JSON.stringify(body))).data.data;
}
/**
* Downloads the signed document
* @param id - The electronic signature ID
* @returns Base64 encoded signed document
*/
async downloadFirmaElettronica(id: string): Promise<string> {
// TODO: Add error handling for invalid ID or document not ready cases
return await (await this.client.get(this.url + '/firma_elettronica/' + id + '/download')).data.content;
}
/**
* Gets the full service URL based on environment
*/
get url() {
return getBaseUrl(this.environment, this.baseUrl)
}
}