@seidor-cloud-produtos/orbit-backend-lib 1.101.24 → 1.101.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,17 @@
1
+ export default class Cnpj {
2
+ readonly value: string;
3
+ private constructor();
4
+ static create(cnpj: string): Cnpj;
5
+ static createCorrectly(cnpj: string): Cnpj;
6
+ private static tamanhoCNPJSemDV;
7
+ private static regexCNPJSemDV;
8
+ private static regexCNPJ;
9
+ private static regexMascara;
10
+ private static regexNaoPermitidos;
11
+ private static valorBase;
12
+ private static pesosDV;
13
+ private static cnpjZerado;
14
+ private static clear;
15
+ static isValid(input: string): boolean;
16
+ private static calculaDV;
17
+ }
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ /* eslint-disable no-useless-escape */
5
+ const invalid_cnpj_error_1 = tslib_1.__importDefault(require("../../../domain/errors/invalid-cnpj-error"));
6
+ class Cnpj {
7
+ value;
8
+ constructor(cnpj) {
9
+ this.value = cnpj;
10
+ }
11
+ static create(cnpj) {
12
+ if (!this.isValid(cnpj)) {
13
+ throw new invalid_cnpj_error_1.default(cnpj);
14
+ }
15
+ return new Cnpj(this.clear(cnpj));
16
+ }
17
+ static createCorrectly(cnpj) {
18
+ return new Cnpj(this.clear(cnpj));
19
+ }
20
+ static tamanhoCNPJSemDV = 12;
21
+ static regexCNPJSemDV = /^[A-Z0-9]{12}$/;
22
+ static regexCNPJ = /^[A-Z0-9]{12}\d{2}$/;
23
+ static regexMascara = /[.\/-]/g;
24
+ static regexNaoPermitidos = /[^A-Z0-9.\/-]/i;
25
+ static valorBase = '0'.charCodeAt(0);
26
+ static pesosDV = [6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2];
27
+ static cnpjZerado = '0'.repeat(14);
28
+ static clear(input) {
29
+ return input.replace(this.regexMascara, '').toUpperCase();
30
+ }
31
+ static isValid(input) {
32
+ if (this.regexNaoPermitidos.test(input))
33
+ return false;
34
+ const raw = this.clear(input);
35
+ if (raw.length !== 14 || raw === this.cnpjZerado)
36
+ return false;
37
+ if (!this.regexCNPJ.test(raw))
38
+ return false;
39
+ const base = raw.substring(0, this.tamanhoCNPJSemDV);
40
+ const dvInformado = raw.substring(this.tamanhoCNPJSemDV);
41
+ const dvCalculado = this.calculaDV(base);
42
+ return dvInformado === dvCalculado;
43
+ }
44
+ static calculaDV(base) {
45
+ if (!this.regexCNPJSemDV.test(base)) {
46
+ throw new Error('Base do CNPJ inválida para cálculo de DV');
47
+ }
48
+ let soma1 = 0;
49
+ let soma2 = 0;
50
+ for (let i = 0; i < this.tamanhoCNPJSemDV; i++) {
51
+ const valor = base.charCodeAt(i) - this.valorBase;
52
+ soma1 += valor * this.pesosDV[i + 1];
53
+ soma2 += valor * this.pesosDV[i];
54
+ }
55
+ const dv1 = soma1 % 11 < 2 ? 0 : 11 - (soma1 % 11);
56
+ soma2 += dv1 * this.pesosDV[this.tamanhoCNPJSemDV];
57
+ const dv2 = soma2 % 11 < 2 ? 0 : 11 - (soma2 % 11);
58
+ return `${dv1}${dv2}`;
59
+ }
60
+ }
61
+ exports.default = Cnpj;
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ const invalid_cnpj_error_1 = tslib_1.__importDefault(require("../../errors/invalid-cnpj-error"));
5
+ const cnpj_1 = tslib_1.__importDefault(require("./cnpj"));
6
+ describe('Value Object - Cnpj', () => {
7
+ it('should be able to crete a new cnpj (numeric)', () => {
8
+ const cnpj = cnpj_1.default.create('46230439000101');
9
+ expect(cnpj.value).toBe('46230439000101');
10
+ });
11
+ it('should be able to return error when cnpj is wrong', () => {
12
+ expect(() => cnpj_1.default.create('12344556')).toThrow(invalid_cnpj_error_1.default);
13
+ });
14
+ it('should be able to return error when cnpj only has one number', () => {
15
+ expect(() => cnpj_1.default.create('11111111111111')).toThrow(invalid_cnpj_error_1.default);
16
+ });
17
+ it('should be able to return error when digit 0 is diff', () => {
18
+ expect(() => cnpj_1.default.create('46230439000111')).toThrow(invalid_cnpj_error_1.default);
19
+ });
20
+ it('should be able to create correctly without validation', () => {
21
+ const cnpj = cnpj_1.default.createCorrectly('46230439000101');
22
+ expect(cnpj.value).toBe('46230439000101');
23
+ });
24
+ // --- cenários alfanuméricos existentes ---
25
+ it('should be able to create a valid alphanumeric cnpj (plain)', () => {
26
+ const raw = '12ABC34501DE35';
27
+ const cnpj = cnpj_1.default.create(raw);
28
+ expect(cnpj.value).toBe(raw);
29
+ });
30
+ it('should be able to create a valid alphanumeric cnpj (formatted)', () => {
31
+ const formatted = '12.ABC.345/01DE-35';
32
+ const cnpj = cnpj_1.default.create(formatted);
33
+ expect(cnpj.value).toBe('12ABC34501DE35');
34
+ });
35
+ it('should throw for alphanumeric cnpj with wrong check digit', () => {
36
+ expect(() => cnpj_1.default.create('12ABC34501DE36')).toThrow(invalid_cnpj_error_1.default);
37
+ });
38
+ it('should throw for alphanumeric cnpj with invalid character', () => {
39
+ expect(() => cnpj_1.default.create('12AB@34501DE35')).toThrow(invalid_cnpj_error_1.default);
40
+ });
41
+ it('should throw for alphanumeric cnpj too short', () => {
42
+ expect(() => cnpj_1.default.create('12ABC34501D')).toThrow(invalid_cnpj_error_1.default);
43
+ });
44
+ it('should throw for cnpj all zeros', () => {
45
+ expect(() => cnpj_1.default.create('00000000000000')).toThrow(invalid_cnpj_error_1.default);
46
+ });
47
+ it('should accept lowercase alphanumeric cnpj and normalize to uppercase', () => {
48
+ const lower = '12abc34501de35';
49
+ const cnpj = cnpj_1.default.create(lower);
50
+ expect(cnpj.value).toBe('12ABC34501DE35');
51
+ });
52
+ it('createCorrectly should remove mask from formatted alphanumeric cnpj without validating', () => {
53
+ const formatted = '12.ABC.345/01DE-XX'; // invalid DV/characters but mask is removed
54
+ const cnpj = cnpj_1.default.createCorrectly(formatted);
55
+ expect(cnpj.value).toBe('12ABC34501DEXX');
56
+ });
57
+ });
@@ -0,0 +1,4 @@
1
+ import DomainError from './domain-error';
2
+ export default class InvalidCnpjError extends DomainError {
3
+ constructor(cnpj: string);
4
+ }
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ const domain_error_1 = tslib_1.__importDefault(require("./domain-error"));
5
+ class InvalidCnpjError extends domain_error_1.default {
6
+ constructor(cnpj) {
7
+ super(`The cnpj "${cnpj}" is invalid.`, 400);
8
+ this.name = 'InvalidCnpjError';
9
+ }
10
+ }
11
+ exports.default = InvalidCnpjError;
@@ -0,0 +1,5 @@
1
+ export declare class Net {
2
+ isCurrentIPInIPWhiteList(whiteList: string[], ignoreInvalidIP?: boolean): Promise<boolean>;
3
+ getCurrentIP(): Promise<any>;
4
+ isIP(value: string): number;
5
+ }
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Net = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const axios_1 = tslib_1.__importDefault(require("axios"));
6
+ const net_1 = tslib_1.__importDefault(require("net"));
7
+ class Net {
8
+ async isCurrentIPInIPWhiteList(whiteList, ignoreInvalidIP = false) {
9
+ const currentIP = await this.getCurrentIP();
10
+ if (!this.isIP(currentIP) && ignoreInvalidIP) {
11
+ return true;
12
+ }
13
+ return whiteList.includes(currentIP);
14
+ }
15
+ async getCurrentIP() {
16
+ try {
17
+ const response = await axios_1.default.get('https://checkip.amazonaws.com/');
18
+ return response.data.trim();
19
+ }
20
+ catch (e) {
21
+ console.log(e);
22
+ return 'invalid';
23
+ }
24
+ }
25
+ isIP(value) {
26
+ return net_1.default.isIP(value);
27
+ }
28
+ }
29
+ exports.Net = Net;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seidor-cloud-produtos/orbit-backend-lib",
3
- "version": "1.101.24",
3
+ "version": "1.101.26",
4
4
  "description": "Internal lib for backend components",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -51,6 +51,7 @@
51
51
  "@types/compression": "1.7.5",
52
52
  "@types/cors": "2.8.17",
53
53
  "@types/express": "4.17.21",
54
+ "@types/jest": "30.0.0",
54
55
  "@types/supertest": "6.0.2",
55
56
  "@typescript-eslint/eslint-plugin": "5.59.9",
56
57
  "@typescript-eslint/parser": "5.59.9",