Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Be 558/add ranking functionality #585

Merged
merged 3 commits into from
Dec 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import { VoteModule } from './vote/vote.module';
import { Vote } from './vote/entities/vote.entity';
import { AnnotationModule } from './annotation/annotation.module';
import { Annotation } from './annotation/entities/annotation.entity';
import { RankingModule } from './ranking/ranking.module';
import { Ranking } from './ranking/entities/ranking.entity';

@Module({
imports: [
Expand All @@ -50,6 +52,7 @@ import { Annotation } from './annotation/entities/annotation.entity';
Report,
Vote,
Annotation,
Ranking
],
database: 'postgres',
synchronize: true,
Expand Down Expand Up @@ -83,6 +86,7 @@ import { Annotation } from './annotation/entities/annotation.entity';
CommentModule,
VoteModule,
AnnotationModule,
RankingModule,
],
controllers: [AppController],
providers: [AppService],
Expand Down
6 changes: 5 additions & 1 deletion app/backend/src/comment/comment.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ import { Poll } from '../poll/entities/poll.entity';
import { BadgeService } from '../badge/badge.service';
import { Report } from '../user/entities/report.entity';
import { TagModule } from '../tag/tag.module';
import { RankingService } from '../ranking/ranking.service';
import { Ranking } from '../ranking/entities/ranking.entity';
import { Vote } from '../vote/entities/vote.entity';

@Module({
imports: [
TypeOrmModule.forFeature([Like, Poll, Tag, User, Badge, Option, Comment, Report]),
TypeOrmModule.forFeature([Like, Poll, Tag, User, Badge, Option, Comment, Report,Ranking,Vote]),
TagModule,
JwtModule.register({
global: true,
Expand All @@ -34,6 +37,7 @@ import { TagModule } from '../tag/tag.module';
PollRepository,
UserService,
BadgeService,
RankingService
],
})
export class CommentModule {}
6 changes: 6 additions & 0 deletions app/backend/src/like/like.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import { UserService } from '../user/user.service';
import { BadgeService } from '../badge/badge.service';
import { Report } from '../user/entities/report.entity';
import { TagModule } from '../tag/tag.module';
import { RankingService } from '../ranking/ranking.service';
import { Ranking } from '../ranking/entities/ranking.entity';
import { Vote } from '../vote/entities/vote.entity';

@Module({
imports: [
Expand All @@ -27,6 +30,8 @@ import { TagModule } from '../tag/tag.module';
Option,
Report,
Comment,
Ranking,
Vote
]),
TagModule,
],
Expand All @@ -37,6 +42,7 @@ import { TagModule } from '../tag/tag.module';
PollRepository,
UserService,
BadgeService,
RankingService
],
})
export class LikeModule {}
5 changes: 2 additions & 3 deletions app/backend/src/poll/entities/poll.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,8 @@ export class Poll {
@OneToMany(() => Option, (option) => option.poll, { cascade: true })
options: Relation<Option[]>;

@OneToOne(() => Option, { nullable: true })
@JoinColumn()
outcome: Option;
@Column({ nullable: true })
outcome: string

@Column({ nullable: true })
outcome_source: string;
Expand Down
6 changes: 6 additions & 0 deletions app/backend/src/poll/poll.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import { Comment } from '../comment/entities/comment.entity';
import { Report } from '../user/entities/report.entity';
import { TagModule } from '../tag/tag.module';
import { TokenDecoderMiddleware } from '../auth/middlewares/tokenDecoder.middleware';
import { RankingService } from '../ranking/ranking.service';
import { Ranking } from '../ranking/entities/ranking.entity';
import { Vote } from '../vote/entities/vote.entity';

@Module({
imports: [
Expand All @@ -36,6 +39,8 @@ import { TokenDecoderMiddleware } from '../auth/middlewares/tokenDecoder.middlew
Like,
Comment,
Report,
Ranking,
Vote
]),
TagModule,
],
Expand All @@ -47,6 +52,7 @@ import { TokenDecoderMiddleware } from '../auth/middlewares/tokenDecoder.middlew
BadgeService,
ModeratorService,
TagService,
RankingService
],
exports: [PollService],
})
Expand Down
20 changes: 10 additions & 10 deletions app/backend/src/poll/poll.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { Like } from '../like/entities/like.entity';
import { Comment } from '../comment/entities/comment.entity';
import { Sort } from './enums/sort.enum';
import { TagService } from '../tag/tag.service';
import { RankingService } from '../ranking/ranking.service';

