Skip to content

Commit

Permalink
switched to internal libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
Seesti committed Jun 18, 2024
1 parent 5c5d4b5 commit f0383f5
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 66 deletions.
3 changes: 2 additions & 1 deletion maventa/MaventaService.system.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ 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";
import { isArray } from "../types/Array";

const MAVENTA_BASE_URL = DEFAULT_MAVENTA_BASE_URL_TEST;
const CLIENT_ID = process.env.CLIENT_ID ?? '';
Expand Down Expand Up @@ -48,7 +49,7 @@ console.log('MaventaService system tests loaded');
describe('#listInvoices', () => {
it('should fetch real invoices from the Maventa API', async () => {
const invoices = await service.listInvoices();
expect(Array.isArray(invoices)).toBe(true);
expect(isArray(invoices)).toBe(true);
expect(invoices.length).toBeGreaterThan(0);
expect(invoices[0]).toHaveProperty('id');
expect(invoices[0]).toHaveProperty('status');
Expand Down
10 changes: 5 additions & 5 deletions maventa/MaventaService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class MaventaService {
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;
Expand All @@ -42,7 +42,7 @@ export class MaventaService {
const response: MaventaTokenResponse = JSON.parse(result);
this._token = response.access_token;
return this._token;
}
};

public async listInvoices(): Promise<MaventaInvoice[]> {
const token = await this._getAccessToken();
Expand All @@ -60,7 +60,7 @@ export class MaventaService {
const invoices = response.filter(isMaventaInvoice);
if (invoices.length !== response.length) {
throw new Error("Some items in the response did not match the expected invoice format");
}
};
return invoices;
}
}
};
};
2 changes: 1 addition & 1 deletion maventa/types/MaventaAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ export interface MaventaAction {
readonly message: string | null;
readonly key: string | null;
readonly happened_at: string;
}
};
2 changes: 1 addition & 1 deletion maventa/types/MaventaConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ export interface MaventaConfig {
readonly scope: string;
readonly vendorApiKey: string;
readonly companyEDI: string;
}
};
2 changes: 1 addition & 1 deletion maventa/types/MaventaFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ export interface MaventaFile {
readonly type: string;
readonly mimetype: string;
readonly href: string;
}
};
101 changes: 54 additions & 47 deletions maventa/types/MaventaInvoice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,63 @@ import { MaventaAction } from "./MaventaAction";
import { MaventaRecipient } from "./MaventaRecipient";
import { MaventaSender } from "./MaventaSender";
import { MaventaFile } from "./MaventaFile";
import { isObject } from "../../types/Object";
import { isString } from "../../types/String";
import { isNumber } from "../../types/Number";
import { isArray } from "../../types/Array";
import { isNull } from "../../types/Null";
import { isUndefined } from "../../types/undefined";


export interface MaventaRevisionObject {
readonly [key: string]: unknown;
}
readonly [key: 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;
}
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 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;
}
const record = data as MaventaInvoice;
return record != null &&
isObject(record) &&
isString(record.id) &&
isString(record.status) &&
(isString(record.reference) || isNull(record.reference)) &&
isString(record.number) &&
isObject(record.sender) &&
isObject(record.recipient) &&
(isString(record.received_at) || isUndefined(record.received_at)) &&
isString(record.created_at) &&
isString(record.date) &&
isString(record.date_due) &&
isString(record.source_format) &&
isNumber(record.sum) &&
isNumber(record.sum_tax) &&
isString(record.currency) &&
(isString(record.destination) || isNull(record.destination)) &&
(isString(record.comment) || isNull(record.comment)) &&
isArray(record.files) &&
record.files.every(file => isObject(file)) &&
isArray(record.actions) &&
record.actions.every(action => isObject(action)) &&
isObject(record.revision);
};
2 changes: 1 addition & 1 deletion maventa/types/MaventaRecipient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ export interface MaventaRecipient {
readonly name: string;
readonly country: string;
readonly operator: string | null;
}
};
2 changes: 1 addition & 1 deletion maventa/types/MaventaSender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ export interface MaventaSender {
readonly bid: string | null;
readonly name: string;
readonly country: string;
}
};
14 changes: 6 additions & 8 deletions maventa/types/MaventaTokenResponse.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
interface MaventaTokenResponse {
access_token: string;
token_type: string;
expires_in: number;
scope: string;
}

export { MaventaTokenResponse }
export interface MaventaTokenResponse {
readonly access_token: string;
readonly token_type: string;
readonly expires_in: number;
readonly scope: string;
};

0 comments on commit f0383f5

Please sign in to comment.