Skip to content

Commit

Permalink
Added Pricing API routes (#855)
Browse files Browse the repository at this point in the history
* Added state management for pricing api routes

* Added post / delete / patch API routes to @monkvision/network
  • Loading branch information
dlymonkai authored Oct 16, 2024
1 parent 56cd03c commit deed3c2
Show file tree
Hide file tree
Showing 34 changed files with 1,297 additions and 51 deletions.
59 changes: 59 additions & 0 deletions packages/common/src/state/actions/createdOnePricing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { PricingV2 } from '@monkvision/types';
import { MonkAction, MonkActionType } from './monkAction';
import { MonkState } from '../state';

/**
* The payload of a MonkCreatedOnePricingPayload.
*/
export interface MonkCreatedOnePricingPayload {
/**
* The pricing created.
*/
pricing: PricingV2;
}

/**
* Action dispatched when a vehicle have been updated.
*/
export interface MonkCreatedOnePricingAction extends MonkAction {
/**
* The type of the action : `MonkActionType.CREATED_ONE_PRICING`.
*/
type: MonkActionType.CREATED_ONE_PRICING;
/**
* The payload of the action containing the fetched entities.
*/
payload: MonkCreatedOnePricingPayload;
}

/**
* Matcher function that matches a CreatedOnePricing while also inferring its type using TypeScript's type predicate
* feature.
*/
export function isCreatedOnePricingAction(
action: MonkAction,
): action is MonkCreatedOnePricingAction {
return action.type === MonkActionType.CREATED_ONE_PRICING;
}

/**
* Reducer function for a createdOnePricing action.
*/
export function createdOnePricing(
state: MonkState,
action: MonkCreatedOnePricingAction,
): MonkState {
const { pricings, inspections } = state;
const { payload } = action;

const inspection = inspections.find((value) => value.id === payload.pricing.inspectionId);
if (inspection) {
inspection.pricings?.push(action.payload.pricing.id);
}
pricings.push(action.payload.pricing);
return {
...state,
pricings: [...pricings],
inspections: [...inspections],
};
}
64 changes: 64 additions & 0 deletions packages/common/src/state/actions/deletedOnePricing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { MonkAction, MonkActionType } from './monkAction';
import { MonkState } from '../state';

/**
* The payload of a MonkDeletedOnePricingPayload.
*/
export interface MonkDeletedtedOnePricingPayload {
/**
* The ID of the inspection to which the pricing was deleted.
*/
inspectionId: string;
/**
* The pricing ID deleted.
*/
pricingId: string;
}

/**
* Action dispatched when a pricing have been deleted.
*/
export interface MonkDeletedOnePricingAction extends MonkAction {
/**
* The type of the action : `MonkActionType.DELETED_ONE_PRICING`.
*/
type: MonkActionType.DELETED_ONE_PRICING;
/**
* The payload of the action containing the fetched entities.
*/
payload: MonkDeletedtedOnePricingPayload;
}

/**
* Matcher function that matches a DeletedOnePricing while also inferring its type using TypeScript's type predicate
* feature.
*/
export function isDeletedOnePricingAction(
action: MonkAction,
): action is MonkDeletedOnePricingAction {
return action.type === MonkActionType.DELETED_ONE_PRICING;
}

/**
* Reducer function for a deletedOnePricing action.
*/
export function deletedOnePricing(
state: MonkState,
action: MonkDeletedOnePricingAction,
): MonkState {
const { pricings, inspections } = state;
const { payload } = action;

const inspection = inspections.find((value) => value.id === payload.inspectionId);
if (inspection) {
inspection.pricings = inspection.pricings?.filter(
(pricingId) => pricingId !== payload.pricingId,
);
}
const newPricings = pricings.filter((pricing) => pricing.id !== payload.pricingId);
return {
...state,
pricings: newPricings,
inspections: [...inspections],
};
}
4 changes: 4 additions & 0 deletions packages/common/src/state/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ export * from './gotOneInspection';
export * from './createdOneImage';
export * from './updatedManyTasks';
export * from './updatedVehicle';
export * from './createdOnePricing';
export * from './deletedOnePricing';
export * from './updatedOnePricing';
export * from './updatedOneInspectionAdditionalData';
24 changes: 20 additions & 4 deletions packages/common/src/state/actions/monkAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ export enum MonkActionType {
* An inspection has been fetched from the API.
*/
GOT_ONE_INSPECTION = 'got_one_inspection',
/**
* An inspection additional data has been updated.
*/
UPDATED_ONE_INSPECTION_ADDITIONAL_DATA = 'updated_one_inspection_additional_data',
/**
* An image has been uploaded to the API.
*/
Expand All @@ -14,14 +18,26 @@ export enum MonkActionType {
* One or more tasks have been updated.
*/
UPDATED_MANY_TASKS = 'updated_many_tasks',
/**
* Clear and reset the state.
*/
RESET_STATE = 'reset_state',
/**
* A vehicle has been updated.
*/
UPDATED_VEHICLE = 'updated_vehicle',
/**
* A pricing has been uploaded to the API.
*/
CREATED_ONE_PRICING = 'created_one_pricing',
/**
* A pricing has been updated.
*/
UPDATED_ONE_PRICING = 'updated_one_pricing',
/**
* A pricing has been deleted.
*/
DELETED_ONE_PRICING = 'deleted_one_pricing',
/**
* Clear and reset the state.
*/
RESET_STATE = 'reset_state',
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { AdditionalData } from '@monkvision/types';
import { MonkAction, MonkActionType } from './monkAction';
import { MonkState } from '../state';

/**
* The payload of a MonkUpdatedOneInspectionAdditionalDataPayload.
*/
export interface MonkUpdatedOneInspectionAdditionalDataPayload {
/**
* The ID of the inspection to which the pricing was updated.
*/
inspectionId: string;
/**
* Additional data used for the update operation.
*/
additionalData?: AdditionalData;
}

/**
* Action dispatched when a inspection have been updated.
*/
export interface MonkUpdatedOneInspectionAdditionalDataAction extends MonkAction {
/**
* The type of the action : `MonkActionType.UPDATED_ONE_INSPECTION_ADDITIONAL_DATA`.
*/
type: MonkActionType.UPDATED_ONE_INSPECTION_ADDITIONAL_DATA;
/**
* The payload of the action containing the fetched entities.
*/
payload: MonkUpdatedOneInspectionAdditionalDataPayload;
}

/**
* Matcher function that matches a UpdatedOneInspection while also inferring its type using TypeScript's type predicate
* feature.
*/
export function isUpdatedOneInspectionAdditionalDataAction(
action: MonkAction,
): action is MonkUpdatedOneInspectionAdditionalDataAction {
return action.type === MonkActionType.UPDATED_ONE_INSPECTION_ADDITIONAL_DATA;
}

/**
* Reducer function for a UpdatedOneInspection action.
*/
export function updatedOneInspectionAdditionalData(
state: MonkState,
action: MonkUpdatedOneInspectionAdditionalDataAction,
): MonkState {
const { inspections } = state;
const { payload } = action;

const inspection = inspections.find((value) => value.id === payload.inspectionId);
if (inspection) {
inspection.additionalData = payload.additionalData;
}
return {
...state,
inspections: [...inspections],
};
}
56 changes: 56 additions & 0 deletions packages/common/src/state/actions/updatedOnePricing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { PricingV2 } from '@monkvision/types';
import { MonkAction, MonkActionType } from './monkAction';
import { MonkState } from '../state';

/**
* The payload of a MonkUpdatedOnePricingPayload.
*/
export interface MonkUpdatedOnePricingPayload {
/**
* The pricing created.
*/
pricing: PricingV2;
}

/**
* Action dispatched when a pricing have been updated.
*/
export interface MonkUpdatedOnePricingAction extends MonkAction {
/**
* The type of the action : `MonkActionType.UPDATED_ONE_PRICING`.
*/
type: MonkActionType.UPDATED_ONE_PRICING;
/**
* The payload of the action containing the fetched entities.
*/
payload: MonkUpdatedOnePricingPayload;
}

/**
* Matcher function that matches a updatedOnePricing while also inferring its type using TypeScript's type predicate
* feature.
*/
export function isUpdatedOnePricingAction(
action: MonkAction,
): action is MonkUpdatedOnePricingAction {
return action.type === MonkActionType.UPDATED_ONE_PRICING;
}

/**
* Reducer function for a updatedOnePricing action.
*/
export function updatedOnePricing(
state: MonkState,
action: MonkUpdatedOnePricingAction,
): MonkState {
const { pricings } = state;
const { payload } = action;

const updatedPricings = pricings.map((pricing) =>
pricing.id === payload.pricing.id ? { ...pricing, ...payload.pricing } : pricing,
);
return {
...state,
pricings: updatedPricings,
};
}
25 changes: 25 additions & 0 deletions packages/common/src/state/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,19 @@ import {
isGotOneInspectionAction,
isResetStateAction,
isUpdatedManyTasksAction,
isCreatedOnePricingAction,
isDeletedOnePricingAction,
isUpdatedOnePricingAction,
isUpdatedOneInspectionAdditionalDataAction,
isUpdatedVehicleAction,
MonkAction,
resetState,
updatedManyTasks,
createdOnePricing,
deletedOnePricing,
updatedOnePricing,
updatedOneInspectionAdditionalData,
updatedVehicle,
} from './actions';
import { MonkState } from './state';

Expand All @@ -21,11 +31,26 @@ export function monkReducer(state: MonkState, action: MonkAction): MonkState {
if (isGotOneInspectionAction(action)) {
return gotOneInspection(state, action);
}
if (isUpdatedOneInspectionAdditionalDataAction(action)) {
return updatedOneInspectionAdditionalData(state, action);
}
if (isCreatedOneImageAction(action)) {
return createdOneImage(state, action);
}
if (isUpdatedManyTasksAction(action)) {
return updatedManyTasks(state, action);
}
if (isCreatedOnePricingAction(action)) {
return createdOnePricing(state, action);
}
if (isDeletedOnePricingAction(action)) {
return deletedOnePricing(state, action);
}
if (isUpdatedOnePricingAction(action)) {
return updatedOnePricing(state, action);
}
if (isUpdatedVehicleAction(action)) {
return updatedVehicle(state, action);
}
return state;
}
6 changes: 6 additions & 0 deletions packages/common/src/state/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Inspection,
Part,
PartOperation,
PricingV2,
RenderedOutput,
SeverityResult,
Task,
Expand Down Expand Up @@ -55,6 +56,10 @@ export interface MonkState {
* The views created during inspections.
*/
views: View[];
/**
* The pricings created during inspections.
*/
pricings: PricingV2[];
}

/**
Expand All @@ -72,5 +77,6 @@ export function createEmptyMonkState(): MonkState {
tasks: [],
vehicles: [],
views: [],
pricings: [],
};
}
Loading

0 comments on commit deed3c2

Please sign in to comment.