diff --git a/src/helper/commit.ts b/src/helper/commit.ts index 4197a9a..61f28d9 100644 --- a/src/helper/commit.ts +++ b/src/helper/commit.ts @@ -1,11 +1,20 @@ -import Entry from "../Entry"; +import { Entry } from "../Entry"; import { mongoTransaction } from "./mongoTransaction"; import { IJournal } from "../models/journal"; +import { MediciError } from "../errors"; export async function commit(...entries: Entry[]): Promise { - let journals: IJournal[] = []; - await mongoTransaction(async (session) => { - journals = await Promise.all(entries.map((entry) => entry.commit({ session }))); - }); - return journals; + if (!entries.length) throw new MediciError("Nothing to commit. At least one entry must be provided."); + + let committedJournals: IJournal[] = []; + + if (entries.length === 1) { + committedJournals.push(await entries[0].commit()); + } else { + await mongoTransaction(async (session) => { + committedJournals = await Promise.all(entries.map((entry) => entry.commit({ session }))); + }); + } + + return committedJournals; }