-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
23 changed files
with
902 additions
and
13 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
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,25 @@ | ||
export type CarRentalPlanningDTO = { | ||
id: string; | ||
pickupDateTime: Date; | ||
dropOffDateTime: Date; | ||
} | ||
|
||
export type CarPlanningDTO = { | ||
rentals: CarRentalPlanningDTO[]; | ||
}; | ||
|
||
type Cursor = { | ||
nextPage: null | string; | ||
} | ||
|
||
type CarPlanningGroupType = { | ||
[key: string]: CarPlanningDTO; | ||
} | ||
|
||
type CarsPlanningDTO = { | ||
cars: CarPlanningGroupType; | ||
cursor: Cursor; | ||
}; | ||
|
||
export default CarsPlanningDTO; | ||
|
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,6 @@ | ||
type DecodedCursor = { | ||
order: string; | ||
address: string; | ||
} | ||
|
||
export default DecodedCursor; |
19 changes: 19 additions & 0 deletions
19
src/core/useCases/car/query/retrieveCarsPlanning/authorizer.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,19 @@ | ||
import UserIsNotAuthorizedToRetrieveCarsPlanningError | ||
from "src/core/useCases/car/query/retrieveCarsPlanning/exceptions/notAuthorized"; | ||
import PermissionsGatewayInterface from "src/core/useCases/common/interfaces/gateways/permissions"; | ||
|
||
export default class RetrieveCarsPlanningAuthorizer { | ||
private readonly permissionsGateway: PermissionsGatewayInterface; | ||
|
||
constructor({permissionsGateway}: { permissionsGateway: PermissionsGatewayInterface }) { | ||
this.permissionsGateway = permissionsGateway; | ||
} | ||
|
||
async authorize({actorId}: { actorId: string }): Promise<void> { | ||
const permissions = await this.permissionsGateway.getUserPermissions({userId: actorId}); | ||
|
||
if (permissions.length === 0) { | ||
throw new UserIsNotAuthorizedToRetrieveCarsPlanningError(); | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
src/core/useCases/car/query/retrieveCarsPlanning/exceptions/notAuthorized.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,3 @@ | ||
export default class UserIsNotAuthorizedToRetrieveCarsPlanningError extends Error { | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
src/core/useCases/car/query/retrieveCarsPlanning/handler.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,32 @@ | ||
import CarsPlanningDTO from "src/core/domain/car/outputBoundaries/outputBoundary"; | ||
import CarReadRepositoryInterface from "src/core/domain/car/interfaces/repositories/read"; | ||
import RetrieveCarsPlanningQuery from "src/core/useCases/car/query/retrieveCarsPlanning/types/query"; | ||
import RetrieveCarsPlanningAuthorizer from "src/core/useCases/car/query/retrieveCarsPlanning/authorizer"; | ||
import {decodeCursor} from "src/core/useCases/common/cursors/encodeDecode"; | ||
|
||
export default class RetrieveCarsPlanning { | ||
private readonly carReadRepository: CarReadRepositoryInterface; | ||
|
||
private readonly authorizer: RetrieveCarsPlanningAuthorizer; | ||
|
||
constructor({ | ||
carReadRepository, | ||
authorizer | ||
}: { carReadRepository: CarReadRepositoryInterface, authorizer: RetrieveCarsPlanningAuthorizer }) { | ||
this.carReadRepository = carReadRepository; | ||
this.authorizer = authorizer; | ||
} | ||
|
||
async execute(query: RetrieveCarsPlanningQuery): Promise<CarsPlanningDTO> { | ||
await this.authorizer.authorize({ | ||
actorId: query.actor.id, | ||
}); | ||
|
||
return this.carReadRepository.getCarsPlanning({ | ||
startDate: query.startDate, | ||
endDate: query.endDate, | ||
limit: query.limit, | ||
cursor: decodeCursor(query.cursor), | ||
}); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/core/useCases/car/query/retrieveCarsPlanning/types/query.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,13 @@ | ||
type Actor = { | ||
id: string; | ||
} | ||
|
||
type RetrieveCarsPlanningQuery = { | ||
startDate: Date; | ||
endDate: Date; | ||
limit: number; | ||
cursor: string; | ||
actor: Actor; | ||
}; | ||
|
||
export default RetrieveCarsPlanningQuery; |
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,36 @@ | ||
import DecodedCursor from "src/core/domain/common/types/cursor"; | ||
|
||
/** | ||
* Encodes a cursor address to a base64 format. | ||
* | ||
* @param cursor The cursor address to encode | ||
* @param order The order of the cursor (gte for next, lte for prev) | ||
*/ | ||
export const encodeCursor = (cursor: string, order: string): string => { | ||
if (cursor === ''){ | ||
return ''; | ||
} | ||
return btoa(`${order === 'gte' ? 'next' : 'prev'}___${cursor}`); | ||
} | ||
|
||
/** | ||
* Decodes a cursor address from a base64 format. | ||
* | ||
* @param cursor The cursor address to decode | ||
*/ | ||
export const decodeCursor = (cursor: string): DecodedCursor => { | ||
if (cursor === '') { | ||
return { | ||
order: 'gte', | ||
address: '' | ||
} | ||
} | ||
// eslint-disable-next-line prefer-regex-literals | ||
const cursorMatch = atob(cursor).match(new RegExp('^(?<order>(next|prev))___(?<address>[\\w\'-]+)$')) as RegExpMatchArray; | ||
const groups = cursorMatch.groups as any; | ||
|
||
return { | ||
order: groups.order === 'next' ? 'gte' : 'lte', | ||
address: groups.address as string, | ||
} | ||
} |
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,5 @@ | ||
import UserPermissionsProfile from "src/core/useCases/common/permissions/types/userPermissionsProfile"; | ||
|
||
export default interface PermissionsGatewayInterface { | ||
getUserPermissions({userId}: { userId: string }): Promise<UserPermissionsProfile[]>; | ||
} |
4 changes: 4 additions & 0 deletions
4
src/core/useCases/common/permissions/types/userPermissionsProfile.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,4 @@ | ||
type UserPermissionsProfile = { | ||
} | ||
|
||
export default UserPermissionsProfile; |
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
type InMemoryCar = { | ||
id: string; | ||
licensePlate: string; | ||
modelId: string; | ||
} | ||
|
||
|
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,15 @@ | ||
import PermissionsGatewayInterface from "src/core/useCases/common/interfaces/gateways/permissions"; | ||
import UserPermissionsProfile from "src/core/useCases/common/permissions/types/userPermissionsProfile"; | ||
|
||
export default class PermissionsStubGateway implements PermissionsGatewayInterface { | ||
private readonly permissionsToProvide: UserPermissionsProfile[]; | ||
|
||
constructor(permissionsToProvide: UserPermissionsProfile[]) { | ||
this.permissionsToProvide = permissionsToProvide; | ||
} | ||
|
||
async getUserPermissions(): Promise<UserPermissionsProfile[]> { | ||
return this.permissionsToProvide; | ||
} | ||
|
||
} |
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,9 @@ | ||
import PermissionsGatewayInterface from "src/core/useCases/common/interfaces/gateways/permissions"; | ||
import UserPermissionsProfile from "src/core/useCases/common/permissions/types/userPermissionsProfile"; | ||
|
||
export default class PermissionsDenyStubGateway implements PermissionsGatewayInterface { | ||
// @ts-ignore | ||
async getUserPermissions(): Promise<UserPermissionsProfile[]> { | ||
return []; | ||
} | ||
} |
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,30 @@ | ||
import {container} from "tsyringe"; | ||
import CarsPlanningDTO from "src/core/domain/car/outputBoundaries/outputBoundary"; | ||
import DateParser from "tests/utils/dateParser"; | ||
import {CarTestCaseEntry} from "tests/unit/utils/testCase.types"; | ||
|
||
export const buildExpectedCarsPlanning = (cars: CarTestCaseEntry[], nextCursor: string, prevCursor: null | string): CarsPlanningDTO => { | ||
const dateParser: DateParser = container.resolve("DateParser"); | ||
const expectedPlanning: CarsPlanningDTO = { | ||
cars: {}, | ||
cursor: { | ||
nextPage: nextCursor, | ||
} | ||
} | ||
for (const carTestCaseEntry of cars) { | ||
expectedPlanning.cars[carTestCaseEntry.id] = { | ||
rentals: [] | ||
} | ||
for (const rentalEntry of carTestCaseEntry.rentals) { | ||
expectedPlanning.cars[carTestCaseEntry.id].rentals.push( | ||
{ | ||
id: rentalEntry.id, | ||
pickupDateTime: dateParser.parse(rentalEntry.pickupDateTime), | ||
dropOffDateTime: dateParser.parse(rentalEntry.dropOffDateTime), | ||
} | ||
) | ||
} | ||
} | ||
|
||
return expectedPlanning; | ||
} |
Oops, something went wrong.