Skip to content

Commit

Permalink
Fix routes
Browse files Browse the repository at this point in the history
  • Loading branch information
csuvajit committed Mar 5, 2024
1 parent daa77d3 commit 911e766
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 45 deletions.
11 changes: 11 additions & 0 deletions apps/service-auth/src/guilds/dto/aggregated.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ClanStoresEntity } from '@app/entities';
import { ClanCategoriesEntity } from '@app/entities/clan-categories.entity';
import { WithId } from 'mongodb';

export class GuildAggregated {
name: string;
guild: string;
createdAt: Date;
clans: WithId<ClanStoresEntity>[];
categories: WithId<ClanCategoriesEntity>[];
}
18 changes: 8 additions & 10 deletions apps/service-auth/src/guilds/guilds.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { JwtAuthGuard } from '@app/auth';
import { Body, Controller, Get, Param, Put, Query, UseGuards } from '@nestjs/common';
import { ApiBearerAuth, ApiBody, ApiTags } from '@nestjs/swagger';
import { ApiBearerAuth, ApiBody, ApiOperation, ApiTags } from '@nestjs/swagger';
import { ReorderCategoriesInput } from './dto/update-clan-categories.dto';
import { GuildsService } from './guilds.service';

Expand All @@ -12,23 +12,21 @@ export class GuildsController {
constructor(private guildsService: GuildsService) {}

@Get('/:guildId/members')
@ApiOperation({ summary: '(Internal)' })
getGuildMembers(@Param('guildId') guildId: string, @Query('q') q: string) {
return this.guildsService.getMembers(guildId, q);
}

@Get('/:guildId/clans')
getClans(@Param('guildId') guildId: string) {
return this.guildsService.getClans(guildId);
}

@Get('/:guildId/clans-and-categories')
getClansWithCategories(@Param('guildId') guildId: string) {
return this.guildsService.getClans(guildId);
@Get('/:guildId')
@ApiOperation({ summary: '(Internal)' })
getGuild(@Param('guildId') guildId: string) {
return this.guildsService.getGuild(guildId);
}

@Put('/:guildId/reorder-clans')
@ApiOperation({ summary: '(Internal)' })
@ApiBody({ type: ReorderCategoriesInput, isArray: true })
updateClansAndCategories(@Param('guildId') guildId: string, @Body() _: ReorderCategoriesInput[]) {
return this.guildsService.getClans(guildId);
return this.guildsService.getGuild(guildId);
}
}
75 changes: 53 additions & 22 deletions apps/service-auth/src/guilds/guilds.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Collections } from '@app/constants';
import { DiscordClientService } from '@app/discord-client';
import { ClanStoresEntity } from '@app/entities';
import { BotGuildsEntity } from '@app/entities/bot-guilds.entity';
import { ClanCategoriesEntity } from '@app/entities/clan-categories.entity';
import { Inject, Injectable } from '@nestjs/common';
import { Inject, Injectable, NotFoundException } from '@nestjs/common';
import { Collection } from 'mongodb';
import { GuildAggregated } from './dto/aggregated.dto';

@Injectable()
export class GuildsService {
Expand All @@ -12,21 +14,55 @@ export class GuildsService {
@Inject(Collections.CLAN_STORES) private clanStoresCollection: Collection<ClanStoresEntity>,
@Inject(Collections.CLAN_CATEGORIES)
private clanCategoriesCollection: Collection<ClanCategoriesEntity>,
@Inject(Collections.BOT_GUILDS)
private botGuildsCollection: Collection<BotGuildsEntity>,
) {}

getMembers(guildId: string, q: string) {
return this.discordClientService.listMembers(guildId, q);
}

async getClansAndCategories(guildId: string) {
const categories = await this.clanCategoriesCollection
.find({ guildId })
.sort({ order: 1 })
.toArray();
const clans = await this.clanStoresCollection
.find({ guild: guildId })
.sort({ order: 1 })
async getGuild(guildId: string) {
const [guild] = await this.botGuildsCollection
.aggregate<GuildAggregated>([
{
$match: {
guild: guildId,
},
},
{
$lookup: {
from: Collections.CLAN_STORES,
localField: 'guild',
foreignField: 'guild',
as: 'clans',
pipeline: [
{
$sort: { order: 1 },
},
],
},
},
{
$lookup: {
from: Collections.CLAN_CATEGORIES,
localField: 'guild',
foreignField: 'guildId',
as: 'categories',
pipeline: [
{
$sort: { order: 1 },
},
],
},
},
])
.toArray();

if (!guild) throw new NotFoundException('Guild not found.');

const categories = guild?.categories ?? [];
const clans = guild?.clans ?? [];
const categoryIds = categories.map((category) => category._id.toHexString());
const categoryMap = Object.fromEntries(categories.map((cat) => [cat._id.toHexString(), cat]));

Expand All @@ -40,7 +76,6 @@ export class GuildsService {
name: curr.name,
tag: curr.tag,
order: curr.order ?? 0,
guildId: curr.guild,
});
return prev;
}, {});
Expand All @@ -66,24 +101,20 @@ export class GuildsService {
clansGrouped.sort((a, b) => a.order - b.order);

return {
categories: categories.map((category) => ({
_id: category._id,
name: category.displayName,
order: category.order,
guildId: category.guildId,
})),
grouped: clansGrouped,
name: guild.name,
guildId: guild.guild,
clans: clans.map((clan) => ({
_id: clan._id,
name: clan.name,
tag: clan.tag,
order: clan.order,
guildId: clan.guild,
})),
categories: categories.map((category) => ({
_id: category._id,
name: category.displayName,
order: category.order,
})),
grouped: clansGrouped,
};
}

