Skip to content

Commit

Permalink
chore: apply lint fixes and refactoring code for standard xo config
Browse files Browse the repository at this point in the history
  • Loading branch information
luffynando committed Jan 9, 2024
1 parent 52c5df5 commit 916a6ed
Show file tree
Hide file tree
Showing 30 changed files with 2,971 additions and 2,936 deletions.
167 changes: 83 additions & 84 deletions src/abstract-invoice-data.ts
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}||`;
}
}
12 changes: 6 additions & 6 deletions src/builders/builder-interface.ts
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>;
};
81 changes: 43 additions & 38 deletions src/builders/pdf-maker-browser-builder.ts
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);
}
}
Loading

0 comments on commit 916a6ed

Please sign in to comment.