feat: change result range check field

This commit is contained in:
2026-09-09 18:22:06 +03:30
parent f3c86da09b
commit bc333b8289
12 changed files with 67 additions and 44 deletions
+1 -1
View File
@@ -4,6 +4,6 @@
}, },
"errors": { "errors": {
"resultRangeOverlappingConstraint": "The specified score range has some overlaps with other records.", "resultRangeOverlappingConstraint": "The specified score range has some overlaps with other records.",
"resultRangeMinMaxConstraint": "minScore value should be less than maxScore." "resultRangeMinMaxConstraint": "minProbability value should be less than maxProbability."
} }
} }
+1 -1
View File
@@ -4,6 +4,6 @@
}, },
"errors": { "errors": {
"resultRangeOverlappingConstraint": "بازه امتیاز مشخص شده با بازه های دیگر تداخل دارد.", "resultRangeOverlappingConstraint": "بازه امتیاز مشخص شده با بازه های دیگر تداخل دارد.",
"resultRangeMinMaxConstraint": "minScore باید کمتر از maxScore باشد." "resultRangeMinMaxConstraint": "minProbability باید کمتر از maxProbability باشد."
} }
} }
@@ -0,0 +1,20 @@
import { MigrationInterface, QueryRunner } from "typeorm";
export class ChangeResultRangeFields1788950646073 implements MigrationInterface {
name = 'ChangeResultRangeFields1788950646073'
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "result_range" DROP COLUMN "minScore"`);
await queryRunner.query(`ALTER TABLE "result_range" DROP COLUMN "maxScore"`);
await queryRunner.query(`ALTER TABLE "result_range" ADD "minProbability" integer NOT NULL`);
await queryRunner.query(`ALTER TABLE "result_range" ADD "maxProbability" integer NOT NULL`);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "result_range" DROP COLUMN "maxProbability"`);
await queryRunner.query(`ALTER TABLE "result_range" DROP COLUMN "minProbability"`);
await queryRunner.query(`ALTER TABLE "result_range" ADD "maxScore" integer NOT NULL`);
await queryRunner.query(`ALTER TABLE "result_range" ADD "minScore" integer NOT NULL`);
}
}
@@ -6,6 +6,6 @@ Content-Type: application/json
"test": 4, "test": 4,
"title": "احتمال خیلی بالا", "title": "احتمال خیلی بالا",
"description": "احتمال ابتلا خیلی بالا است.", "description": "احتمال ابتلا خیلی بالا است.",
"minScore": 21, "minProbability": 21,
"maxScore": 40 "maxProbability": 40
} }
@@ -8,11 +8,11 @@ export class CreateResultRangeDTO {
@IsNumber() @IsNumber()
@IsNotEmpty() @IsNotEmpty()
minScore!: number; minProbability!: number;
@IsNumber() @IsNumber()
@IsNotEmpty() @IsNotEmpty()
maxScore!: number; maxProbability!: number;
@IsString() @IsString()
@IsNotEmpty() @IsNotEmpty()
@@ -14,11 +14,11 @@ export class FindResultRangeDTO extends PartialType(PaginationQueryDTO) {
@IsNumber() @IsNumber()
@IsOptional() @IsOptional()
minScore?: number; minProbability?: number;
@IsNumber() @IsNumber()
@IsOptional() @IsOptional()
maxScore?: number; maxProbability?: number;
@IsString() @IsString()
@IsOptional() @IsOptional()
@@ -15,13 +15,13 @@ export class ResultRange {
type: 'int', type: 'int',
nullable: false, nullable: false,
}) })
minScore!: number; minProbability!: number;
@Column({ @Column({
type: 'int', type: 'int',
nullable: false, nullable: false,
}) })
maxScore!: number; maxProbability!: number;
@Column({ @Column({
type: 'varchar', type: 'varchar',
@@ -1,8 +1,8 @@
export enum ResultRangeSortField { export enum ResultRangeSortField {
ID = 'id', ID = 'id',
TEST = 'test', TEST = 'test',
MIN_SCORE = 'minScore', MIN_PROBABILITY = 'minProbability',
MAX_SCORE = 'maxScore', MAX_PROBABILITY = 'maxProbability',
TITLE = 'title', TITLE = 'title',
DESCRIPTION = 'description', DESCRIPTION = 'description',
} }
@@ -17,12 +17,12 @@ export const filterResultRanges = (query: FindResultRangeDTO) => {
where.title = ILike(`%${query.title}%`); where.title = ILike(`%${query.title}%`);
} }
if (query.maxScore !== undefined) { if (query.maxProbability !== undefined) {
where.maxScore = query.maxScore; where.maxProbability = query.maxProbability;
} }
if (query.minScore !== undefined) { if (query.minProbability !== undefined) {
where.minScore = query.minScore; where.minProbability = query.minProbability;
} }
return where; return where;
@@ -32,16 +32,16 @@ export class ResultRangeCommandProvider {
private async checkRangeOverlap( private async checkRangeOverlap(
test: number, test: number,
minScore: number, minProbability: number,
maxScore: number, maxProbability: number,
excludeSelf: boolean = false, excludeSelf: boolean = false,
): Promise<void> { ): Promise<void> {
const queryBuilder = this.resultRangeRepository const queryBuilder = this.resultRangeRepository
.createQueryBuilder('resultRange') .createQueryBuilder('resultRange')
.where('resultRange.testId = :test', { test }) .where('resultRange.testId = :test', { test })
.andWhere( .andWhere(
'(resultRange.minScore BETWEEN :minScore AND :maxScore) OR (resultRange.maxScore BETWEEN :minScore AND :maxScore) OR (:minScore BETWEEN resultRange.minScore AND resultRange.maxScore) OR (:maxScore BETWEEN resultRange.minScore AND resultRange.maxScore)', '(resultRange.minProbability BETWEEN :minProbability AND :maxProbability) OR (resultRange.maxProbability BETWEEN :minProbability AND :maxProbability) OR (:minProbability BETWEEN resultRange.minProbability AND resultRange.maxProbability) OR (:maxProbability BETWEEN resultRange.minProbability AND resultRange.maxProbability)',
{ minScore, maxScore }, { minProbability, maxProbability },
); );
let overlappingRanges: Array<ResultRange>; let overlappingRanges: Array<ResultRange>;
@@ -92,8 +92,8 @@ export class ResultRangeCommandProvider {
await this.checkRangeOverlap( await this.checkRangeOverlap(
test.id, test.id,
createResultRangeDto.minScore, createResultRangeDto.minProbability,
createResultRangeDto.maxScore, createResultRangeDto.maxProbability,
); );
const newResultRange = this.resultRangeRepository.create({ const newResultRange = this.resultRangeRepository.create({
@@ -129,23 +129,23 @@ export class ResultRangeCommandProvider {
? updateResultRangeDto.test ? updateResultRangeDto.test
: resultRange.test.id; : resultRange.test.id;
const minScore = const minProbability =
updateResultRangeDto.minScore !== undefined updateResultRangeDto.minProbability !== undefined
? updateResultRangeDto.minScore ? updateResultRangeDto.minProbability
: resultRange.minScore; : resultRange.minProbability;
const maxScore = const maxProbability =
updateResultRangeDto.maxScore !== undefined updateResultRangeDto.maxProbability !== undefined
? updateResultRangeDto.maxScore ? updateResultRangeDto.maxProbability
: resultRange.maxScore; : resultRange.maxProbability;
if (minScore >= maxScore) { if (minProbability >= maxProbability) {
throw new BadRequestException( throw new BadRequestException(
'scoring.errors.resultRangeMinMaxConstraint', 'scoring.errors.resultRangeMinMaxConstraint',
); );
} }
await this.checkRangeOverlap(testId, minScore, maxScore, true); await this.checkRangeOverlap(testId, minProbability, maxProbability, true);
Object.assign(resultRange, updateResultRangeDto); Object.assign(resultRange, updateResultRangeDto);
@@ -14,15 +14,15 @@ export class ResultRangeValidator implements ValidatorConstraintInterface {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const obj = validationArguments?.object as any; const obj = validationArguments?.object as any;
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const { minScore, maxScore } = obj; const { minProbability, maxProbability } = obj;
return minScore < maxScore; return minProbability < maxProbability;
} }
defaultMessage?(): string { defaultMessage?(): string {
const i18n = I18nContext.current(); const i18n = I18nContext.current();
return ( return (
i18n?.t('scoring.errors.resultRangeMinMaxConstraint') || i18n?.t('scoring.errors.resultRangeMinMaxConstraint') ||
'minScore value should be less than maxScore.' 'minProbability value should be less than maxProbability.'
); );
} }
} }
@@ -72,12 +72,12 @@ export class CalculateResultProvider {
rangeRules: true, rangeRules: true,
}, },
}), }),
async (score) => async (probability) =>
manager.find(ResultRange, { manager.find(ResultRange, {
where: { where: {
test: { id: submission.test.id }, test: { id: submission.test.id },
minScore: LessThanOrEqual(score), minProbability: LessThanOrEqual(probability),
maxScore: MoreThanOrEqual(score), maxProbability: MoreThanOrEqual(probability),
}, },
}), }),
); );
@@ -119,12 +119,12 @@ export class CalculateResultProvider {
rangeRules: true, rangeRules: true,
}, },
}), }),
async (score) => async (probability) =>
this.resultRangeRepository.find({ this.resultRangeRepository.find({
where: { where: {
test: { id: submission.testId }, test: { id: submission.testId },
minScore: LessThanOrEqual(score), minProbability: LessThanOrEqual(probability),
maxScore: MoreThanOrEqual(score), maxProbability: MoreThanOrEqual(probability),
}, },
}), }),
); );
@@ -150,7 +150,7 @@ export class CalculateResultProvider {
numericValue?: number; numericValue?: number;
}>, }>,
loadQuestions: (questionIds: number[]) => Promise<Question[]>, loadQuestions: (questionIds: number[]) => Promise<Question[]>,
loadResultRanges: (score: number) => Promise<ResultRange[]>, loadResultRanges: (probability: number) => Promise<ResultRange[]>,
) { ) {
let score = 0; let score = 0;
@@ -239,11 +239,14 @@ export class CalculateResultProvider {
} }
}); });
const resultRanges = await loadResultRanges(score); const probability =
totalScores > 0 ? Math.ceil((score / totalScores) * 100) : 0;
const resultRanges = await loadResultRanges(probability);
return { return {
score, score,
probability: totalScores > 0 ? Math.ceil((score / totalScores) * 100) : 0, probability,
resultRange: resultRanges.length > 0 ? resultRanges[0] : undefined, resultRange: resultRanges.length > 0 ? resultRanges[0] : undefined,
}; };
} }