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

Add send credential mails button for reviewers #209

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
25 changes: 24 additions & 1 deletion pages/admin/reviewers/index.tsx
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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<string> }
Expand All @@ -14,6 +17,7 @@ function ReviewersList() {
const [reviewers, setReviewers] = useState<Users>({})
const [pageReady, setPageReady] = useState<boolean>(false)
const [selectedSets, setSelectedSets] = useState<SelectedSetsType>()
const [loading, setLoading] = useState(false)

useEffect(() => {
getUsersByRole(Roles.REVIEWER)
Expand All @@ -29,7 +33,6 @@ function ReviewersList() {
.catch(() => alert('Try again, network error!'))
.finally(() => setPageReady(true))
}, [])
console.log(selectedSets)

return (
<AdminLayout>
Expand Down Expand Up @@ -128,6 +131,26 @@ function ReviewersList() {
))}
</tbody>
</table>
<button
className="text-white text-base md:text-lg py-1 px-3 mt-10 rounded-lg bg-red-850"
onClick={() => {
if (loading) return

setLoading(true)
sendRevSetMails().finally(() => setLoading(false))
}}
>
{!loading ? (
'Send Credential Mails'
) : (
<FontAwesomeIcon
icon={faSpinner}
width={30}
spin={true}
className="mx-5"
/>
)}
</button>
</div>
</div>
) : (
Expand Down
40 changes: 39 additions & 1 deletion pages/api/updateReviewSet.ts
Original file line number Diff line number Diff line change
@@ -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<User>) => {
// 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<User>,
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))
}