From 01ab14960406e4a5c6fdacbfa49747b1edeaff29 Mon Sep 17 00:00:00 2001 From: "Sandra Viktoria D. Toftemo" Date: Tue, 6 Feb 2024 16:39:15 +0100 Subject: [PATCH] feat: add usage summary (#77) * delete main Co-authored-by: Tigua002 * revert commit: delete main Co-authored-by: Tigua002 * implement list usage summary Co-authored-by: Tigua002 * add method listUsageSummary * format * put in proper spot * convert to type import * rename to match method * update path name * lint --------- Co-authored-by: Tigua002 --- src/abax-client.ts | 28 ++++++++++++++++++++++++++++ src/calls/list-usage-summary.ts | 27 +++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 src/calls/list-usage-summary.ts diff --git a/src/abax-client.ts b/src/abax-client.ts index 773cde0..b433982 100644 --- a/src/abax-client.ts +++ b/src/abax-client.ts @@ -35,6 +35,11 @@ import { type ListTripsResponse, listTripsResponseSchema, } from './calls/list-trips.js'; +import { + type ListUsageSummaryInput, + type ListUsageSummaryResponse, + listUsageSummaryResponseSchema, +} from './calls/list-usage-summary.js'; import { type ListVehiclesInput, type ListVehiclesResponse, @@ -112,6 +117,29 @@ export class AbaxClient { return this.performRequest(apiKey => call({ input, apiKey })); } + listUsageSummary( + input: ListUsageSummaryInput, + ): Promise { + // URL: /v1/vehicles/{vehicle-id}/usage-summary?from=&to= + const call = this.authenticatedCall() + .args<{ input: ListUsageSummaryInput }>() + .method('get') + .path( + ({ input: { vehicle_id } }) => + `/v1/vehicles/${vehicle_id}/usage-summary`, + ) + .query(({ input }) => { + const queryParams = new URLSearchParams(); + queryParams.append('from', format(input.date_from, 'yyyy-MM-dd')); + queryParams.append('to', format(input.date_to, 'yyyy-MM-dd')); + return queryParams; + }) + .parseJson(withZod(listUsageSummaryResponseSchema)) + .build(); + + return this.performRequest(apiKey => call({ input, apiKey })); + } + async listTripExpenses( input: ListTripExpensesInput, ): Promise { diff --git a/src/calls/list-usage-summary.ts b/src/calls/list-usage-summary.ts new file mode 100644 index 0000000..52380e0 --- /dev/null +++ b/src/calls/list-usage-summary.ts @@ -0,0 +1,27 @@ +import { z } from 'zod'; + +export interface ListUsageSummaryInput { + // the id of the vehicle + vehicle_id?: string; + + /** The period cannot be longer than 3 months */ + date_from: Date; + + /** The period cannot be longer than 3 months */ + date_to: Date; +} + +export const listUsageSummaryResponseSchema = z.object({ + PrivateUsage: z.object({ + DistanceDrivenInMeter: z.number(), + TotalTollStationsPassed: z.number(), + }), + CorporateUsageSummary: z.object({ + DistanceDrivenInMeter: z.number(), + TotalTollStationsPassed: z.number(), + }), +}); + +export type ListUsageSummaryResponse = z.infer< + typeof listUsageSummaryResponseSchema +>;