Skip to content

Commit

Permalink
Added support for return_format for sendanor/project-sendanor.fi#170
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaakko Heusala committed Jul 15, 2024
1 parent cf27be2 commit a029359
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 8 deletions.
3 changes: 3 additions & 0 deletions maventa/MaventaService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) 2024. Sendanor <info@sendanor.fi>. All rights reserved.

import { MaventaInvoice } from "./types/MaventaInvoice";
import { MaventaReturnFormat } from "./types/MaventaReturnFormat";

export interface MaventaService {

Expand All @@ -24,9 +25,11 @@ export interface MaventaService {
* Get invoice
*
* @param id
* @param returnFormat
*/
getInvoice(
id : string,
returnFormat ?: MaventaReturnFormat | undefined,
): Promise<MaventaInvoice|undefined>

}
13 changes: 11 additions & 2 deletions maventa/MaventaServiceImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
Expand Down Expand Up @@ -88,8 +89,9 @@ export class MaventaServiceImpl implements MaventaService {
*/
public async getInvoice(
id: string,
returnFormat : MaventaReturnFormat = MaventaReturnFormat.PEPPOLBIS30,
): Promise<MaventaInvoice | undefined> {
return this._getInvoice(id);
return this._getInvoice(id, returnFormat ?? MaventaReturnFormat.PEPPOLBIS30);
}

/**
Expand Down Expand Up @@ -139,10 +141,12 @@ export class MaventaServiceImpl implements MaventaService {
/**
*
* @param id
* @param return_format
* @private
*/
private async _getInvoice(
id: string,
return_format: MaventaReturnFormat,
): Promise<MaventaInvoice | undefined> {
const token = await this._getAccessToken();
const headers = {
Expand All @@ -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");
Expand Down
33 changes: 27 additions & 6 deletions maventa/types/MaventaInvoice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import {
explainArrayOf,
isArrayOf,
} from "../../types/Array";
import {
explainBooleanOrNullOrUndefined,
isBooleanOrNullOrUndefined,
} from "../../types/Boolean";
import {
explain,
explainNot,
Expand All @@ -26,9 +30,11 @@ import {
import {
explainString,
explainStringOrNull,
explainStringOrNullOrUndefined,
explainStringOrUndefined,
isString,
isStringOrNull,
isStringOrNullOrUndefined,
isStringOrUndefined,
} from "../../types/String";
import { isUndefined } from "../../types/undefined";
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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<MaventaFile>(value?.files, isMaventaFile)
&& isArrayOf<MaventaAction>(value?.actions, isMaventaAction)
&& isMaventaRevisionObjectOrUndefined(value?.revision)
&& isStringOrNullOrUndefined(value?.origin)
&& isStringOrNullOrUndefined(value?.origin_type)
&& isBooleanOrNullOrUndefined(value?.virtual_barcode_exists)
);
}

Expand Down Expand Up @@ -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))
Expand All @@ -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>("MaventaFile", explainMaventaFile, value?.files, isMaventaFile))
, explainProperty("actions", explainArrayOf<MaventaAction>("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))
]
);
}
Expand Down
52 changes: 52 additions & 0 deletions maventa/types/MaventaReturnFormat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (c) 2024. Sendanor <info@sendanor.fi>. 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']));
}

0 comments on commit a029359

Please sign in to comment.