diff --git a/controllers/external.controller.js b/controllers/external.controller.js index ca791b0b..f1e4843d 100644 --- a/controllers/external.controller.js +++ b/controllers/external.controller.js @@ -280,4 +280,188 @@ exports.externalDeleteTwitterChannel = async (req, res) => { err.message ? err.message : err.error ) } +} + + + +module.exports.externalVerifyLink = async (req, response) => { + try { + const user = await UserExternalWallet.findOne({walletId: req.address}) + var userId = user.UserId + var { typeSN, idUser, idPost } = req.params + let profileLinedin = null + if (!typeSN || !idUser || !idPost) { + return makeResponseError(response, 400, 'please provide all fields') + } + + var linked = false + var deactivate = false + var res = false + switch (typeSN) { + case '1': + let fbProfile = await FbProfile.findOne( + { UserId: userId }, + { accessToken: 1 } + ).lean() + await updateFacebookPages(userId, fbProfile.accessToken, false) + let fbPage = await FbPage.findOne( + { + UserId: userId, + username: idUser, + }, + { token: 1, id: 1 } + ).lean() + if (fbProfile && fbPage) { + linked = true + res = await verifyFacebook(idPost, fbPage) + + if (res && res.deactivate === true) { + deactivate = true + } + } + break + case '2': + var googleProfile = await GoogleProfile.findOne( + { + UserId: userId, + }, + { refreshToken: 1 } + ).lean() + + if (googleProfile) { + const data = await rp.post( + 'https://oauth2.googleapis.com/token', + { + client_id: oauth.google.googleClientId, + client_secret: oauth.google.googleClientSecret, + refresh_token: googleProfile.refreshToken, + grant_type: 'refresh_token', + } + ) + const access_token = data.data.access_token + await GoogleProfile.updateOne( + { UserId: userId }, + { $set: { accessToken: access_token } } + ) + linked = true + res = await verifyYoutube(userId, idPost, access_token) + if (res && res.deactivate === true) deactivate = true + } + + break + case '3': + var page = await FbPage.findOne({ + $and: [ + { UserId: userId }, + { instagram_id: { $exists: true } }, + ], + }) + if (page) { + linked = true + res = await verifyInsta(userId, idPost) + if (res === 'deactivate') deactivate = true + } + + break + case '4': + var twitterProfile = await TwitterProfile.findOne( + { + UserId: userId, + }, + { access_token_key: 1, access_token_secret: 1 } + ).lean() + if (twitterProfile) { + linked = true + res = await verifyTwitter(twitterProfile, userId, idPost) + if (res === 'deactivate') deactivate = true + } + + break + case '5': + var linkedinProfile = await LinkedinProfile.find( + { userId }, + { accessToken: 1, pages: 1, linkedinId: 1 } + ) + if (linkedinProfile.length) { + linked = true + for (let profile of linkedinProfile) { + res = await verifyLinkedin(profile, idPost) + if (res === true) { + profileLinedin = profile + break + } + if (res === 'deactivate') deactivate = true + } + } + + break + case '6': + var tiktokProfile = await TikTokProfile.findOne({ + userId, + }).lean() + if (tiktokProfile) { + linked = true + res = await verifytiktok(tiktokProfile, idPost) + if (res === 'deactivate') deactivate = true + } + + break + case '7': + var threads = await FbPage.findOne( + { + UserId: userId, + instagram_id: { $exists: true }, + threads_id: { $exists: true }, + }, + { threads_id: 1, instagram_username: 1 } + ).lean() + + if (threads) { + linked = true + res = await verifyThread( + idPost, + threads.threads_id, + threads.instagram_username + ) + + if (res === 'deactivate') deactivate = true + } + + break + default: + } + + if (!linked) + return makeResponseError(response, 406, 'account not linked') + else if (res === 'lien_invalid') + return makeResponseError(response, 406, 'invalid link') + else if (deactivate) + return makeResponseError(response, 405, 'account deactivated') + else if (res === 'link_not_found') + return makeResponseError(response, 406, 'link not found') + else { + if (typeSN == '7') + return makeResponseData( + response, + 200, + 'success', + res ? 'true' : 'false', + threads.instagram_username + ) + else + return makeResponseData( + response, + 200, + 'success', + res ? 'true' : 'false', + res === true && typeSN == '5' && profileLinedin?.linkedinId + ) + } + } catch (err) { + return makeResponseError( + response, + 500, + err.message ? err.message : err.error + ) + } } \ No newline at end of file diff --git a/routes/external.routes.js b/routes/external.routes.js index e40d2fba..5e40f612 100644 --- a/routes/external.routes.js +++ b/routes/external.routes.js @@ -13,7 +13,8 @@ const { externalDeleteLinkedinChannels, externalDeleteLinkedinChannel, externalDeleteTwitterChannels, - externalDeleteTwitterChannel + externalDeleteTwitterChannel, + externalVerifyLink } = require('../controllers/external.controller'); const verifySignatureMiddleware = require('./../middleware/verifySignature.middleware') @@ -57,5 +58,8 @@ router.delete('/RemoveTwitterChannels', verifySignatureMiddleware, externalDelet router.delete('/RemoveTwitterChannel/:id', verifySignatureMiddleware, externalDeleteTwitterChannel) +router.get('/link/verify/:typeSN/:idUser/:idPost', verifySignatureMiddleware, externalVerifyLink) + + module.exports = router; \ No newline at end of file