Skip to content

Commit

Permalink
changed configuration of variables, removed SendInvoiceToMyCompany
Browse files Browse the repository at this point in the history
  • Loading branch information
Seesti committed Jun 14, 2024
1 parent 75da85c commit 5c5d4b5
Show file tree
Hide file tree
Showing 10 changed files with 176 additions and 142 deletions.
16 changes: 0 additions & 16 deletions maventa/MaventaConfig.ts

This file was deleted.

85 changes: 56 additions & 29 deletions maventa/MaventaService.system.test.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,59 @@
// MaventaService.system.test.ts

import { ProcessUtils } from "../ProcessUtils";
ProcessUtils.initEnvFromDefaultFiles();

import { MaventaService } from './MaventaService';
import { MaventaConfig } from './MaventaConfig';
import { LogLevel } from "../types/LogLevel";
import { RequestClientImpl } from "../RequestClientImpl";
import { HttpService } from "../HttpService";
import { HgNode } from '../../node/HgNode';
import { MaventaConfig } from './types/MaventaConfig';
import { DEFAULT_MAVENTA_BASE_URL_TEST, DEFAULT_MAVENTA_SCOPE } from "./maventa-constants";

const MAVENTA_BASE_URL = DEFAULT_MAVENTA_BASE_URL_TEST;
const CLIENT_ID = process.env.CLIENT_ID ?? '';
const CLIENT_SECRET = process.env.CLIENT_SECRET ?? '';
const SCOPE = DEFAULT_MAVENTA_SCOPE;
const VENDOR_API_KEY = process.env.VENDOR_API_KEY ?? '';
const COMPANY_EDI = process.env.COMPANY_EDI ?? '';

RequestClientImpl.setLogLevel(LogLevel.NONE);
HttpService.setLogLevel(LogLevel.NONE);

console.log('MaventaService system tests loaded');

(CLIENT_ID && CLIENT_SECRET && VENDOR_API_KEY && COMPANY_EDI ? describe : describe.skip)('system', () => {

describe('MaventaService', () => {
let service: MaventaService;
let config: MaventaConfig;

beforeAll(() => {
HgNode.initialize();
});

beforeEach(() => {
config = {
baseUrl: MAVENTA_BASE_URL,
clientId: CLIENT_ID,
clientSecret: CLIENT_SECRET,
scope: SCOPE,
vendorApiKey: VENDOR_API_KEY,
companyEDI: COMPANY_EDI
};
service = new MaventaService(config);
});

describe('#listInvoices', () => {
it('should fetch real invoices from the Maventa API', async () => {
const invoices = await service.listInvoices();
expect(Array.isArray(invoices)).toBe(true);
expect(invoices.length).toBeGreaterThan(0);
expect(invoices[0]).toHaveProperty('id');
expect(invoices[0]).toHaveProperty('status');
});
});
});

describe('MaventaService System Tests', () => {

beforeAll(() => {
HgNode.initialize();
console.log('Running system tests against:', MaventaConfig.baseUrl);
});

it('should fetch real invoices from the Maventa API', async () => {
const invoices = await MaventaService.listInvoices();
console.log('Fetched invoices:', invoices);

expect(invoices).toBeInstanceOf(Array);
if (invoices.length > 0) {
console.log(`Displaying details of the first invoice:`);
console.log(`Invoice ID: ${invoices[0].id}`);
console.log(`Invoice Status: ${invoices[0].status}`);
console.log(`Invoice Amount: ${invoices[0].sum}`);

expect(invoices[0]).toHaveProperty('id');
expect(invoices[0]).toHaveProperty('status');
expect(invoices[0].id).toBeDefined();
expect(invoices[0].status).toBeDefined();
} else {
console.log('No invoices found.');
}
});

});
});
75 changes: 34 additions & 41 deletions maventa/MaventaService.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,59 @@
function isMaventaInvoice(data: any): data is MaventaInvoice {
return data && typeof data === 'object' && 'id' in data && 'status' in data;
}

