diff --git a/apps/server/src/lib/badges/index.test.ts b/apps/server/src/lib/badges/index.test.ts index 44cdd52a..df6d35a3 100644 --- a/apps/server/src/lib/badges/index.test.ts +++ b/apps/server/src/lib/badges/index.test.ts @@ -11,6 +11,7 @@ describe("awardAllBadgeXp", () => { await fixtures.Badge({ name: "Test", + tracker: "bottle", checks: [ { type: "everyTasting", @@ -109,10 +110,10 @@ describe("awardAllBadgeXp", () => { maxLevel: 10, }); - for (const region of [regionKy, regionTn, regionTx, regionHi]) { + for (const region of [regionKy, regionTn, regionTx, regionHi, null]) { const brand = await fixtures.Entity({ - regionId: region.id, - countryId: region.countryId, + regionId: region ? region.id : null, + countryId: region ? region.countryId : countryUs.id, }); const tasting = await createTastingForBadge( fixtures, diff --git a/apps/server/src/lib/badges/index.ts b/apps/server/src/lib/badges/index.ts index e7f6d73a..2f6e725a 100644 --- a/apps/server/src/lib/badges/index.ts +++ b/apps/server/src/lib/badges/index.ts @@ -225,6 +225,11 @@ async function awardXp( } } + if (!trackedObjects.length) { + console.info(`[badges] Badge ${badge.id} did not track any objects.`); + return; + } + return await db.transaction(async (tx) => { let [award] = await tx .insert(badgeAwards) @@ -244,25 +249,21 @@ async function awardXp( .returning(); let count = 0; - if (trackedObjects.length) { - for (const target of trackedObjects) { - const query = await tx - .insert(badgeAwardTrackedObjects) - .values({ - awardId: award.id, - objectType: target.type, - objectId: target.id, - }) - .onConflictDoNothing(); - if (query.rowCount) { - count += query.rowCount; - if (query.rowCount > 1) { - throw new Error("wtf"); - } + for (const target of trackedObjects) { + const query = await tx + .insert(badgeAwardTrackedObjects) + .values({ + awardId: award.id, + objectType: target.type, + objectId: target.id, + }) + .onConflictDoNothing(); + if (query.rowCount) { + count += query.rowCount; + if (query.rowCount > 1) { + throw new Error("wtf"); } } - } else { - count += 1; } // there were no new entries