-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: apply lint fixes and refactoring code for standard xo config
- Loading branch information
1 parent
52c5df5
commit 916a6ed
Showing
30 changed files
with
2,971 additions
and
2,936 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,120 +1,119 @@ | ||
import { DiscoverExtractor } from '@nodecfdi/cfdi-expresiones'; | ||
import { CNodeInterface, XmlNodeUtils, getParser } from '@nodecfdi/cfdiutils-common'; | ||
|
||
import { normalizeSpace } from './utils/normalize-space'; | ||
import { type CNodeInterface, XmlNodeUtils, getParser } from '@nodecfdi/cfdiutils-common'; | ||
import { normalizeSpace } from './utils/normalize-space.js'; | ||
|
||
export abstract class AbstractInvoiceData { | ||
protected _emisor!: CNodeInterface; | ||
protected _emisor!: CNodeInterface; | ||
|
||
protected _receptor!: CNodeInterface; | ||
protected _receptor!: CNodeInterface; | ||
|
||
protected _timbreFiscalDigital!: CNodeInterface; | ||
protected _timbreFiscalDigital!: CNodeInterface; | ||
|
||
protected _qrUrl!: string; | ||
protected _qrUrl!: string; | ||
|
||
protected _tfdSourceString!: string; | ||
protected _tfdSourceString!: string; | ||
|
||
protected _logo: string | undefined; | ||
protected _logo: string | undefined; | ||
|
||
protected _additionalFields: { title: string; value: string }[] | undefined; | ||
protected _additionalFields: Array<{ title: string; value: string }> | undefined; | ||
|
||
public emisor(): CNodeInterface { | ||
return this._emisor; | ||
} | ||
public emisor(): CNodeInterface { | ||
return this._emisor; | ||
} | ||
|
||
public receptor(): CNodeInterface { | ||
return this._receptor; | ||
} | ||
public receptor(): CNodeInterface { | ||
return this._receptor; | ||
} | ||
|
||
public timbreFiscalDigital(): CNodeInterface { | ||
return this._timbreFiscalDigital; | ||
} | ||
public timbreFiscalDigital(): CNodeInterface { | ||
return this._timbreFiscalDigital; | ||
} | ||
|
||
public qrUrl(): string { | ||
return this._qrUrl; | ||
} | ||
|
||
public tfdSourceString(): string { | ||
return this._tfdSourceString; | ||
} | ||
public qrUrl(): string { | ||
return this._qrUrl; | ||
} | ||
|
||
public logo(): string | undefined { | ||
return this._logo; | ||
} | ||
public tfdSourceString(): string { | ||
return this._tfdSourceString; | ||
} | ||
|
||
public additionalFields(): { title: string; value: string }[] | undefined { | ||
return this._additionalFields; | ||
} | ||
public logo(): string | undefined { | ||
return this._logo; | ||
} | ||
|
||
private createTfdSourceString1_0(): void { | ||
const tfd = this.timbreFiscalDigital(); | ||
public additionalFields(): Array<{ title: string; value: string }> | undefined { | ||
return this._additionalFields; | ||
} | ||
|
||
let finalSource = ''; | ||
// Version - Required | ||
finalSource = `${finalSource}|${normalizeSpace(tfd.get('version'))}`; | ||
public buildTfdSource(): void { | ||
const tfd = this.timbreFiscalDigital(); | ||
|
||
// UUID - Required | ||
finalSource = `${finalSource}|${normalizeSpace(tfd.get('UUID'))}`; | ||
if (tfd.offsetExists('Version')) { | ||
this.createTfdSourceString1_1(); | ||
} else { | ||
this.createTfdSourceString1_0(); | ||
} | ||
} | ||
|
||
public buildUrlQr(node: CNodeInterface): void { | ||
const rawString = XmlNodeUtils.nodeToXmlString(node, true); | ||
const document = getParser().parseFromString(rawString, 'text/xml'); | ||
try { | ||
this._qrUrl = new DiscoverExtractor().extract(document); | ||
} catch { | ||
// Not generated qrURL | ||
} | ||
} | ||
|
||
// FechaTimbrado - Required | ||
finalSource = `${finalSource}|${normalizeSpace(tfd.get('FechaTimbrado'))}`; | ||
private createTfdSourceString1_0(): void { | ||
const tfd = this.timbreFiscalDigital(); | ||
|
||
// SelloCFD - Required | ||
finalSource = `${finalSource}|${normalizeSpace(tfd.get('selloCFD'))}`; | ||
let finalSource = ''; | ||
// Version - Required | ||
finalSource = `${finalSource}|${normalizeSpace(tfd.get('version'))}`; | ||
|
||
// NoCertificadoSAT | ||
finalSource = `${finalSource}|${normalizeSpace(tfd.get('noCertificadoSAT'))}`; | ||
// UUID - Required | ||
finalSource = `${finalSource}|${normalizeSpace(tfd.get('UUID'))}`; | ||
|
||
this._tfdSourceString = `|${finalSource}||`; | ||
} | ||
// FechaTimbrado - Required | ||
finalSource = `${finalSource}|${normalizeSpace(tfd.get('FechaTimbrado'))}`; | ||
|
||
private createTfdSourceString1_1(): void { | ||
const tfd = this.timbreFiscalDigital(); | ||
// SelloCFD - Required | ||
finalSource = `${finalSource}|${normalizeSpace(tfd.get('selloCFD'))}`; | ||
|
||
let finalSource = ''; | ||
// Version - Required | ||
finalSource = `${finalSource}|${normalizeSpace(tfd.get('Version'))}`; | ||
// NoCertificadoSAT | ||
finalSource = `${finalSource}|${normalizeSpace(tfd.get('noCertificadoSAT'))}`; | ||
|
||
// UUID - Required | ||
finalSource = `${finalSource}|${normalizeSpace(tfd.get('UUID'))}`; | ||
this._tfdSourceString = `|${finalSource}||`; | ||
} | ||
|
||
// FechaTimbrado - Required | ||
finalSource = `${finalSource}|${normalizeSpace(tfd.get('FechaTimbrado'))}`; | ||
private createTfdSourceString1_1(): void { | ||
const tfd = this.timbreFiscalDigital(); | ||
|
||
// RfcProvCertif - Required | ||
finalSource = `${finalSource}|${normalizeSpace(tfd.get('RfcProvCertif'))}`; | ||
let finalSource = ''; | ||
// Version - Required | ||
finalSource = `${finalSource}|${normalizeSpace(tfd.get('Version'))}`; | ||
|
||
// Leyenda - Optional | ||
if (tfd.offsetExists('Leyenda')) { | ||
finalSource = `${finalSource}|${normalizeSpace(tfd.get('Leyenda'))}`; | ||
} | ||
// UUID - Required | ||
finalSource = `${finalSource}|${normalizeSpace(tfd.get('UUID'))}`; | ||
|
||
// SelloCFD - Required | ||
finalSource = `${finalSource}|${normalizeSpace(tfd.get('SelloCFD'))}`; | ||
// FechaTimbrado - Required | ||
finalSource = `${finalSource}|${normalizeSpace(tfd.get('FechaTimbrado'))}`; | ||
|
||
// NoCertificadoSAT | ||
finalSource = `${finalSource}|${normalizeSpace(tfd.get('NoCertificadoSAT'))}`; | ||
// RfcProvCertif - Required | ||
finalSource = `${finalSource}|${normalizeSpace(tfd.get('RfcProvCertif'))}`; | ||
|
||
this._tfdSourceString = `|${finalSource}||`; | ||
// Leyenda - Optional | ||
if (tfd.offsetExists('Leyenda')) { | ||
finalSource = `${finalSource}|${normalizeSpace(tfd.get('Leyenda'))}`; | ||
} | ||
|
||
public buildTfdSource(): void { | ||
const tfd = this.timbreFiscalDigital(); | ||
// SelloCFD - Required | ||
finalSource = `${finalSource}|${normalizeSpace(tfd.get('SelloCFD'))}`; | ||
|
||
if (tfd.offsetExists('Version')) { | ||
this.createTfdSourceString1_1(); | ||
} else { | ||
this.createTfdSourceString1_0(); | ||
} | ||
} | ||
// NoCertificadoSAT | ||
finalSource = `${finalSource}|${normalizeSpace(tfd.get('NoCertificadoSAT'))}`; | ||
|
||
public buildUrlQr(node: CNodeInterface): void { | ||
const rawString = XmlNodeUtils.nodeToXmlString(node, true); | ||
const document = getParser().parseFromString(rawString, 'text/xml'); | ||
try { | ||
this._qrUrl = new DiscoverExtractor().extract(document); | ||
} catch (e) { | ||
// not generated qrURL | ||
} | ||
} | ||
this._tfdSourceString = `|${finalSource}||`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
import { CatalogsInterface } from 'src/catalogs/catalogs-interface'; | ||
import { AbstractInvoiceData } from '../abstract-invoice-data'; | ||
import { type CatalogsInterface } from '../catalogs/catalogs-interface.js'; | ||
import { type AbstractInvoiceData } from '../abstract-invoice-data.js'; | ||
|
||
export interface BuilderInterface<T extends AbstractInvoiceData> { | ||
build(data: T, destination: string, catalogs?: CatalogsInterface): Promise<void>; | ||
export type BuilderInterface<T extends AbstractInvoiceData> = { | ||
build(data: T, destination: string, catalogs?: CatalogsInterface): Promise<void>; | ||
|
||
buildBase64(data: T, catalogs?: CatalogsInterface): Promise<string>; | ||
} | ||
buildBase64(data: T, catalogs?: CatalogsInterface): Promise<string>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,53 @@ | ||
/* istanbul ignore file */ | ||
import { Style, TDocumentDefinitions } from 'pdfmake/interfaces'; | ||
import { BuilderInterface } from './builder-interface'; | ||
import { DocumentTranslatorInterface } from '../templates/document-translator-interface'; | ||
import { AbstractInvoiceData } from '../abstract-invoice-data'; | ||
import { getPdfMake, PdfMakeBrowser } from '../pdfmake-builder'; | ||
import { StaticCatalogs } from '../catalogs/static-catalogs'; | ||
import { CatalogsInterface } from '../catalogs/catalogs-interface'; | ||
import { type Style, type TDocumentDefinitions } from 'pdfmake/interfaces'; | ||
import { type DocumentTranslatorInterface } from '../templates/document-translator-interface.js'; | ||
import { type AbstractInvoiceData } from '../abstract-invoice-data.js'; | ||
import { getPdfMake, type PdfMakeBrowser } from '../pdfmake-builder.js'; | ||
import { StaticCatalogs } from '../catalogs/static-catalogs.js'; | ||
import { type CatalogsInterface } from '../catalogs/catalogs-interface.js'; | ||
import { type BuilderInterface } from './builder-interface.js'; | ||
|
||
export class PdfMakerBrowserBuilder<T extends AbstractInvoiceData> implements BuilderInterface<T> { | ||
private _documentTranslator: DocumentTranslatorInterface<T>; | ||
|
||
private readonly _defaultStyle: Style; | ||
|
||
constructor(documentTranslator: DocumentTranslatorInterface<T>, defaultStyle?: Style) { | ||
this._documentTranslator = documentTranslator; | ||
if (!defaultStyle) { | ||
defaultStyle = { | ||
font: 'Roboto' | ||
}; | ||
} | ||
this._defaultStyle = defaultStyle; | ||
} | ||
private readonly _documentTranslator: DocumentTranslatorInterface<T>; | ||
|
||
public build(_data: T, _destination: string, _catalogs: CatalogsInterface = new StaticCatalogs()): Promise<void> { | ||
console.warn('This method not permitted on browser'); | ||
private readonly _defaultStyle: Style; | ||
|
||
return Promise.reject('Method not work on browser'); | ||
constructor(documentTranslator: DocumentTranslatorInterface<T>, defaultStyle?: Style) { | ||
this._documentTranslator = documentTranslator; | ||
if (!defaultStyle) { | ||
defaultStyle = { | ||
font: 'Roboto', | ||
}; | ||
} | ||
|
||
public buildBase64(data: T, catalogs: CatalogsInterface = new StaticCatalogs()): Promise<string> { | ||
return new Promise<string>((resolve, reject) => { | ||
try { | ||
const pdfTemplate = this.buildPdf(data, catalogs); | ||
const pfdDocGenerator = getPdfMake<PdfMakeBrowser>().createPdf(pdfTemplate); | ||
pfdDocGenerator.getBase64((pdfBase64) => { | ||
resolve(pdfBase64); | ||
}); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
this._defaultStyle = defaultStyle; | ||
} | ||
|
||
public async build( | ||
_data: T, | ||
_destination: string, | ||
_catalogs: CatalogsInterface = new StaticCatalogs(), | ||
): Promise<void> { | ||
console.warn('This method not permitted on browser'); | ||
|
||
throw new Error('Method not work on browser'); | ||
} | ||
|
||
public async buildBase64(data: T, catalogs: CatalogsInterface = new StaticCatalogs()): Promise<string> { | ||
return new Promise<string>((resolve, reject) => { | ||
try { | ||
const pdfTemplate = this.buildPdf(data, catalogs); | ||
const pfdDocGenerator = getPdfMake<PdfMakeBrowser>().createPdf(pdfTemplate); | ||
pfdDocGenerator.getBase64((pdfBase64) => { | ||
resolve(pdfBase64); | ||
}); | ||
} | ||
} catch (error) { | ||
reject(error); | ||
} | ||
}); | ||
} | ||
|
||
public buildPdf(data: T, catalogs: CatalogsInterface): TDocumentDefinitions { | ||
return this._documentTranslator.translate(data, this._defaultStyle, catalogs); | ||
} | ||
public buildPdf(data: T, catalogs: CatalogsInterface): TDocumentDefinitions { | ||
return this._documentTranslator.translate(data, this._defaultStyle, catalogs); | ||
} | ||
} |
Oops, something went wrong.