@@ -2,34 +2,57 @@ import express from "express";
22import GithubClient from "../clients/githubClient.js" ;
33
44const ACCOUNT_AGE_MINIMUM_DAYS = 30 ;
5+ const MIN_PUBLIC_REPOS = 1 ;
6+ const MIN_FOLLOWERS = 0 ; // optional stricter
7+
58const router = express . Router ( ) ;
69
710const githubClient = new GithubClient ( ) ;
811
912const daysSince = ( date ) => {
10- const msPerDay = 1000 * 60 * 60 * 24 ;
11- return Math . floor ( ( new Date ( ) - new Date ( date ) ) / msPerDay ) ;
13+ const msPerDay = 1000 * 60 * 60 * 24 ;
14+ return Math . floor ( ( new Date ( ) - new Date ( date ) ) / msPerDay ) ;
1215} ;
1316
14- router . get ( '/gh-validation/:userId' , async ( req , res ) => {
15- const { userId } = req . params ;
16-
17- try {
18- const { data : userData } = await githubClient . request ( 'GET /user/{user_id}' , {
19- user_id : userId ,
20- } ) ;
21-
22- const accountAge = daysSince ( userData . created_at ) ;
23- const valid = accountAge >= ACCOUNT_AGE_MINIMUM_DAYS ;
24-
25- if ( ! valid ) {
26- console . error ( `Github User ID ${ userId } is invalid. Username: ${ userData . login } ` )
27- }
28- res . status ( 200 ) . json ( { valid} ) ;
29- } catch ( error ) {
30- console . error ( "Error calling GitHub API:" , error ) ;
31- res . status ( 500 ) . json ( { error : "Internal server error." } ) ;
17+ router . get ( "/gh-validation/:userId" , async ( req , res ) => {
18+ const { userId } = req . params ;
19+
20+ try {
21+ const { data : userData } = await githubClient . request (
22+ "GET /user/{user_id}" ,
23+ {
24+ user_id : userId ,
25+ }
26+ ) ;
27+
28+ const accountAge = daysSince ( userData . created_at ) ;
29+ const validAge = accountAge >= ACCOUNT_AGE_MINIMUM_DAYS ;
30+ const validRepos = userData . public_repos >= MIN_PUBLIC_REPOS ;
31+ const validFollowers = userData . followers >= MIN_FOLLOWERS ;
32+ const isUser = userData . type === "User" ;
33+
34+ const valid =
35+ validAge && validRepos && validFollowers && isUser ;
36+
37+ if ( ! valid ) {
38+ console . error (
39+ `Github User ID ${ userId } is invalid. Username: ${ userData . login } `
40+ ) ;
41+ }
42+ res . status ( 200 ) . json ( { valid } ) ;
43+ } catch ( error ) {
44+ console . error ( "Error calling GitHub API:" , error ) ;
45+
46+ // Handle 404 error when user is not found
47+ if ( error . response ?. status === 404 ) {
48+ return res . status ( 404 ) . json ( {
49+ error : "GitHub user not found" ,
50+ valid : false ,
51+ } ) ;
3252 }
53+
54+ res . status ( 500 ) . json ( { error : "Internal server error." } ) ;
55+ }
3356} ) ;
3457
35- export default router ;
58+ export default router ;
0 commit comments