Skip to content

Commit

Permalink
Further simplify form-get after removal of getAllEvents #86
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Haarhoff committed Nov 25, 2024
1 parent 39c7551 commit 48ae1f1
Showing 1 changed file with 11 additions and 24 deletions.
35 changes: 11 additions & 24 deletions src/http/form-get.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,38 @@
import {Request, Response} from 'express';
import * as E from 'fp-ts/Either';
import * as O from 'fp-ts/Option';
import * as TE from 'fp-ts/TaskEither';
import {pipe} from 'fp-ts/lib/function';
import {StatusCodes} from 'http-status-codes';
import {getUserFromSession} from '../authentication';
import {Dependencies} from '../dependencies';
import {oopsPage, pageTemplate} from '../templates';
import {sequenceS} from 'fp-ts/lib/Apply';
import {Form} from '../types/form';
import {failureWithStatus} from '../types/failure-with-status';
import {CompleteHtmlDocument, sanitizeString} from '../types/html';
import {logInPath} from '../authentication/auth-routes';

const getUser = (req: Request, deps: Dependencies) =>
pipe(
req.session,
getUserFromSession(deps),
TE.fromOption(() =>
failureWithStatus('You are not logged in.', StatusCodes.UNAUTHORIZED)()
)
);

// See formPost for a more indepth discussion about the design decisions around why this is how it is.
// formGet is like formPost but rather than processing a command formGet handles calling a read model to
// get a view of the current state of a resource. This should be completely pure because its read-only and
// is where conflict resolution etc. is handled as described in form-post.
export const formGet =
<T>(deps: Dependencies, form: Form<T>) =>
async (req: Request, res: Response<CompleteHtmlDocument>) => {
(req: Request, res: Response<CompleteHtmlDocument>) => {
const user = getUserFromSession(deps)(req.session);
const isSuperUser = false;
if (O.isNone(user)) {
res.redirect(logInPath);
return;
}
await pipe(
const isSuperUser = false;
pipe(
{
user: getUser(req, deps),
user: user.value,
readModel: deps.sharedReadModel,
},
sequenceS(TE.ApplyPar),
TE.let('readModel', () => deps.sharedReadModel),
TE.chainEitherK(form.constructForm({...req.query, ...req.params})),
TE.map(form.renderForm),
TE.map(({title, body}) =>
form.constructForm({...req.query, ...req.params}),
E.map(form.renderForm),
E.map(({title, body}) =>
pageTemplate(title, user.value, isSuperUser)(body)
),
TE.matchW(
E.matchW(
failure => {
deps.logger.error(failure, 'Failed to show form to a user');
res
Expand All @@ -54,5 +41,5 @@ export const formGet =
},
page => res.status(200).send(page)
)
)();
);
};

0 comments on commit 48ae1f1

Please sign in to comment.