Skip to content

Commit

Permalink
feat: replace cacheManager to redisService at user service
Browse files Browse the repository at this point in the history
  • Loading branch information
stae1102 committed Mar 29, 2024
1 parent 3b4032c commit f479e1b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
12 changes: 3 additions & 9 deletions src/users/users.module.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import { CacheModule, Module } from '@nestjs/common';
import { Module } from '@nestjs/common';
import { UsersController } from './users.controller';
import { UsersService } from './users.service';
import * as redisStore from 'cache-manager-redis-store';
import { UserRepository } from './repository/user.repository';
import { RedisModule } from '../infra/redis/redis.module';

@Module({
imports: [
CacheModule.register({
store: redisStore,
host: process.env.REDIS_HOST,
port: process.env.REDIS_PORT,
}),
],
imports: [RedisModule],
controllers: [UsersController],
providers: [UsersService, UserRepository],
exports: [UserRepository],
Expand Down
15 changes: 8 additions & 7 deletions src/users/users.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import {
CACHE_MANAGER,
Inject,
Injectable,
NotFoundException,
UnauthorizedException,
Expand All @@ -12,15 +10,16 @@ import {
ResetPasswordOutput,
} from './dtos/reset-password.dto';
import { User } from './entities/user.entity';
import { Cache } from 'cache-manager';
import { DeleteAccountOutput } from './dtos/delete-account.dto';
import { UserRepository } from './repository/user.repository';
import { RedisService } from '../infra/redis/redis.service';
import { PASSWORD_CODE_KEY } from '../auth/constants';

@Injectable()
export class UsersService {
constructor(
private readonly userRepository: UserRepository,
@Inject(CACHE_MANAGER) private readonly cacheManager: Cache,
private readonly redisService: RedisService,
) {}

async editProfile(
Expand Down Expand Up @@ -53,11 +52,13 @@ export class UsersService {
queryRunnerManager: EntityManager,
): Promise<ResetPasswordOutput> {
try {
const userId: number | undefined = await this.cacheManager.get(code);
const userId = await this.redisService.get(
`${PASSWORD_CODE_KEY}:${code}`,
);

if (userId) {
const user = await queryRunnerManager.findOne(User, {
where: { id: userId },
where: { id: parseInt(userId) },
select: { id: true, password: true },
});
if (!user) {
Expand All @@ -66,7 +67,7 @@ export class UsersService {
user.password = password;

await queryRunnerManager.save(user); // update password
await this.cacheManager.del(code); // delete verification value
await this.redisService.del(`${PASSWORD_CODE_KEY}:${code}`); // delete verification value

return {};
} else {
Expand Down

0 comments on commit f479e1b

Please sign in to comment.