Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CTC-141 add listing functions #46

Merged
merged 2 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/fft-api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ export * from './auth';
export * from './common';
export * from './facility';
export * from './handover';
export * from './listing';
export * from './loadunit';
export * from './order';
export * from './packjob';
export * from './parcel';
export * from './pickjob';
export * from './packjob';
export * from './process';
export * from './routing-plan';
export * from './shipment';
export * from './stock';
export * from './subscription';
export * from './tag';
export * from './types';
export * from './stock';
export * from './zone';
118 changes: 118 additions & 0 deletions src/fft-api/listing/fftListingService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import { Logger } from 'tslog';
import { ResponseError } from 'superagent';

import { FftApiClient } from '../common';
import { CustomLogger } from '../../common';
import { Listing, ListingForReplacement, ModifyListingAction, StrippedListings } from '../types';

export class FftListingService {
private readonly path = 'listings';
private readonly logger: Logger<FftListingService> = new CustomLogger<FftListingService>();

constructor(private readonly apiClient: FftApiClient) {}

public async create(facilityId: string, listing: ListingForReplacement): Promise<Listing> {
try {
const listingsForReplacement = { listings: [listing] };
return await this.apiClient.put<Listing>(`facilities/${facilityId}/${this.path}`, {
...listingsForReplacement,
});
} catch (err) {
const httpError = err as ResponseError;
this.logger.error(
`Could not create listing ${listing.tenantArticleId} for facility ${facilityId}. Failed with status ${
httpError.status
}, error: ${httpError.response ? JSON.stringify(httpError.response.body) : ''}`
);

throw err;
}
}

public async get(facilityId: string, tenantArticleId: string, relaxed = false): Promise<Listing | undefined> {
try {
return await this.apiClient.get<Listing>(`facilities/${facilityId}/${this.path}/${tenantArticleId}`);
} catch (err) {
const httpError = err as ResponseError;
if (httpError.status === 404 && relaxed) {
return undefined;
}
this.logger.error(
`Could not fetch listing ${tenantArticleId} for facility ${facilityId}. Failed with status ${
httpError.status
}, error: ${httpError.response ? JSON.stringify(httpError.response.body) : ''}`
);

throw err;
}
}

public async getAll(facilityId: string, size = 25): Promise<StrippedListings> {
arnoerpenbeck marked this conversation as resolved.
Show resolved Hide resolved
try {
return await this.apiClient.get<StrippedListings>(`facilities/${facilityId}/${this.path}`, {
...(size && { size: size.toString() }),
});
} catch (error) {
const httpError = error as ResponseError;
this.logger.error(
`Could not get listings for facility ${facilityId}. Failed with status ${httpError.status}, error: ${
httpError.response ? JSON.stringify(httpError.response.body) : ''
}`
);
throw error;
}
}

public async update(facilityId: string, tenantArticleId: string, action: ModifyListingAction): Promise<Listing> {
try {
action.action = ModifyListingAction.ActionEnum.ModifyListing;
const listing = await this.get(facilityId, tenantArticleId);
const listingPatchActions = {
actions: [action],
version: listing?.version,
};
return await this.apiClient.patch<Listing>(`facilities/${facilityId}/${this.path}/${tenantArticleId}`, {
...listingPatchActions,
});
} catch (err) {
const httpError = err as ResponseError;
this.logger.error(
`Could not update listing ${tenantArticleId} for facility ${facilityId}. Failed with status ${
httpError.status
}, error: ${httpError.response ? JSON.stringify(httpError.response.body) : ''}`
);

throw err;
}
}

public async delete(facilityId: string, tenantArticleId: string): Promise<void> {
try {
return await this.apiClient.delete<void>(`facilities/${facilityId}/${this.path}/${tenantArticleId}`);
} catch (err) {
const httpError = err as ResponseError;
this.logger.error(
`Could not delete listing ${tenantArticleId} for facility ${facilityId}. Failed with status ${
httpError.status
}, error: ${httpError.response ? JSON.stringify(httpError.response.body) : ''}`
);

throw err;
}
}

public async deleteAll(facilityId: string): Promise<void> {
try {
return await this.apiClient.delete<void>(`facilities/${facilityId}/${this.path}`);
} catch (err) {
const httpError = err as ResponseError;
this.logger.error(
`Could not delete listings for facility ${facilityId}. Failed with status ${httpError.status}, error: ${
httpError.response ? JSON.stringify(httpError.response.body) : ''
}`
);

throw err;
}
}
}
1 change: 1 addition & 0 deletions src/fft-api/listing/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './fftListingService';
2 changes: 1 addition & 1 deletion src/fft-api/types/api-ctl.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"

TARGET_DIR="${SCRIPTDIR}/typescript-fetch-client"
apiUrl="https://raw.githubusercontent.com/fulfillmenttools/fulfillmenttools-api-reference/master/api.swagger.yaml"
openApiVersion="3.0.54"
openApiVersion="3.0.57"
swaggerFile="swagger-codegen-cli-${openApiVersion}.jar"
localSwaggerFile="${SCRIPTDIR}/${swaggerFile}"
swaggerCodeGenUrl="https://repo1.maven.org/maven2/io/swagger/codegen/v3/swagger-codegen-cli/${openApiVersion}/${swaggerFile}"
Expand Down
Loading