getClans(guildId: string) {
return [guildId];
}
}
24 changes: 13 additions & 11 deletions apps/service-auth/src/links/links.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CurrentUser, JwtAuthGuard, JwtUser } from '@app/auth';
import { Body, Controller, Delete, Get, HttpCode, Param, Post, UseGuards } from '@nestjs/common';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
import { BulkLinksDto } from './dto/bulk-links.dto';
import { CreateLinkInput } from './dto/create-links.dto';
import { DeleteLinkInput } from './dto/delete-link.dto';
Expand All @@ -13,16 +13,6 @@ import { LinksService } from './links.service';
export class LinksController {
constructor(private linksService: LinksService) {}

@Post('/')
async createLink(@Body() body: CreateLinkInput) {
return this.linksService.createLink(body);
}

@Delete('/')
async deleteLink(@CurrentUser() user: JwtUser, @Body() body: DeleteLinkInput) {
return this.linksService.deleteLink(user.sub, body);
}

@Get('/:userIdOrTag')
getLink(@Param('userIdOrTag') userIdOrTag: string) {
return this.linksService.getLinksById(userIdOrTag);
Expand All @@ -33,4 +23,16 @@ export class LinksController {
getLinks(@Body() body: BulkLinksDto) {
return this.linksService.getLinks(body.input);
}

@Post('/')
@ApiOperation({ summary: '(Internal)' })
async createLink(@Body() body: CreateLinkInput) {
return this.linksService.createLink(body);
}

@Delete('/')
@ApiOperation({ summary: '(Internal)' })
async deleteLink(@CurrentUser() user: JwtUser, @Body() body: DeleteLinkInput) {
return this.linksService.deleteLink(user.sub, body);
}
}
4 changes: 4 additions & 0 deletions apps/service-auth/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ async function bootstrap() {
.setVersion('1.0')
.addBearerAuth()
.addTag('AUTH')
.addTag('LINKS')
.addTag('PLAYERS')
.addTag('CLANS')
.addTag('GUILDS')
.build();
const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('/', app, document);
Expand Down
5 changes: 3 additions & 2 deletions apps/service-auth/src/players/players.controller.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Controller, Get, Param, Query } from '@nestjs/common';
import { Controller, Get, Param, Query, UseGuards } from '@nestjs/common';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { AttackHistoryQueryInput } from './dto/attack-history-input.dto';
import { AttackHistoryOutput } from './dto/attack-history-output.dto';
import { CWLAttackSummaryOutput } from './dto/attack-summary-output.dto';
import { PlayersService } from './players.service';
import { JwtAuthGuard } from '@app/auth';

@ApiTags('PLAYERS')
@ApiBearerAuth()
// @UseGuards(JwtAuthGuard)
@UseGuards(JwtAuthGuard)
@Controller('/players')
export class PlayersController {
constructor(private playersService: PlayersService) {}
Expand Down
7 changes: 7 additions & 0 deletions libs/entities/src/bot-guilds.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export class BotGuildsEntity {
name: string;

guild: string;

createdAt: Date;
}

0 comments on commit 911e766

Please sign in to comment.