Skip to content

Commit

Permalink
added edpoint to remove invalid user
Browse files Browse the repository at this point in the history
  • Loading branch information
DaKingKong committed May 31, 2022
1 parent 0e49c4e commit 49ee493
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ringcentral-google-drive-notification-add-in",
"version": "0.2.16",
"version": "0.2.17",
"description": "RingCentral Add-In App",
"keywords": [
"RingCentral",
Expand Down
36 changes: 34 additions & 2 deletions src/server/handlers/dbAccessHandler.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const { GoogleUser } = require('../models/googleUserModel');
const { Subscription } = require('..//models/subscriptionModel');
const { RcUser } = require('../models/rcUserModel');

const subscriptionHandler = require('../handlers/subscriptionHandler');

const basicAuthString = Buffer.from(`${process.env.RINGCENTRAL_CHATBOT_ADMIN_USERNAME}:${process.env.RINGCENTRAL_CHATBOT_ADMIN_PASSWORD}`).toString('base64');

Expand All @@ -21,7 +24,7 @@ async function getGoogleUserList(req, res) {
const basicAuth = req.headers.authorization.split('Basic ')[1];
if (basicAuth == basicAuthString) {
const googleUsers = await GoogleUser.findAll();
const resultData = googleUsers.map(g => { return { email: g.email, botId: g.botId, isReceiveNewFile: g.isReceiveNewFile, googleSubscriptionId: g.googleSubscriptionId, accessToken: g.accessToken } });
const resultData = googleUsers.map(g => { return { email: g.email, botId: g.botId, isReceiveNewFile: g.isReceiveNewFile, googleSubscriptionId: g.googleSubscriptionId, tokenExpiredAt: g.tokenExpiredAt } });
res.status(200);
res.json(resultData);
}
Expand All @@ -32,5 +35,34 @@ async function getGoogleUserList(req, res) {
return;
}

async function removeGoogleUserByEmail(req, res){
const basicAuth = req.headers.authorization.split('Basic ')[1];
if (basicAuth == basicAuthString) {
const googleUser = await GoogleUser.findOne({
where:{
email: req.query.email
}
});
const rcUser = await RcUser.findByPk(googleUser.rcUserId);
if(rcUser)
{
await rcUser.destroy();
}
if(googleUser)
{
await subscriptionHandler.stopSubscriptionForUser(googleUser);
await googleUser.destroy();
}
res.status(200);
res.send('User and Subscriptions removed.');
}
else {
res.status(401);
res.send('Auth failed.');
}
return;
}

exports.getSubscriptionCount = getSubscriptionCount;
exports.getGoogleUserList = getGoogleUserList;
exports.getGoogleUserList = getGoogleUserList;
exports.removeGoogleUserByEmail = removeGoogleUserByEmail;
7 changes: 4 additions & 3 deletions src/server/handlers/subscriptionHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,14 @@ async function stopSubscriptionForUser(googleUser) {
console.log(e.message);
}

const existingSub = await Subscription.findOne({
const existingSubs = await Subscription.findAll({
where: {
googleUserId: googleUser.id
}
});
if (existingSub) {
await existingSub.destroy();

for (const sub of existingSubs) {
await sub.destroy();
}

console.log(`stopped subscription for user ${googleUser.email}`);
Expand Down
1 change: 1 addition & 0 deletions src/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ exports.appExtend = (app) => {
app.post('/interactive-messages', interactiveMessageHandler.interactiveMessages);
app.get('/db/subCount', dbAccessHandler.getSubscriptionCount);
app.get('/db/googleUser', dbAccessHandler.getGoogleUserList);
app.get('/db/removeUserByEmail', dbAccessHandler.removeGoogleUserByEmail);

// host home page
app.get('/home', function (req, res) {
Expand Down

0 comments on commit 49ee493

Please sign in to comment.