Skip to content

Commit

Permalink
feat: added hook and fix doc init bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Zain-ul-din committed Mar 20, 2024
1 parent 952a83f commit 050d5bb
Showing 1 changed file with 27 additions and 21 deletions.
48 changes: 27 additions & 21 deletions src/pages/api/util/workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
const user = usersSnapShot.docs.map((doc)=> doc.data())[0] as UserDocType

const isAdmin = user.email === process.env.NEXT_PUBLIC_ADMIN_EMAIL;

const quotaDoc = await initWorkFlowDoc()

if(!isAdmin) {
const { valid, message } = await canTriggerWorkFlow(user);
const { valid, message } = await canTriggerWorkFlow(quotaDoc as WorkFlowQuotaType,user);
if(!valid) {
res.status(405).send({ message })
return;
Expand Down Expand Up @@ -66,12 +67,32 @@ interface WorkFlowQuotaType {
participants: string []
}

async function initWorkFlowDoc() {
const quotaDocRef = doc(workFlowColRef, new Date().toDateString());
let quotaSnapShot = await getDoc(quotaDocRef);

if(!quotaSnapShot.exists()) {
await setDoc(quotaDocRef, {
last_updated: serverTimestamp(),
users: [],
participants: []
} as WorkFlowQuotaType, { merge: true });
return {
message: '',
valid: true
};
}

return quotaSnapShot.data()
}

async function triggerWorkFlow(uid: string, session_id: string,) {
const quotaDocRef = doc(workFlowColRef, new Date().toDateString());
const auxDoc = doc(workFlowColRef);

const GITHUB_URL = 'https://api.github.com/repos/Zain-ul-din/lgu-crawler/actions/workflows/89946576/dispatches'

const hook = process.env.RE_DEPLOYMENT_HOOK || 'https://www.google.com'

await fetch(GITHUB_URL, {
method: 'POST',
headers: {
Expand All @@ -81,7 +102,7 @@ async function triggerWorkFlow(uid: string, session_id: string,) {
},
body: JSON.stringify({
ref: "master",
inputs: { session_id }
inputs: { session_id, hook }
})
})

Expand Down Expand Up @@ -113,24 +134,10 @@ const PRO_COOL_DOWN_TIME = ONE_HOUR_IN_MS * 0.25;
const MAX_REQ_PER_USER = 2;
const MAX_REQ_PER_PRO_USER = 4;

async function canTriggerWorkFlow(user: UserDocType) {

const quotaDocRef = doc(workFlowColRef, new Date().toDateString());
let quotaSnapShot = await getDoc(quotaDocRef);

if(!quotaSnapShot.exists()) {
await setDoc(quotaDocRef, {
last_updated: serverTimestamp(),
users: [],
participants: []
} as WorkFlowQuotaType, { merge: true });
return {
message: '',
valid: true
};
}
async function canTriggerWorkFlow(quota: WorkFlowQuotaType,user: UserDocType) {

const { last_updated, users } = quotaSnapShot.data() as WorkFlowQuotaType
const { last_updated, users } = quota

if(users.length === MAX_WORK_FLOW_PER_DAY) {
return {
Expand Down Expand Up @@ -167,4 +174,3 @@ async function canTriggerWorkFlow(user: UserDocType) {
valid: true
}
}

0 comments on commit 050d5bb

Please sign in to comment.