Skip to content

Instantly share code, notes, and snippets.

@the-frey
Created November 27, 2020 14:54
Show Gist options
  • Save the-frey/8ccfcfc44b966451aa2857f514f54073 to your computer and use it in GitHub Desktop.
Save the-frey/8ccfcfc44b966451aa2857f514f54073 to your computer and use it in GitHub Desktop.
const sharedPipeline = (event: requests.MinimalRequestEvent): RTE.ReaderTaskEither<db.System, t.Error, st.DbSave[]> =>
Do(RTE.readerTaskEither)
.bind('userId', RTE.fromEither(requests.getIdFromPath(event)))
.bindL('user', ({ userId }) => checkIdAgainstHeaderRTE(event, userId))
.bindL('savesRes', ({ user }) => sDb.getSavesByUserIdRTE(user.uuid_id))
.bindL('saves', ({ savesRes }) => RTE.fromEither(sXfms.getSavesFromResE(savesRes)))
.return(({ saves }) => saves);
const uasPipeline = (event: requests.MinimalRequestEvent): RTE.ReaderTaskEither<db.System, t.Error, t.Entity> =>
Do(RTE.readerTaskEither)
.bind('saves', sharedPipeline(event))
.bindL('activityUuids', ({ saves }) => RTE.fromEither(sXfms.getEntityIdsForTypeE(t.EntityType.activity, saves)))
.bindL('activities', ({ activityUuids }) => aDb.getPublishedActivitiesByIdsRTE(activityUuids))
.bindL('serialisedActivities', ({ activities }) => RTE.fromEither(aXfms.dbActivitiesToCCE(activities)))
.return(({ serialisedActivities }) => { return { entityType: t.EntityType.activities, entity: serialisedActivities }; });
const ugsPipeline = (event: requests.MinimalRequestEvent): RTE.ReaderTaskEither<db.System, t.Error, t.Entity> =>
Do(RTE.readerTaskEither)
.bind('saves', sharedPipeline(event))
.bindL('groupUuids', ({ saves }) => RTE.fromEither(sXfms.getEntityIdsForTypeE(t.EntityType.group, saves)))
.bindL('groups', ({ groupUuids }) => gDb.getPublishedGroupsByIdsRTE(groupUuids))
.bindL('serialisedGroups', ({ groups }) => RTE.fromEither(gXfms.dbGroupsToCCE(groups)))
.return(({ serialisedGroups }) => { return { entityType: t.EntityType.groups, entity: serialisedGroups }; });
const main = (event: requests.MinimalRequestEvent, pipelineFn: (e: requests.MinimalRequestEvent) => RTE.ReaderTaskEither<db.System, t.Error, t.Entity>): RT.ReaderTask<db.System, responses.Response> =>
pipe(
pipelineFn(event),
RTE.fold(
errRes => RT.of(responses.errorToResponse(errRes)),
userRes => RT.of(responses.entityToResponse(userRes))
));
export const usersActivitiesSaved = async (event: requests.MinimalRequestEvent): Promise<responses.Response> => {
const system: db.System = await db.getSystem();
const client = system.pgclient;
try {
const response = await main(event, uasPipeline)(system)();
return response;
} catch (err) {
console.log(err);
return responses.respond500('project/error', 'Something went wrong');
} finally {
await db.closeDbConn(client);
}
};
export const usersGroupsSaved = async (event: requests.MinimalRequestEvent): Promise<responses.Response> => {
const system: db.System = await db.getSystem();
const client = system.pgclient;
try {
const response = await main(event, ugsPipeline)(system)();
return response;
} catch (err) {
console.log(err);
return responses.respond500('project/error', 'Something went wrong');
} finally {
await db.closeDbConn(client);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment