Skip to content

Commit

Permalink
be-310: Create moderator entitiy
Browse files Browse the repository at this point in the history
  • Loading branch information
BatuhanIlhan committed Nov 18, 2023
1 parent ca1f4e5 commit bf4dedf
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 2 deletions.
2 changes: 2 additions & 0 deletions app/backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { Tag } from './tag/entities/tag.entity';
import { Option } from './option/entities/option.entity';
import { TagModule } from './tag/tag.module';
import { OptionModule } from './option/option.module';
import { ModeratorModule } from './moderator/moderator.module';

@Module({
imports: [
Expand Down Expand Up @@ -48,6 +49,7 @@ import { OptionModule } from './option/option.module';
PollModule,
TagModule,
OptionModule,
ModeratorModule,
],
controllers: [AppController],
providers: [AppService],
Expand Down
3 changes: 1 addition & 2 deletions app/backend/src/auth/guards/auth.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import {
UnauthorizedException,
} from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { log } from 'console';
import { Request } from 'express';
import { UserService } from '../../user/user.service';
import { UserService } from '../../user/user.service';

@Injectable()
export class AuthGuard implements CanActivate {
Expand Down
61 changes: 61 additions & 0 deletions app/backend/src/moderator/entities/moderator.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import * as bcrypt from 'bcrypt';
import { Poll } from '../../poll/entities/poll.entity';
import {
Entity,
Column,
PrimaryGeneratedColumn,
BeforeInsert,
BeforeUpdate,
OneToMany,
Relation,
JoinTable,
ManyToMany,
} from 'typeorm';

const SALT_ROUNDS = 10;

@Entity('moderators')
export class User {
@PrimaryGeneratedColumn('uuid')
id: string;

@Column({ nullable: false, unique: true })
email: string;

@Column({ unique: true, nullable: true, default: null })
username: string;

@Column({ nullable: false, default: false })
isVerified: boolean;

@Column({ nullable: true })
password: string;

@Column({ nullable: true })
verification_code: number;

@Column({ nullable: true })
reset_password_token: number;

@BeforeInsert()
async hashPasswordBeforeInsert() {
if (this.password) {
this.password = await this.getEncryptedPassword(this.password);
}
}

@BeforeUpdate()
async hashPasswordBeforeUpdate() {
if (this.password) {
this.password = await this.getEncryptedPassword(this.password);
}
}

async getEncryptedPassword(password: string): Promise<string> {
return bcrypt.hash(password, SALT_ROUNDS);
}

async compareEncryptedPassword(password: string): Promise<boolean> {
return bcrypt.compare(password, this.password);
}
}
20 changes: 20 additions & 0 deletions app/backend/src/moderator/moderator.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Test, TestingModule } from '@nestjs/testing';
import { ModeratorController } from './moderator.controller';
import { ModeratorService } from './moderator.service';

describe('ModeratorController', () => {
let controller: ModeratorController;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [ModeratorController],
providers: [ModeratorService],
}).compile();

controller = module.get<ModeratorController>(ModeratorController);
});

it('should be defined', () => {
expect(controller).toBeDefined();
});
});
7 changes: 7 additions & 0 deletions app/backend/src/moderator/moderator.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Controller } from '@nestjs/common';
import { ModeratorService } from './moderator.service';

@Controller('moderator')
export class ModeratorController {
constructor(private readonly moderatorService: ModeratorService) {}
}
9 changes: 9 additions & 0 deletions app/backend/src/moderator/moderator.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Module } from '@nestjs/common';
import { ModeratorService } from './moderator.service';
import { ModeratorController } from './moderator.controller';

@Module({
controllers: [ModeratorController],
providers: [ModeratorService],
})
export class ModeratorModule {}
18 changes: 18 additions & 0 deletions app/backend/src/moderator/moderator.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { ModeratorService } from './moderator.service';

describe('ModeratorService', () => {
let service: ModeratorService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [ModeratorService],
}).compile();

service = module.get<ModeratorService>(ModeratorService);
});

it('should be defined', () => {
expect(service).toBeDefined();
});
});
4 changes: 4 additions & 0 deletions app/backend/src/moderator/moderator.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { Injectable } from '@nestjs/common';

@Injectable()
export class ModeratorService {}

0 comments on commit bf4dedf

Please sign in to comment.