Skip to content

Commit

Permalink
recheck user after api calls
Browse files Browse the repository at this point in the history
  • Loading branch information
TheKhanj committed May 2, 2024
1 parent 3a88156 commit 73172a9
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 43 deletions.
3 changes: 2 additions & 1 deletion src/api/api.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ export class ApiModule {
const userService = new ApiUserService(
configModule.get("config-service"),
databaseModule.get("database"),
userModule.get("get-user-opt")
userModule.get("get-user-opt"),
userModule.get("recheck-user-opt")
);
const userController = new ApiUserController(userService);
userController.register(router);
Expand Down
6 changes: 5 additions & 1 deletion src/api/api.user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import { IUserStats } from "../database/user.stats";
import { GetUserOpt } from "../user/ops/get.user.opt";
import { IUserConfig } from "../config/config.dto";
import { ConfigService } from "../config/config.service";
import { RecheckUserOpt } from "../user/ops/recheck.user.opt";

export class ApiUserService {
public constructor(
private readonly configService: ConfigService,
private readonly database: Database,
private readonly getUserOpt: GetUserOpt
private readonly getUserOpt: GetUserOpt,
private readonly recheckUserOpt: RecheckUserOpt
) {}

public async queryUsers(offset?: number, limit?: number) {
Expand All @@ -33,9 +35,11 @@ export class ApiUserService {

public async updateUserStats(stats: IUserStats) {
await this.database.set(stats.key, stats.up, stats.down);
await this.recheckUserOpt.execute(stats.key);
}

public async updateUserConfig(userConfig: IUserConfig) {
await this.configService.updateUserConfig(userConfig);
await this.recheckUserOpt.execute(userConfig.key);
}
}
11 changes: 6 additions & 5 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,20 @@ export class AppModule {
const config = await readConfigFile(configPath)();
const configModule = ConfigModule.create(configPath);
const databaseModule = DatabaseModule.create(config.database);
const proxyModule = ProxyModule.create();
const userModule = UserModule.create(
databaseModule.get("database"),
configModule.get("config-service")
databaseModule,
configModule,
proxyModule
);
const proxyModule = ProxyModule.create();

const masterController = new MasterController(
configModule.get("config-event-emitter"),
proxyModule.get("proxy-event-emitter"),
databaseModule.get("database"),
userModule.get("get-user-opt"),
userModule.get("assert-user-stats-opt"),
proxyModule.get("proxy-storage")
proxyModule.get("proxy-storage"),
userModule.get("recheck-user-opt")
);

const apiModule = ApiModule.create(
Expand Down
33 changes: 7 additions & 26 deletions src/master.controller.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Logger } from "./logger";
import { Database } from "./database/database";
import { GetUserOpt } from "./user/ops/get.user.opt";
import { IUserConfig } from "./config/config.dto";
import { ProxyStorage } from "./proxy/proxy.storage";
import { RecheckUserOpt } from "./user/ops/recheck.user.opt";
import { withErrorLogging } from "./utils";
import { ProxyEventEmitter } from "./proxy/proxy.event";
import { ConfigEventEmitter } from "./config/config.event";
Expand All @@ -15,9 +15,9 @@ export class MasterController {
configEventEmitter: ConfigEventEmitter,
proxyEventEmitter: ProxyEventEmitter,
private readonly database: Database,
private readonly getUserOpt: GetUserOpt,
private readonly assertUserStatsOpt: AssertUserStatsOpt,
private readonly proxyStorage: ProxyStorage
private readonly proxyStorage: ProxyStorage,
private readonly recheckUserOpt: RecheckUserOpt
) {
this.registerConfigEventHandlers(configEventEmitter);
this.registerProxyEventHandlers(proxyEventEmitter);
Expand All @@ -33,7 +33,7 @@ export class MasterController {
return;
}

await this.recheckUser(userConfig.key);
await this.recheckUserOpt.execute(userConfig.key);
}

private async handleDeleteUserConfig(userConfig: IUserConfig) {
Expand All @@ -44,30 +44,11 @@ export class MasterController {
return;
}

await this.recheckUser(userConfig.key);
await this.recheckUserOpt.execute(userConfig.key);
}

private async handleUpdateUserConfig(userConfig: IUserConfig) {
await this.recheckUser(userConfig.key);
}

private async recheckUser(userKey: string) {
const user = await this.getUserOpt.execute(userKey);
const proxyExists = this.proxyStorage.exists(userKey);

if (proxyExists && !user.isEnabled()) await this.disableUser(userKey);
else if (!proxyExists && user.isEnabled())
await this.enableUser(user.config);
}

private async enableUser(userConfig: IUserConfig) {
await this.proxyStorage.add(userConfig.key, userConfig.proxy);
this.logger.info(`Enabled user ${userConfig.key}`);
}

private async disableUser(userKey: string) {
await this.proxyStorage.delete(userKey);
this.logger.info(`Disabled user ${userKey}`);
await this.recheckUserOpt.execute(userConfig.key);
}

private registerConfigEventHandlers(ev: ConfigEventEmitter) {
Expand Down Expand Up @@ -95,7 +76,7 @@ export class MasterController {
type === "up" ? amount : 0,
type === "down" ? amount : 0
);
await this.recheckUser(userKey);
await this.recheckUserOpt.execute(userKey);
}

private registerProxyEventHandlers(ev: ProxyEventEmitter) {
Expand Down
13 changes: 13 additions & 0 deletions src/user/ops/disable.user.opt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Logger } from "../../logger";
import { ProxyStorage } from "../../proxy/proxy.storage";

export class DisableUserOpt {
private readonly logger = new Logger("DisableUserOpt");

public constructor(private readonly proxyStorage: ProxyStorage) {}

public async execute(userKey: string) {
await this.proxyStorage.delete(userKey);
this.logger.info(`Disabled user ${userKey}`);
}
}
14 changes: 14 additions & 0 deletions src/user/ops/enable.user.opt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Logger } from "../../logger";
import { IUserConfig } from "../../config/config.dto";
import { ProxyStorage } from "../../proxy/proxy.storage";

export class EnableUserOpt {
private readonly logger = new Logger("EnableUserOpt");

public constructor(private readonly proxyStorage: ProxyStorage) {}

public async execute(userConfig: IUserConfig) {
await this.proxyStorage.add(userConfig.key, userConfig.proxy);
this.logger.info(`Enabled user ${userConfig.key}`);
}
}
23 changes: 23 additions & 0 deletions src/user/ops/recheck.user.opt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { GetUserOpt } from "./get.user.opt";
import { ProxyStorage } from "../../proxy/proxy.storage";
import { EnableUserOpt } from "./enable.user.opt";
import { DisableUserOpt } from "./disable.user.opt";

export class RecheckUserOpt {
public constructor(
private readonly getUserOpt: GetUserOpt,
private readonly proxyStorage: ProxyStorage,
private readonly disableUserOpt: DisableUserOpt,
private readonly enableUserOpt: EnableUserOpt
) {}

public async execute(userKey: string) {
const user = await this.getUserOpt.execute(userKey);
const proxyExists = this.proxyStorage.exists(userKey);

if (proxyExists && !user.isEnabled())
await this.disableUserOpt.execute(userKey);
else if (!proxyExists && user.isEnabled())
await this.enableUserOpt.execute(user.config);
}
}
47 changes: 37 additions & 10 deletions src/user/user.module.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,67 @@
import { Database } from "../database/database";
import { GetUserOpt } from "./ops/get.user.opt";
import { ConfigService } from "../config/config.service";
import { ProxyModule } from "../proxy/proxy.module";
import { ConfigModule } from "../config/config.module";
import { EnableUserOpt } from "./ops/enable.user.opt";
import { RecheckUserOpt } from "./ops/recheck.user.opt";
import { DisableUserOpt } from "./ops/disable.user.opt";
import { DatabaseModule } from "../database/database.module";
import { AssertUserStatsOpt } from "./ops/assert.user.stats.opt";
import { CreateUserStatsOpt } from "./ops/create.user.stats.opt";

export class UserModule {
private constructor(
private readonly getUserOpt: GetUserOpt,
private readonly recheckUserOpt: RecheckUserOpt,
private readonly assertUserStatsOpt: AssertUserStatsOpt,
private readonly createUserStatsOpt: CreateUserStatsOpt
) {}

public static create(
database: Database,
configService: ConfigService
databaseModule: DatabaseModule,
configModule: ConfigModule,
proxyModule: ProxyModule
): UserModule {
const getUserOpt = new GetUserOpt(database, configService);
const createUserStatsOpt = new CreateUserStatsOpt(database);
const getUserOpt = new GetUserOpt(
databaseModule.get("database"),
configModule.get("config-service")
);
const createUserStatsOpt = new CreateUserStatsOpt(
databaseModule.get("database")
);
const assertUserStatsOpt = new AssertUserStatsOpt(
database,
databaseModule.get("database"),
createUserStatsOpt
);
const disableUserOpt = new DisableUserOpt(proxyModule.get("proxy-storage"));
const enableUserOpt = new EnableUserOpt(proxyModule.get("proxy-storage"));
const recheckUserOpt = new RecheckUserOpt(
getUserOpt,
proxyModule.get("proxy-storage"),
disableUserOpt,
enableUserOpt
);

return new UserModule(getUserOpt, assertUserStatsOpt, createUserStatsOpt);
return new UserModule(
getUserOpt,
recheckUserOpt,
assertUserStatsOpt,
createUserStatsOpt
);
}

public get(key: "get-user-opt"): GetUserOpt;
public get(key: "recheck-user-opt"): RecheckUserOpt;
public get(key: "assert-user-stats-opt"): AssertUserStatsOpt;
public get(key: "create-user-stats"): CreateUserStatsOpt;
public get(key: "create-user-stats-opt"): CreateUserStatsOpt;
public get(key: string): unknown {
switch (key) {
case "get-user-opt":
return this.getUserOpt;
case "recheck-user-opt":
return this.recheckUserOpt;
case "assert-user-stats-opt":
return this.assertUserStatsOpt;
case "create-user-stats":
case "create-user-stats-opt":
return this.createUserStatsOpt;
default:
throw new Error("Unreachable code");
Expand Down

0 comments on commit 73172a9

Please sign in to comment.