diff --git a/src/app/api/admin/content/route.ts b/src/app/api/admin/content/route.ts index d43a12599..9bf525233 100644 --- a/src/app/api/admin/content/route.ts +++ b/src/app/api/admin/content/route.ts @@ -1,6 +1,40 @@ import db from '@/db'; +import axios from 'axios'; import { NextRequest, NextResponse } from 'next/server'; +interface DiscordData { + type: string; + thumbnail: string; + title: string; + courseTitle: string; + courseId: number; + currFolderId: number; + mediaId: number; +} + +const sendUpdateToDiscord = async (data: DiscordData) => { + const body = { + content: 'Hello @everyone', + tts: false, + color: 'white', + embeds: [ + { + title: `New ${data?.type === 'notion' ? 'NOTE' : data?.type?.toUpperCase()}`, + description: `${data?.title} has been added in the ${data?.courseTitle} , [Click here to visit this ${data?.type === 'notion' ? 'note' : data?.type}](https://app.100xdevs.com/courses/${data.courseId}/${data.currFolderId}/${data.mediaId})`, + }, + ], + }; + + try { + await axios.post( + process.env.NEXT_PUBLIC_DISCORD_WEBHOOK_URL as string, + body, + ); + } catch (error) { + console.error('Failed to send update to Discord:', error); + } +}; + export const POST = async (req: NextRequest) => { const { type, @@ -10,6 +44,9 @@ export const POST = async (req: NextRequest) => { parentContentId, metadata, adminPassword, + courseTitle, + rest, + checked, }: { type: 'video' | 'folder' | 'notion'; thumbnail: string; @@ -18,6 +55,9 @@ export const POST = async (req: NextRequest) => { parentContentId: number; metadata: any; adminPassword: string; + courseTitle: string; + rest: string[]; + checked: boolean; } = await req.json(); if (adminPassword !== process.env.ADMIN_SECRET) { @@ -103,5 +143,34 @@ export const POST = async (req: NextRequest) => { }); } } - return NextResponse.json({ id: content.id }, { status: 200 }); + + if (checked && (type === 'notion' || type === 'video')) { + if (!process.env.NEXT_PUBLIC_DISCORD_WEBHOOK_URL) { + return NextResponse.json( + { message: 'Environment variable for discord webhook is not set' }, + { status: 500 }, + ); + } + const data: DiscordData = { + type, + thumbnail: + 'https://d2szwvl7yo497w.cloudfront.net/courseThumbnails/video.png', + title, + courseTitle, + courseId, + currFolderId: parseInt(rest[0], 10), + mediaId: content.id, + }; + await sendUpdateToDiscord(data); + } + + return NextResponse.json( + { + message: + checked && (type === 'notion' || type === 'video') + ? 'Content Added and Discord notification has been sent' + : 'Content has been added', + }, + { status: 200 }, + ); }; diff --git a/src/app/services/DiscordService.tsx b/src/app/services/DiscordService.tsx deleted file mode 100644 index b52d51c22..000000000 --- a/src/app/services/DiscordService.tsx +++ /dev/null @@ -1,40 +0,0 @@ -import axios from 'axios'; - -interface DiscordData { - type: string; - thumbnail: string; - title: string; - courseTitle: string; - courseId: number; - mediaId: number; - currFolderId: number; -} - -export default function DiscordService() { - const sendUpdateToDiscord = async (data: DiscordData) => { - const body = { - content: 'Hello @everyone', - tts: false, - color: 'white', - embeds: [ - { - title: `New ${data?.type === 'notion' ? 'NOTE' : data?.type?.toUpperCase()}`, - description: `${data?.title} has been added in the ${data?.courseTitle} , [Click here to visit this ${data?.type === 'notion' ? 'note' : data?.type}](https://app.100xdevs.com/courses/${data.courseId}/${data.currFolderId}/${data.mediaId})`, - }, - ], - }; - - try { - const res = await axios.post( - `${process.env.NEXT_PUBLIC_DISCORD_WEBHOOK_URL}`, - body, - ); - console.log(res); - } catch (error) { - console.log(error); - } - }; - return { - sendUpdateToDiscord, - }; -} diff --git a/src/components/admin/AddContent.tsx b/src/components/admin/AddContent.tsx index 2b5435724..993738da0 100644 --- a/src/components/admin/AddContent.tsx +++ b/src/components/admin/AddContent.tsx @@ -4,8 +4,8 @@ import { AddNotionMetadata } from './AddNotionMetadata'; import { Input } from '../ui/input'; import { useRecoilState } from 'recoil'; import { loader } from '@/store/atoms/loader'; -import DiscordService from '@/app/services/DiscordService'; import { Checkbox } from '../ui/checkbox'; +import { toast } from 'sonner'; export const AddContent = ({ rest, @@ -26,22 +26,6 @@ export const AddContent = ({ const [adminPassword, setAdminPassword] = useState(''); const [loading, setLoading] = useRecoilState(loader); - const { sendUpdateToDiscord } = DiscordService(); - - interface DiscordData { - type: string; - thumbnail: string; - title: string; - courseTitle: string; - courseId: number; - currFolderId: number; - mediaId: number; - } - - const postOnDiscord = (data: DiscordData) => { - sendUpdateToDiscord(data); - }; - const handleContentSubmit = async () => { setLoading(true); const response = await fetch('/api/admin/content', { @@ -54,6 +38,9 @@ export const AddContent = ({ parentContentId, metadata, adminPassword, + courseTitle, + rest, + checked, }), method: 'POST', headers: { @@ -61,27 +48,15 @@ export const AddContent = ({ }, }); setLoading(false); + const responseData = await response.json(); + console.log(responseData); - const respData: { id: number } = await response.json(); - - const data = { - type, - thumbnail: - 'https://d2szwvl7yo497w.cloudfront.net/courseThumbnails/video.png', - title, - courseTitle, - courseId, - currFolderId: parseInt(rest[0], 10), - mediaId: respData.id, - }; - - if (checked && response.status === 200) { - if (type === 'notion' || type === 'video') { - postOnDiscord(data); - setLoading(false); - } + if (response.status === 200) { + // handle success if needed + toast.success(responseData.message); } else { - setLoading(false); + // handle error if needed + toast.error(responseData.message || 'Something went wrong'); } }; diff --git a/tsconfig.json b/tsconfig.json index cec286f06..1f308869a 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -16,14 +16,14 @@ "incremental": true, "plugins": [ { - "name": "next", - }, + "name": "next" + } ], "paths": { "@/*": ["./src/*"], - "@public/*": ["./public/*"], - }, + "@public/*": ["./public/*"] + } }, "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"], - "exclude": ["node_modules"], + "exclude": ["node_modules"] }