Skip to content

Commit

Permalink
Added post / delete / patch API routes to @monkvision/network
Browse files Browse the repository at this point in the history
  • Loading branch information
dlymonkai committed Oct 16, 2024
1 parent ee2ecce commit dcf2e41
Show file tree
Hide file tree
Showing 16 changed files with 740 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ function mockGetInspectionResponse(
severityResults: [],
vehicles: [],
views: [],
pricings: [],
},
};
takenSights.forEach((sight, index) => {
Expand Down
53 changes: 53 additions & 0 deletions packages/network/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,59 @@ Update the vehicle of an inspection.
|-----------|--------------------------------|-----------------------------|----------|
| options | UpdateInspectionVehicleOptions | The options of the request. | ✔️ |

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

MonkApi.createPricing(options, apiConfig, dispatch);
```

Create a new pricing of an inspection.

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

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

MonkApi.updatePricing(options, apiConfig, dispatch);
```

Update a pricing of an inspection.

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

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

MonkApi.deletePricing(options, apiConfig, dispatch);
```

Update a pricing of an inspection.

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

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

MonkApi.updateAdditionalData(options, apiConfig, dispatch);
```

Update the additional data of an inspection.

| Parameter | Type | Description | Required |
|-----------|-----------------------------|-----------------------------|----------|
| options | UpdateAdditionalDataOptions | 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
custom hook returns a custom version of the `MonkApi` object described in the section above, in which the requests do
Expand Down
7 changes: 6 additions & 1 deletion packages/network/src/api/api.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { getInspection, createInspection } from './inspection';
import { getInspection, createInspection, updateAdditionalData } from './inspection';
import { addImage } from './image';
import { startInspectionTasks, updateTaskStatus } from './task';
import { getLiveConfig } from './liveConfigs';
import { updateInspectionVehicle } from './vehicle';
import { createPricing, deletePricing, updatePricing } from './pricing';

/**
* Object regrouping the different API requests available to communicate with the API using the `@monkvision/network`
Expand All @@ -16,4 +17,8 @@ export const MonkApi = {
startInspectionTasks,
getLiveConfig,
updateInspectionVehicle,
updateAdditionalData,
createPricing,
deletePricing,
updatePricing,
};
36 changes: 20 additions & 16 deletions packages/network/src/api/inspection/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
MileageUnit,
MonkEntityType,
Part,
PricingV2,
PricingV2Details,
PricingV2RelatedItemType,
ProgressStatus,
Expand Down Expand Up @@ -197,6 +196,8 @@ function mapPricingV2Details(
const details = apiPricingV2Details as ApiPricingV2Details;
return {
inspectionId,
id: details.id,
entityType: MonkEntityType.PRICING,
relatedItemType: details.related_item_type as PricingV2RelatedItemType,
relatedItemId: details.related_item_id,
pricing: details.pricing,
Expand All @@ -205,22 +206,21 @@ function mapPricingV2Details(
};
}

function mapPricingV2(response: ApiInspectionGet): PricingV2 | undefined {
function mapPricingV2(response: ApiInspectionGet): {
pricings: PricingV2Details[];
pricingIds: string[];
} {
const pricings: PricingV2Details[] = [];
const pricingIds: string[] = [];

if (!response.pricing) {
return undefined;
return { pricings, pricingIds };
}
return {
details: response?.pricing.details
? Object.keys(response.pricing.details).reduce(
(prev, curr) => ({
...prev,
[curr]: mapPricingV2Details(response.pricing?.details[curr], response.id),
}),
{} as Record<string, PricingV2Details>,
)
: {},
totalPrice: response.pricing.total_price,
};
Object.values(response.pricing.details).forEach((details) => {
pricingIds.push(details.id);
pricings.push(mapPricingV2Details(details, response.id));
});
return { pricings, pricingIds };
}

function mapSeverityResultRepairOperation(
Expand Down Expand Up @@ -353,6 +353,7 @@ function mapInspection(
severityResultIds: string[];
taskIds: string[];
vehicleId?: string;
pricingIds: string[];
},
): Inspection {
return {
Expand All @@ -365,7 +366,7 @@ function mapInspection(
vehicle: ids.vehicleId,
wheelAnalysis: mapWheelAnalysis(response),
severityResults: ids.severityResultIds,
pricing: mapPricingV2(response),
pricings: ids.pricingIds,
additionalData: response.additional_data,
};
}
Expand All @@ -384,6 +385,7 @@ export function mapApiInspectionGet(
const { parts, partIds } = mapParts(response);
const { severityResults, severityResultIds } = mapSeverityResults(response);
const { tasks, taskIds } = mapTasks(response);
const { pricings, pricingIds } = mapPricingV2(response);
const vehicle = mapVehicle(response);
const inspection = mapInspection(response, {
imageIds,
Expand All @@ -393,6 +395,7 @@ export function mapApiInspectionGet(
partIds,
severityResultIds,
taskIds,
pricingIds,
});

return {
Expand All @@ -405,6 +408,7 @@ export function mapApiInspectionGet(
tasks,
vehicles: vehicle ? [vehicle] : [],
views,
pricings,
partOperations: [],
};
}
Expand Down
62 changes: 61 additions & 1 deletion packages/network/src/api/inspection/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import {
MonkActionType,
MonkGotOneInspectionAction,
MonkState,
MonkUpdatedOneInspectionAdditionalDataAction,
} from '@monkvision/common';
import { ComplianceOptions, CreateInspectionOptions } from '@monkvision/types';
import { AdditionalData, ComplianceOptions, CreateInspectionOptions } from '@monkvision/types';
import { Dispatch } from 'react';
import { getDefaultOptions, MonkApiConfig } from '../config';
import { ApiIdColumn, ApiInspectionGet } from '../models';
Expand Down Expand Up @@ -86,3 +87,62 @@ export async function createInspection(
body,
};
}

/**
* Options passed to the `updateAdditionalData` API request.
*/
export interface UpdateAdditionalDataOptions {
/**
* The ID of the inspection to update via the API.
*/
id: string;
/**
* Callback function that takes optional additional data and returns the updated additional data.
*/
callback: (additionalData?: AdditionalData) => AdditionalData;
}

/**
* Update the additional data of inspection with the given options.
* See the `UpdateAdditionalDataOptions` interface for more details.
*
* @param options The options of the request.
* @param config The API config.
* @param [dispatch] Optional MonkState dispatch function that you can pass if you want this request to handle React
* state management for you.
* @see UpdateAdditionalDataOptions
*/
export async function updateAdditionalData(
options: UpdateAdditionalDataOptions,
config: MonkApiConfig,
dispatch?: Dispatch<MonkUpdatedOneInspectionAdditionalDataAction>,
): Promise<MonkApiResponse> {
const { entities } = await getInspection({ id: options.id }, config);
const inspection = entities.inspections.find((i) => i.id === options.id);
if (!inspection) {
throw new Error('Inspection does not exist');
}
const newAdditionalData = options.callback(inspection.additionalData);
const newOptions = { id: options.id, additionalData: newAdditionalData };

try {
const kyOptions = getDefaultOptions(config);
const response = await ky.patch(`inspections/${options.id}`, {
...kyOptions,
json: { additional_data: newOptions.additionalData },
});
const body = await response.json<ApiIdColumn>();
dispatch?.({
type: MonkActionType.UPDATED_ONE_INSPECTION_ADDITIONAL_DATA,
payload: { inspectionId: newOptions.id, additionalData: newOptions.additionalData },
});
return {
id: body.id,
response,
body,
};
} catch (error) {
console.error('Error updating additional data:', error);
throw error;
}
}
1 change: 1 addition & 0 deletions packages/network/src/api/models/pricingV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export type ApiRepairOperationsTypes =
export type ApiHours = Record<string, number>;

export interface ApiPricingV2Details {
id: string;
hours?: ApiHours;
operations?: ApiRepairOperationsTypes[];
pricing?: number;
Expand Down
1 change: 1 addition & 0 deletions packages/network/src/api/pricing/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './requests';
23 changes: 23 additions & 0 deletions packages/network/src/api/pricing/mappers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {
MonkEntityType,
PricingV2Details,
PricingV2RelatedItemType,
RepairOperationType,
} from '@monkvision/types';
import { ApiPricingV2Details } from '../models';

export function mapApiPricingPost(
inspectionId: string,
response: ApiPricingV2Details,
): PricingV2Details {
return {
inspectionId,
id: response.id,
entityType: MonkEntityType.PRICING,
relatedItemType: response.related_item_type as PricingV2RelatedItemType,
relatedItemId: response.related_item_id,
pricing: response.pricing,
operations: response.operations as RepairOperationType[] | undefined,
hours: response.hours,
};
}
Loading

0 comments on commit dcf2e41

Please sign in to comment.