-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
129 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
'use server'; | ||
import db from '@/db'; | ||
import { Cache } from '@/db/Cache'; | ||
import { getAllCourses } from '@/db/course'; | ||
import { checkUserEmailForPurchase } from '@/utiles/appx-check-mail'; | ||
import { Course } from '@prisma/client'; | ||
|
||
type RefreshDbFn = (args: { userId: string; email: string }) => Promise<{ | ||
error: boolean; | ||
message: string; | ||
}>; | ||
|
||
export const refreshDb: RefreshDbFn = async ({ userId, email }) => { | ||
// Only allow user to refetch every minute | ||
if (Cache.getInstance().get('rate-limit', [userId])) { | ||
return { | ||
error: true, | ||
message: 'Wait sometime before refetching', | ||
}; | ||
} | ||
|
||
const allCourses = (await getAllCourses()) as Course[]; | ||
|
||
// Check which course the user has purchased | ||
const userCourses = await db.userPurchases.findMany({ | ||
where: { | ||
userId, | ||
}, | ||
}); | ||
|
||
const coursesWithoutUser = allCourses.filter((course) => { | ||
return !userCourses.some((userCourse) => userCourse.courseId === course.id); | ||
}); | ||
|
||
const responses: Course[] = []; | ||
|
||
const promises = coursesWithoutUser | ||
.filter((x) => !x.openToEveryone) | ||
.map(async (course) => { | ||
const courseId = course.appxCourseId.toString(); | ||
const data = await checkUserEmailForPurchase(email, courseId); | ||
if (data.data === '1') { | ||
responses.push(course); | ||
} | ||
}); | ||
|
||
await Promise.all(promises); | ||
|
||
responses.forEach(async (res) => { | ||
try { | ||
await db.userPurchases.create({ | ||
data: { | ||
userId, | ||
courseId: res.id, | ||
}, | ||
}); | ||
} catch { | ||
return { error: true, message: 'Unable to insert courses' }; | ||
} | ||
}); | ||
|
||
Cache.getInstance().evict('courses', [email]); | ||
Cache.getInstance().set('rate-limit', [userId], true, 60); | ||
|
||
return { error: false, message: 'Refetched Courses' }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
'use client'; | ||
import { refreshDb } from '@/actions/refresh-db'; | ||
import { Button } from './ui/button'; | ||
import { toast } from 'sonner'; | ||
import { useSession } from 'next-auth/react'; | ||
|
||
export function RefreshDb() { | ||
const session = useSession(); | ||
console.log(session); | ||
|
||
const handleClick = async () => { | ||
// @ts-ignore | ||
const res = await refreshDb({ userId: session.data.user.id }); | ||
if (res.error) { | ||
toast.error(res.message); | ||
} else { | ||
toast.info(res.message); | ||
} | ||
}; | ||
|
||
if (session.status === 'loading') return <>Loading...</>; | ||
|
||
return ( | ||
<div className="flex flex-col gap-2"> | ||
<h1>Don't see all your courses?</h1> | ||
<Button className="dark:text-white" onClick={handleClick}> | ||
Refresh Database | ||
</Button> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const APPX_AUTH_KEY = process.env.APPX_AUTH_KEY; | ||
const APPX_CLIENT_SERVICE = process.env.APPX_CLIENT_SERVICE; | ||
const APPX_BASE_API = process.env.APPX_BASE_API; | ||
|
||
const baseUrl = `${APPX_BASE_API}/get/checkemailforpurchase`; | ||
|
||
const headers = { | ||
'Client-Service': APPX_CLIENT_SERVICE, | ||
'Auth-Key': APPX_AUTH_KEY, | ||
}; | ||
|
||
export async function checkUserEmailForPurchase( | ||
email: string, | ||
courseId: string, | ||
) { | ||
const params = new URLSearchParams({ | ||
email, | ||
itemtype: '10', | ||
itemid: courseId, | ||
}); | ||
//@ts-ignore | ||
const response = await fetch(`${baseUrl}?${params}`, { headers }); | ||
return await response.json(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters