Skip to content

Commit

Permalink
Repository setup for common functions
Browse files Browse the repository at this point in the history
  • Loading branch information
csuvajit committed Dec 30, 2023
1 parent affaa56 commit 3077f11
Show file tree
Hide file tree
Showing 17 changed files with 126 additions and 24 deletions.
2 changes: 1 addition & 1 deletion apps/service-auth/src/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class AuthService {
@Inject(Tokens.REDIS) private readonly redis: RedisClient,
@Inject(Tokens.REST) private readonly restHandler: RestHandler,
private readonly redisService: RedisService,
private readonly mongoService: MongoDbService,
private readonly mongoDbService: MongoDbService,
private readonly jwtService: JwtService,
) {}

Expand Down
6 changes: 3 additions & 3 deletions apps/service-capital/src/service-capital.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class CapitalService {
@Inject(Tokens.REST) private restHandler: RestHandler,
private restService: RestService,
private redisService: RedisService,
private mongoService: MongoDbService,
private mongoDbService: MongoDbService,

@Inject(Collections.CAPITAL_RAID_SEASONS)
private raidSeasonsCollection: Collection<CapitalRaidSeasonsEntity>,
Expand Down Expand Up @@ -63,7 +63,7 @@ export class CapitalService {
}

private async loadClans() {
const clans = await this.mongoService.getTrackedClans();
const clans = await this.mongoDbService.getTrackedClans();
for (const clan of clans) this.cached.set(clan.tag, clan);
}

Expand Down Expand Up @@ -170,7 +170,7 @@ export class CapitalService {
}

if (players.length) {
await this.mongoService.trackActivity(players);
await this.mongoDbService.trackActivity(players);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions apps/service-clans/src/service-clans.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class ClansService {
@Inject(Tokens.REDIS) private redis: RedisClient,
@Inject(Tokens.REST) private restHandler: RestHandler,
private redisService: RedisService,
private mongoService: MongoDbService,
private mongoDbService: MongoDbService,

@Inject(Collections.LAST_SEEN)
private lastSeenCollection: Collection<LastSeenEntity>,
Expand Down Expand Up @@ -43,7 +43,7 @@ export class ClansService {
}

private async loadClans() {
const clans = await this.mongoService.getTrackedClans();
const clans = await this.mongoDbService.getTrackedClans();
for (const clan of clans) this.cached.set(clan.tag, clan);
}

Expand Down
4 changes: 2 additions & 2 deletions apps/service-players/src/service-players.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class PlayersService {
@Inject(Tokens.REDIS) private redis: RedisClient,
@Inject(Tokens.REST) private restHandler: RestHandler,
private redisService: RedisService,
private mongoService: MongoDbService,
private mongoDbService: MongoDbService,

@Inject(Collections.LAST_SEEN)
private lastSeenCollection: Collection<LastSeenEntity>,
Expand Down Expand Up @@ -43,7 +43,7 @@ export class PlayersService {
}

private async loadClans() {
const clans = await this.mongoService.getTrackedClans();
const clans = await this.mongoDbService.getTrackedClans();
for (const clan of clans) this.cached.set(clan.tag, clan);
}

Expand Down
4 changes: 2 additions & 2 deletions apps/service-ranking/src/service-ranking.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class RankingService {
@Inject(Tokens.REDIS) private redis: RedisClient,
@Inject(Tokens.REST) private restHandler: RestHandler,
private redisService: RedisService,
private mongoService: MongoDbService,
private mongoDbService: MongoDbService,

@Inject(Collections.LAST_SEEN)
private lastSeenCollection: Collection<LastSeenEntity>,
Expand Down Expand Up @@ -43,7 +43,7 @@ export class RankingService {
}

private async loadClans() {
const clans = await this.mongoService.getTrackedClans();
const clans = await this.mongoDbService.getTrackedClans();
for (const clan of clans) this.cached.set(clan.tag, clan);
}

Expand Down
14 changes: 4 additions & 10 deletions apps/service-wars/src/service-wars.service.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
import { Collections, Tokens } from '@app/constants';
import { Collections } from '@app/constants';
import { LastSeenEntity } from '@app/entities';
import { MongoDbService, TrackedClanList } from '@app/mongodb';
import { RedisClient, RedisService } from '@app/redis';
import RestHandler from '@app/rest/rest.module';
import { Inject, Injectable, Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { Collection, Db } from 'mongodb';
import { Collection } from 'mongodb';

@Injectable()
export class WarsService {
constructor(
private configService: ConfigService,
@Inject(Tokens.MONGODB) private db: Db,
@Inject(Tokens.REDIS) private redis: RedisClient,
@Inject(Tokens.REST) private restHandler: RestHandler,
private redisService: RedisService,
private mongoService: MongoDbService,
private mongoDbService: MongoDbService,

@Inject(Collections.LAST_SEEN)
private lastSeenCollection: Collection<LastSeenEntity>,
Expand Down Expand Up @@ -43,7 +37,7 @@ export class WarsService {
}

private async loadClans() {
const clans = await this.mongoService.getTrackedClans();
const clans = await this.mongoDbService.getTrackedClans();
for (const clan of clans) this.cached.set(clan.tag, clan);
}

Expand Down
4 changes: 2 additions & 2 deletions libs/helper/src/app-cluster.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable, Logger } from '@nestjs/common';
import cluster from 'cluster';
import os from 'os';
import cluster from 'node:cluster';
import os from 'node:os';

const numCPUs = os.cpus().length;

Expand Down
37 changes: 37 additions & 0 deletions libs/redis/src/redis.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,43 @@ export const getRedisKey = (prefix: RedisKeyPrefixes, key: string): string => {
return `${prefix}${key}`;
};

export class SerializedPlayer {
name: string;
tag: string;
townHallLevel: number;
townHallWeaponLevel: number;
trophies: number;
attackWins: number;
defenseWins: number;
donations: number;
donationsReceived: number;

leagueId: string;

clan: {
tag: string;
name: string;
};

troops: {
//
};

spells: {
//
};

heroes: {
//
};

achievements: {
//
};

lastSeen: Date;
}

@Injectable()
export class RedisService {
constructor(@Inject(Tokens.REDIS) private readonly redis: RedisClient) {}
Expand Down
12 changes: 12 additions & 0 deletions libs/repositories/src/capital-raid-reminders.repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Collections } from '@app/constants';
import { CapitalRaidRemindersEntity } from '@app/entities';
import { Inject, Injectable } from '@nestjs/common';
import { Collection } from 'mongodb';

@Injectable()
export class CapitalRaidRemindersRepository {
constructor(
@Inject(Collections.RAID_REMINDERS)
public collection: Collection<CapitalRaidRemindersEntity>,
) {}
}
12 changes: 12 additions & 0 deletions libs/repositories/src/capital-raid-seasons.repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Collections } from '@app/constants';
import { CapitalRaidSeasonsEntity } from '@app/entities';
import { Inject, Injectable } from '@nestjs/common';
import { Collection } from 'mongodb';

@Injectable()
export class CapitalRaidSeasonsRepository {
constructor(
@Inject(Collections.CAPITAL_RAID_SEASONS)
public collection: Collection<CapitalRaidSeasonsEntity>,
) {}
}
12 changes: 12 additions & 0 deletions libs/repositories/src/clan-stores.repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Collections } from '@app/constants';
import { ClanStoresEntity } from '@app/entities';
import { Inject, Injectable } from '@nestjs/common';
import { Collection } from 'mongodb';

@Injectable()
export class ClanStoresRepository {
constructor(
@Inject(Collections.CLAN_STORES)
public collection: Collection<ClanStoresEntity>,
) {}
}
3 changes: 3 additions & 0 deletions libs/repositories/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './capital-raid-seasons.repository';
export * from './clan-stores.repository';
export * from './repositories.module';
7 changes: 7 additions & 0 deletions libs/repositories/src/repositories.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Module } from '@nestjs/common';

@Module({
providers: [],
exports: [],
})
export class RepositoryModule {}
9 changes: 9 additions & 0 deletions libs/repositories/tsconfig.lib.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"declaration": true,
"outDir": "../../dist/libs/repositories"
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist", "test", "**/*spec.ts"]
}
11 changes: 10 additions & 1 deletion nest-cli.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,15 @@
"compilerOptions": {
"tsConfigPath": "apps/service-ranking/tsconfig.app.json"
}
},
"repositories": {
"type": "library",
"root": "libs/repositories",
"entryFile": "index",
"sourceRoot": "libs/repositories/src",
"compilerOptions": {
"tsConfigPath": "libs/repositories/tsconfig.lib.json"
}
}
}
}
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@
"^@app/auth(|/.*)$": "<rootDir>/libs/auth/src/$1",
"^@app/elastic(|/.*)$": "<rootDir>/libs/elastic/src/$1",
"^@app/helper(|/.*)$": "<rootDir>/libs/helper/src/$1",
"^@app/rest(|/.*)$": "<rootDir>/libs/rest/src/$1"
"^@app/rest(|/.*)$": "<rootDir>/libs/rest/src/$1",
"^@app/repositories(|/.*)$": "<rootDir>/libs/repositories/src/$1"
}
}
}
6 changes: 6 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@
],
"@app/rest/*": [
"libs/rest/src/*"
],
"@app/repositories": [
"libs/repositories/src"
],
"@app/repositories/*": [
"libs/repositories/src/*"
]
}
}
Expand Down

0 comments on commit 3077f11

Please sign in to comment.