-
Notifications
You must be signed in to change notification settings - Fork 10
fix(catalog): link the caller's account on catalog re-claim (chat#1867) #775
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
lib/supabase/account_catalogs/__tests__/upsertAccountCatalog.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import { describe, it, expect, vi, beforeEach } from "vitest"; | ||
|
|
||
| import { upsertAccountCatalog } from "../upsertAccountCatalog"; | ||
|
|
||
| const mockFrom = vi.fn(); | ||
| const mockUpsert = vi.fn(); | ||
|
|
||
| vi.mock("@/lib/supabase/serverClient", () => ({ | ||
| default: { | ||
| from: (...args: unknown[]) => mockFrom(...args), | ||
| }, | ||
| })); | ||
|
|
||
| describe("upsertAccountCatalog", () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| mockFrom.mockReturnValue({ upsert: mockUpsert }); | ||
| mockUpsert.mockResolvedValue({ error: null }); | ||
| }); | ||
|
|
||
| it("idempotently links an account to a catalog (upsert, ignore duplicates)", async () => { | ||
| await upsertAccountCatalog({ account: "acct-1", catalog: "cat-1" }); | ||
|
|
||
| expect(mockFrom).toHaveBeenCalledWith("account_catalogs"); | ||
| expect(mockUpsert).toHaveBeenCalledWith( | ||
| { account: "acct-1", catalog: "cat-1" }, | ||
| { onConflict: "account,catalog", ignoreDuplicates: true }, | ||
| ); | ||
| }); | ||
|
|
||
| it("throws when the upsert fails", async () => { | ||
| mockUpsert.mockResolvedValue({ error: { message: "boom" } }); | ||
|
|
||
| await expect(upsertAccountCatalog({ account: "acct-1", catalog: "cat-1" })).rejects.toThrow( | ||
| /Failed to link account_catalogs/, | ||
| ); | ||
| }); | ||
| }); |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import supabase from "../serverClient"; | ||
|
|
||
| /** | ||
| * Links a catalog to an account by upserting an `account_catalogs` row. | ||
| * | ||
| * Idempotent: linking an account already linked to the catalog is a no-op | ||
| * (`onConflict` on the `(account, catalog)` unique constraint) rather than a | ||
| * unique-violation, so claim paths can call it unconditionally to guarantee | ||
| * ownership without first checking for an existing link. | ||
| * | ||
| * @param params.account - Account id (owner) | ||
| * @param params.catalog - Catalog id to link | ||
| * @throws Error if the upsert fails | ||
| */ | ||
| export async function upsertAccountCatalog(params: { | ||
| account: string; | ||
| catalog: string; | ||
| }): Promise<void> { | ||
| const { error } = await supabase | ||
| .from("account_catalogs") | ||
| .upsert( | ||
| { account: params.account, catalog: params.catalog }, | ||
| { onConflict: "account,catalog", ignoreDuplicates: true }, | ||
| ); | ||
|
|
||
| if (error) { | ||
| throw new Error(`Failed to link account_catalogs: ${error.message}`); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
KISS
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done — renamed
insertAccountCatalog→upsertAccountCatalog(file + function + all call sites + tests), matching the existingupsertAccountSnapshotconvention. Pure rename, no behavior change; 21 tests green, tsc/lint clean. Pushed asea1fdc19.