Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions lib/artist/__tests__/updateArtistSocials.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,49 @@ describe("updateArtistSocials", () => {
expect(mockInsertSocials).not.toHaveBeenCalled();
expect(mockInsertAccountSocial).toHaveBeenCalledWith(artistId, "existing-db-social");
});

it("re-inserts Spotify relationship when updating both YouTube and Spotify URLs", async () => {
// BUG: When AI passes both YouTube AND existing Spotify URL, the Spotify
// social gets deleted but NOT re-inserted due to stale accountSocials check
const existingSpotifySocial = {
id: "account-social-spotify",
account_id: artistId,
social_id: "social-spotify-1",
social: {
id: "social-spotify-1",
username: "artistname",
profile_url: "open.spotify.com/artist/0TnOYISbd1XYRBk9myaseg",
},
};

const existingSpotifySocialRecord = {
id: "social-spotify-1",
username: "artistname",
profile_url: "open.spotify.com/artist/0TnOYISbd1XYRBk9myaseg",
};

mockSelectAccountSocials.mockResolvedValue([existingSpotifySocial]);
mockSelectSocials.mockImplementation(({ profile_url }) => {
if (profile_url === "open.spotify.com/artist/0TnOYISbd1XYRBk9myaseg") {
return Promise.resolve([existingSpotifySocialRecord]);
}
return Promise.resolve([]);
});
mockInsertSocials.mockResolvedValue([
{ id: "new-youtube-social", username: "artistchannel", profile_url: "youtube.com/@artistchannel" },
]);

// Update both YouTube AND Spotify (same Spotify URL as existing)
await updateArtistSocials(artistId, {
YOUTUBE: "https://youtube.com/@artistchannel",
SPOTIFY: "https://open.spotify.com/artist/0TnOYISbd1XYRBk9myaseg",
});

// Should delete existing Spotify (to replace it)
expect(mockDeleteAccountSocial).toHaveBeenCalledWith(artistId, "social-spotify-1");
// CRITICAL: Should re-insert the Spotify relationship after deleting it
expect(mockInsertAccountSocial).toHaveBeenCalledWith(artistId, "social-spotify-1");
// Should also insert YouTube
expect(mockInsertAccountSocial).toHaveBeenCalledWith(artistId, "new-youtube-social");
});
});
11 changes: 4 additions & 7 deletions lib/artist/updateArtistSocials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,10 @@ export async function updateArtistSocials(

// Insert new social
if (social) {
// Social already exists, check if account_social relationship exists
const existing = (accountSocials || []).find(
(as: AccountSocialWithSocial) => as.social_id === social.id,
);
if (!existing) {
await insertAccountSocial(artistId, social.id);
}
// Always insert the account_social relationship since we deleted the old one
// for this platform type (if it existed). The stale accountSocials array
// would incorrectly skip this insert if we checked it.
await insertAccountSocial(artistId, social.id);
} else {
// Create new social record
const username = getUsernameFromProfileUrl(value);
Expand Down