import { HttpService } from '../HttpService';
import { MaventaConfig as config } from './MaventaConfig';
import { MaventaInvoice } from './types/MaventaInvoice';
import { MaventaConfig } from './types/MaventaConfig';
import { MaventaInvoice, isMaventaInvoice } from './types/MaventaInvoice';
import { MaventaTokenResponse } from './types/MaventaTokenResponse';
import { DEFAULT_MAVENTA_BASE_URL, DEFAULT_MAVENTA_BASE_URL_TEST, DEFAULT_MAVENTA_SCOPE } from './maventa-constants';

export class MaventaService {
private readonly _config: MaventaConfig;
private _token: string | undefined;

constructor(config: Partial<MaventaConfig>) {
const isTesting = process.env.IS_TESTING === 'true';
this._config = {
baseUrl: config.baseUrl || (isTesting ? DEFAULT_MAVENTA_BASE_URL_TEST : DEFAULT_MAVENTA_BASE_URL),
clientId: config.clientId || process.env.CLIENT_ID!,
clientSecret: config.clientSecret || process.env.CLIENT_SECRET!,
scope: config.scope || DEFAULT_MAVENTA_SCOPE,
vendorApiKey: config.vendorApiKey || process.env.VENDOR_API_KEY!,
companyEDI: config.companyEDI || process.env.COMPANY_EDI!,
};
}

private async _getAccessToken(): Promise<string> {
if (this._token) return this._token;

private static async _getAccessToken(): Promise<string> {
const postData = new URLSearchParams({
grant_type: 'client_credentials',
client_id: config.clientId,
client_secret: config.clientSecret,
scope: config.scope,
vendor_api_key: config.vendorApiKey,
client_id: this._config.clientId,
client_secret: this._config.clientSecret,
scope: this._config.scope,
vendor_api_key: this._config.vendorApiKey,
}).toString();

const headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(postData).toString(),
};

const url = `${config.baseUrl}/oauth2/token`;
const url = `${this._config.baseUrl}/oauth2/token`;
const result = await HttpService.postText(url, postData, headers);
if (!result) throw new Error("Failed to retrieve access token");
const response: MaventaTokenResponse = JSON.parse(result);
return response.access_token;
this._token = response.access_token;
return this._token;
}

public static async listInvoices(): Promise<MaventaInvoice[]> {
const token = await MaventaService._getAccessToken();
public async listInvoices(): Promise<MaventaInvoice[]> {
const token = await this._getAccessToken();
const headers = {
'Authorization': `Bearer ${token}`,
'Accept': 'from json',
'User-Api-Key': config.clientSecret,
'Company-UUID': config.clientId,
'Accept': 'application/json',
'User-Api-Key': this._config.clientSecret,
'Company-UUID': this._config.clientId,
};

const url = `${config.baseUrl}/v1/invoices`;
const url = `${this._config.baseUrl}/v1/invoices`;
const response = await HttpService.getJson(url, headers);
if (!response || !Array.isArray(response)) throw new Error("Failed to list invoices or wrong format");

Expand All @@ -49,25 +63,4 @@ export class MaventaService {
}
return invoices;
}

public static async sendInvoiceToMyCompany(): Promise<MaventaInvoice> {
const token = await MaventaService._getAccessToken();
const url = `${config.baseUrl}/v1/invoices`;
const headers = {
'Authorization': `Bearer ${token}`,
'User-Api-Key': config.clientSecret,
'Company-UUID': config.clientId,
};

const formData = {
file: { path: './src/io/hyperify/core/maventa/types/invoice.xml', filename: 'invoice.xml' },
format: 'VISMAXL60',
recipient_eia: config.companyEDI,
};

const result = await HttpService.postJson(url, formData, headers);
if (!result || !isMaventaInvoice(result)) throw new Error("Failed to send invoice or response format is incorrect");

return result;
}
}
}
3 changes: 3 additions & 0 deletions maventa/maventa-constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const DEFAULT_MAVENTA_BASE_URL = 'https://ax.maventa.com';
export const DEFAULT_MAVENTA_BASE_URL_TEST = 'https://ax-stage.maventa.com';
export const DEFAULT_MAVENTA_SCOPE = 'eui';
16 changes: 7 additions & 9 deletions maventa/types/MaventaAction.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
interface MaventaAction {
type: string;
channel: string;
message: string | null;
key: string | null;
happened_at: string;
}

