From 3890a38c157ccb52cf985044d1f2a9be48120a70 Mon Sep 17 00:00:00 2001 From: Arno Erpenbeck <88486704+arnoerpenbeck@users.noreply.github.com> Date: Mon, 24 Jun 2024 17:21:25 +0200 Subject: [PATCH] feat: add zone functions --- src/fft-api/index.ts | 1 + src/fft-api/zone/fftZoneService.ts | 97 ++++++++++++++++++++++++++++++ src/fft-api/zone/index.ts | 1 + 3 files changed, 99 insertions(+) create mode 100644 src/fft-api/zone/fftZoneService.ts create mode 100644 src/fft-api/zone/index.ts diff --git a/src/fft-api/index.ts b/src/fft-api/index.ts index 6b8481e..7e40735 100644 --- a/src/fft-api/index.ts +++ b/src/fft-api/index.ts @@ -13,3 +13,4 @@ export * from './shipment'; export * from './subscription'; export * from './types'; export * from './stock'; +export * from './zone'; diff --git a/src/fft-api/zone/fftZoneService.ts b/src/fft-api/zone/fftZoneService.ts new file mode 100644 index 0000000..655e295 --- /dev/null +++ b/src/fft-api/zone/fftZoneService.ts @@ -0,0 +1,97 @@ +import { ResponseError } from 'superagent'; +import { Logger } from 'tslog'; +import { FftApiClient } from '../common'; +import { CustomLogger } from '../../common'; +import { Zone, ZoneForCreation, ZoneForReplacement } from '../types'; + +export class FftZoneService { + private readonly PATH = 'zones'; + + private readonly logger: Logger = new CustomLogger(); + + constructor(private readonly apiClient: FftApiClient) {} + + public async create(facilityId: string, name: string, score: number): Promise { + const zoneForCreation: ZoneForCreation = { + name: name, + score: score, + }; + try { + return await this.apiClient.post(`facilities/${facilityId}/${this.PATH}`, { ...zoneForCreation }); + } catch (error) { + const httpError = error as ResponseError; + this.logger.error( + `Could not create zone. Failed with status ${httpError.status}, error: ${ + httpError.response ? JSON.stringify(httpError.response.body) : '' + }` + ); + throw error; + } + } + + public async getAll(facilityId: string, size = 25): Promise { + try { + return await this.apiClient.get(`facilities/${facilityId}/${this.PATH}`, { + ...(size && { size: size.toString() }), + }); + } catch (error) { + const httpError = error as ResponseError; + this.logger.error( + `Could not get zones. Failed with status ${httpError.status}, error: ${ + httpError.response ? JSON.stringify(httpError.response.body) : '' + }` + ); + throw error; + } + } + + public async get(facilityId: string, zoneId: string): Promise { + try { + return await this.apiClient.get(`facilities/${facilityId}/${this.PATH}/${zoneId}`); + } catch (error) { + const httpError = error as ResponseError; + this.logger.error( + `Could not get zone ${zoneId}. Failed with status ${httpError.status}, error: ${ + httpError.response ? JSON.stringify(httpError.response.body) : '' + }` + ); + throw error; + } + } + + public async update(facilityId: string, zoneId: string, name: string, score: number): Promise { + try { + const zone = await this.get(facilityId, zoneId); + const zoneForReplacement: ZoneForReplacement = { + name: name, + score: score, + version: zone.version, + }; + return await this.apiClient.put(`facilities/${facilityId}/${this.PATH}/${zoneId}`, { + ...zoneForReplacement, + }); + } catch (error) { + const httpError = error as ResponseError; + this.logger.error( + `Could not update zone ${zoneId}. Failed with status ${httpError.status}, error: ${ + httpError.response ? JSON.stringify(httpError.response.body) : '' + }` + ); + throw error; + } + } + + public async delete(facilityId: string, zoneId: string): Promise { + try { + return await this.apiClient.delete(`facilities/${facilityId}/${this.PATH}/${zoneId}`); + } catch (error) { + const httpError = error as ResponseError; + this.logger.error( + `Could not delete zone ${zoneId}. Failed with status ${httpError.status}, error: ${ + httpError.response ? JSON.stringify(httpError.response.body) : '' + }` + ); + throw error; + } + } +} diff --git a/src/fft-api/zone/index.ts b/src/fft-api/zone/index.ts new file mode 100644 index 0000000..5ad9ced --- /dev/null +++ b/src/fft-api/zone/index.ts @@ -0,0 +1 @@ +export * from './fftZoneService';