Skip to content

Commit

Permalink
feat: add zone functions
Browse files Browse the repository at this point in the history
  • Loading branch information
arnoerpenbeck committed Jun 24, 2024
1 parent 251f45e commit 3890a38
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/fft-api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ export * from './shipment';
export * from './subscription';
export * from './types';
export * from './stock';
export * from './zone';
97 changes: 97 additions & 0 deletions src/fft-api/zone/fftZoneService.ts
Original file line number Diff line number Diff line change
@@ -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<FftZoneService> = new CustomLogger<FftZoneService>();

constructor(private readonly apiClient: FftApiClient) {}

public async create(facilityId: string, name: string, score: number): Promise<Zone> {
const zoneForCreation: ZoneForCreation = {
name: name,
score: score,
};
try {
return await this.apiClient.post<Zone>(`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<Zone> {
try {
return await this.apiClient.get<Zone>(`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<Zone> {
try {
return await this.apiClient.get<Zone>(`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<Zone> {
try {
const zone = await this.get(facilityId, zoneId);
const zoneForReplacement: ZoneForReplacement = {
name: name,
score: score,
version: zone.version,
};
return await this.apiClient.put<Zone>(`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<void> {
try {
return await this.apiClient.delete<void>(`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;
}
}
}
1 change: 1 addition & 0 deletions src/fft-api/zone/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './fftZoneService';

0 comments on commit 3890a38

Please sign in to comment.