Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { ChallengesModule } from './modules/challenges/challenges.module';
import { AlertsModule } from './modules/alerts/alerts.module';
import { AdminModule } from './modules/admin/admin.module';
import { MailModule } from './modules/mail/mail.module';
import { RedisCacheModule } from './modules/cache/cache.module';
import { CacheModule } from './modules/cache/cache.module';
import { WebhooksModule } from './modules/webhooks/webhooks.module';
import { ClaimsModule } from './modules/claims/claims.module';
import { DisputesModule } from './modules/disputes/disputes.module';
Expand Down Expand Up @@ -172,7 +172,7 @@ const envValidationSchema = Joi.object({
},
}),
AuthModule,
RedisCacheModule,
CacheModule,
HealthModule,
BlockchainModule,
UserModule,
Expand Down
7 changes: 7 additions & 0 deletions backend/src/auth/auth.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Test, TestingModule } from '@nestjs/testing';
import { AuthService } from './auth.service';
import { UserService } from '../modules/user/user.service';
import { JwtService } from '@nestjs/jwt';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { ConflictException, UnauthorizedException } from '@nestjs/common';
// import { CACHE_MANAGER } from '@nestjs/cache-manager';
import * as bcrypt from 'bcrypt';
Expand Down Expand Up @@ -42,6 +43,12 @@ describe('AuthService', () => {
sign: jest.fn().mockReturnValue('mock-token'),
},
},
{
provide: EventEmitter2,
useValue: {
emit: jest.fn(),
},
},
// {
// provide: CACHE_MANAGER,
// useValue: mockCacheManager,
Expand Down
13 changes: 11 additions & 2 deletions backend/src/auth/dto/auth.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { IsEmail, IsString, MinLength, MaxLength, IsOptional } from 'class-validator';
import {
IsEmail,
IsString,
MinLength,
MaxLength,
IsOptional,
} from 'class-validator';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { IsStellarPublicKey } from '../../common/validators/is-stellar-key.validator';

Expand All @@ -17,7 +23,10 @@ export class RegisterDto {
@IsString()
name?: string;

@ApiPropertyOptional({ example: 'ABC12345', description: 'Referral code from another user' })
@ApiPropertyOptional({
example: 'ABC12345',
description: 'Referral code from another user',
})
@IsOptional()
@IsString()
referralCode?: string;
Expand Down
26 changes: 14 additions & 12 deletions backend/src/common/interceptors/cache.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import {
ExecutionContext,
CallHandler,
} from '@nestjs/common';
import { Observable, of } from 'rxjs';
import { tap } from 'rxjs/operators';
import { Observable, from, of } from 'rxjs';
import { switchMap, tap } from 'rxjs/operators';
import { CacheStrategyService } from '../../modules/cache/cache-strategy.service';

@Injectable()
Expand All @@ -23,16 +23,18 @@ export class CacheInterceptor implements NestInterceptor {

const cacheKey = `${url}:${JSON.stringify(query)}`;

return this.cacheStrategy.get(cacheKey).then((cached) => {
if (cached) {
return of(cached);
}
return from(this.cacheStrategy.get(cacheKey)).pipe(
switchMap((cached) => {
if (cached) {
return of(cached);
}

return next.handle().pipe(
tap((data) => {
this.cacheStrategy.set(cacheKey, data);
}),
);
});
return next.handle().pipe(
tap((data) => {
this.cacheStrategy.set(cacheKey, data);
}),
);
}),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
ExecutionContext,
CallHandler,
} from '@nestjs/common';
import { Observable } from 'rxjs';
import { EMPTY, Observable } from 'rxjs';
import { finalize } from 'rxjs/operators';
import { GracefulShutdownService } from '../services/graceful-shutdown.service';

Expand All @@ -20,7 +20,7 @@ export class GracefulShutdownInterceptor implements NestInterceptor {
statusCode: 503,
message: 'Service is shutting down',
});
return;
return EMPTY;
}

this.gracefulShutdown.incrementActiveRequests();
Expand Down
21 changes: 17 additions & 4 deletions backend/src/common/services/graceful-shutdown.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ export class GracefulShutdownService implements OnApplicationShutdown {
await this.closeRedis();

const shutdownDuration = Date.now() - shutdownStartTime;
this.logger.log(
`Graceful shutdown completed in ${shutdownDuration}ms`,
);
this.logger.log(`Graceful shutdown completed in ${shutdownDuration}ms`);
}

private async waitForInFlightRequests(): Promise<void> {
Expand Down Expand Up @@ -93,7 +91,22 @@ export class GracefulShutdownService implements OnApplicationShutdown {
try {
if (this.cacheManager) {
this.logger.log('Closing Redis connections...');
await this.cacheManager.reset();
const cacheAny = this.cacheManager as unknown as {
clear?: () => Promise<void>;
stores?: Array<{ disconnect?: () => Promise<void> }>;
};

if (cacheAny.clear) {
await cacheAny.clear();
}

if (Array.isArray(cacheAny.stores)) {
for (const store of cacheAny.stores) {
if (store?.disconnect) {
await store.disconnect();
}
}
}
this.logger.log('Redis connections closed');
}
} catch (error) {
Expand Down
25 changes: 20 additions & 5 deletions backend/src/config/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,25 @@ export default () => ({
),
},
balanceSync: {
cacheTtlSeconds: parseInt(process.env.BALANCE_CACHE_TTL_SECONDS || '300', 10),
pollIntervalMs: parseInt(process.env.BALANCE_POLL_INTERVAL_MS || '5000', 10),
reconnectInitialDelayMs: parseInt(process.env.BALANCE_RECONNECT_INIT_MS || '1000', 10),
reconnectMaxDelayMs: parseInt(process.env.BALANCE_RECONNECT_MAX_MS || '60000', 10),
metricsPersistIntervalMs: parseInt(process.env.BALANCE_METRICS_PERSIST_MS || '60000', 10),
cacheTtlSeconds: parseInt(
process.env.BALANCE_CACHE_TTL_SECONDS || '300',
10,
),
pollIntervalMs: parseInt(
process.env.BALANCE_POLL_INTERVAL_MS || '5000',
10,
),
reconnectInitialDelayMs: parseInt(
process.env.BALANCE_RECONNECT_INIT_MS || '1000',
10,
),
reconnectMaxDelayMs: parseInt(
process.env.BALANCE_RECONNECT_MAX_MS || '60000',
10,
),
metricsPersistIntervalMs: parseInt(
process.env.BALANCE_METRICS_PERSIST_MS || '60000',
10,
),
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import {
TableIndex,
} from 'typeorm';

export class CreateInterestHistoryTable1775400000000
implements MigrationInterface
{
export class CreateInterestHistoryTable1775400000000 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.createTable(
new Table({
Expand Down
8 changes: 7 additions & 1 deletion backend/src/migrations/1776000000000-CreateReferralsTable.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { MigrationInterface, QueryRunner, Table, TableIndex, TableForeignKey } from 'typeorm';
import {
MigrationInterface,
QueryRunner,
Table,
TableIndex,
TableForeignKey,
} from 'typeorm';

export class CreateReferralsTable1776000000000 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
Expand Down
Loading