Skip to content

Commit

Permalink
Fix issues with getAllInspections requests (#886)
Browse files Browse the repository at this point in the history
* Fixed wrong export for inspection API types

* Updated getAllInspections return types
  • Loading branch information
souyahia-monk authored Nov 22, 2024
1 parent 11cefd6 commit 655d51d
Show file tree
Hide file tree
Showing 16 changed files with 691 additions and 130 deletions.
20 changes: 10 additions & 10 deletions packages/network/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,33 +210,33 @@ Delete a damage of an inspection.
|-----------|---------------------|-----------------------------|----------|
| options | DeleteDamageOptions | The options of the request. | ✔️ |

### getInspections
### getAllInspections
```typescript
import { MonkApi } from '@monkvision/network';

MonkApi.getInspections(options, apiConfig, dispatch);
MonkApi.getAllInspections(options, apiConfig, dispatch);
```

Fetch the details of multiple inspections using the provided filters. The resulting action of this request will contain
a list of all entities that match the specified criteria.

| Parameter | Type | Description | Required |
|-----------|-----------------------|-----------------------------|----------|
| options | getInspectionsOptions | The options of the request. | ✔️ |
| Parameter | Type | Description | Required |
|-----------|--------------------------|-----------------------------|----------|
| options | GetAllInspectionsOptions | The options of the request. | ✔️ |


### getInspectionsCount
### getAllInspectionsCount
```typescript
import { MonkApi } from '@monkvision/network';

MonkApi.getInspectionsCount(options, apiConfig, dispatch);
MonkApi.getAllInspectionsCount(options, apiConfig, dispatch);
```

Gets the count of inspections that match the given filters.

| Parameter | Type | Description | Required |
|-----------|-----------------------|-----------------------------|----------|
| options | getInspectionsOptions | The options of the request. | ✔️ |
| Parameter | Type | Description | Required |
|-----------|--------------------------|-----------------------------|----------|
| options | GetAllInspectionsOptions | The options of the request. | ✔️ |

# React Tools
In order to simply integrate the Monk Api requests into your React app, you can make use of the `useMonkApi` hook. This
Expand Down
8 changes: 4 additions & 4 deletions packages/network/src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import {
getInspection,
createInspection,
updateAdditionalData,
getInspections,
getInspectionsCount,
getAllInspections,
getAllInspectionsCount,
} from './inspection';
import { addImage } from './image';
import { startInspectionTasks, updateTaskStatus } from './task';
Expand All @@ -18,8 +18,8 @@ import { createDamage, deleteDamage } from './damage';
*/
export const MonkApi = {
getInspection,
getInspections,
getInspectionsCount,
getAllInspections,
getAllInspectionsCount,
createInspection,
addImage,
updateTaskStatus,
Expand Down
8 changes: 7 additions & 1 deletion packages/network/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ export { MonkNetworkError, type MonkHTTPError } from './error';
export { MonkApi } from './api';
export * from './hooks';

export { type GetInspectionOptions, type GetInspectionResponse } from './inspection';
export {
type GetInspectionOptions,
type GetInspectionResponse,
type GetAllInspectionsCountResponse,
type UpdateAdditionalDataOptions,
type GetAllInspectionsResponse,
} from './inspection';
export {
type AddImageResponse,
type AddBeautyShotImageOptions,
Expand Down
137 changes: 84 additions & 53 deletions packages/network/src/api/inspection/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,34 @@ import {
WheelName,
} from '@monkvision/types';
import {
ApiAllInspectionsVerboseGet,
ApiCommentSeverityValue,
ApiDamageDetectionTaskPostComponent,
type ApiDamageIds,
ApiHinlTaskPostComponent,
ApiImageRegion,
ApiImagesOCRTaskPostComponent,
ApiInspectionGet,
ApiInspectionPost,
ApiInspectionsGet,
ApiPartSeverityValue,
ApiPricingTaskPostComponent,
ApiPricingV2Details,
type ApiRelatedImages,
ApiRenderedOutput,
type ApiRenderedOutputs,
ApiSeverityResult,
ApiTasksComponent,
ApiView,
type ApiViews,
ApiWheelAnalysisTaskPostComponent,
} from '../models';
import { mapApiImage } from '../image/mappers';
import { sdkVersion } from '../config';

function mapDamages(response: ApiInspectionGet): { damages: Damage[]; damageIds: string[] } {
function mapDamages(response: ApiInspectionGet | ApiAllInspectionsVerboseGet): {
damages: Damage[];
damageIds: string[];
} {
const damages: Damage[] = [];
const damageIds: string[] = [];
response.damages?.forEach((damage) => {
Expand All @@ -65,7 +72,12 @@ function mapDamages(response: ApiInspectionGet): { damages: Damage[]; damageIds:
type: damage.damage_type as DamageType,
size: damage.damage_size_cm,
parts: damage.part_ids ?? [],
relatedImages: damage.related_images?.map((relatedImage) => relatedImage.base_image_id) ?? [],
relatedImages:
('related_images' in damage
? (damage.related_images as ApiRelatedImages | undefined)?.map(
(relatedImage) => relatedImage.base_image_id,
)
: []) ?? [],
});
damageIds.push(damage.id);
});
Expand Down Expand Up @@ -120,7 +132,7 @@ function mapView(view: ApiView): { view: View; renderedOutputs: RenderedOutput[]
}

function mapImages(
response: ApiInspectionGet,
response: ApiInspectionGet | ApiAllInspectionsVerboseGet,
thumbnailDomain: string,
complianceOptions?: ComplianceOptions,
): {
Expand All @@ -142,20 +154,24 @@ function mapImages(
const imageRenderedOutputs: string[] = [];
const imageViews: string[] = [];

image.rendered_outputs?.forEach((renderedOutput) => {
renderedOutputIds.push(renderedOutput.id);
imageRenderedOutputs.push(renderedOutput.id);
renderedOutputs.push(mapRenderedOutput(renderedOutput));
});
if ('rendered_outputs' in image) {
(image.rendered_outputs as ApiRenderedOutputs | undefined)?.forEach((renderedOutput) => {
renderedOutputIds.push(renderedOutput.id);
imageRenderedOutputs.push(renderedOutput.id);
renderedOutputs.push(mapRenderedOutput(renderedOutput));
});
}

image.views?.forEach((apiView) => {
const { view, renderedOutputs: viewRenderedOutputs } = mapView(apiView);
viewIds.push(view.id);
imageViews.push(view.id);
views.push(view);
renderedOutputs.push(...viewRenderedOutputs);
renderedOutputIds.push(...view.renderedOutputs);
});
if ('views' in image) {
(image.views as ApiViews | undefined)?.forEach((apiView) => {
const { view, renderedOutputs: viewRenderedOutputs } = mapView(apiView);
viewIds.push(view.id);
imageViews.push(view.id);
views.push(view);
renderedOutputs.push(...viewRenderedOutputs);
renderedOutputIds.push(...view.renderedOutputs);
});
}

imageIds.push(image.id);
images.push({
Expand All @@ -175,7 +191,10 @@ function mapImages(
};
}

function mapParts(response: ApiInspectionGet): { parts: Part[]; partIds: string[] } {
function mapParts(response: ApiInspectionGet | ApiAllInspectionsVerboseGet): {
parts: Part[];
partIds: string[];
} {
const parts: Part[] = [];
const partIds: string[] = [];

Expand All @@ -186,8 +205,13 @@ function mapParts(response: ApiInspectionGet): { parts: Part[]; partIds: string[
entityType: MonkEntityType.PART,
inspectionId: response.id,
type: part.part_type as VehiclePart,
damages: part.damage_ids ?? [],
relatedImages: part.related_images?.map((relatedImage) => relatedImage.base_image_id) ?? [],
damages: ('damage_ids' in part ? (part.damage_ids as ApiDamageIds | undefined) : []) ?? [],
relatedImages:
('related_images' in part
? (part.related_images as ApiRelatedImages | undefined)?.map(
(relatedImage) => relatedImage.base_image_id,
)
: []) ?? [],
});
});
return { partIds, parts };
Expand All @@ -210,7 +234,7 @@ function mapPricingV2Details(
};
}

function mapPricingV2(response: ApiInspectionGet): {
function mapPricingV2(response: ApiInspectionGet | ApiAllInspectionsVerboseGet): {
pricings: PricingV2[];
pricingIds: string[];
} {
Expand Down Expand Up @@ -300,7 +324,7 @@ function mapTasks(response: ApiInspectionGet): { tasks: Task[]; taskIds: string[
return { taskIds, tasks };
}

function mapVehicle(response: ApiInspectionGet): Vehicle | undefined {
function mapVehicle(response: ApiInspectionGet | ApiAllInspectionsVerboseGet): Vehicle | undefined {
return response?.vehicle
? {
id: response.vehicle.id,
Expand Down Expand Up @@ -333,24 +357,28 @@ function mapVehicle(response: ApiInspectionGet): Vehicle | undefined {
: undefined;
}

function mapWheelAnalysis(response: ApiInspectionGet): WheelAnalysis[] {
function mapWheelAnalysis(
response: ApiInspectionGet | ApiAllInspectionsVerboseGet,
): WheelAnalysis[] {
return (
response.wheel_analysis?.map((wheelAnalysis) => ({
inspectionId: response.id,
rimCondition: wheelAnalysis.rim_condition,
rimMaterial: wheelAnalysis.rim_material,
rimVisualAspect: wheelAnalysis.rim_visual_aspect,
hubcapOverRim: wheelAnalysis.hubcap_over_rim,
hubcapCondition: wheelAnalysis.hubcap_condition,
hubcapVisualAspect: wheelAnalysis.hubcap_visual_aspect,
imageId: wheelAnalysis.image_id,
wheelName: wheelAnalysis.wheel_name as WheelName | undefined,
})) ?? []
('wheel_analysis' in response
? response.wheel_analysis?.map((wheelAnalysis) => ({
inspectionId: response.id,
rimCondition: wheelAnalysis.rim_condition,
rimMaterial: wheelAnalysis.rim_material,
rimVisualAspect: wheelAnalysis.rim_visual_aspect,
hubcapOverRim: wheelAnalysis.hubcap_over_rim,
hubcapCondition: wheelAnalysis.hubcap_condition,
hubcapVisualAspect: wheelAnalysis.hubcap_visual_aspect,
imageId: wheelAnalysis.image_id,
wheelName: wheelAnalysis.wheel_name as WheelName | undefined,
}))
: []) ?? []
);
}

function mapInspection(
response: ApiInspectionGet,
response: ApiInspectionGet | ApiAllInspectionsVerboseGet,
ids: {
imageIds: string[];
renderedOutputIds: string[];
Expand Down Expand Up @@ -378,8 +406,8 @@ function mapInspection(
};
}

export function mapApiInspectionsGet(
response: ApiInspectionsGet,
export function mapApiAllInspectionsVerboseGet(
data: ApiAllInspectionsVerboseGet[],
thumbnailDomain: string,
): MonkState {
const state: MonkState = {
Expand All @@ -395,19 +423,16 @@ export function mapApiInspectionsGet(
pricings: [],
partOperations: [],
};
if (!response.data) {
return state;
}
return response.data.reduce<MonkState>((acc, inspection) => {
return data.reduce<MonkState>((acc, inspection) => {
const { images, renderedOutputs, imageIds, renderedOutputIds, viewIds } = mapImages(
inspection as ApiInspectionGet,
inspection,
thumbnailDomain,
);
const { damages, damageIds } = mapDamages(inspection as ApiInspectionGet);
const { parts, partIds } = mapParts(inspection as ApiInspectionGet);
const { pricings, pricingIds } = mapPricingV2(inspection as ApiInspectionGet);
const vehicle = mapVehicle(inspection as ApiInspectionGet);
const mappedInspection = mapInspection(inspection as ApiInspectionGet, {
const { damages, damageIds } = mapDamages(inspection);
const { parts, partIds } = mapParts(inspection);
const { pricings, pricingIds } = mapPricingV2(inspection);
const vehicle = mapVehicle(inspection);
const mappedInspection = mapInspection(inspection, {
imageIds,
renderedOutputIds,
viewIds,
Expand Down Expand Up @@ -665,9 +690,9 @@ export interface SortRequestParams {
}

/**
* Options passed to the `getInspections` or `getInspectionsCount` API request.
* Options passed to the `getAllInspections` or `getAllInspectionsCount` API request.
*/
export interface GetInspectionsOptions {
export interface GetAllInspectionsOptions {
/**
* The filter request parameters.
*/
Expand All @@ -682,13 +707,19 @@ export interface GetInspectionsOptions {
sort?: SortRequestParams;
}

export function mapApiInspectionsUrlParamsGet(options: GetInspectionsOptions): string {
export function mapApiAllInspectionsUrlParamsGet(
options: GetAllInspectionsOptions,
verbose: boolean,
): string {
const params = new URLSearchParams();
const url = options.filters || options.pagination ? '?' : '';
params.append('verbose', verbose ? '1' : '0');

const ignoredFilters = ['verbose'];
if (options.filters) {
Object.entries(options.filters).forEach(([key, value]) => {
params.append(key, value.toString());
if (!ignoredFilters.includes(key)) {
params.append(key, value.toString());
}
});
}
if (options.pagination) {
Expand All @@ -702,5 +733,5 @@ export function mapApiInspectionsUrlParamsGet(options: GetInspectionsOptions): s
if (options.sort?.sortOrder) {
params.append('pagination_order', options.sort.sortOrder.toString());
}
return `${url}${params.toString()}`;
return `?${params.toString()}`;
}
Loading

0 comments on commit 655d51d

Please sign in to comment.