@riocrypto/common-server 1.0.1022 → 1.0.1024

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, User } from "@riocrypto/common";
2
+ declare class InvopopClient {
3
+ private apiKey;
4
+ private baseUrl;
5
+ constructor(apiKey: string);
6
+ createJob(order: Order, user: User): 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,137 @@
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, user) {
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: user.mexicoFiscalName
50
+ ? user.mexicoFiscalName
51
+ : user.type === common_1.UserType.Business
52
+ ? user.companyName.toLocaleUpperCase()
53
+ : `${user.firstName.toLocaleUpperCase()} ${user.lastName.toLocaleUpperCase()}`,
54
+ tax_id: {
55
+ country: "MX",
56
+ zone: user.postalCode,
57
+ code: user.rfc || "XAXX010101000",
58
+ },
59
+ },
60
+ lines: [
61
+ {
62
+ i: 1,
63
+ quantity: "1",
64
+ item: {
65
+ name: "Facilitación de Compra-Venta de Activos Virtuales",
66
+ identities: [
67
+ {
68
+ type: "SAT",
69
+ code: "80151500",
70
+ },
71
+ ],
72
+ price: (_a = order.amountFiat) === null || _a === void 0 ? void 0 : _a.toFixed(2),
73
+ },
74
+ taxes: [
75
+ {
76
+ cat: "VAT",
77
+ rate: "standard",
78
+ },
79
+ ],
80
+ },
81
+ ],
82
+ payment: {
83
+ instructions: {
84
+ key: "credit-transfer",
85
+ },
86
+ },
87
+ },
88
+ }, {
89
+ headers: {
90
+ Authorization: `Bearer ${this.apiKey}`,
91
+ },
92
+ });
93
+ return data;
94
+ });
95
+ }
96
+ getJob(id) {
97
+ return __awaiter(this, void 0, void 0, function* () {
98
+ const { data } = yield axios_1.default.get(`${this.baseUrl}/transform/v1/jobs/${id}`, {
99
+ headers: {
100
+ Authorization: `Bearer ${this.apiKey}`,
101
+ },
102
+ });
103
+ if (data.status === "ok") {
104
+ const xmlAttachment = data.attachments.find((attachment) => attachment.mime === "text/xml; charset=utf-8");
105
+ const pdfAttachment = data.attachments.find((attachment) => attachment.mime === "application/pdf");
106
+ if (!xmlAttachment || !pdfAttachment) {
107
+ throw new Error("An attachment was not found");
108
+ }
109
+ return {
110
+ status: "completed",
111
+ pdfUrl: pdfAttachment.url,
112
+ xmlUrl: xmlAttachment.url,
113
+ };
114
+ }
115
+ else if (data.status === "ko") {
116
+ return {
117
+ status: "failed",
118
+ };
119
+ }
120
+ else {
121
+ return {
122
+ status: "pending",
123
+ };
124
+ }
125
+ });
126
+ }
127
+ }
128
+ const buildInvopopClient = () => __awaiter(void 0, void 0, void 0, function* () {
129
+ // Retrieve secrets asynchronously
130
+ const INVOPOP_API_KEY = yield secret_manager_client_1.secretManagerClient.getSecretValue("INVOPOP_API_KEY");
131
+ if (!INVOPOP_API_KEY) {
132
+ throw new common_1.SecretManagerError();
133
+ }
134
+ // Create GuenoClient instance and return
135
+ return new InvopopClient(INVOPOP_API_KEY);
136
+ });
137
+ 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.1024",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",