viettel-s-invoice
Version:
Viettel S Invoice
251 lines (250 loc) • 12.3 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const constants_1 = require("./constants");
const axios_1 = __importDefault(require("axios"));
const exceptions_1 = require("./exceptions");
class ViettelSInvoice {
constructor({ apiEndPoint, username, password }) {
this.apiEndPoint = apiEndPoint || constants_1.API_ENDPOINT;
this.username = username;
this.password = password;
if (!this.username || !this.password) {
throw new exceptions_1.ViettelSInvoiceException('Username or password is required');
}
if (apiEndPoint && !this.isValidUrl(apiEndPoint)) {
throw new exceptions_1.ViettelSInvoiceException('API endpoint is not valid');
}
}
isValidUrl(url) {
const pattern = /^(https?:\/\/)?([\w-]+\.)+[\w-]+(\/[\w-]*)*$/;
return pattern.test(url);
}
validateDate(date) {
const dateRegex = /^\d{2}\/\d{2}\/\d{4}$/;
return dateRegex.test(date);
}
getApiUrl(path) {
return `${this.apiEndPoint}${path}`;
}
login() {
return __awaiter(this, void 0, void 0, function* () {
var _a;
try {
const { data } = yield axios_1.default.post(this.getApiUrl('/auth/login'), {
username: this.username,
password: this.password
});
return data;
}
catch (error) {
if (axios_1.default.isAxiosError(error)) {
throw new exceptions_1.ViettelSInvoiceLoginException(error.message, (_a = error === null || error === void 0 ? void 0 : error.response) === null || _a === void 0 ? void 0 : _a.data);
}
throw new exceptions_1.ViettelSInvoiceLoginException(`Unexpected error: ${error.message}`, error);
}
});
}
/**
* Previews a draft invoice by sending the invoice data to the server.
*
* @param invoice - The invoice data to be previewed.
* @returns A promise that resolves to the draft invoice response.
* @throws ReviewDraftInvoiceException if the server response is not successful.
*/
previewDraftInvoice(invoice) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
const { access_token } = yield this.login();
try {
const { data } = yield axios_1.default.post(this.getApiUrl(`/services/einvoiceapplication/api/InvoiceAPI/InvoiceUtilsWS/createInvoiceDraftPreview/${this.username}`), invoice, {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${access_token}`
}
});
return data;
}
catch (error) {
if (axios_1.default.isAxiosError(error)) {
throw new exceptions_1.ViettelSInvoicePreviewDraftInvoiceException(error.message, (_a = error === null || error === void 0 ? void 0 : error.response) === null || _a === void 0 ? void 0 : _a.data);
}
throw new exceptions_1.ViettelSInvoicePreviewDraftInvoiceException(`Unexpected error: ${error.message}`);
}
});
}
/**
* Creates an invoice by sending the provided invoice data to the server.
*
* @param invoice - The invoice data to be created.
* @returns A promise that resolves to the invoice response.
* @throws ViettelSInvoiceCreateInvoiceException if the server response is not successful.
*/
createInvoice(invoice) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
const { access_token } = yield this.login();
try {
const { data } = yield axios_1.default.post(this.getApiUrl(`/services/einvoiceapplication/api/InvoiceAPI/InvoiceWS/createInvoice/${this.username}`), invoice, {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${access_token}`
}
});
return data;
}
catch (error) {
if (axios_1.default.isAxiosError(error)) {
throw new exceptions_1.ViettelSInvoiceCreateInvoiceException(error.message, (_a = error === null || error === void 0 ? void 0 : error.response) === null || _a === void 0 ? void 0 : _a.data);
}
throw new exceptions_1.ViettelSInvoiceCreateInvoiceException(`Unexpected error: ${error.message}`, error);
}
});
}
/**
* Retrieves an invoice by its transaction UUID.
*
* @param transactionUuid - The unique identifier for the transaction.
* @returns A promise that resolves to the invoice detail response.
* @throws ViettelSInvoiceGetInvoiceException if the server response is not successful.
*/
getInvoiceByTransactionUuid(transactionUuid) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
const { access_token } = yield this.login();
const buildDataToSend = new URLSearchParams({
supplierTaxCode: this.username,
transactionUuid
});
try {
const { data } = yield axios_1.default.post(this.getApiUrl('/services/einvoiceapplication/api/InvoiceAPI/InvoiceWS/searchInvoiceByTransactionUuid'), buildDataToSend, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
Authorization: `Bearer ${access_token}`
}
});
return data;
}
catch (error) {
if (axios_1.default.isAxiosError(error)) {
throw new exceptions_1.ViettelSInvoiceGetInvoiceException(error.message, (_a = error === null || error === void 0 ? void 0 : error.response) === null || _a === void 0 ? void 0 : _a.data);
}
throw new exceptions_1.ViettelSInvoiceGetInvoiceException(`Unexpected error: ${error.message}`, error);
}
});
}
/**
* Retrieves a list of invoices within a specified date range.
*
* @param fromDate - The start date of the range in 'dd/MM/yyyy' format.
* @param toDate - The end date of the range in 'dd/MM/yyyy' format.
* @returns A promise that resolves to the invoice detail response.
* @throws ViettelSInvoiceGetInvoicesException if the date format is invalid or the server response is not successful.
*/
getInvoicesByDateRange(fromDate, toDate) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
if (!this.validateDate(fromDate) || !this.validateDate(toDate)) {
throw new exceptions_1.ViettelSInvoiceGetInvoicesException('Invalid date format: dd/MM/yyyy');
}
const { access_token } = yield this.login();
try {
const { data } = yield axios_1.default.post(this.getApiUrl('/services/einvoiceapplication/api/InvoiceAPI/InvoiceUtilsWS/getListInvoiceDataControl'), {
supplierTaxCode: this.username,
fromDate,
toDate
}, {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${access_token}`
}
});
return data;
}
catch (error) {
if (axios_1.default.isAxiosError(error)) {
throw new exceptions_1.ViettelSInvoiceGetInvoicesException(error.message, (_a = error === null || error === void 0 ? void 0 : error.response) === null || _a === void 0 ? void 0 : _a.data);
}
throw new exceptions_1.ViettelSInvoiceGetInvoicesException(`Unexpected error: ${error.message}`, error);
}
});
}
/**
* Retrieves the file representation of an invoice based on the provided parameters.
*
* @param invoiceNo - The invoice number to identify the invoice.
* @param templateCode - The template code associated with the invoice.
* @param fileType - The type of file to retrieve (PDF, ZIP).
* @returns A promise that resolves to the invoice file response.
* @throws ViettelSInvoiceGetInvoiceFileException if the server response is not successful.
*/
getInvoiceFile(_a) {
return __awaiter(this, arguments, void 0, function* ({ invoiceNo, templateCode, fileType }) {
var _b;
const { access_token } = yield this.login();
try {
const { data } = yield axios_1.default.post(this.getApiUrl('/services/einvoiceapplication/api/InvoiceAPI/InvoiceUtilsWS/getInvoiceRepresentationFile'), {
supplierTaxCode: this.username,
invoiceNo,
templateCode,
fileType
}, {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${access_token}`
}
});
return data;
}
catch (error) {
if (axios_1.default.isAxiosError(error)) {
throw new exceptions_1.ViettelSInvoiceGetInvoiceFileException(error.message, (_b = error === null || error === void 0 ? void 0 : error.response) === null || _b === void 0 ? void 0 : _b.data);
}
throw new exceptions_1.ViettelSInvoiceGetInvoiceFileException(`Unexpected error: ${error.message}`, error);
}
});
}
/**
* Retrieves available invoice templates for a specified invoice type.
*
* @param invoiceType - The type of invoice for which templates are requested.
* @returns A promise that resolves to the response containing invoice templates.
* @throws ViettelSInvoiceGetTemplatesException if the server response is not successful.
*/
getInvoiceTemplates(invoiceType) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
const { access_token } = yield this.login();
try {
const { data } = yield axios_1.default.post(this.getApiUrl('/services/einvoiceapplication/api/InvoiceAPI/InvoiceUtilsWS/getInvoiceTemplates'), {
taxCode: this.username,
invoiceType
}, {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${access_token}`
}
});
return data;
}
catch (error) {
if (axios_1.default.isAxiosError(error)) {
throw new exceptions_1.ViettelSInvoiceGetTemplatesException(error.message, (_a = error === null || error === void 0 ? void 0 : error.response) === null || _a === void 0 ? void 0 : _a.data);
}
throw new exceptions_1.ViettelSInvoiceGetTemplatesException(`Unexpected error: ${error.message}`, error);
}
});
}
}
exports.default = ViettelSInvoice;