Skip to content

Commit

Permalink
Fix routing
Browse files Browse the repository at this point in the history
  • Loading branch information
yuri-becker committed Dec 27, 2023
1 parent 9514b60 commit e4727e3
Show file tree
Hide file tree
Showing 68 changed files with 656 additions and 203 deletions.
13 changes: 7 additions & 6 deletions backend/src/guild/guild-user/guild-user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { RequestUser } from 'src/util/decorators/request-user.decorator';
import { GuildUserService } from './guild-user.service';
import { JwtAuthGuard } from 'src/auth/jwt/guards/jwt-auth.guard';

@Controller('user')
@Controller()
@UseGuards(JwtAuthGuard)
export class GuildUserController {
constructor(
Expand All @@ -22,12 +22,13 @@ export class GuildUserController {
): Promise<SelfDto> {
const [{ rank }, { avatarUrl, name }] = await Promise.all([
this.userService.getGuildUser(userId, guildId),
this.client.users.fetch(userId).then((it) => {
return {
this.client.guilds
.fetch(guildId)
.then((it) => it.members.fetch(userId))
.then((it) => ({
avatarUrl: it.avatarURL({ size: 128 }),
name: it.username,
};
}),
name: it.displayName,
})),
]);
return plainToInstance(SelfDto, {
userId,
Expand Down
2 changes: 1 addition & 1 deletion backend/src/guild/guild-user/guild-user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class GuildUserService {
userId: string,
guildId: string,
): Promise<GuildUser | undefined> {
return await this.database.guildUser.findUnique({
return this.database.guildUser.findUnique({
where: { guildId_userId: { userId, guildId } },
});
}
Expand Down
12 changes: 6 additions & 6 deletions backend/src/guild/moderation/user/user.controller.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import {
Controller,
Get,
Post,
Param,
UseGuards,
NotFoundException,
Req,
Inject,
Logger,
NotFoundException,
Param,
Post,
UseGuards,
} from '@nestjs/common';
import { ApiBearerAuth, ApiOperation, ApiResponse } from '@nestjs/swagger';
import { ChannelType, Client, User } from 'discord.js';
import {
DiscordUser,
usersResponseSchema,
userResponseSchema,
usersResponseSchema,
} from '../dto/user';
import { InjectDiscordClient } from '@discord-nestjs/core';
import { JwtAuthGuard } from 'src/auth/jwt/guards/jwt-auth.guard';
Expand All @@ -27,6 +26,7 @@ import {
UserPurgeEvent,
UserTimeOutEvent,
} from '../events/user.events';

@Controller('/user')
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
Expand Down
4 changes: 2 additions & 2 deletions backend/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ async function bootstrap() {
SwaggerModule.createDocument(
app,
new DocumentBuilder()
.setTitle('Cardinal System')
.setDescription('Cardinal System API description')
.setTitle('Mega Transformers Bot')
.setDescription('Mega Transformers Bot API description')
.setLicense(
`Copyright (C) 2023-${new Date().getFullYear()} `,
'https://github.com/89Q12/discord-bot/blob/main/LICENSE',
Expand Down
16 changes: 16 additions & 0 deletions backend/src/user/self.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Controller, Get, Inject, UseGuards } from '@nestjs/common';
import { SelfDto } from './self.dto';
import { RequestUser } from '../util/decorators/request-user.decorator';
import { JwtAuthGuard } from '../auth/jwt/guards/jwt-auth.guard';
import { SelfService } from './self.service';

@Controller('/user/self')
@UseGuards(JwtAuthGuard)
export class SelfController {
constructor(@Inject(SelfService) private readonly selfService: SelfService) {}

@Get()
async getSelf(@RequestUser() userId: string): Promise<SelfDto> {
return this.selfService.fetchSelf(userId);
}
}
15 changes: 15 additions & 0 deletions backend/src/user/self.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { GuildUser } from '@prisma/client';

export class Guild {
@ApiProperty({ type: String }) guildId: GuildUser['guildId'];
@ApiProperty({ type: String }) name: string;
@ApiProperty({ type: String }) image: string;
}

export class SelfDto {
@ApiProperty({ type: String }) userId: GuildUser['userId'];
@ApiProperty({ type: String }) avatarUrl: string;
@ApiProperty({ type: String }) name: string;
@ApiPropertyOptional({ type: [Guild] }) guilds: Guild[];
}
45 changes: 45 additions & 0 deletions backend/src/user/self.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Inject, Injectable } from '@nestjs/common';
import { InjectDiscordClient } from '@discord-nestjs/core';
import { Client, Guild } from 'discord.js';
import { PrismaService } from '../prisma.service';
import { SelfDto } from './self.dto';
import { User } from '@prisma/client';
import { plainToInstance } from '../util/functions/plain-to-instance';

@Injectable()
export class SelfService {
constructor(
@InjectDiscordClient() private readonly discord: Client,
@Inject(PrismaService) private readonly prisma: PrismaService,
) {}

async fetchSelf(userId: User['userId']): Promise<SelfDto> {
const [discordUser, guilds] = await Promise.all([
this.discord.users.fetch(userId),
this._fetchGuilds(userId),
]);

return plainToInstance(SelfDto, {
userId: discordUser.id,
avatarUrl: discordUser.avatarURL({ size: 128 }),
name: discordUser.username,
guilds: guilds.map((guild) => ({
guildId: guild.id,
name: guild.name,
image: guild.iconURL({ size: 128 }),
})),
});
}

private async _fetchGuilds(userId: User['userId']): Promise<Guild[]> {
return this.prisma.guildUser
.findMany({ where: { userId: userId } })
.then((guildUsers) =>
Promise.all(
guildUsers.map((guildUser) =>
this.discord.guilds.fetch(guildUser.guildId),
),
),
);
}
}
5 changes: 4 additions & 1 deletion backend/src/user/user.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ import { Module } from '@nestjs/common';
import { UserService } from './user.service';
import { PrismaService } from 'src/prisma.service';
import { DiscordModule } from '@discord-nestjs/core';
import { SelfController } from './self.controller';
import { SelfService } from './self.service';

@Module({
imports: [DiscordModule.forFeature()],
providers: [UserService, PrismaService],
providers: [UserService, PrismaService, SelfService],
controllers: [SelfController],
exports: [UserService],
})
export class UserModule {}
2 changes: 1 addition & 1 deletion backend/src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export class UserService {
constructor(@Inject(PrismaService) private database: PrismaService) {}

async findOneUser(userId: string): Promise<User | undefined> {
return await this.database.user.findUnique({
return this.database.user.findUnique({
where: { userId },
});
}
Expand Down
2 changes: 1 addition & 1 deletion frontend/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>🐶 Cardinal System</title>
<title>🐶 Mega Transformers Bot</title>
</head>
<body>
<div id="root"></div>
Expand Down
15 changes: 9 additions & 6 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { RouterProvider } from 'react-router';
import { PageSpinner } from './components/page-spinner';
import { useRouter } from './hooks/ui/use-router.tsx';
import { theme } from './theme';
import { ProvideUserContext } from './state/user.context';
import { ProvideSelfContext } from './state/self.context.tsx';
import { ProvideGuildSelfContext } from './state/guild-self.context.tsx';

const Router = () => {
const routes = useRouter();
Expand All @@ -14,11 +15,13 @@ const Router = () => {
const App: React.FC = () => {
return (
<ChakraProvider theme={theme}>
<ProvideUserContext>
<Suspense fallback={<PageSpinner />}>
<Router />
</Suspense>
</ProvideUserContext>
<ProvideSelfContext>
<ProvideGuildSelfContext>
<Suspense fallback={<PageSpinner />}>
<Router />
</Suspense>
</ProvideGuildSelfContext>
</ProvideSelfContext>
</ChakraProvider>
);
};
Expand Down
Loading

0 comments on commit e4727e3

Please sign in to comment.