@Injectable()
export class PollService {
Expand All @@ -29,6 +30,7 @@ export class PollService {
@InjectRepository(Comment)
private readonly commentRepository: Repository<Comment>,
private readonly tagService: TagService,
private readonly rankingService: RankingService
) {}

public async createPoll(createPollDto: any): Promise<Poll> {
Expand Down Expand Up @@ -74,10 +76,10 @@ export class PollService {
settlePollDto: SettlePollRequestDto,
): Promise<void> {
const poll = await this.pollRepository.findOne({
where: { id, creator: { id: user.id } },
relations: ['options', 'outcome'],
where: { id:id , creator: { id: user.id } },
relations: ['options'],
});

console.log(poll);
if (!poll) {
throw new NotFoundException('Poll not found.');
}
Expand All @@ -86,18 +88,15 @@ export class PollService {
throw new BadRequestException('Poll is not eligible to be settled.');
}

if (poll.due_date < new Date()) {
throw new BadRequestException('Due date is passed.');
}
const option = await this.optionRepository.findOne({
where: { answer: settlePollDto.outcome },
where: { answer: settlePollDto.outcome, poll :{ id:id} },
});

if (!option) {
throw new BadRequestException('Outcome option not found.');
}

poll.outcome = option;
poll.outcome = option.id;
poll.outcome_source = settlePollDto.outcome_source;
poll.is_settled = Settle.PENDING;
await this.pollRepository.save(poll);
Expand All @@ -106,7 +105,7 @@ export class PollService {
public async settlePoll(id: string, decision: boolean, feedback: string): Promise<void> {
const poll = await this.pollRepository.findOne({
where: { id },
relations: ['options', 'outcome'],
relations: ['options','tags'],
});

if (!poll) {
Expand All @@ -117,14 +116,15 @@ export class PollService {
throw new BadRequestException('Poll is not eligible to be settled.');
}

const option = poll.options.find((option) => option.id === poll.outcome.id);
const option = poll.options.find((option) => option.id === poll.outcome);

if (!option) {
throw new BadRequestException('Outcome option not found.');
}

if (decision) {
poll.is_settled = Settle.SETTLED;
await this.rankingService.settlePoints(poll,option)
} else {
poll.is_settled = Settle.CANCELLED;
}
Expand Down
3 changes: 1 addition & 2 deletions app/backend/src/poll/repository/poll.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class PollRepository extends Repository<Poll> {
public async findPollById(id) {
return await this.findOne({
where: { id },
relations: ['options', 'tags', 'creator', 'outcome'],
relations: ['options', 'tags', 'creator'],
});
}

Expand Down Expand Up @@ -116,7 +116,6 @@ export class PollRepository extends Repository<Poll> {
.leftJoinAndSelect('poll.options', 'options')
.leftJoinAndSelect('poll.tags', 'tags')
.leftJoinAndSelect('poll.creator', 'creator')
.leftJoinAndSelect('poll.outcome', 'outcome')
.getRawAndEntities();

const combinedResults = entities.map((entity) => {
Expand Down
1 change: 1 addition & 0 deletions app/backend/src/ranking/dto/create-ranking.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export class CreateRankingDto {}
4 changes: 4 additions & 0 deletions app/backend/src/ranking/dto/update-ranking.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { PartialType } from '@nestjs/swagger';
import { CreateRankingDto } from './create-ranking.dto';

export class UpdateRankingDto extends PartialType(CreateRankingDto) {}
23 changes: 23 additions & 0 deletions app/backend/src/ranking/entities/ranking.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Tag } from '../../tag/entities/tag.entity';
import { User } from '../../user/entities/user.entity';
import {
Column,
Entity,ManyToOne, PrimaryGeneratedColumn, Relation
} from 'typeorm';


@Entity('rankings')
export class Ranking {
@PrimaryGeneratedColumn('uuid')
id: string;

@Column({nullable:false})
score: number;

@ManyToOne(type => User, (user)=>user.rankings,{ nullable: false, onDelete: "CASCADE"})
user: Relation<User>;

@ManyToOne(type => Tag, (tag)=>tag.rankings, { nullable: false, onDelete: "CASCADE"} )
tag: Relation<Tag>;

}
20 changes: 20 additions & 0 deletions app/backend/src/ranking/ranking.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Test, TestingModule } from '@nestjs/testing';
import { RankingController } from './ranking.controller';
import { RankingService } from './ranking.service';

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

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

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

it('should be defined', () => {
expect(controller).toBeDefined();
});
});
16 changes: 16 additions & 0 deletions app/backend/src/ranking/ranking.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common';
import { RankingService } from './ranking.service';
import { CreateRankingDto } from './dto/create-ranking.dto';
import { UpdateRankingDto } from './dto/update-ranking.dto';

@Controller('ranking')
export class RankingController {
constructor(private readonly rankingService: RankingService) {}

@Get(':id')
findAll(@Param("id") id : string) {
return this.rankingService.findAll(id);
}


}
24 changes: 24 additions & 0 deletions app/backend/src/ranking/ranking.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Module } from '@nestjs/common';
import { RankingService } from './ranking.service';
import { RankingController } from './ranking.controller';
import { Ranking } from './entities/ranking.entity';
import { TypeOrmModule } from '@nestjs/typeorm';
import { VoteService } from '../vote/vote.service';
import { Vote } from '../vote/entities/vote.entity';
import { User } from '../user/entities/user.entity';
import { Badge } from '../badge/entities/badge.entity';
import { Option } from '../option/entities/option.entity';
import { OptionService } from '../option/option.service';
import { Poll } from '../poll/entities/poll.entity';

@Module({
imports: [
TypeOrmModule.forFeature([Ranking,Vote,
Option,
User,
Badge,Poll])
],
controllers: [RankingController],
providers: [RankingService,VoteService,OptionService],
})
export class RankingModule {}
18 changes: 18 additions & 0 deletions app/backend/src/ranking/ranking.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { RankingService } from './ranking.service';

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

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

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

it('should be defined', () => {
expect(service).toBeDefined();
});
});