-
Notifications
You must be signed in to change notification settings - Fork 278
Feature/one time user colors #735
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from 11 commits
2fed9ad
0f505b7
cd2c96d
5bd0f9a
5e4f75a
04c43e7
964dd56
0f65a11
146304a
01c8138
7a75444
54c94ce
f4d60d3
64f56cc
b210076
804c64c
b2c9c3c
b495022
807df54
2ecedb5
4b0df82
cc10b82
b8c7521
231cbef
48f6ef3
f322598
018f829
62fb581
542d504
7e0d00a
a522f0f
c723578
d76b090
c87fe9c
6aaafd7
c2151cd
0852243
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| module.exports = [ | ||
| { COLOR_ID: 1 }, | ||
| { COLOR_ID: 2 }, | ||
| { COLOR_ID: 3 }, | ||
| { COLOR_ID: 4 }, | ||
| { COLOR_ID: 5 }, | ||
| { COLOR_ID: 6 }, | ||
| { COLOR_ID: 7 }, | ||
| { COLOR_ID: 8 }, | ||
| { COLOR_ID: 9 }, | ||
| { COLOR_ID: 10 }, | ||
| ]; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| const userQuery = require("../models/migrations"); | ||
| const logger = require("../utils/logger"); | ||
|
|
||
| /** | ||
| * Returns the lists of usernames where default colors were added | ||
| * | ||
| * @param req {Object} - Express request object | ||
| * @param res {Object} - Express response object | ||
| */ | ||
|
|
||
| const addDefaultColors = async (req, res) => { | ||
| try { | ||
| const addedDefaultColorsData = await userQuery.addDefaultColors(); | ||
|
|
||
| return res.json({ | ||
| message: "User colors updated successfully!", | ||
| ...addedDefaultColorsData, | ||
| }); | ||
| } catch (error) { | ||
| logger.error(`Error adding default colors to users: ${error}`); | ||
| return res.boom.badImplementation("Something went wrong. Please contact admin"); | ||
| } | ||
| }; | ||
|
|
||
| module.exports = { | ||
| addDefaultColors, | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| const firestore = require("../utils/firestore"); | ||
| const userModel = firestore.collection("users"); | ||
| const cardColorIdArray = require("../constants/cardColorIdArray"); | ||
| const { getRandomIndex } = require("../utils/helpers"); | ||
| /** | ||
| * Adds default colors property | ||
| * @return {Promise<usersMigrated|Object>} | ||
| */ | ||
|
|
||
| const addDefaultColors = async () => { | ||
| try { | ||
| const usersSnapshot = await userModel.get(); | ||
| const migratedUsers = []; | ||
| const updateUserPromises = []; | ||
| const usersArr = []; | ||
|
|
||
| usersSnapshot.forEach((doc) => usersArr.push({ id: doc.id, ...doc.data() })); | ||
|
|
||
| for (const user of usersArr) { | ||
| const colors = user.colors ? user.colors : {}; | ||
| if (user.colors === undefined) { | ||
| const userColorIndex = getRandomIndex(cardColorIdArray); | ||
|
|
||
| colors.color_id = cardColorIdArray[parseInt(userColorIndex)].COLOR_ID; | ||
isVivek99 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| updateUserPromises.push(userModel.doc(user.id).set({ ...user, colors })); | ||
| migratedUsers.push(user.username); | ||
| } | ||
| } | ||
|
|
||
| await Promise.all(updateUserPromises); | ||
| return { count: migratedUsers.length, users: migratedUsers }; | ||
| } catch (err) { | ||
| logger.error("Error adding default colors to users", err); | ||
| throw err; | ||
| } | ||
| }; | ||
|
|
||
| module.exports = { | ||
| addDefaultColors, | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| const express = require("express"); | ||
| const router = express.Router(); | ||
| const authenticate = require("../middlewares/authenticate"); | ||
| const authorizeRoles = require("../middlewares/authorizeRoles"); | ||
| const { SUPERUSER } = require("../constants/roles"); | ||
| const migrations = require("../controllers/migrations"); | ||
|
|
||
| router.patch("/addDefaultColorProperty", authenticate, authorizeRoles([SUPERUSER]), migrations.addDefaultColors); | ||
|
|
||
| module.exports = router; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -115,6 +115,9 @@ module.exports = () => { | |
| app_owner: true, | ||
| archived: true, | ||
| }, | ||
| colors: { | ||
| color_id: 2, | ||
isVivek99 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }, | ||
|
||
| }, | ||
| { | ||
| username: "mehul", | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| const chai = require("chai"); | ||
| const { expect } = chai; | ||
| const chaiHttp = require("chai-http"); | ||
|
|
||
| const app = require("../../server"); | ||
| const authService = require("../../services/authService"); | ||
| const addUser = require("../utils/addUser"); | ||
| const cleanDb = require("../utils/cleanDb"); | ||
| // Import fixtures | ||
| const userData = require("../fixtures/user/user")(); | ||
| const superUser = userData[4]; | ||
| const nonSuperUser = userData[0]; | ||
| const userWithColorProperty = [userData[5].username]; | ||
| const colorBearingUsernames = [superUser.username, nonSuperUser.username]; | ||
|
|
||
| const config = require("config"); | ||
| const cookieName = config.get("userToken.cookieName"); | ||
|
|
||
| chai.use(chaiHttp); | ||
|
|
||
| describe("Migrations", function () { | ||
| let superUserId; | ||
| let superUserAuthToken; | ||
| let userId = ""; | ||
| let nonSuperUserId = ""; | ||
| beforeEach(async function () { | ||
| userId = await addUser(); | ||
| superUserId = await addUser(superUser); | ||
| nonSuperUserId = userId; | ||
| superUserAuthToken = authService.generateAuthToken({ userId: superUserId }); | ||
| }); | ||
|
|
||
| afterEach(async function () { | ||
| await cleanDb(); | ||
| }); | ||
|
|
||
| describe("PATCH /migrations/addDefaultColorProperty", function () { | ||
| it("Should return 401 if user is not a super user", function (done) { | ||
| const nonSuperUserJwt = authService.generateAuthToken({ userId: nonSuperUserId }); | ||
| chai | ||
| .request(app) | ||
| .patch(`/migrations/addDefaultColorProperty`) | ||
| .set("cookie", `${cookieName}=${nonSuperUserJwt}`) | ||
| .end((err, res) => { | ||
| if (err) { | ||
| return done(err); | ||
| } | ||
| expect(res).to.have.status(401); | ||
| expect(res.body).to.be.a("object"); | ||
| expect(res.body.message).to.equal("You are not authorized for this action."); | ||
| return done(); | ||
| }); | ||
| }); | ||
| it("Should add default color property to all users,using authorized user (super_user)", function (done) { | ||
| chai | ||
| .request(app) | ||
| .patch(`/migrations/addDefaultColorProperty`) | ||
| .set("cookie", `${cookieName}=${superUserAuthToken}`) | ||
| .end((err, res) => { | ||
| if (err) { | ||
| return done(err); | ||
| } | ||
|
|
||
| expect(res).to.have.status(200); | ||
| expect(res.body.count).to.be.equal(colorBearingUsernames.length); | ||
| const migratedUsernames = res.body.users; | ||
|
|
||
| expect(migratedUsernames).to.include( | ||
| colorBearingUsernames[0], | ||
| "Should add default color property to user without color property" | ||
| ); | ||
|
|
||
| expect(migratedUsernames).to.not.include.any.members( | ||
| userWithColorProperty, | ||
| "Should not modify color property of user with color object" | ||
| ); | ||
| return done(); | ||
| }); | ||
| }); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| /** | ||
| * Returns a random object from the array of colors to user | ||
| * @param array {array} : array containing objects | ||
| * @returns random Index number : index between the range 0 to array.length | ||
| */ | ||
| const getRandomIndex = (array = []) => { | ||
| return Math.floor(Math.random() * array.length); | ||
| }; | ||
|
|
||
| module.exports = { | ||
| getRandomIndex, | ||
| }; |
Uh oh!
There was an error while loading. Please reload this page.