47 lines
1.7 KiB
TypeScript
47 lines
1.7 KiB
TypeScript
import { MatchingRepositoryPort } from '../core/application/ports/matching.repository.port';
|
|
import { MatchingEntity } from '../core/domain/matching.entity';
|
|
import { Redis } from 'ioredis';
|
|
import { MatchingMapper } from '../matching.mapper';
|
|
import { ConfigService } from '@nestjs/config';
|
|
import { MatchingNotFoundException } from '../core/domain/matching.errors';
|
|
import { InjectRedis } from '@songkeys/nestjs-redis';
|
|
|
|
const REDIS_MATCHING_TTL = 900;
|
|
const REDIS_MATCHING_KEY = 'MATCHER:MATCHING';
|
|
|
|
export class MatchingRepository implements MatchingRepositoryPort {
|
|
private _redisKey: string;
|
|
private _redisTtl: number;
|
|
constructor(
|
|
@InjectRedis() private readonly redis: Redis,
|
|
private readonly configService: ConfigService,
|
|
private readonly mapper: MatchingMapper,
|
|
) {
|
|
this._redisKey =
|
|
this.configService.get('REDIS_MATCHING_KEY') !== undefined
|
|
? (this.configService.get('REDIS_MATCHING_KEY') as string)
|
|
: REDIS_MATCHING_KEY;
|
|
this._redisTtl =
|
|
this.configService.get('REDIS_MATCHING_TTL') !== undefined
|
|
? (this.configService.get('REDIS_MATCHING_TTL') as number)
|
|
: REDIS_MATCHING_TTL;
|
|
}
|
|
|
|
get = async (matchingId: string): Promise<MatchingEntity> => {
|
|
const matching: string | null = await this.redis.get(
|
|
`${this._redisKey}:${matchingId}`,
|
|
);
|
|
if (matching) return this.mapper.toDomain(matching);
|
|
throw new MatchingNotFoundException(new Error('Matching not found'));
|
|
};
|
|
|
|
save = async (matching: MatchingEntity): Promise<void> => {
|
|
await this.redis.set(
|
|
`${this._redisKey}:${matching.id}`,
|
|
this.mapper.toPersistence(matching),
|
|
'EX',
|
|
this._redisTtl,
|
|
);
|
|
};
|
|
}
|