Skip to content

Commit 4a58cab

Browse files
Adding Spotify: If Duplicate, update current instead of creating enw
1 parent a6ae722 commit 4a58cab

2 files changed

Lines changed: 36 additions & 8 deletions

File tree

src/controller/Spotify.controller.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,27 @@ exports.storeSpotifyCredentials = ({ email, display_name, product, country, spot
122122
})
123123
}
124124

125+
exports.updateSpotifyCredentials = (providerId: string, { email, display_name, product, country, spotifyId,
126+
expires_in, access_token, refresh_token }, userId) => {
127+
return new Promise(async (resolve, reject) => {
128+
try {
129+
const connection = await connect();
130+
const repo = connection.getRepository(MusicProviders);
131+
132+
const additional = { product, country, spotifyId, email, display_name };
133+
const response = await repo.save({
134+
providerId, userId,
135+
country, expiresIn: expires_in, accessToken: access_token, refreshToken: refresh_token, additional, displayName: display_name
136+
})
137+
138+
resolve(response);
139+
140+
} catch (error) {
141+
reject(error);
142+
}
143+
});
144+
}
145+
125146
/**
126147
* Spotify Search for song
127148
*

src/routes/Callback.route.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,29 @@ const NAMESPACE = 'Callback';
55

66
/** Import dependecies */
77
const Spotify = require('../controller/Spotify.controller');
8-
8+
99

1010
router.get('/spotify', async (req: Request, res: Response) => {
1111
try {
12-
globalThis.Logger.log('info', `[${NAMESPACE}] Authenticating from Spotify `, { ...globalThis.LoggingParams})
12+
globalThis.Logger.log('info', `[${NAMESPACE}] Authenticating from Spotify `, { ...globalThis.LoggingParams })
1313

1414
const { code, state } = req.query;
1515
const [userId, device] = state.toString().split("+");
1616
const { email, display_name, product, country, spotifyId,
1717
expires_in, access_token, refresh_token } = await Spotify.authorizeUser(code);
1818

19-
await Spotify.storeSpotifyCredentials({
20-
email, display_name, product, country, spotifyId,
21-
expires_in, access_token, refresh_token
22-
}, userId);
2319
const { isDuplicate, providerId } = await Spotify.checkIfSpotifyAccountDuplicate(userId, spotifyId);
20+
if (isDuplicate) {
21+
await Spotify.updateSpotifyCredentials(providerId, {
22+
email, display_name, product, country, spotifyId,
23+
expires_in, access_token, refresh_token
24+
}, userId);
25+
} else {
26+
await Spotify.storeSpotifyCredentials({
27+
email, display_name, product, country, spotifyId,
28+
expires_in, access_token, refresh_token
29+
}, userId);
30+
}
2431

2532
/** Requirement for Hosts only
2633
if(product != "premium") res.status().json({
@@ -32,11 +39,11 @@ router.get('/spotify', async (req: Request, res: Response) => {
3239
} else if (device == 'Android') {
3340
res.redirect("https://fonzmusic.com/spotify");
3441
} else {
35-
res.status(400).send({ message: "Invalid device"});
42+
res.status(400).send({ message: "Invalid device" });
3643
}
3744
} catch (error) {
3845
globalThis.Logger.log('error', `[${NAMESPACE}] Could not Authenticate from Spotify `, { ...globalThis.LoggingParams, error },)
39-
46+
4047
res.status(error.status || 500).send(error);
4148
}
4249
});

0 commit comments

Comments
 (0)