Skip to content

Commit

Permalink
Fix mergeEntity behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
dcramer committed Aug 3, 2024
1 parent 51b1a6f commit 7020ef1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
34 changes: 34 additions & 0 deletions apps/server/src/worker/jobs/mergeEntity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,37 @@ test("merge duplicate bottle", async ({ fixtures }) => {
expect(newBottleB).toBeDefined();
expect(newBottleB.name).toEqual("Duplicate");
});

test("merge unique bottle", async ({ fixtures }) => {
const entityA = await fixtures.Entity({ totalTastings: 1, totalBottles: 2 });
const bottleA = await fixtures.Bottle({
brandId: entityA.id,
name: "Unique",
});
const entityB = await fixtures.Entity({ totalTastings: 3, totalBottles: 1 });
const bottleB = await fixtures.Bottle({
brandId: entityB.id,
name: "More Unique",
});

await mergeEntity({
fromEntityIds: [entityA.id],
toEntityId: entityB.id,
});

const [newBottleA] = await db
.select()
.from(bottles)
.where(eq(bottles.id, bottleA.id));
expect(newBottleA).toBeDefined();
expect(newBottleA.brandId).toEqual(entityB.id);
expect(newBottleA.name).toEqual("Unique");

const [newBottleB] = await db
.select()
.from(bottles)
.where(eq(bottles.id, bottleB.id));
expect(newBottleB).toBeDefined();
expect(newBottleB.brandId).toEqual(entityB.id);
expect(newBottleB.name).toEqual("More Unique");
});
6 changes: 6 additions & 0 deletions apps/server/src/worker/jobs/mergeEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ export default async function mergeEntity({
fromBottleIds: [bottle.id],
db: tx,
});
} else {
// there was no conflict so lets udate it
await tx
.update(bottles)
.set({ brandId: toEntity.id })
.where(eq(bottles.id, bottle.id));
}
}

Expand Down

0 comments on commit 7020ef1

Please sign in to comment.