Skip to content

Commit a6ae722

Browse files
Adding Spotify Update: Check if Spotify account exists
Prevent duplicate Spotify accounts being added
1 parent dbc5f7d commit a6ae722

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

src/controller/Spotify.controller.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,27 @@ exports.getCurrent = async () => {
302302
})
303303
}
304304

305+
exports.checkIfSpotifyAccountDuplicate = (userId: string, spotifyId: string) => {
306+
return new Promise(async (resolve, reject) => {
307+
try {
308+
const connection = await connect();
309+
const repo = connection.getRepository(MusicProviders);
310+
311+
// repo.find({ where: { userId, additional: Raw(() => additional->'$.spotifyId' = spotifyId )}});
312+
const duplicate = await repo.find({ where: { userId } });
313+
if (!duplicate) return resolve({ isDuplicate: false, providerId: null });
314+
duplicate.forEach((spotify) => {
315+
if (spotify.additional.spotifyId == spotifyId) {
316+
return resolve({ isDuplicate: true, providerId: spotify.providerId })
317+
}
318+
})
319+
resolve({ isDuplicate: false, providerId: null })
320+
} catch (error) {
321+
reject(error);
322+
}
323+
})
324+
}
325+
305326
exports.addToQueue = (songUri, device_id) => {
306327
return new Promise(async (resolve, reject) => {
307328
try {

src/routes/Callback.route.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ router.get('/spotify', async (req: Request, res: Response) => {
2020
email, display_name, product, country, spotifyId,
2121
expires_in, access_token, refresh_token
2222
}, userId);
23+
const { isDuplicate, providerId } = await Spotify.checkIfSpotifyAccountDuplicate(userId, spotifyId);
2324

2425
/** Requirement for Hosts only
2526
if(product != "premium") res.status().json({

0 commit comments

Comments
 (0)