Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add entry controller tests #212

Merged
merged 10 commits into from
Jan 10, 2025
17 changes: 10 additions & 7 deletions backend/src/controllers/entry/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const getAllEntries = async (
) => {
const { journalId } = req.params;

const entries = await EntryServices.getAllEntriesInJournal(journalId);
const entries = await EntryServices.getAllEntries(journalId);

if (entries.length === 0) {
req.flash('info', 'Submit your first entry to get started.');
Expand Down Expand Up @@ -158,7 +158,7 @@ export const getEntryAnalysis = async (
return next(new ExpressError('Entry analysis not found.', 404));
}

res.status(200).json(entryAnalysis.toObject());
res.status(200).json(entryAnalysis);
};

/**
Expand All @@ -183,7 +183,7 @@ export const updateEntryAnalysis = async (
res
.status(200)
.json({
...entryAnalysis.toObject(),
...entryAnalysis,
entry: entry.toObject(),
flash: req.flash(),
});
Expand All @@ -204,7 +204,7 @@ export const getEntryConversation = async (req: Request, res: Response) => {

const response = await EntryServices.getEntryConversation(entryId);
const entryConversation = response
? { ...response.toObject(), chatId: response.id }
? { ...response, chatId: response.id }
: {};

res.status(200).json(entryConversation);
Expand All @@ -224,13 +224,12 @@ export const createEntryConversation = async (
try {
const configId = await verifyJournalExists(journalId);

const response = await EntryServices.createEntryConversation(
const entryConversation = await EntryServices.createEntryConversation(
entryId,
configId,
messageData
);

const entryConversation = response ? response.toObject() : {};
req.flash('success', 'Successfully created conversation.');
res.status(201).json({ ...entryConversation, flash: req.flash() });
} catch (error) {
Expand All @@ -253,7 +252,11 @@ export const updateEntryConversation = async (
const configId = await verifyJournalExists(journalId);

const response = await EntryServices.updateEntryConversation(chatId, configId, messageData);
res.status(200).json({ ...response.toObject(), flash: req.flash() });

/* FIXME: No flash messages are attached to prevent over-displaying--this
* seems like something the frontend should handle
*/
res.status(200).json({ ...response, flash: req.flash() });
} catch (error) {
return next(error);
}
Expand Down
2 changes: 1 addition & 1 deletion backend/src/models/services/entry/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ interface EntryResponse {
* @param journalId Journal._id as string
* @returns array of documents in journal with journalId
*/
export async function getAllEntriesInJournal(
export async function getAllEntries(
journalId: string
): Promise<HydratedDocument<EntryType>[]> {
return await Entry.find({ journal: journalId });
Expand Down
Loading