|
1 | | -import fs from 'fs/promises'; |
2 | 1 | import Service from './service.js'; |
3 | 2 | import Pdf from './pdf.js'; |
| 3 | +import Addenda from './addenda.js'; |
| 4 | +import File from './file.js'; |
4 | 5 |
|
5 | | -const { F_OK, R_OK } = fs.constants; |
6 | | - |
7 | | -export default class Cfdi { |
| 6 | +export default class Cfdi extends File { |
8 | 7 | constructor() { |
| 8 | + super(); |
9 | 9 | this.pdf = null; |
10 | | - this.addenda = null; |
11 | | - this.filePath = null; |
12 | | - this.fileBuffer = null; |
13 | | - this.fileContent = null; |
14 | | - this.service = Service.getInstance(); |
15 | | - } |
16 | 10 |
|
17 | | - fromFile(filePath) { |
18 | | - this.filePath = filePath; |
19 | | - return this; |
20 | | - } |
| 11 | + this.addenda = null |
| 12 | + this.addendaReplaceValues = null |
21 | 13 |
|
22 | | - fromString(fileContent) { |
23 | | - this.fileContent = fileContent; |
24 | | - return this; |
| 14 | + this.service = Service.getInstance(); |
25 | 15 | } |
26 | 16 |
|
27 | | - async getXmlContent() { |
28 | | - if(this.fileContent){ |
29 | | - return { content: this.fileContent, type: 'string' } |
| 17 | + setAddenda(addenda, replaceValues = null) { |
| 18 | + if (addenda && !(addenda instanceof Addenda)) { |
| 19 | + throw new TypeError('Addenda must be Addenda instance.'); |
30 | 20 | } |
31 | 21 |
|
32 | | - if(this.fileBuffer){ |
33 | | - return { content: this.fileBuffer, type: 'buffer' } |
34 | | - } |
35 | | - |
36 | | - if(this.filePath){ |
37 | | - try { |
38 | | - await fs.access(this.filePath, F_OK | R_OK); |
39 | | - } catch (err) { |
40 | | - if (err.code === 'ENOENT') { |
41 | | - throw new Error(`Failed to read XML content from file: ${this.filePath}. The file does not exist.`); |
42 | | - } else if (err.code === 'EACCES') { |
43 | | - throw new Error(`Permission denied: ${this.filePath}. The file exists but cannot be read.`); |
44 | | - } else { |
45 | | - throw err; |
46 | | - } |
47 | | - } |
48 | | - |
49 | | - this.fileBuffer = await fs.readFile(this.filePath) |
50 | | - return { content: this.fileBuffer, type: 'buffer' } |
51 | | - } |
52 | | - |
53 | | - throw new Error('XML content for the CFDI must be provided.'); |
54 | | - } |
55 | | - |
56 | | - setAddenda ( addenda ) { |
57 | | - if (typeof addenda !== 'string') { |
58 | | - throw new TypeError('setAddenda function only accepts a string parameter.'); |
| 22 | + if (replaceValues && Object.prototype.toString.call(replaceValues) !== '[object Object]') { |
| 23 | + throw new TypeError('Addenda replace values must be a valid key - value object.'); |
59 | 24 | } |
60 | 25 |
|
61 | 26 | this.addenda = addenda |
| 27 | + this.addendaReplaceValues = replaceValues |
62 | 28 | } |
63 | 29 |
|
64 | | - getAddenda () { |
65 | | - return this.addenda |
66 | | - } |
67 | | - |
68 | | - async toPdf( payload = {} ) { |
69 | | - |
70 | | - if(this.pdf){ |
| 30 | + async toPdf(payload = {}) { |
| 31 | + if (this.pdf) { |
71 | 32 | return this.pdf; |
72 | 33 | } |
73 | 34 |
|
74 | 35 | if (Object.prototype.toString.call(payload) !== '[object Object]') { |
75 | 36 | throw new TypeError('toPdf function only accepts an object as a parameter.'); |
76 | 37 | } |
77 | 38 |
|
78 | | - const file = await this.getXmlContent(); |
79 | | - payload.format = 'pdf'; |
80 | | - |
81 | | - if (this.getAddenda()) { |
82 | | - payload.addenda = this.getAddenda(); |
| 39 | + const file = await this.getFile(); |
| 40 | + |
| 41 | + if (this.addenda) { |
| 42 | + const addendaContent = await this.addenda.getFileContent(this.addendaReplaceValues); |
| 43 | + payload.addenda = addendaContent; |
83 | 44 | } |
84 | | - |
85 | | - const result = await this.service.cfdisConvert({file, payload}); |
| 45 | + |
| 46 | + payload.format = 'pdf'; |
| 47 | + |
| 48 | + const result = await this.service.cfdisConvert({ file, payload }); |
86 | 49 | this.pdf = new Pdf(result); |
87 | | - |
| 50 | + |
88 | 51 | return this.pdf; |
89 | 52 | } |
90 | 53 | } |
0 commit comments