(userId?: string, email?: string)
| 21 | }; |
| 22 | |
| 23 | export async function refreshDbInternal(userId?: string, email?: string) { |
| 24 | if (!email) { |
| 25 | return { |
| 26 | error: true, |
| 27 | message: 'You are not logged in', |
| 28 | }; |
| 29 | } |
| 30 | |
| 31 | if (process.env.LOCAL_CMS_PROVIDER) { |
| 32 | return { error: false, message: 'Refetched Courses' }; |
| 33 | } |
| 34 | |
| 35 | if (!userId) { |
| 36 | return { |
| 37 | error: true, |
| 38 | message: 'You are not logged in', |
| 39 | }; |
| 40 | } |
| 41 | |
| 42 | // Only allow user to refetch every minute |
| 43 | if (await cache.get('rate-limit', [email])) { |
| 44 | return { |
| 45 | error: true, |
| 46 | message: 'Wait sometime before refetching', |
| 47 | }; |
| 48 | } |
| 49 | |
| 50 | const allCourses = (await getAllCourses()) as Course[]; |
| 51 | |
| 52 | // Check which course the user has purchased |
| 53 | const userCourses = await db.userPurchases.findMany({ |
| 54 | where: { |
| 55 | userId, |
| 56 | }, |
| 57 | }); |
| 58 | |
| 59 | const coursesWithoutUser = allCourses |
| 60 | .filter((course) => { |
| 61 | return !userCourses.some( |
| 62 | (userCourse) => userCourse.courseId === course.id, |
| 63 | ); |
| 64 | }) |
| 65 | .filter((x) => APPX_COURSE_IDS.includes(x.id)); |
| 66 | |
| 67 | const responses: Course[] = []; |
| 68 | |
| 69 | const promises = coursesWithoutUser |
| 70 | .filter((x) => !x.openToEveryone) |
| 71 | .map(async (course) => { |
| 72 | const courseId = course.appxCourseId.toString(); |
| 73 | |
| 74 | const data = await checkUserEmailForPurchase(email, courseId); |
| 75 | |
| 76 | if (data.data === '1') { |
| 77 | responses.push(course); |
| 78 | } |
| 79 | }); |
| 80 |
no test coverage detected