Skip to content

Commit deed3c2

Browse files
authored
Added Pricing API routes (#855)
* Added state management for pricing api routes * Added post / delete / patch API routes to @monkvision/network
1 parent 56cd03c commit deed3c2

34 files changed

+1297
-51
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { PricingV2 } from '@monkvision/types';
2+
import { MonkAction, MonkActionType } from './monkAction';
3+
import { MonkState } from '../state';
4+
5+
/**
6+
* The payload of a MonkCreatedOnePricingPayload.
7+
*/
8+
export interface MonkCreatedOnePricingPayload {
9+
/**
10+
* The pricing created.
11+
*/
12+
pricing: PricingV2;
13+
}
14+
15+
/**
16+
* Action dispatched when a vehicle have been updated.
17+
*/
18+
export interface MonkCreatedOnePricingAction extends MonkAction {
19+
/**
20+
* The type of the action : `MonkActionType.CREATED_ONE_PRICING`.
21+
*/
22+
type: MonkActionType.CREATED_ONE_PRICING;
23+
/**
24+
* The payload of the action containing the fetched entities.
25+
*/
26+
payload: MonkCreatedOnePricingPayload;
27+
}
28+
29+
/**
30+
* Matcher function that matches a CreatedOnePricing while also inferring its type using TypeScript's type predicate
31+
* feature.
32+
*/
33+
export function isCreatedOnePricingAction(
34+
action: MonkAction,
35+
): action is MonkCreatedOnePricingAction {
36+
return action.type === MonkActionType.CREATED_ONE_PRICING;
37+
}
38+
39+
/**
40+
* Reducer function for a createdOnePricing action.
41+
*/
42+
export function createdOnePricing(
43+
state: MonkState,
44+
action: MonkCreatedOnePricingAction,
45+
): MonkState {
46+
const { pricings, inspections } = state;
47+
const { payload } = action;
48+
49+
const inspection = inspections.find((value) => value.id === payload.pricing.inspectionId);
50+
if (inspection) {
51+
inspection.pricings?.push(action.payload.pricing.id);
52+
}
53+
pricings.push(action.payload.pricing);
54+
return {
55+
...state,
56+
pricings: [...pricings],
57+
inspections: [...inspections],
58+
};
59+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { MonkAction, MonkActionType } from './monkAction';
2+
import { MonkState } from '../state';
3+
4+
/**
5+
* The payload of a MonkDeletedOnePricingPayload.
6+
*/
7+
export interface MonkDeletedtedOnePricingPayload {
8+
/**
9+
* The ID of the inspection to which the pricing was deleted.
10+
*/
11+
inspectionId: string;
12+
/**
13+
* The pricing ID deleted.
14+
*/
15+
pricingId: string;
16+
}
17+
18+
/**
19+
* Action dispatched when a pricing have been deleted.
20+
*/
21+
export interface MonkDeletedOnePricingAction extends MonkAction {
22+
/**
23+
* The type of the action : `MonkActionType.DELETED_ONE_PRICING`.
24+
*/
25+
type: MonkActionType.DELETED_ONE_PRICING;
26+
/**
27+
* The payload of the action containing the fetched entities.
28+
*/
29+
payload: MonkDeletedtedOnePricingPayload;
30+
}
31+
32+
/**
33+
* Matcher function that matches a DeletedOnePricing while also inferring its type using TypeScript's type predicate
34+
* feature.
35+
*/
36+
export function isDeletedOnePricingAction(
37+
action: MonkAction,
38+
): action is MonkDeletedOnePricingAction {
39+
return action.type === MonkActionType.DELETED_ONE_PRICING;
40+
}
41+
42+
/**
43+
* Reducer function for a deletedOnePricing action.
44+
*/
45+
export function deletedOnePricing(
46+
state: MonkState,
47+
action: MonkDeletedOnePricingAction,
48+
): MonkState {
49+
const { pricings, inspections } = state;
50+
const { payload } = action;
51+
52+
const inspection = inspections.find((value) => value.id === payload.inspectionId);
53+
if (inspection) {
54+
inspection.pricings = inspection.pricings?.filter(
55+
(pricingId) => pricingId !== payload.pricingId,
56+
);
57+
}
58+
const newPricings = pricings.filter((pricing) => pricing.id !== payload.pricingId);
59+
return {
60+
...state,
61+
pricings: newPricings,
62+
inspections: [...inspections],
63+
};
64+
}

packages/common/src/state/actions/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@ export * from './gotOneInspection';
44
export * from './createdOneImage';
55
export * from './updatedManyTasks';
66
export * from './updatedVehicle';
7+
export * from './createdOnePricing';
8+
export * from './deletedOnePricing';
9+
export * from './updatedOnePricing';
10+
export * from './updatedOneInspectionAdditionalData';

packages/common/src/state/actions/monkAction.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ export enum MonkActionType {
66
* An inspection has been fetched from the API.
77
*/
88
GOT_ONE_INSPECTION = 'got_one_inspection',
9+
/**
10+
* An inspection additional data has been updated.
11+
*/
12+
UPDATED_ONE_INSPECTION_ADDITIONAL_DATA = 'updated_one_inspection_additional_data',
913
/**
1014
* An image has been uploaded to the API.
1115
*/
@@ -14,14 +18,26 @@ export enum MonkActionType {
1418
* One or more tasks have been updated.
1519
*/
1620
UPDATED_MANY_TASKS = 'updated_many_tasks',
17-
/**
18-
* Clear and reset the state.
19-
*/
20-
RESET_STATE = 'reset_state',
2121
/**
2222
* A vehicle has been updated.
2323
*/
2424
UPDATED_VEHICLE = 'updated_vehicle',
25+
/**
26+
* A pricing has been uploaded to the API.
27+
*/
28+
CREATED_ONE_PRICING = 'created_one_pricing',
29+
/**
30+
* A pricing has been updated.
31+
*/
32+
UPDATED_ONE_PRICING = 'updated_one_pricing',
33+
/**
34+
* A pricing has been deleted.
35+
*/
36+
DELETED_ONE_PRICING = 'deleted_one_pricing',
37+
/**
38+
* Clear and reset the state.
39+
*/
40+
RESET_STATE = 'reset_state',
2541
}
2642

2743
/**
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { AdditionalData } from '@monkvision/types';
2+
import { MonkAction, MonkActionType } from './monkAction';
3+
import { MonkState } from '../state';
4+
5+
/**
6+
* The payload of a MonkUpdatedOneInspectionAdditionalDataPayload.
7+
*/
8+
export interface MonkUpdatedOneInspectionAdditionalDataPayload {
9+
/**
10+
* The ID of the inspection to which the pricing was updated.
11+
*/
12+
inspectionId: string;
13+
/**
14+
* Additional data used for the update operation.
15+
*/
16+
additionalData?: AdditionalData;
17+
}
18+
19+
/**
20+
* Action dispatched when a inspection have been updated.
21+
*/
22+
export interface MonkUpdatedOneInspectionAdditionalDataAction extends MonkAction {
23+
/**
24+
* The type of the action : `MonkActionType.UPDATED_ONE_INSPECTION_ADDITIONAL_DATA`.
25+
*/
26+
type: MonkActionType.UPDATED_ONE_INSPECTION_ADDITIONAL_DATA;
27+
/**
28+
* The payload of the action containing the fetched entities.
29+
*/
30+
payload: MonkUpdatedOneInspectionAdditionalDataPayload;
31+
}
32+
33+
/**
34+
* Matcher function that matches a UpdatedOneInspection while also inferring its type using TypeScript's type predicate
35+
* feature.
36+
*/
37+
export function isUpdatedOneInspectionAdditionalDataAction(
38+
action: MonkAction,
39+
): action is MonkUpdatedOneInspectionAdditionalDataAction {
40+
return action.type === MonkActionType.UPDATED_ONE_INSPECTION_ADDITIONAL_DATA;
41+
}
42+
43+
/**
44+
* Reducer function for a UpdatedOneInspection action.
45+
*/
46+
export function updatedOneInspectionAdditionalData(
47+
state: MonkState,
48+
action: MonkUpdatedOneInspectionAdditionalDataAction,
49+
): MonkState {
50+
const { inspections } = state;
51+
const { payload } = action;
52+
53+
const inspection = inspections.find((value) => value.id === payload.inspectionId);
54+
if (inspection) {
55+
inspection.additionalData = payload.additionalData;
56+
}
57+
return {
58+
...state,
59+
inspections: [...inspections],
60+
};
61+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { PricingV2 } from '@monkvision/types';
2+
import { MonkAction, MonkActionType } from './monkAction';
3+
import { MonkState } from '../state';
4+
5+
/**
6+
* The payload of a MonkUpdatedOnePricingPayload.
7+
*/
8+
export interface MonkUpdatedOnePricingPayload {
9+
/**
10+
* The pricing created.
11+
*/
12+
pricing: PricingV2;
13+
}
14+
15+
/**
16+
* Action dispatched when a pricing have been updated.
17+
*/
18+
export interface MonkUpdatedOnePricingAction extends MonkAction {
19+
/**
20+
* The type of the action : `MonkActionType.UPDATED_ONE_PRICING`.
21+
*/
22+
type: MonkActionType.UPDATED_ONE_PRICING;
23+
/**
24+
* The payload of the action containing the fetched entities.
25+
*/
26+
payload: MonkUpdatedOnePricingPayload;
27+
}
28+
29+
/**
30+
* Matcher function that matches a updatedOnePricing while also inferring its type using TypeScript's type predicate
31+
* feature.
32+
*/
33+
export function isUpdatedOnePricingAction(
34+
action: MonkAction,
35+
): action is MonkUpdatedOnePricingAction {
36+
return action.type === MonkActionType.UPDATED_ONE_PRICING;
37+
}
38+
39+
/**
40+
* Reducer function for a updatedOnePricing action.
41+
*/
42+
export function updatedOnePricing(
43+
state: MonkState,
44+
action: MonkUpdatedOnePricingAction,
45+
): MonkState {
46+
const { pricings } = state;
47+
const { payload } = action;
48+
49+
const updatedPricings = pricings.map((pricing) =>
50+
pricing.id === payload.pricing.id ? { ...pricing, ...payload.pricing } : pricing,
51+
);
52+
return {
53+
...state,
54+
pricings: updatedPricings,
55+
};
56+
}

packages/common/src/state/reducer.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,19 @@ import {
55
isGotOneInspectionAction,
66
isResetStateAction,
77
isUpdatedManyTasksAction,
8+
isCreatedOnePricingAction,
9+
isDeletedOnePricingAction,
10+
isUpdatedOnePricingAction,
11+
isUpdatedOneInspectionAdditionalDataAction,
12+
isUpdatedVehicleAction,
813
MonkAction,
914
resetState,
1015
updatedManyTasks,
16+
createdOnePricing,
17+
deletedOnePricing,
18+
updatedOnePricing,
19+
updatedOneInspectionAdditionalData,
20+
updatedVehicle,
1121
} from './actions';
1222
import { MonkState } from './state';
1323

@@ -21,11 +31,26 @@ export function monkReducer(state: MonkState, action: MonkAction): MonkState {
2131
if (isGotOneInspectionAction(action)) {
2232
return gotOneInspection(state, action);
2333
}
34+
if (isUpdatedOneInspectionAdditionalDataAction(action)) {
35+
return updatedOneInspectionAdditionalData(state, action);
36+
}
2437
if (isCreatedOneImageAction(action)) {
2538
return createdOneImage(state, action);
2639
}
2740
if (isUpdatedManyTasksAction(action)) {
2841
return updatedManyTasks(state, action);
2942
}
43+
if (isCreatedOnePricingAction(action)) {
44+
return createdOnePricing(state, action);
45+
}
46+
if (isDeletedOnePricingAction(action)) {
47+
return deletedOnePricing(state, action);
48+
}
49+
if (isUpdatedOnePricingAction(action)) {
50+
return updatedOnePricing(state, action);
51+
}
52+
if (isUpdatedVehicleAction(action)) {
53+
return updatedVehicle(state, action);
54+
}
3055
return state;
3156
}

packages/common/src/state/state.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
Inspection,
55
Part,
66
PartOperation,
7+
PricingV2,
78
RenderedOutput,
89
SeverityResult,
910
Task,
@@ -55,6 +56,10 @@ export interface MonkState {
5556
* The views created during inspections.
5657
*/
5758
views: View[];
59+
/**
60+
* The pricings created during inspections.
61+
*/
62+
pricings: PricingV2[];
5863
}
5964

6065
/**
@@ -72,5 +77,6 @@ export function createEmptyMonkState(): MonkState {
7277
tasks: [],
7378
vehicles: [],
7479
views: [],
80+
pricings: [],
7581
};
7682
}

0 commit comments

Comments
 (0)