Skip to content

Commit

Permalink
save as rc students when register
Browse files Browse the repository at this point in the history
  • Loading branch information
BlueHorn07 committed Dec 3, 2023
1 parent 7fabd27 commit 3260c95
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
2 changes: 2 additions & 0 deletions src/popo/user/user.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import {TypeOrmModule} from "@nestjs/typeorm";
import {UserController} from './user.controller';
import {UserService} from './user.service';
import {User} from "./user.entity";
import { SettingModule } from '../setting/setting.module';

@Module({
imports: [
TypeOrmModule.forFeature([User]),
SettingModule,
],
controllers: [UserController],
providers: [UserService],
Expand Down
30 changes: 17 additions & 13 deletions src/popo/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import * as crypto from 'crypto';

import { User } from './user.entity';
import { CreateUserDto, UpdateUserDto } from './user.dto';
import { UserStatus } from './user.meta'
import { UserStatus, UserType } from './user.meta'
import { SettingService } from '../setting/setting.service';

const Message = {
EXISTING_EMAIL: 'This email is already used.',
Expand All @@ -18,26 +19,29 @@ export class UserService {
constructor(
@InjectRepository(User)
private readonly userRepo: Repository<User>,
private readonly settingService: SettingService,
) {}

async save(dto: CreateUserDto) {
const existUser = await this.userRepo.findOneBy({ email: dto.email });

if (existUser) {
throw new BadRequestException(Message.EXISTING_EMAIL);
} else {
const cryptoSalt = crypto.randomBytes(64).toString('base64');
const encryptedPassword = this.encryptPassword(dto.password, cryptoSalt);

return this.userRepo.save({
email: dto.email,
password: encryptedPassword,
cryptoSalt: cryptoSalt,
name: dto.name,
userType: dto.userType,
lastLoginAt: new Date(),
});
}

const cryptoSalt = crypto.randomBytes(64).toString('base64');
const encryptedPassword = this.encryptPassword(dto.password, cryptoSalt);

const isRcStudent = await this.settingService.checkRcStudent(dto.email);

return this.userRepo.save({
email: dto.email,
password: encryptedPassword,
cryptoSalt: cryptoSalt,
name: dto.name,
userType: isRcStudent ? UserType.rc_student : dto.userType,
lastLoginAt: new Date(),
});
}

find(findOptions: object) {
Expand Down

0 comments on commit 3260c95

Please sign in to comment.