Skip to content

Commit

Permalink
chore: make facility lookup by tenantFacilityId lenient when not found (
Browse files Browse the repository at this point in the history
#20)

* chore: make facility lookup by tenantFacilityId lenient when not found
  • Loading branch information
arnoerpenbeck authored Nov 7, 2023
1 parent fb077f4 commit edef3bb
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions src/fft-api/facility/fftFacilityService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,33 @@ export class FftFacilityService {

constructor(private readonly apiClient: FftApiClient) {}

public async getFacilityId(tenantFacilityId: string): Promise<string> {
public async getFacilityId(tenantFacilityId: string, relaxed = false): Promise<string | undefined> {
if (FftFacilityService.facilityCache.has(tenantFacilityId)) {
return FftFacilityService.facilityCache.get(tenantFacilityId) as string;
}

const strippedFacilities = await this.apiClient.get<StrippedFacilities>(this.PATH, { tenantFacilityId });
if (!strippedFacilities.facilities?.length) {
this.logger.error(`Did not find facility with tenantFacilityId '${tenantFacilityId}'`);
throw new Error(`Did not find facility with tenantFacilityId '${tenantFacilityId}'`);
}

if (strippedFacilities.facilities.length > 1) {
this.logger.warn(
`Did not find exactly 1 facility with tenantFacilityId '${tenantFacilityId}' but ${strippedFacilities.facilities.length}, returning first one with id '${strippedFacilities.facilities[0].id}'`
);
const length = strippedFacilities.facilities?.length || 0;
let facility: StrippedFacility;

if (length >= 1) {
facility = strippedFacilities.facilities?.[0] as StrippedFacility;
FftFacilityService.facilityCache.set(tenantFacilityId, facility.id);
if (length > 1) {
this.logger.warn(
`Did not find exactly 1 facility with tenantFacilityId '${tenantFacilityId}' but ${length}, returning first one with id '${facility.id}'`
);
}
} else {
if (relaxed) {
this.logger.info(`Did not find facility with tenantFacilityId '${tenantFacilityId}'`);
return undefined;
} else {
this.logger.error(`Did not find facility with tenantFacilityId '${tenantFacilityId}'`);
throw new Error(`Did not find facility with tenantFacilityId '${tenantFacilityId}'`);
}
}

const [facility] = strippedFacilities.facilities;
FftFacilityService.facilityCache.set(tenantFacilityId, facility.id);

return facility.id;
}

Expand Down

0 comments on commit edef3bb

Please sign in to comment.