From 6ffa59581c425926b7cef293e2e115984d98e500 Mon Sep 17 00:00:00 2001 From: Himanshu Date: Wed, 15 Mar 2023 19:15:32 +0530 Subject: [PATCH] Add send credential mails button for reviewers --- pages/admin/reviewers/index.tsx | 25 ++++++++++++++++++++- pages/api/updateReviewSet.ts | 40 ++++++++++++++++++++++++++++++++- 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/pages/admin/reviewers/index.tsx b/pages/admin/reviewers/index.tsx index b899d15..3e16a7f 100644 --- a/pages/admin/reviewers/index.tsx +++ b/pages/admin/reviewers/index.tsx @@ -1,3 +1,5 @@ +import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' +import { faSpinner } from '@fortawesome/free-solid-svg-icons' import { useEffect, useState } from 'react' import SetDropdown from '../../../components/Admin/SetDropdown' import Loading from '../../../components/Loading' @@ -6,6 +8,7 @@ import Roles from '../../../constants/roles' import AdminLayout from '../../../layouts/admin/admin-layout' import { Users } from '../../../types' import { getUsersByRole } from '../../api/getUserDetails' +import { sendRevSetMails } from '../../api/updateReviewSet' import { updateUserSets } from '../../api/updateUserSets' type SelectedSetsType = { [key: string]: Array } @@ -14,6 +17,7 @@ function ReviewersList() { const [reviewers, setReviewers] = useState({}) const [pageReady, setPageReady] = useState(false) const [selectedSets, setSelectedSets] = useState() + const [loading, setLoading] = useState(false) useEffect(() => { getUsersByRole(Roles.REVIEWER) @@ -29,7 +33,6 @@ function ReviewersList() { .catch(() => alert('Try again, network error!')) .finally(() => setPageReady(true)) }, []) - console.log(selectedSets) return ( @@ -128,6 +131,26 @@ function ReviewersList() { ))} + ) : ( diff --git a/pages/api/updateReviewSet.ts b/pages/api/updateReviewSet.ts index 131d40f..5e22a9f 100644 --- a/pages/api/updateReviewSet.ts +++ b/pages/api/updateReviewSet.ts @@ -1,8 +1,46 @@ import path from 'path' -import { firestore } from '../../firebase' +import firebase, { firestore } from '../../firebase' +import { User, userController } from '../../classes/user' export const updateReviewSet = (userId: string, set: string) => { return firestore .doc(path.join('admin_portal_data', userId)) .update({ review_set: set }) } + +export const sendRevSetMails = () => { + return firestore + .collection('users') + .where('role', '==', 'REVIEWER') + .withConverter(userController) + .get() + .then((users: firebase.firestore.QuerySnapshot) => { + // this code needs update because if users are more than 20, + // request will be closed and not able to wait for each mail request + users.docs.map( + async ( + userData: firebase.firestore.QueryDocumentSnapshot, + index: number, + ) => { + const user = userData.data() + setTimeout(() => { + fetch( + process.env.NEXT_PUBLIC_REVIEWER_CRED_MAIL_LINK + + new URLSearchParams({ + email: user.email, + name: user.name, + sets: user.sets.join(', '), + }), + ) + }, 1000 * index) + + if (index == users.docs.length - 1) { + setTimeout(() => { + console.log('Completed! Mail sent to all reviewers.') + }, 1000 * index) + } + }, + ) + }) + .catch((error) => console.log(error)) +}