cognovi-ai / the-cdj

The Cognitive Distortion Journal (CDJ) is a smart journaling tool that helps remedy distorted thinking. It can feel impossible to follow the CBT technique of labeling and reframing distorted thinking when you've been thinking this way your whole life. The CDJ can help with that 🧠✨ -- v2 is in development!
https://thecdj.app
4 stars 0 forks source link

refactor!: create service layers for create, update, and delete operations #192

Closed davidzlu closed 3 days ago

davidzlu commented 1 week ago

This PR contains the following changes:

Entry Controller

The entry controller was a very large file and difficult to test as it combined code for performing CRUD operations on entries with Express error handling, request parsing, and response creation. This PR made the following changes:

Build error in app.ts

There was a tsc error where the user type in the passport and and passport-local-mongoose modules didn't match. I realized this was caused by the way Express.User was getting defined in our custom type declaration file. I changed it so that Express.User extends our UserType in models to resolve the build error.

Refactoring EntryAnalysis and EntryConversation

These models had the functions getAnalysisContent and getChatContent respectively as the interface for getting content from the LLM. However, the functions seemed to overlap a lot and were awkward fits in the model, so I moved them to a new service layer that would act as the interface for all LLM operations, /models/services/CdGpt.ts. I made the following changes:

Jest setup script

To support deletes, I had to deploy mongodb as a replica set rather than a standalone instance. This works, but occasionally I get a ECONNRESET error on cleanup. This doesn't affect any of the unit tests, and only occurs after all of them have run. I think it may have something to do with Jest closing before all of the replica instances close, but I haven't confirmed it. This error doesn't appear to break anything in the test suite, and deploying memory mongodb as a replica set is necessary to support deletes.

davidzlu commented 3 days ago

See #195 instead