Skip to content

Commit 2ff8a20

Browse files
sweetmantechclaude
andauthored
fix: prevent Spotify social deletion when updating multiple platforms (#150)
Bug: When AI calls update_artist_socials with both YouTube AND existing Spotify URLs, the Spotify social gets deleted but NOT re-inserted. Root cause: The accountSocials array is fetched once at the start of updateArtistSocials and becomes stale after deleting a social. The check on lines 52-54 used this stale array to determine if the account_social relationship exists, incorrectly skipping the insert. Fix: Always insert the account_social relationship after processing a platform, since we know we deleted the old one (if it existed). Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 302a62b commit 2ff8a20

File tree

2 files changed

+49
-7
lines changed

2 files changed

+49
-7
lines changed

lib/artist/__tests__/updateArtistSocials.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,49 @@ describe("updateArtistSocials", () => {
126126
expect(mockInsertSocials).not.toHaveBeenCalled();
127127
expect(mockInsertAccountSocial).toHaveBeenCalledWith(artistId, "existing-db-social");
128128
});
129+
130+
it("re-inserts Spotify relationship when updating both YouTube and Spotify URLs", async () => {
131+
// BUG: When AI passes both YouTube AND existing Spotify URL, the Spotify
132+
// social gets deleted but NOT re-inserted due to stale accountSocials check
133+
const existingSpotifySocial = {
134+
id: "account-social-spotify",
135+
account_id: artistId,
136+
social_id: "social-spotify-1",
137+
social: {
138+
id: "social-spotify-1",
139+
username: "artistname",
140+
profile_url: "open.spotify.com/artist/0TnOYISbd1XYRBk9myaseg",
141+
},
142+
};
143+
144+
const existingSpotifySocialRecord = {
145+
id: "social-spotify-1",
146+
username: "artistname",
147+
profile_url: "open.spotify.com/artist/0TnOYISbd1XYRBk9myaseg",
148+
};
149+
150+
mockSelectAccountSocials.mockResolvedValue([existingSpotifySocial]);
151+
mockSelectSocials.mockImplementation(({ profile_url }) => {
152+
if (profile_url === "open.spotify.com/artist/0TnOYISbd1XYRBk9myaseg") {
153+
return Promise.resolve([existingSpotifySocialRecord]);
154+
}
155+
return Promise.resolve([]);
156+
});
157+
mockInsertSocials.mockResolvedValue([
158+
{ id: "new-youtube-social", username: "artistchannel", profile_url: "youtube.com/@artistchannel" },
159+
]);
160+
161+
// Update both YouTube AND Spotify (same Spotify URL as existing)
162+
await updateArtistSocials(artistId, {
163+
YOUTUBE: "https://youtube.com/@artistchannel",
164+
SPOTIFY: "https://open.spotify.com/artist/0TnOYISbd1XYRBk9myaseg",
165+
});
166+
167+
// Should delete existing Spotify (to replace it)
168+
expect(mockDeleteAccountSocial).toHaveBeenCalledWith(artistId, "social-spotify-1");
169+
// CRITICAL: Should re-insert the Spotify relationship after deleting it
170+
expect(mockInsertAccountSocial).toHaveBeenCalledWith(artistId, "social-spotify-1");
171+
// Should also insert YouTube
172+
expect(mockInsertAccountSocial).toHaveBeenCalledWith(artistId, "new-youtube-social");
173+
});
129174
});

lib/artist/updateArtistSocials.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,10 @@ export async function updateArtistSocials(
4848

4949
// Insert new social
5050
if (social) {
51-
// Social already exists, check if account_social relationship exists
52-
const existing = (accountSocials || []).find(
53-
(as: AccountSocialWithSocial) => as.social_id === social.id,
54-
);
55-
if (!existing) {
56-
await insertAccountSocial(artistId, social.id);
57-
}
51+
// Always insert the account_social relationship since we deleted the old one
52+
// for this platform type (if it existed). The stale accountSocials array
53+
// would incorrectly skip this insert if we checked it.
54+
await insertAccountSocial(artistId, social.id);
5855
} else {
5956
// Create new social record
6057
const username = getUsernameFromProfileUrl(value);

0 commit comments

Comments
 (0)