Skip to content

Commit

Permalink
Fix new behavior which guarantees objects tracked
Browse files Browse the repository at this point in the history
  • Loading branch information
dcramer committed Aug 2, 2024
1 parent 204c2bf commit 7f77c4f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
7 changes: 4 additions & 3 deletions apps/server/src/lib/badges/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe("awardAllBadgeXp", () => {

await fixtures.Badge({
name: "Test",
tracker: "bottle",
checks: [
{
type: "everyTasting",
Expand Down Expand Up @@ -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,
Expand Down
35 changes: 18 additions & 17 deletions apps/server/src/lib/badges/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 7f77c4f

Please sign in to comment.