Skip to content
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

feat: image service integrated with feature flag #2003

Open
wants to merge 25 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
2b21785
feat: image service integrated with feature flag
ivinayakg Apr 4, 2024
ad42791
Merge branch 'develop' into feat/image-service-live
Achintya-Chatterjee Apr 4, 2024
6ae08e1
Merge branch 'develop' into feat/image-service-live
sahsisunny Apr 7, 2024
e3a945e
Merge branch 'develop' into feat/image-service-live
joyguptaa Apr 10, 2024
c4163bf
refact: image service for updating profile picture activated
ivinayakg Apr 10, 2024
dc64727
Merge branch 'feat/image-service-live' of https://github.com/Real-Dev…
ivinayakg Apr 10, 2024
207b7c8
test: fix for photo verification unit tests
ivinayakg Apr 11, 2024
6c68259
fix: discord action for photo verification request
ivinayakg Apr 11, 2024
d1bebe3
test: integration tests for photo verification
ivinayakg Apr 11, 2024
7818242
fix: formatting of discord-actions tests
ivinayakg Apr 11, 2024
b27d720
feat: image service integrated with feature flag
ivinayakg Apr 4, 2024
7450443
refact: image service for updating profile picture activated
ivinayakg Apr 10, 2024
d81ee2a
test: fix for photo verification unit tests
ivinayakg Apr 11, 2024
6719b8a
fix: discord action for photo verification request
ivinayakg Apr 11, 2024
43c51da
test: integration tests for photo verification
ivinayakg Apr 11, 2024
ec1480f
fix: formatting of discord-actions tests
ivinayakg Apr 11, 2024
0e5574e
refact: discord-action for photo verification
ivinayakg Apr 19, 2024
4c564e1
Merge branch 'feat/image-service-live' of https://github.com/Real-Dev…
ivinayakg Apr 19, 2024
52b6d2f
fix: discord-actions tests fix
ivinayakg Apr 19, 2024
1a29a6a
fix: discord-actions test for photo-verification
ivinayakg Apr 21, 2024
6e16753
Merge branch 'develop' into feat/image-service-live
ivinayakg Apr 24, 2024
cd2d922
Merge branch 'develop' into feat/image-service-live
iamitprakash Apr 28, 2024
00ecbc9
Merge branch 'develop' into feat/image-service-live
iamitprakash May 5, 2024
0781021
refact: made suggested changes in photoRequestVerification
ivinayakg May 12, 2024
818307d
Merge branch 'feat/image-service-live' of https://github.com/Real-Dev…
ivinayakg May 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion constants/imageVerificationTypes.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const IMAGE_VERIFICATION_TYPES = ["profile", "discord"];
const IMAGE_VERIFICATION_TYPES = ["profile", "discord", "both"];

module.exports = { IMAGE_VERIFICATION_TYPES };
7 changes: 7 additions & 0 deletions constants/users.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
const photoVerificationRequestStatus = {
PENDING: "PENDING",
APPROVED: "APPROVED",
REJECTED: "REJECTED",
};

