@riocrypto/common-server 1.0.1022 → 1.0.1023

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,16 @@
1
+ import { Order } from "@riocrypto/common";
2
+ declare class InvopopClient {
3
+ private apiKey;
4
+ private baseUrl;
5
+ constructor(apiKey: string);
6
+ createJob(order: Order): Promise<{
7
+ id: string;
8
+ }>;
9
+ getJob(id: string): Promise<{
10
+ status: "completed" | "failed" | "pending";
11
+ pdfUrl?: string;
12
+ xmlUrl?: string;
13
+ }>;
14
+ }
15
+ export declare const buildInvopopClient: () => Promise<InvopopClient>;
16
+ export {};
@@ -0,0 +1,133 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.buildInvopopClient = void 0;
16
+ const secret_manager_client_1 = require("./secret-manager-client");
17
+ const axios_1 = __importDefault(require("axios"));
18
+ const common_1 = require("@riocrypto/common");
19
+ const uuid_1 = require("uuid");
20
+ class InvopopClient {
21
+ constructor(apiKey) {
22
+ this.apiKey = apiKey;
23
+ this.baseUrl = "https://api.invopop.com";
24
+ }
25
+ createJob(order) {
26
+ var _a;
27
+ return __awaiter(this, void 0, void 0, function* () {
28
+ const uuid = (0, uuid_1.v1)();
29
+ const { data } = yield axios_1.default.put(`${this.baseUrl}/transform/v1/jobs/${uuid}`, {
30
+ workflow_id: "5b123900-1035-4c8c-a97a-36b940d7a21e",
31
+ data: {
32
+ $schema: "https://gobl.org/draft-0/bill/invoice",
33
+ code: "",
34
+ type: "standard",
35
+ currency: order.fiat,
36
+ tax: {
37
+ tags: ["use+general-expenses"],
38
+ },
39
+ issue_date: "2023-08-07",
40
+ supplier: {
41
+ name: "ESCUELA KEMPER URGATE",
42
+ tax_id: {
43
+ country: "MX",
44
+ zone: "21000",
45
+ code: "EKU9003173C9",
46
+ },
47
+ },
48
+ customer: {
49
+ name: "UNIVERSIDAD ROBOTICA ESPAÑOLA",
50
+ tax_id: {
51
+ country: "MX",
52
+ zone: "86991",
53
+ code: "URE180429TM6",
54
+ },
55
+ },
56
+ lines: [
57
+ {
58
+ i: 1,
59
+ quantity: "1",
60
+ item: {
61
+ name: "Facilitación de Compra-Venta de Activos Virtuales",
62
+ identities: [
63
+ {
64
+ type: "SAT",
65
+ code: "80151500",
66
+ },
67
+ ],
68
+ price: (_a = order.amountFiat) === null || _a === void 0 ? void 0 : _a.toFixed(2),
69
+ },
70
+ taxes: [
71
+ {
72
+ cat: "VAT",
73
+ rate: "standard",
74
+ },
75
+ ],
76
+ },
77
+ ],
78
+ payment: {
79
+ instructions: {
80
+ key: "credit-transfer",
81
+ },
82
+ },
83
+ },
84
+ }, {
85
+ headers: {
86
+ Authorization: `Bearer ${this.apiKey}`,
87
+ },
88
+ });
89
+ return data;
90
+ });
91
+ }
92
+ getJob(id) {
93
+ return __awaiter(this, void 0, void 0, function* () {
94
+ const { data } = yield axios_1.default.get(`${this.baseUrl}/transform/v1/jobs/${id}`, {
95
+ headers: {
96
+ Authorization: `Bearer ${this.apiKey}`,
97
+ },
98
+ });
99
+ if (data.status === "ok") {
100
+ const xmlAttachment = data.attachments.find((attachment) => attachment.mime === "text/xml; charset=utf-8");
101
+ const pdfAttachment = data.attachments.find((attachment) => attachment.mime === "application/pdf");
102
+ if (!xmlAttachment || !pdfAttachment) {
103
+ throw new Error("An attachment was not found");
104
+ }
105
+ return {
106
+ status: "completed",
107
+ pdfUrl: pdfAttachment.url,
108
+ xmlUrl: xmlAttachment.url,
109
+ };
110
+ }
111
+ else if (data.status === "ko") {
112
+ return {
113
+ status: "failed",
114
+ };
115
+ }
116
+ else {
117
+ return {
118
+ status: "pending",
119
+ };
120
+ }
121
+ });
122
+ }
123
+ }
124
+ const buildInvopopClient = () => __awaiter(void 0, void 0, void 0, function* () {
125
+ // Retrieve secrets asynchronously
126
+ const INVOPOP_API_KEY = yield secret_manager_client_1.secretManagerClient.getSecretValue("INVOPOP_API_KEY");
127
+ if (!INVOPOP_API_KEY) {
128
+ throw new common_1.SecretManagerError();
129
+ }
130
+ // Create GuenoClient instance and return
131
+ return new InvopopClient(INVOPOP_API_KEY);
132
+ });
133
+ exports.buildInvopopClient = buildInvopopClient;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@riocrypto/common-server",
3
- "version": "1.0.1022",
3
+ "version": "1.0.1023",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",