diff --git a/maventa/MaventaService.ts b/maventa/MaventaService.ts index 8d87d68..bed1a81 100644 --- a/maventa/MaventaService.ts +++ b/maventa/MaventaService.ts @@ -1,6 +1,7 @@ // Copyright (c) 2024. Sendanor . All rights reserved. import { MaventaInvoice } from "./types/MaventaInvoice"; +import { MaventaReturnFormat } from "./types/MaventaReturnFormat"; export interface MaventaService { @@ -24,9 +25,11 @@ export interface MaventaService { * Get invoice * * @param id + * @param returnFormat */ getInvoice( id : string, + returnFormat ?: MaventaReturnFormat | undefined, ): Promise } diff --git a/maventa/MaventaServiceImpl.ts b/maventa/MaventaServiceImpl.ts index 70565f7..d25f49a 100644 --- a/maventa/MaventaServiceImpl.ts +++ b/maventa/MaventaServiceImpl.ts @@ -16,6 +16,7 @@ import { isMaventaInvoice, MaventaInvoice, } from './types/MaventaInvoice'; +import { MaventaReturnFormat } from "./types/MaventaReturnFormat"; import { MaventaTokenResponse } from './types/MaventaTokenResponse'; const LOG = LogService.createLogger( 'MaventaServiceImpl' ); @@ -88,8 +89,9 @@ export class MaventaServiceImpl implements MaventaService { */ public async getInvoice( id: string, + returnFormat : MaventaReturnFormat = MaventaReturnFormat.PEPPOLBIS30, ): Promise { - return this._getInvoice(id); + return this._getInvoice(id, returnFormat ?? MaventaReturnFormat.PEPPOLBIS30); } /** @@ -139,10 +141,12 @@ export class MaventaServiceImpl implements MaventaService { /** * * @param id + * @param return_format * @private */ private async _getInvoice( id: string, + return_format: MaventaReturnFormat, ): Promise { const token = await this._getAccessToken(); const headers = { @@ -151,7 +155,12 @@ export class MaventaServiceImpl implements MaventaService { 'User-Api-Key': this._clientSecret, 'Company-UUID': this._clientId, }; - const url = `${this._baseUrl}/v1/invoice/${q(id)}`; + + const params = [ + ...(return_format ? [`return_format=${q(return_format)}`] : []), + ]; + + const url = `${this._baseUrl}/v1/invoices/${q(id)}?${params.join('&')}`; const response = await HttpService.getJson(url, headers); if (!response) { throw new Error("Failed to list invoices or wrong format"); diff --git a/maventa/types/MaventaInvoice.ts b/maventa/types/MaventaInvoice.ts index 630e7fc..0d05c99 100644 --- a/maventa/types/MaventaInvoice.ts +++ b/maventa/types/MaventaInvoice.ts @@ -4,6 +4,10 @@ import { explainArrayOf, isArrayOf, } from "../../types/Array"; +import { + explainBooleanOrNullOrUndefined, + isBooleanOrNullOrUndefined, +} from "../../types/Boolean"; import { explain, explainNot, @@ -26,9 +30,11 @@ import { import { explainString, explainStringOrNull, + explainStringOrNullOrUndefined, explainStringOrUndefined, isString, isStringOrNull, + isStringOrNullOrUndefined, isStringOrUndefined, } from "../../types/String"; import { isUndefined } from "../../types/undefined"; @@ -66,18 +72,21 @@ export interface MaventaInvoice { readonly sender: MaventaSender; readonly recipient: MaventaRecipient; readonly received_at?: string; - readonly created_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 destination ?: string | null; readonly comment: string | null; readonly files: readonly MaventaFile[]; readonly actions: readonly MaventaAction[]; readonly revision ?: MaventaRevisionObject; + readonly origin ?: string | null; + readonly origin_type ?: string | null; + readonly virtual_barcode_exists ?: boolean | null; } export function isMaventaInvoice (value: unknown) : value is MaventaInvoice { @@ -103,6 +112,9 @@ export function isMaventaInvoice (value: unknown) : value is MaventaInvoice { 'files', 'actions', 'revision', + 'origin', + 'origin_type', + 'virtual_barcode_exists', ]) && isString(value?.id) && isString(value?.status) @@ -111,18 +123,21 @@ export function isMaventaInvoice (value: unknown) : value is MaventaInvoice { && isMaventaSender(value?.sender) && isMaventaRecipient(value?.recipient) && isStringOrUndefined(value?.received_at) - && isString(value?.created_at) + && isStringOrUndefined(value?.created_at) && isString(value?.date) && isString(value?.date_due) && isString(value?.source_format) && isNumber(value?.sum) && isNumber(value?.sum_tax) && isString(value?.currency) - && isStringOrNull(value?.destination) + && isStringOrNullOrUndefined(value?.destination) && isStringOrNull(value?.comment) && isArrayOf(value?.files, isMaventaFile) && isArrayOf(value?.actions, isMaventaAction) && isMaventaRevisionObjectOrUndefined(value?.revision) + && isStringOrNullOrUndefined(value?.origin) + && isStringOrNullOrUndefined(value?.origin_type) + && isBooleanOrNullOrUndefined(value?.virtual_barcode_exists) ); } @@ -150,6 +165,9 @@ export function explainMaventaInvoice (value: any) : string { 'files', 'actions', 'revision', + 'origin', + 'origin_type', + 'virtual_barcode_exists', ]) , explainProperty("id", explainString(value?.id)) , explainProperty("status", explainString(value?.status)) @@ -158,18 +176,21 @@ export function explainMaventaInvoice (value: any) : string { , explainProperty("sender", explainMaventaSender(value?.sender)) , explainProperty("recipient", explainMaventaRecipient(value?.recipient)) , explainProperty("received_at", explainStringOrUndefined(value?.received_at)) - , explainProperty("created_at", explainString(value?.created_at)) + , explainProperty("created_at", explainStringOrUndefined(value?.created_at)) , explainProperty("date", explainString(value?.date)) , explainProperty("date_due", explainString(value?.date_due)) , explainProperty("source_format", explainString(value?.source_format)) , explainProperty("sum", explainNumber(value?.sum)) , explainProperty("sum_tax", explainNumber(value?.sum_tax)) , explainProperty("currency", explainString(value?.currency)) - , explainProperty("destination", explainStringOrNull(value?.destination)) + , explainProperty("destination", explainStringOrNullOrUndefined(value?.destination)) , explainProperty("comment", explainStringOrNull(value?.comment)) , explainProperty("files", explainArrayOf("MaventaFile", explainMaventaFile, value?.files, isMaventaFile)) , explainProperty("actions", explainArrayOf("MaventaAction", explainMaventaAction, value?.actions, isMaventaAction)) , explainProperty("revision", explainMaventaRevisionObjectOrUndefined(value?.revision)) + , explainProperty("origin", explainStringOrNullOrUndefined(value?.origin)) + , explainProperty("origin_type", explainStringOrNullOrUndefined(value?.origin_type)) + , explainProperty("virtual_barcode_exists", explainBooleanOrNullOrUndefined(value?.virtual_barcode_exists)) ] ); } diff --git a/maventa/types/MaventaReturnFormat.ts b/maventa/types/MaventaReturnFormat.ts new file mode 100644 index 0000000..88da9d4 --- /dev/null +++ b/maventa/types/MaventaReturnFormat.ts @@ -0,0 +1,52 @@ +// Copyright (c) 2024. Sendanor . All rights reserved. + +import { + explainEnum, + isEnum, + parseEnum, + stringifyEnum, +} from "../../types/Enum"; +import { + explainNot, + explainOk, + explainOr, +} from "../../types/explain"; +import { isUndefined } from "../../types/undefined"; + +export enum MaventaReturnFormat { + VISMAXML60 = "VISMAXML60", + PEPPOLBIS30 = "PEPPOLBIS30", + SIUBL20 = "SIUBL20", + OIOUBL21 = "OIOUBL21", + VISMAUBL30 = "VISMAUBL30", + FINVOICE30 = "FINVOICE30", + TEAPPS30 = "TEAPPS30", + ORIGINAL_IMAGE = "ORIGINAL_IMAGE", + GENERATED_IMAGE = "GENERATED_IMAGE", + ORIGINAL_OR_GENERATED_IMAGE = "ORIGINAL_OR_GENERATED_IMAGE", + EXTENDED_DETAILS = "EXTENDED_DETAILS", +} + +export function isMaventaReturnFormat (value: unknown) : value is MaventaReturnFormat { + return isEnum(MaventaReturnFormat, value); +} + +export function explainMaventaReturnFormat (value : unknown) : string { + return explainEnum("MaventaReturnFormat", MaventaReturnFormat, isMaventaReturnFormat, value); +} + +export function stringifyMaventaReturnFormat (value : MaventaReturnFormat) : string { + return stringifyEnum(MaventaReturnFormat, value); +} + +export function parseMaventaReturnFormat (value: any) : MaventaReturnFormat | undefined { + return parseEnum(MaventaReturnFormat, value) as MaventaReturnFormat | undefined; +} + +export function isMaventaReturnFormatOrUndefined (value: unknown): value is MaventaReturnFormat | undefined { + return isUndefined(value) || isMaventaReturnFormat(value); +} + +export function explainMaventaReturnFormatOrUndefined (value: unknown): string { + return isMaventaReturnFormatOrUndefined(value) ? explainOk() : explainNot(explainOr(['MaventaReturnFormat', 'undefined'])); +}