ivinayakg marked this conversation as resolved.
Show resolved Hide resolved
const profileStatus = {
PENDING: "PENDING",
APPROVED: "APPROVED",
Expand Down Expand Up @@ -61,4 +67,5 @@ module.exports = {
months,
discordNicknameLength,
SIMULTANEOUS_WORKER_CALLS,
photoVerificationRequestStatus
};
4 changes: 2 additions & 2 deletions controllers/discordactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@
*/
const updateDiscordImageForVerification = async (req, res) => {
try {
const { id: userDiscordId } = req.params;
const discordAvatarUrl = await discordRolesModel.updateDiscordImageForVerification(userDiscordId);
const { discordId } = req.params;
ivinayakg marked this conversation as resolved.
Show resolved Hide resolved
const discordAvatarUrl = await discordRolesModel.updateDiscordImageForVerification(discordId);
return res.json({
message: "Discord avatar URL updated successfully!",
discordAvatarUrl,
Expand Down Expand Up @@ -290,7 +290,7 @@
const nickNameUpdatedUsers = [];
let counter = 0;
for (let i = 0; i < usersToBeEffected.length; i++) {
const { discordId, username, first_name: firstName } = usersToBeEffected[i];

Check warning on line 293 in controllers/discordactions.js

View workflow job for this annotation

GitHub Actions / build (20.11.x)

Variable Assigned to Object Injection Sink
try {
if (counter % 10 === 0 && counter !== 0) {
await new Promise((resolve) => setTimeout(resolve, 5500));
Expand All @@ -306,7 +306,7 @@
if (message) {
counter++;
totalNicknamesUpdated.count++;
nickNameUpdatedUsers.push(usersToBeEffected[i].id);

Check warning on line 309 in controllers/discordactions.js

View workflow job for this annotation

GitHub Actions / build (20.11.x)

Generic Object Injection Sink
}
}
} catch (error) {
Expand Down
73 changes: 55 additions & 18 deletions controllers/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -443,9 +443,10 @@ const updateSelf = async (req, res) => {
*/
const postUserPicture = async (req, res) => {
const { file } = req;
const { dev } = req.query;
const { id: userId, discordId } = req.userData;
const { coordinates } = req.body;
let discordAvatarUrl = "";
let discordAvatarUrl;
let imageData;
let verificationResult;
try {
Expand All @@ -456,17 +457,26 @@ const postUserPicture = async (req, res) => {
}
try {
const coordinatesObject = coordinates && JSON.parse(coordinates);
imageData = await imageService.uploadProfilePicture({ file, userId, coordinates: coordinatesObject });
imageData = await imageService.uploadProfilePicture({ file, userId, coordinates: coordinatesObject }, dev);
} catch (error) {
logger.error(`Error while adding profile picture of user: ${error}`);
return res.boom.badImplementation(INTERNAL_SERVER_ERROR);
}
try {
verificationResult = await userQuery.addForVerification(userId, discordId, imageData.url, discordAvatarUrl);
} catch (error) {
logger.error(`Error while adding profile picture of user: ${error}`);
return res.boom.badImplementation(INTERNAL_SERVER_ERROR);
if (dev) {
try {
verificationResult = await userQuery.addForVerification(
userId,
discordId,
imageData.url,
discordAvatarUrl,
imageData.publicId
);
} catch (error) {
logger.error(`Error while adding profile picture of user: ${error}`);
return res.boom.badImplementation(INTERNAL_SERVER_ERROR);
}
}

return res.status(201).json({
message: `Profile picture uploaded successfully! ${verificationResult.message}`,
image: imageData,
Expand All @@ -482,11 +492,12 @@ const postUserPicture = async (req, res) => {

const verifyUserImage = async (req, res) => {
try {
const { type: imageType } = req.query;
const { id: userId } = req.params;
await userQuery.markAsVerified(userId, imageType);
return res.json({
message: `${imageType} image was verified successfully!`,
const { type: imageType, status } = req.query;
const { userId } = req.params;

const verificationResponse = await userQuery.changePhotoVerificationStatus(userId, imageType, status);
return res.status(200).json({
message: verificationResponse,
});
} catch (error) {
logger.error(`Error while verifying image of user: ${error}`);
Expand Down Expand Up @@ -565,20 +576,45 @@ const markUnverified = async (req, res) => {
* @param res {Object} - Express response object
*/

const getUserImageForVerification = async (req, res) => {
const getUserPhotoVerificationRequests = async (req, res) => {
try {
const { id: userId } = req.params;
const userImageVerificationData = await userQuery.getUserImageForVerification(userId);
const { userId } = req.params;
const userData = req.userData;
if ((userData.id !== userId && !userData.roles[ROLES.SUPERUSER]) || !userId) {
return res.boom.unauthorized("You are not authorized to view this user's image verification data");
}
pallabez marked this conversation as resolved.
Show resolved Hide resolved
const userImageVerificationData = await userQuery.getUserPhotoVerificationRequests(userId);
return res.json({
message: "User image verification record fetched successfully!",
data: userImageVerificationData,
data: userImageVerificationData[0],
});
} catch (error) {
logger.error(`Error while verifying image of user: ${error}`);
logger.error(`Error while querying image of user: ${error}`);
ivinayakg marked this conversation as resolved.
Show resolved Hide resolved
return res.boom.badImplementation(INTERNAL_SERVER_ERROR);
}
};

/**
* Updates the user data
*
* @param req {Object} - Express request object
* @param res {Object} - Express response object
*/

const getAllUsersPhotoVerificationRequests = async (req, res) => {
ivinayakg marked this conversation as resolved.
Show resolved Hide resolved
const { username } = req.query;
try {
const userImageVerificationData = await userQuery.getAllUsersPhotoVerificationRequests(username);
return res.json({
message: "User image verification record fetched successfully!",
data: userImageVerificationData,
});
} catch (error) {
logger.error(`Error while querying image of user: ${error}`);
return res.boom.badImplementation(`Error while querying image verification data for ${username}`);
}
};

/**
* Updates the user data
*
Expand Down Expand Up @@ -954,7 +990,8 @@ module.exports = {
getUserSkills,
filterUsers,
verifyUserImage,
getUserImageForVerification,
getAllUsersPhotoVerificationRequests,
getUserPhotoVerificationRequests,
nonVerifiedDiscordUsers,
setInDiscordScript,
markUnverified,
Expand Down
1 change: 0 additions & 1 deletion middlewares/authenticate.js
ivinayakg marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ module.exports = async (req, res, next) => {
// add user data to `req.userData` for further use
const userData = await dataAccess.retrieveUsers({ id: userId });
req.userData = userData.user;

return checkRestricted(req, res, next);
} catch (err) {
logger.error(err);
Expand Down
8 changes: 7 additions & 1 deletion middlewares/validators/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const {
USER_STATUS,
USERS_PATCH_HANDLER_ACTIONS,
USERS_PATCH_HANDLER_ERROR_MESSAGES,
photoVerificationRequestStatus,
} = require("../../constants/users");
const ROLES = require("../../constants/roles");
const { IMAGE_VERIFICATION_TYPES } = require("../../constants/imageVerificationTypes");
Expand Down Expand Up @@ -251,11 +252,16 @@ async function validateUserQueryParams(req, res, next) {
* @param next {Object} - Express middleware function
*/
const validateImageVerificationQuery = async (req, res, next) => {
const { type: imageType } = req.query;
const { type: imageType, status } = req.query;
try {
if (!IMAGE_VERIFICATION_TYPES.includes(imageType)) {
throw new Error("Invalid verification type was provided!");
}

if (status !== photoVerificationRequestStatus.APPROVED && status !== photoVerificationRequestStatus.REJECTED) {
pallabez marked this conversation as resolved.
Show resolved Hide resolved
throw new Error("Invalid verification status was provided!");
}

next();
} catch (error) {
logger.error(`Error validating createLevel payload : ${error}`);
Expand Down
8 changes: 6 additions & 2 deletions models/discordactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
const { BATCH_SIZE_IN_CLAUSE } = require("../constants/firebase");
const { getAllUserStatus, getGroupRole, getUserStatus } = require("./userStatus");
const { userState } = require("../constants/userStatus");
const { ONE_DAY_IN_MS, SIMULTANEOUS_WORKER_CALLS } = require("../constants/users");
const { ONE_DAY_IN_MS, SIMULTANEOUS_WORKER_CALLS, photoVerificationRequestStatus } = require("../constants/users");
const userModel = firestore.collection("users");
const photoVerificationModel = firestore.collection("photo-verification");
const dataAccess = require("../services/dataAccessLayer");
Expand Down Expand Up @@ -215,9 +215,13 @@
const updateDiscordImageForVerification = async (userDiscordId) => {
try {
const discordAvatarUrl = await generateDiscordProfileImageUrl(userDiscordId);
const verificationDataSnapshot = await photoVerificationModel.where("discordId", "==", userDiscordId).get();
const verificationDataSnapshot = await photoVerificationModel
.where("discordId", "==", userDiscordId)
.where("status", "==", photoVerificationRequestStatus.PENDING)
.get();
const unverifiedUserDiscordImage = {
discord: { url: discordAvatarUrl, approved: false, date: admin.firestore.Timestamp.fromDate(new Date()) },
profile: { approved: false, date: admin.firestore.Timestamp.fromDate(new Date()) },
};
if (verificationDataSnapshot.empty) {
throw new Error("No user verification record found");
Expand Down Expand Up @@ -490,7 +494,7 @@

for (let i = 0; i < nicknameUpdateBatches.length; i++) {
const promises = [];
const usersStatusDocsBatch = nicknameUpdateBatches[i];

Check warning on line 497 in models/discordactions.js

View workflow job for this annotation

GitHub Actions / build (20.11.x)

Variable Assigned to Object Injection Sink
usersStatusDocsBatch.forEach((document) => {
const doc = document.data();
const userId = doc.userId;
Expand Down
Loading
Loading