(req: NextRequest)
| 24 | ]; |
| 25 | |
| 26 | export async function POST(req: NextRequest) { |
| 27 | const session = await getServerSession(authOptions); |
| 28 | const data = await req.json(); |
| 29 | |
| 30 | if (!session || !session?.user) { |
| 31 | return NextResponse.json({}, { status: 401 }); |
| 32 | } |
| 33 | |
| 34 | const res = await getPurchases(session.user.email || ''); |
| 35 | if (res.type === 'error') { |
| 36 | return NextResponse.json( |
| 37 | { msg: 'Ratelimited by appx please try again later' }, |
| 38 | { status: 401 }, |
| 39 | ); |
| 40 | } |
| 41 | |
| 42 | const purchases = res.courses; |
| 43 | |
| 44 | if (!purchases.length) { |
| 45 | return NextResponse.json( |
| 46 | { msg: 'You have not bought a course yet' }, |
| 47 | { status: 401 }, |
| 48 | ); |
| 49 | } |
| 50 | //@ts-ignore |
| 51 | const { discordId, discordUsername } = await giveAccess( |
| 52 | data.code, |
| 53 | [ |
| 54 | ...purchases.map((purchase: any) => purchase.discordRoleId), |
| 55 | ROLES[Math.floor(Math.random() * ROLES.length)], |
| 56 | ], |
| 57 | process.env.GUILD_ID ?? '', |
| 58 | process.env.BOT_TOKEN ?? '', |
| 59 | process.env.DISCORD_ACCESS_KEY ?? '', |
| 60 | process.env.DISCORD_ACCESS_SECRET ?? '', |
| 61 | process.env.DISCORD_REDIRECT_URI ?? '', |
| 62 | ); |
| 63 | |
| 64 | if (!discordId || !discordUsername) { |
| 65 | return NextResponse.json( |
| 66 | { msg: 'Something went wrong while connecting your discord account' }, |
| 67 | { status: 401 }, |
| 68 | ); |
| 69 | } |
| 70 | |
| 71 | await db.discordConnectBulk.create({ |
| 72 | data: { |
| 73 | username: discordUsername, |
| 74 | //@ts-ignore |
| 75 | userId: session.user.id, |
| 76 | discordId, |
| 77 | }, |
| 78 | }); |
| 79 | |
| 80 | return NextResponse.json({}); |
| 81 | } |
nothing calls this directly
no test coverage detected