diff --git a/src/server/index.js b/src/server/index.js index 7bda3f2..ef05449 100644 --- a/src/server/index.js +++ b/src/server/index.js @@ -5,6 +5,8 @@ const bodyParser = require('body-parser'); const authorizationRoute = require('./routes/authorization'); const subscriptionRoute = require('./routes/subscription'); const notificationRoute = require('./routes/notification'); +const maintainRoute = require('./routes/maintain'); + const viewRoute = require('./routes/view'); const constants = require('./lib/constants'); const { checkAuth } = require('./middlewares/auth'); @@ -61,6 +63,8 @@ app.get(constants.route.forClient.GET_FORM_DATA, refererChecker, checkAuth, subs app.post(constants.route.forThirdParty.NOTIFICATION, notificationRoute.notification); // Home page app.get('/home', viewRoute.home); +app.get('/maintain/remove-user-name', maintainRoute.removeUserName); + if (process.env.GOOGLE_SITE_VERIFICATION_TOKEN) { app.get(`/${process.env.GOOGLE_SITE_VERIFICATION_TOKEN}.html`, (req, res) => { res.send(`google-site-verification: ${process.env.GOOGLE_SITE_VERIFICATION_TOKEN}.html`); diff --git a/src/server/routes/maintain.js b/src/server/routes/maintain.js new file mode 100644 index 0000000..f4bdf38 --- /dev/null +++ b/src/server/routes/maintain.js @@ -0,0 +1,41 @@ +const { User } = require('../models/userModel'); +const { errorLogger } = require('../lib/logger'); + +async function removeUserName(req, res) { + if (!process.env.MAINTAIN_TOKEN) { + res.status(404); + res.send('Not found'); + return; + } + if (req.query.maintain_token !== process.env.MAINTAIN_TOKEN) { + res.status(403); + res.send('Forbidden'); + return; + } + let lastKey = req.query.last_key; + try { + const users = await User.findAll({ + limit: 50, + lastKey: lastKey ? { id: lastKey } : undefined, + }); + if (users.lastKey) { + lastKey = users.lastKey.id; + } else { + lastKey = ''; + } + for (const user of users) { + if (user.name) { + await User.update({ name: '' }, { where: { id: user.id } }); + } + } + res.json({ + last_key: lastKey, + }); + } catch (e) { + errorLogger(e); + res.status(500); + res.send('Internal error'); + } +} + +exports.removeUserName = removeUserName; diff --git a/tests/maintain.test.js b/tests/maintain.test.js new file mode 100644 index 0000000..d6683d5 --- /dev/null +++ b/tests/maintain.test.js @@ -0,0 +1,64 @@ +const request = require('supertest'); +const { server } = require('../src/server'); +const { User } = require('../src/server/models/userModel'); + +describe('Maintain', () => { + it('should return 404 when no MAINTAIN_TOKEN env', async () => { + const res = await request(server).get('/maintain/remove-user-name'); + expect(res.status).toEqual(404); + }); + + it('should return 403 when maintain_token is invalid', async () => { + process.env.MAINTAIN_TOKEN = 'valid'; + const res = await request(server).get('/maintain/remove-user-name?maintain_token=invalid'); + expect(res.status).toEqual(403); + }); + + it('should return 200 when maintain_token is valid', async () => { + await User.create({ + id: 'test1', + name: 'test' + }); + await User.create({ + id: 'test2', + name: '' + }); + await User.create({ + id: 'test3', + name: 'test3' + }); + const user2 = await User.findByPk('test2'); + process.env.MAINTAIN_TOKEN = 'valid'; + const res = await request(server).get('/maintain/remove-user-name?maintain_token=valid'); + expect(res.status).toEqual(200); + const users = await User.findAll(); + expect(users[0].name).toEqual(''); + expect(users[1].name).toEqual(''); + expect(users[2].name).toEqual(''); + expect(user2.updatedAt).toEqual(users[1].updatedAt); + await User.destroy({ where: { id: 'test1' } }); + await User.destroy({ where: { id: 'test2' } }); + await User.destroy({ where: { id: 'test3' } }); + }); + + it('should return 200 with lastKey', async () => { + await User.create({ + id: 'test1', + name: 'test' + }); + await User.create({ + id: 'test2', + name: '' + }); + await User.create({ + id: 'test3', + name: 'test3' + }); + process.env.MAINTAIN_TOKEN = 'valid'; + const res = await request(server).get('/maintain/remove-user-name?maintain_token=valid&last_key=test1'); + expect(res.status).toEqual(200); + await User.destroy({ where: { id: 'test1' } }); + await User.destroy({ where: { id: 'test2' } }); + await User.destroy({ where: { id: 'test3' } }); + }); +});