Skip to content

Commit

Permalink
Fix /link delete API
Browse files Browse the repository at this point in the history
  • Loading branch information
csuvajit committed Apr 16, 2024
1 parent 63c03c7 commit ec883f4
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 5 deletions.
2 changes: 2 additions & 0 deletions apps/service-auth/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import { GuildsModule } from './guilds/guilds.module';
import { LinksModule } from './links/links.module';
import { PlayersModule } from './players/players.module';
import { RostersModule } from './rosters/rosters.module';
import { ScheduleModule } from '@nestjs/schedule';

@Module({
imports: [
ConfigModule.forRoot({ isGlobal: true }),
ScheduleModule.forRoot(),
MongoDbModule,
RedisModule,
AuthModule,
Expand Down
41 changes: 41 additions & 0 deletions apps/service-auth/src/links/discord-link.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Injectable } from '@nestjs/common';
import { Interval } from '@nestjs/schedule';

@Injectable()
export class DiscordLinkService {
private bearerToken: string;

private onModuleInit() {
this.login();
}

@Interval(1000 * 60 * 60)
private async login() {
const res = await fetch('https://cocdiscord.link/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
username: process.env.DISCORD_LINK_USERNAME,
password: process.env.DISCORD_LINK_PASSWORD,
}),
}).catch(() => null);
const data = (await res?.json().catch(() => null)) as { token?: string } | null;

if (data?.token) this.bearerToken = data.token;
return res?.status === 200 && this.bearerToken;
}

public async unlinkPlayerTag(playerTag: string) {
const res = await fetch(`https://cocdiscord.link/links/${encodeURIComponent(playerTag)}`, {
method: 'DELETE',
headers: {
Authorization: `Bearer ${this.bearerToken}`,
'Content-Type': 'application/json',
},
}).catch(() => null);

return Promise.resolve(res?.status === 200);
}
}
3 changes: 2 additions & 1 deletion apps/service-auth/src/links/links.module.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { ClashClientModule } from '@app/clash-client';
import { DiscordClientModule } from '@app/discord-client';
import { Module } from '@nestjs/common';
import { DiscordLinkService } from './discord-link.service';
import { LinksController } from './links.controller';
import { LinksService } from './links.service';

@Module({
imports: [ClashClientModule, DiscordClientModule],
controllers: [LinksController],
providers: [LinksService],
providers: [LinksService, DiscordLinkService],
})
export class LinksModule {}
7 changes: 6 additions & 1 deletion apps/service-auth/src/links/links.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { DiscordClientService } from '@app/discord-client';
import { PlayerLinksEntity } from '@app/entities';
import { ForbiddenException, Inject, Injectable, Logger, NotFoundException } from '@nestjs/common';
import { Collection } from 'mongodb';
import { DiscordLinkService } from './discord-link.service';
import { CreateLinkInput, DeleteLinkInput } from './dto';

@Injectable()
Expand All @@ -13,7 +14,7 @@ export class LinksService {
constructor(
private discordClientService: DiscordClientService,
private clashClientService: ClashClientService,

private discordLinkService: DiscordLinkService,
@Inject(Collections.PLAYER_LINKS)
private playerLinksCollection: Collection<PlayerLinksEntity>,
) {}
Expand Down Expand Up @@ -80,6 +81,10 @@ export class LinksService {
async deleteLink(userId: string, input: DeleteLinkInput) {
await this.postUnlinkActions(userId, input.playerTag);
await this.playerLinksCollection.deleteOne({ tag: input.playerTag });
await this.discordLinkService.unlinkPlayerTag(input.playerTag);

this.logger.log(`Link ${input.playerTag} deleted by ${userId}`);

return { message: 'OK' };
}

Expand Down
6 changes: 4 additions & 2 deletions apps/service-auth/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { morganLogger } from '@app/helper';
import { Logger, ValidationPipe, VersioningType } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { NestExpressApplication } from '@nestjs/platform-express';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { AppModule } from './app.module';
import { morganLogger } from '@app/helper';

async function bootstrap() {
const app = await NestFactory.create(AppModule, { cors: true });
const app = await NestFactory.create<NestExpressApplication>(AppModule);
const logger = new Logger(AppModule.name);

app.enableShutdownHooks();
app.enableCors();
app.use(morganLogger(logger));
app.useGlobalPipes(new ValidationPipe({ transform: true, whitelist: true }));

Expand Down
36 changes: 36 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"@nestjs/microservices": "^10.2.6",
"@nestjs/passport": "^10.0.2",
"@nestjs/platform-express": "^10.0.0",
"@nestjs/schedule": "^4.0.2",
"@nestjs/swagger": "^7.3.0",
"clashofclans.js": "^3.3.4",
"class-transformer": "^0.5.1",
Expand Down Expand Up @@ -109,4 +110,4 @@
"^@app/validators(|/.*)$": "<rootDir>/libs/validators/src/$1"
}
}
}
}

0 comments on commit ec883f4

Please sign in to comment.