fix(mood): write tag links inside the entry transaction

The mood entry was committed before createTagLinks/replaceTagLinks ran,
so a tag-link failure 5xx'd after the entry already existed and a client
retry minted a duplicate entry. Wrap the entry write and the tag-link
write in one $transaction and thread the tx client through both helpers
so they commit or roll back together.
This commit is contained in:
Marc-André Bombeck committed 2026-06-01 21:17:02 +02:00
1 parent fcf5114620
commit d5580917cc
4 files changed
+165 -38

No files matched your search

+24 -8
View File
@@ -1,3 +1,4 @@
import type { Prisma, PrismaClient } from "@/generated/prisma/client";
import { prisma } from "@/lib/db";
/**
@@ -7,16 +8,29 @@ import { prisma } from "@/lib/db";
* write the `mood_entry_tag_links` join for a mood entry. The catalog is
* the source of truth: unknown keys are dropped silently so a stale
* client can never mint a link to a tag the deployment doesn't carry.
*
* Every helper accepts an optional `db` client so the caller can thread
* the same `$transaction` client that wrote the entry — the entry row and
* its links then commit (or roll back) together.
*/
/**
* A Prisma client the helpers can run against: the singleton or a
* `$transaction` interactive client.
*/
type TagLinkDb = PrismaClient | Prisma.TransactionClient;
/**
* Resolve catalog tag keys to ids, dropping unknown / inactive keys.
* Returns the ids in catalog order (deduped).
*/
export async function resolveTagKeysToIds(keys: string[]): Promise<string[]> {
export async function resolveTagKeysToIds(
keys: string[],
db: TagLinkDb = prisma,
): Promise<string[]> {
const unique = Array.from(new Set(keys));
if (unique.length === 0) return [];
const rows = await prisma.moodTag.findMany({
const rows = await db.moodTag.findMany({
where: { key: { in: unique }, isActive: true },
select: { id: true },
});
@@ -30,10 +44,11 @@ export async function resolveTagKeysToIds(keys: string[]): Promise<string[]> {
export async function createTagLinks(
moodEntryId: string,
keys: string[],
db: TagLinkDb = prisma,
): Promise<void> {
const tagIds = await resolveTagKeysToIds(keys);
const tagIds = await resolveTagKeysToIds(keys, db);
if (tagIds.length === 0) return;
await prisma.moodEntryTagLink.createMany({
await db.moodEntryTagLink.createMany({
data: tagIds.map((moodTagId) => ({ moodEntryId, moodTagId })),
skipDuplicates: true,
});
@@ -48,9 +63,10 @@ export async function createTagLinks(
export async function replaceTagLinks(
moodEntryId: string,
keys: string[],
db: TagLinkDb = prisma,
): Promise<void> {
const desiredIds = new Set(await resolveTagKeysToIds(keys));
const existing = await prisma.moodEntryTagLink.findMany({
const desiredIds = new Set(await resolveTagKeysToIds(keys, db));
const existing = await db.moodEntryTagLink.findMany({
where: { moodEntryId },
select: { moodTagId: true },
});
@@ -60,12 +76,12 @@ export async function replaceTagLinks(
const toCreate = [...desiredIds].filter((id) => !existingIds.has(id));
if (toDelete.length > 0) {
await prisma.moodEntryTagLink.deleteMany({
await db.moodEntryTagLink.deleteMany({
where: { moodEntryId, moodTagId: { in: toDelete } },
});
}
if (toCreate.length > 0) {
await prisma.moodEntryTagLink.createMany({
await db.moodEntryTagLink.createMany({
data: toCreate.map((moodTagId) => ({ moodEntryId, moodTagId })),
skipDuplicates: true,
});