export { MaventaAction }
export interface MaventaAction {
readonly type: string;
readonly channel: string;
readonly message: string | null;
readonly key: string | null;
readonly happened_at: string;
}
8 changes: 8 additions & 0 deletions maventa/types/MaventaConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export interface MaventaConfig {
readonly baseUrl: string;
readonly clientId: string;
readonly clientSecret: string;
readonly scope: string;
readonly vendorApiKey: string;
readonly companyEDI: string;
}
16 changes: 7 additions & 9 deletions maventa/types/MaventaFile.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
interface MaventaFile {
id: string;
filename: string;
type: string;
mimetype: string;
href: string;
}

export { MaventaFile }
export interface MaventaFile {
readonly id: string;
readonly filename: string;
readonly type: string;
readonly mimetype: string;
readonly href: string;
}
71 changes: 50 additions & 21 deletions maventa/types/MaventaInvoice.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,57 @@
import { MaventaAction } from "./MaventaAction";
import { MaventaRecipient } from "./MaventaRecipient";
import { MaventaSender } from "./MaventaSender";
import { MaventaFile } from "./MaventaFile";

export interface MaventaRevisionObject {
readonly [key: string]: unknown;
}

interface MaventaInvoice {
id: string;
status: string;
reference: string | null;
number: string;
sender: MaventaSender;
recipient: MaventaRecipient;
received_at?: string;
created_at: string;
date: string;
date_due: string;
source_format: string;
sum: number;
sum_tax: number;
currency: string;
destination: string | null;
comment: string | null;
files: Array<File>;
actions: Array<MaventaAction>;
revision: Record<string, unknown>;
export interface MaventaInvoice {
readonly id: string;
readonly status: string;
readonly reference: string | null;
readonly number: string;
readonly sender: MaventaSender;
readonly recipient: MaventaRecipient;
readonly received_at?: string;
readonly created_at: string;
readonly date: string;
readonly date_due: string;
readonly source_format: string;
readonly sum: number;
readonly sum_tax: number;
readonly currency: string;
readonly destination: string | null;
readonly comment: string | null;
readonly files: readonly MaventaFile[];
readonly actions: readonly MaventaAction[];
readonly revision: MaventaRevisionObject;
}

export { MaventaInvoice };
export function isMaventaInvoice(data: unknown): data is MaventaInvoice {
const record = data as MaventaInvoice;
return record != null &&
typeof record === 'object' &&
typeof record.id === 'string' &&
typeof record.status === 'string' &&
(typeof record.reference === 'string' || record.reference === null) &&
typeof record.number === 'string' &&
typeof record.sender === 'object' && record.sender !== null &&
typeof record.recipient === 'object' && record.recipient !== null &&
(typeof record.received_at === 'string' || record.received_at === undefined) &&
typeof record.created_at === 'string' &&
typeof record.date === 'string' &&
typeof record.date_due === 'string' &&
typeof record.source_format === 'string' &&
typeof record.sum === 'number' &&
typeof record.sum_tax === 'number' &&
typeof record.currency === 'string' &&
(typeof record.destination === 'string' || record.destination === null) &&
(typeof record.comment === 'string' || record.comment === null) &&
Array.isArray(record.files) &&
record.files.every(file => typeof file === 'object' && file !== null) &&
Array.isArray(record.actions) &&
record.actions.every(action => typeof action === 'object' && action !== null) &&
typeof record.revision === 'object' && record.revision !== null;
}
14 changes: 5 additions & 9 deletions maventa/types/MaventaRecipient.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
interface MaventaRecipient {
eia: string | null;
bid: string | null;
name: string;
country: string;
operator: string | null;
}

export { MaventaRecipient }
export interface MaventaRecipient {
readonly name: string;
readonly country: string;
readonly operator: string | null;
}
14 changes: 6 additions & 8 deletions maventa/types/MaventaSender.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
interface MaventaSender {
eia: string | null;
bid: string | null;
name: string;
country: string;
}

export { MaventaSender }
export interface MaventaSender {
readonly eia: string | null;
readonly bid: string | null;
readonly name: string;
readonly country: string;
}

0 comments on commit 5c5d4b5

Please sign in to comment.