-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also: - Readd /event-log link to admin page. - Support using query string parameters in query routes.
- Loading branch information
Showing
10 changed files
with
121 additions
and
32 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
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,28 +1,58 @@ | ||
import {Params} from '../query'; | ||
import {pipe} from 'fp-ts/lib/function'; | ||
import {User} from '../../types'; | ||
import {DomainEvent, User} from '../../types'; | ||
import {Dependencies} from '../../dependencies'; | ||
import * as TE from 'fp-ts/TaskEither'; | ||
import {ViewModel} from './view-model'; | ||
import {ViewModel, LogSearch} from './view-model'; | ||
import * as RA from 'fp-ts/ReadonlyArray'; | ||
import {readModels} from '../../read-models'; | ||
import {failureWithStatus} from '../../types/failure-with-status'; | ||
import {StatusCodes} from 'http-status-codes'; | ||
|
||
export const constructViewModel = (deps: Dependencies) => (user: User) => | ||
pipe( | ||
deps.getAllEvents(), | ||
TE.filterOrElse(readModels.superUsers.is(user.memberNumber), () => | ||
failureWithStatus( | ||
'You do not have the necessary permission to see this page.', | ||
StatusCodes.FORBIDDEN | ||
)() | ||
), | ||
TE.map(RA.reverse), | ||
TE.map( | ||
events => | ||
({ | ||
function parseQueryToLogSearch(query: Params): LogSearch { | ||
const rawOffset = query['offset']; | ||
const offsetAsNumber = Number(rawOffset); | ||
const offset = isNaN(offsetAsNumber) ? 0 : offsetAsNumber; | ||
|
||
const maxLimit = 100; | ||
const rawLimit = query['limit']; | ||
const limitAsNumber = Number(rawLimit); | ||
const limit = isNaN(limitAsNumber) ? maxLimit : limitAsNumber; | ||
|
||
return { | ||
offset: Math.max(0, offset), | ||
limit: Math.min(maxLimit, limit), | ||
}; | ||
} | ||
|
||
const applyLogSearch = | ||
(search: LogSearch) => (events: ReadonlyArray<DomainEvent>) => | ||
pipe(events, events => { | ||
const start = search.offset; | ||
const end = search.offset + search.limit; | ||
return events.slice(start, end); | ||
}); | ||
|
||
export const constructViewModel = | ||
(deps: Dependencies) => (user: User) => (queryParams: Params) => | ||
pipe( | ||
deps.getAllEvents(), | ||
TE.filterOrElse(readModels.superUsers.is(user.memberNumber), () => | ||
failureWithStatus( | ||
'You do not have the necessary permission to see this page.', | ||
StatusCodes.FORBIDDEN | ||
)() | ||
), | ||
TE.map(RA.reverse), | ||
TE.map(allEvents => { | ||
const count = allEvents.length; | ||
const search = parseQueryToLogSearch(queryParams); | ||
const events = applyLogSearch(search)(allEvents); | ||
return { | ||
count, | ||
events, | ||
user, | ||
}) satisfies ViewModel | ||
) | ||
); | ||
search, | ||
} satisfies ViewModel; | ||
}) | ||
); |
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 |
---|---|---|
@@ -1,6 +1,13 @@ | ||
import {DomainEvent, User} from '../../types'; | ||
|
||
export interface LogSearch { | ||
offset: number; | ||
limit: number; | ||
} | ||
|
||
export type ViewModel = { | ||
user: User; | ||
count: number; | ||
search: LogSearch; | ||
events: ReadonlyArray<DomainEvent>; | ||
}; |
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