Skip to content

Commit

Permalink
feat(CTC-125): add methods for order, pickjob, handoverjob cancellati…
Browse files Browse the repository at this point in the history
…on (#33)
  • Loading branch information
arnoerpenbeck authored Apr 25, 2024
1 parent 9c05389 commit b3bd8b8
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 18 deletions.
25 changes: 25 additions & 0 deletions src/fft-api/handover/fftHandoverService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { ResponseError } from 'superagent';
import {
HandoverJobCancelActionEnum,
HandoverJobCancelReason,
Handoverjob,
HandoverjobPatchActions,
HandoverjobStatus,
Expand Down Expand Up @@ -72,4 +74,27 @@ export class FftHandoverService {
throw err;
}
}

public async cancel(handoverJobId: string, version: number, reason: HandoverJobCancelReason): Promise<Handoverjob> {
try {
const handoverJob = await this.apiClient.post<Handoverjob>(`${this.path}/${handoverJobId}/actions`, {
name: HandoverJobCancelActionEnum.CANCEL,
version,
payload: {
handoverJobCancelReason: reason,
},
});

return handoverJob;
} catch (err) {
const httpError = err as ResponseError;
this.logger.error(
`Could not cancel handover job with id '${handoverJobId}' and version ${version}. Failed with status ${
httpError.status
}, error: ${httpError.response ? JSON.stringify(httpError.response.body) : ''}`
);

throw err;
}
}
}
48 changes: 38 additions & 10 deletions src/fft-api/order/fftOrderService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { ResponseError } from 'superagent';
import { Order, OrderForCreation, StrippedOrder, StrippedOrders } from '../types';
import {
Order,
OrderCancelActionParameter,
OrderForCreation,
OrderUnlockActionParameter,
StrippedOrder,
StrippedOrders,
} from '../types';
import { FftApiClient } from '../common';
import { Logger } from 'tslog';
import { CustomLogger } from '../../common';
Expand All @@ -11,9 +18,9 @@ export class FftOrderService {

public async create(orderForCreation: OrderForCreation): Promise<Order> {
try {
const order = await this.apiClient.post<Order>('orders', { ...orderForCreation });
const order = await this.apiClient.post<Order>(`${this.path}`, { ...orderForCreation });
this.logger.info(
`Successfully posted order with tenantOrderId '${orderForCreation.tenantOrderId}' and order id '${order.id}'`
`Successfully created order with tenantOrderId '${orderForCreation.tenantOrderId}' and order id '${order.id}'`
);

return order;
Expand All @@ -29,18 +36,40 @@ export class FftOrderService {
}
}

public async cancel(orderId: string) {
public async cancel(orderId: string, version: number): Promise<Order> {
try {
const order = await this.apiClient.post<Order>(`orders/${orderId}/cancel`);
this.logger.info(`Successfully canceled order with order id '${orderId}'`);
const order = await this.apiClient.post<Order>(`${this.path}/${orderId}/actions`, {
name: OrderCancelActionParameter.NameEnum.CANCEL,
version,
});

return order;
} catch (err) {
const httpError = err as ResponseError;
this.logger.error(
`FFT cancel order for id '${orderId}' failed with status ${httpError.status}, error: ${
httpError.response ? JSON.stringify(httpError.response.body) : ''
}`
`Could not cancel order with id '${orderId}' and version ${version}. Failed with status ${
httpError.status
}, error: ${httpError.response ? JSON.stringify(httpError.response.body) : ''}`
);

throw err;
}
}

public async unlock(orderId: string, version: number): Promise<Order> {
try {
const order = await this.apiClient.post<Order>(`${this.path}/${orderId}/actions`, {
name: OrderUnlockActionParameter.NameEnum.UNLOCK,
version,
});

return order;
} catch (err) {
const httpError = err as ResponseError;
this.logger.error(
`Could not unlock order with id '${orderId}' and version ${version}. Failed with status ${
httpError.status
}, error: ${httpError.response ? JSON.stringify(httpError.response.body) : ''}`
);

throw err;
Expand All @@ -58,7 +87,6 @@ export class FftOrderService {
const length = strippedOrders.orders?.length || 0;
const firstOrder = strippedOrders.orders?.[0];
if (!firstOrder) {
this.logger.info(`Did not find order with tenantOrderId '${tenantOrderId}'`);
return undefined;
}

Expand Down
37 changes: 29 additions & 8 deletions src/fft-api/pickjob/fftPickJobService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { isDate } from 'date-fns';
import { AbstractModificationAction, PickJob, StrippedPickJobs } from '../types';
import {
AbstractModificationAction,
PickJob,
PickJobAbortActionEnum,
PickJobObsoleteActionEnum,
PickJobResetActionEnum,
PickJobRestartActionEnum,
StrippedPickJobs,
} from '../types';
import { FftApiClient } from '../common';
import { ResponseError } from 'superagent';
import { CustomLogger, QueryParams } from '../../common';
Expand All @@ -26,19 +34,31 @@ export class FftPickJobService {
}

public async abort(pickJobId: string, version: number): Promise<PickJob> {
return await this.updateAction(pickJobId, version, PickJobAbortActionEnum.ABORT);
}

public async restart(pickJobId: string, version: number): Promise<PickJob> {
return await this.updateAction(pickJobId, version, PickJobRestartActionEnum.RESTART);
}

public async reset(pickJobId: string, version: number): Promise<PickJob> {
return await this.updateAction(pickJobId, version, PickJobResetActionEnum.RESET);
}

public async obsolete(pickJobId: string, version: number): Promise<PickJob> {
return await this.updateAction(pickJobId, version, PickJobObsoleteActionEnum.OBSOLETE);
}

public async updateAction(pickJobId: string, version: number, action: string): Promise<PickJob> {
try {
return await this.apiClient.patch<PickJob>(`${this.path}/${pickJobId}`, {
return await this.apiClient.post<PickJob>(`${this.path}/${pickJobId}/actions`, {
name: action,
version,
actions: [
{
action: 'AbortPickJob',
},
],
});
} catch (err) {
const httpError = err as ResponseError;
this.logger.error(
`Could not abort pick job with id '${pickJobId}' and version ${version}. Failed with status ${
`Could not update pick job with id '${pickJobId}' and version ${version}. Failed with status ${
httpError.status
}, error: ${httpError.response ? JSON.stringify(httpError.response.body) : ''}`
);
Expand Down Expand Up @@ -87,6 +107,7 @@ export class FftPickJobService {
throw err;
}
}

public async update(pickJob: PickJob, actions: AbstractModificationAction[]): Promise<PickJob> {
try {
return await this.apiClient.patch<PickJob>(`${this.path}/${pickJob.id}`, { version: pickJob.version, actions });
Expand Down

0 comments on commit b3bd8b8

Please sign in to comment.