BREAKING CHANGE(core): Refactor contact and PDF handling across the library by replacing IContact with TContact and updating PDF processing to use a structured IPdf object. These changes ensure that empty contact objects include registration details, founded/closed dates, and status, and that PDF loading/exporting uniformly wraps buffers in a proper object.

This commit is contained in:
2025-03-20 13:57:45 +00:00
parent 75b720a98d
commit 6906e2f778
8 changed files with 164 additions and 31 deletions

View File

@ -90,7 +90,7 @@ export class XInvoice implements plugins.tsclass.business.ILetter {
}
/**
* Creates an empty IContact object
* Creates an empty TContact object
*/
private createEmptyContact(): plugins.tsclass.business.TContact {
return {
@ -103,7 +103,23 @@ export class XInvoice implements plugins.tsclass.business.ILetter {
city: '',
country: '',
postalCode: ''
}
},
registrationDetails: {
vatId: '',
registrationId: '',
registrationName: ''
},
foundedDate: {
year: 2000,
month: 1,
day: 1
},
closedDate: {
year: 9999,
month: 12,
day: 31
},
status: 'active'
};
}
@ -198,7 +214,15 @@ export class XInvoice implements plugins.tsclass.business.ILetter {
* @param pdfBuffer PDF buffer
*/
public async loadPdf(pdfBuffer: Uint8Array | Buffer): Promise<void> {
this.pdf = Uint8Array.from(pdfBuffer);
// Create a valid IPdf object
this.pdf = {
name: 'invoice.pdf',
id: `invoice-${Date.now()}`,
metadata: {
textExtraction: ''
},
buffer: Uint8Array.from(pdfBuffer)
};
try {
// Try to extract embedded XML
@ -224,7 +248,7 @@ export class XInvoice implements plugins.tsclass.business.ILetter {
}
try {
const pdfDoc = await PDFDocument.load(this.pdf);
const pdfDoc = await PDFDocument.load(this.pdf.buffer);
// Get the document's metadata dictionary
const namesDictObj = pdfDoc.catalog.lookup(PDFName.of('Names'));
@ -313,9 +337,9 @@ export class XInvoice implements plugins.tsclass.business.ILetter {
this.to = { ...letter.to };
this.content = {
invoiceData: letter.content.invoiceData ? { ...letter.content.invoiceData } : this.createEmptyInvoice(),
textData: letter.content.textData,
timesheetData: letter.content.timesheetData,
contractData: letter.content.contractData
textData: null,
timesheetData: null,
contractData: null
};
this.needsCoverSheet = letter.needsCoverSheet;
this.objectActions = [...letter.objectActions];
@ -412,7 +436,7 @@ export class XInvoice implements plugins.tsclass.business.ILetter {
const xmlContent = await this.exportXml(format);
// Load the PDF
const pdfDoc = await PDFDocument.load(this.pdf);
const pdfDoc = await PDFDocument.load(this.pdf.buffer);
// Convert the XML string to a Uint8Array
const xmlBuffer = new TextEncoder().encode(xmlContent);
@ -452,8 +476,13 @@ export class XInvoice implements plugins.tsclass.business.ILetter {
// Save the modified PDF
const modifiedPdfBytes = await pdfDoc.save();
// Update the pdf property
this.pdf = modifiedPdfBytes;
// Update the pdf property with a proper IPdf object
this.pdf = {
name: this.pdf.name,
id: this.pdf.id,
metadata: this.pdf.metadata,
buffer: modifiedPdfBytes
};
return modifiedPdfBytes;
} catch (error) {