Skip to content

Commit

Permalink
fix: remove user courses dependency from appx
Browse files Browse the repository at this point in the history
  • Loading branch information
devsargam committed Mar 11, 2024
1 parent e562e80 commit a6c02ae
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 47 deletions.
2 changes: 1 addition & 1 deletion src/app/api/discord/redirect/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { giveAccess } from '@/utiles/discord';
import { NextRequest, NextResponse } from 'next/server';
import db from '@/db';
import { getServerSession } from 'next-auth';
import { getPurchases } from '@/utiles/appx';
import { getPurchases } from '@/utiles/getContentForUser';
import { authOptions } from '@/lib/auth';

export async function POST(req: NextRequest) {
Expand Down
2 changes: 1 addition & 1 deletion src/app/courses/[...courseId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from '@/db/course';
import { getServerSession } from 'next-auth';
import { authOptions } from '@/lib/auth';
import { getPurchases } from '@/utiles/appx';
import { getPurchases } from '@/utiles/getContentForUser';
import { redirect } from 'next/navigation';
import { CourseView } from '@/components/CourseView';
import { QueryParams } from '@/actions/types';
Expand Down
3 changes: 2 additions & 1 deletion src/components/MyCourses.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Courses } from '@/components/Courses';
import { authOptions } from '@/lib/auth';
import { getPurchases } from '@/utiles/appx';
import { getPurchases } from '@/utiles/getContentForUser';
import { getServerSession } from 'next-auth';
import { Logout } from './Logout';
import { Course } from '@prisma/client';

const getCourses = async () => {
const session = await getServerSession(authOptions);
// TODO: Hack might give error
Expand Down
45 changes: 1 addition & 44 deletions src/utiles/appx.ts → src/utiles/getContentForUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,8 @@ import {
getVideoProgressForUser,
} from '@/db/course';
import { authOptions } from '@/lib/auth';
import { Course } from '@/store/atoms';
import { getServerSession } from 'next-auth';

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 LOCAL_CMS_PROVIDER = process.env.LOCAL_CMS_PROVIDER;

// TODO: Refactor this in future
// TODO: a lot of logic can be simplified
//! Works but can be done in future
Expand Down Expand Up @@ -57,42 +51,5 @@ export async function getPurchases(email: string) {
};
});

if (LOCAL_CMS_PROVIDER) {
return courses;
}

const baseUrl = `${APPX_BASE_API}/get/checkemailforpurchase`;

const headers = {
'Client-Service': APPX_CLIENT_SERVICE,
'Auth-Key': APPX_AUTH_KEY,
};

const responses: Course[] = [];

const promises = courses.map(async (course) => {
const params = new URLSearchParams({
email,
itemtype: '10',
itemid: course.appxCourseId.toString(),
});
//@ts-ignore
const response = await fetch(`${baseUrl}?${params}`, { headers });
const data = await response.json();

if (data.data === '1') {
responses.push(course);
}
});

await Promise.all(promises);

if (responses.length) {
for (const course of courses) {
if (course.openToEveryone) {
responses.push(course);
}
}
}
return responses;
return courses;
}

0 comments on commit a6c02ae

Please sign in to comment.