-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added API create / delete damage routes (#859)
* Added API create / delete damage routes * Added state management for create and delete damage
- Loading branch information
Showing
23 changed files
with
600 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { Damage } from '@monkvision/types'; | ||
import { MonkAction, MonkActionType } from './monkAction'; | ||
import { MonkState } from '../state'; | ||
|
||
/** | ||
* The payload of a MonkCreatedOneDamagePayload. | ||
*/ | ||
export interface MonkCreatedOneDamagePayload { | ||
/** | ||
* The damage created. | ||
*/ | ||
damage: Damage; | ||
} | ||
|
||
/** | ||
* Action dispatched when a vehicle have been updated. | ||
*/ | ||
export interface MonkCreatedOneDamageAction extends MonkAction { | ||
/** | ||
* The type of the action : `MonkActionType.CREATED_ONE_DAMAGE`. | ||
*/ | ||
type: MonkActionType.CREATED_ONE_DAMAGE; | ||
/** | ||
* The payload of the action containing the fetched entities. | ||
*/ | ||
payload: MonkCreatedOneDamagePayload; | ||
} | ||
|
||
/** | ||
* Matcher function that matches a CreatedOneDamage while also inferring its type using TypeScript's type predicate | ||
* feature. | ||
*/ | ||
export function isCreatedOneDamageAction(action: MonkAction): action is MonkCreatedOneDamageAction { | ||
return action.type === MonkActionType.CREATED_ONE_DAMAGE; | ||
} | ||
|
||
/** | ||
* Reducer function for a createdOneDamage action. | ||
*/ | ||
export function createdOneDamage(state: MonkState, action: MonkCreatedOneDamageAction): MonkState { | ||
const { damages, inspections, parts } = state; | ||
const { payload } = action; | ||
|
||
const inspection = inspections.find((value) => value.id === payload.damage.inspectionId); | ||
if (inspection) { | ||
inspection.damages.push(action.payload.damage.id); | ||
} | ||
const partsRelated = action.payload.damage.parts | ||
.map((part) => parts.find((value) => value.type === part)?.id) | ||
.filter((v) => v !== undefined) as string[]; | ||
damages.push({ ...action.payload.damage, parts: partsRelated }); | ||
return { | ||
...state, | ||
damages: [...damages], | ||
inspections: [...inspections], | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { MonkAction, MonkActionType } from './monkAction'; | ||
import { MonkState } from '../state'; | ||
|
||
/** | ||
* The payload of a MonkDeletedOneDamagePayload. | ||
*/ | ||
export interface MonkDeletedOneDamagePayload { | ||
/** | ||
* The ID of the inspection to which the damage was deleted. | ||
*/ | ||
inspectionId: string; | ||
/** | ||
* The damage ID deleted. | ||
*/ | ||
damageId: string; | ||
} | ||
|
||
/** | ||
* Action dispatched when a vehicle have been updated. | ||
*/ | ||
export interface MonkDeletedOneDamageAction extends MonkAction { | ||
/** | ||
* The type of the action : `MonkActionType.DELETED_ONE_DAMAGE`. | ||
*/ | ||
type: MonkActionType.DELETED_ONE_DAMAGE; | ||
/** | ||
* The payload of the action containing the fetched entities. | ||
*/ | ||
payload: MonkDeletedOneDamagePayload; | ||
} | ||
|
||
/** | ||
* Matcher function that matches a DeletedOneDamage while also inferring its type using TypeScript's type predicate | ||
* feature. | ||
*/ | ||
export function isDeletedOneDamageAction(action: MonkAction): action is MonkDeletedOneDamageAction { | ||
return action.type === MonkActionType.DELETED_ONE_DAMAGE; | ||
} | ||
|
||
/** | ||
* Reducer function for a deletedOneDamage action. | ||
*/ | ||
export function deletedOneDamage(state: MonkState, action: MonkDeletedOneDamageAction): MonkState { | ||
const { damages, inspections } = state; | ||
const { payload } = action; | ||
|
||
const inspection = inspections.find((value) => value.id === payload.inspectionId); | ||
if (inspection) { | ||
inspection.damages = inspection.damages?.filter((damageId) => damageId !== payload.damageId); | ||
} | ||
const newDamages = damages.filter((damage) => damage.id !== payload.damageId); | ||
return { | ||
...state, | ||
damages: [...newDamages], | ||
inspections: [...inspections], | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
packages/common/test/state/actions/createdOneDamage.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { | ||
createEmptyMonkState, | ||
MonkActionType, | ||
createdOneDamage, | ||
isCreatedOneDamageAction, | ||
MonkCreatedOneDamageAction, | ||
} from '../../../src'; | ||
import { Inspection, MonkEntityType, VehiclePart, DamageType, Part } from '@monkvision/types'; | ||
|
||
const action: MonkCreatedOneDamageAction = { | ||
type: MonkActionType.CREATED_ONE_DAMAGE, | ||
payload: { | ||
damage: { | ||
entityType: MonkEntityType.DAMAGE, | ||
id: 'test-id', | ||
inspectionId: 'inspections-test', | ||
parts: [VehiclePart.ROOF], | ||
relatedImages: [], | ||
type: DamageType.SCRATCH, | ||
}, | ||
}, | ||
}; | ||
|
||
describe('CreatedOneDamage action handlers', () => { | ||
describe('Action matcher', () => { | ||
it('should return true if the action has the proper type', () => { | ||
expect(isCreatedOneDamageAction({ type: MonkActionType.CREATED_ONE_DAMAGE })).toBe(true); | ||
}); | ||
|
||
it('should return false if the action does not have the proper type', () => { | ||
expect(isCreatedOneDamageAction({ type: MonkActionType.RESET_STATE })).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('Action handler', () => { | ||
it('should return a new state', () => { | ||
const state = createEmptyMonkState(); | ||
expect(Object.is(createdOneDamage(state, action), state)).toBe(false); | ||
}); | ||
|
||
it('should create damage in the state', () => { | ||
const state = createEmptyMonkState(); | ||
const part = { | ||
id: 'part-id', | ||
type: VehiclePart.ROOF, | ||
}; | ||
state.inspections.push({ | ||
id: 'inspections-test', | ||
damages: [] as string[], | ||
} as Inspection); | ||
state.parts.push(part as Part); | ||
const newState = createdOneDamage(state, action); | ||
const inspectionDamage = newState.inspections.find( | ||
(ins) => ins.id === action.payload.damage.inspectionId, | ||
)?.damages; | ||
|
||
expect(inspectionDamage?.length).toBe(1); | ||
expect(inspectionDamage).toContainEqual(action.payload.damage.id); | ||
expect(newState.damages).toContainEqual({ | ||
...action.payload.damage, | ||
parts: [part.id], | ||
}); | ||
}); | ||
}); | ||
}); |
53 changes: 53 additions & 0 deletions
53
packages/common/test/state/actions/deletedOneDamage.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { | ||
createEmptyMonkState, | ||
MonkActionType, | ||
isDeletedOneDamageAction, | ||
deletedOneDamage, | ||
MonkDeletedOneDamageAction, | ||
} from '../../../src'; | ||
import { Inspection } from '@monkvision/types'; | ||
|
||
const action: MonkDeletedOneDamageAction = { | ||
type: MonkActionType.DELETED_ONE_DAMAGE, | ||
payload: { | ||
inspectionId: 'inspections-test', | ||
damageId: 'pricing-id-test', | ||
}, | ||
}; | ||
|
||
describe('DeletedOneDamage action handlers', () => { | ||
describe('Action matcher', () => { | ||
it('should return true if the action has the proper type', () => { | ||
expect(isDeletedOneDamageAction({ type: MonkActionType.DELETED_ONE_DAMAGE })).toBe(true); | ||
}); | ||
|
||
it('should return false if the action does not have the proper type', () => { | ||
expect(isDeletedOneDamageAction({ type: MonkActionType.RESET_STATE })).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('Action handler', () => { | ||
it('should return a new state', () => { | ||
const state = createEmptyMonkState(); | ||
expect(Object.is(deletedOneDamage(state, action), state)).toBe(false); | ||
}); | ||
|
||
it('should delete damage in the state', () => { | ||
const state = createEmptyMonkState(); | ||
state.inspections.push({ | ||
id: 'inspections-test', | ||
damages: [action.payload.damageId] as string[], | ||
} as Inspection); | ||
const newState = deletedOneDamage(state, action); | ||
const inspectionDamage = newState.inspections.find( | ||
(ins) => ins.id === action.payload.inspectionId, | ||
)?.damages; | ||
|
||
expect(inspectionDamage?.length).toBe(0); | ||
expect(inspectionDamage).not.toContainEqual(action.payload.damageId); | ||
expect( | ||
newState.damages.find((damage) => damage.id === action.payload.damageId), | ||
).toBeUndefined(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.