Skip to content

Commit 0815f36

Browse files
committed
fix(test): update mock input to meet min length requirements
1 parent 020ed0c commit 0815f36

6 files changed

Lines changed: 61 additions & 20 deletions

File tree

backend/src/app.module.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,13 @@ import { AuthModule } from './auth/auth.module';
2121
host: configService.get<string>('POSTGRES_HOST') ?? 'localhost',
2222
port: configService.get<number>('POSTGRES_PORT') ?? 5432,
2323
username: configService.get<string>('POSTGRES_USER') ?? 'myuser',
24-
password: configService.get<string>('POSTGRES_PASSWORD') ?? 'mypassword',
24+
password:
25+
configService.get<string>('POSTGRES_PASSWORD') ?? 'mypassword',
2526
database: configService.get<string>('POSTGRES_DB') ?? 'deutschmock',
26-
ssl: configService.get<string>('POSTGRES_HOST') !== 'localhost' ? { rejectUnauthorized: false } : false,
27+
ssl:
28+
configService.get<string>('POSTGRES_HOST') !== 'localhost'
29+
? { rejectUnauthorized: false }
30+
: false,
2731
autoLoadEntities: true,
2832
synchronize: true, // Auto-create tables (careful in prod, but ok for MVP)
2933
};
@@ -37,4 +41,4 @@ import { AuthModule } from './auth/auth.module';
3741
controllers: [AppController],
3842
providers: [AppService],
3943
})
40-
export class AppModule { }
44+
export class AppModule {}

backend/src/auth/auth.controller.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { AuthService } from './auth.service';
1313

1414
@Controller('auth')
1515
export class AuthController {
16-
constructor(private authService: AuthService) { }
16+
constructor(private authService: AuthService) {}
1717

1818
@Get('google')
1919
@UseGuards(AuthGuard('google'))
@@ -40,7 +40,9 @@ export class AuthController {
4040
});
4141

4242
// Redirect to Frontend (deliver token)
43-
const frontendUrls = (process.env.FRONTEND_URL || 'http://localhost:3000').split(',');
43+
const frontendUrls = (
44+
process.env.FRONTEND_URL || 'http://localhost:3000'
45+
).split(',');
4446
const primaryUrl = frontendUrls[0].trim();
4547
res.redirect(`${primaryUrl}/auth/callback?${params.toString()}`);
4648
}

backend/src/evaluation/evaluation.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ import { AuthModule } from '../auth/auth.module';
1111
controllers: [EvaluationController],
1212
providers: [EvaluationService],
1313
})
14-
export class EvaluationModule { }
14+
export class EvaluationModule {}

backend/src/evaluation/evaluation.service.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ describe('EvaluationService', () => {
106106
it('should create an evaluation by calling AI and saving to DB', async () => {
107107
// Mock Data
108108
const createEvaluationDto = {
109-
answer: 'My German Text',
109+
answer: 'My German Text is now long enough to pass validation',
110110
level: 'A2',
111111
part: 1,
112112
module: 'writing',

backend/src/evaluation/evaluation.service.ts

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
/* eslint-disable @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-argument */
2-
import { Injectable, NotFoundException, ForbiddenException } from '@nestjs/common';
2+
import {
3+
Injectable,
4+
NotFoundException,
5+
ForbiddenException,
6+
} from '@nestjs/common';
37
import { InjectRepository } from '@nestjs/typeorm';
48
import { Repository } from 'typeorm';
59
import { CreateEvaluationDto } from './dto/create-evaluation.dto';
@@ -29,10 +33,14 @@ export class EvaluationService {
2933
async create(createEvaluationDto: CreateEvaluationDto, user?: User) {
3034
// 1. Check Usage Limit (if user exists)
3135
if (user) {
32-
const freshUser = await this.userRepository.findOne({ where: { id: user.id } });
36+
const freshUser = await this.userRepository.findOne({
37+
where: { id: user.id },
38+
});
3339
if (freshUser) {
3440
const today = new Date().toDateString();
35-
const lastDate = freshUser.last_usage_date ? freshUser.last_usage_date.toDateString() : null;
41+
const lastDate = freshUser.last_usage_date
42+
? freshUser.last_usage_date.toDateString()
43+
: null;
3644

3745
// Reset if new day
3846
if (lastDate !== today) {
@@ -41,7 +49,9 @@ export class EvaluationService {
4149
}
4250

4351
if (freshUser.usage_count >= freshUser.usage_limit) {
44-
throw new ForbiddenException('Daily trial limit reached (10/10). Please upgrade or try again tomorrow.');
52+
throw new ForbiddenException(
53+
'Daily trial limit reached (10/10). Please upgrade or try again tomorrow.',
54+
);
4555
}
4656
}
4757
}
@@ -50,7 +60,9 @@ export class EvaluationService {
5060

5161
// Validation: Minimum Length
5262
if (!answer || answer.trim().length < 20) {
53-
throw new ForbiddenException('Answer is too short. Please write at least 20 characters to proceed.');
63+
throw new ForbiddenException(
64+
'Answer is too short. Please write at least 20 characters to proceed.',
65+
);
5466
}
5567

5668
// Use level/part from task or DTO if available, defaults provided
@@ -68,19 +80,33 @@ export class EvaluationService {
6880
// 2. Increment Usage Count & Update Date (Only on successful AI call)
6981
if (user) {
7082
// We perform a safe update that handles the reset implicitly by setting the value
71-
const freshUser = await this.userRepository.findOne({ where: { id: user.id } });
83+
const freshUser = await this.userRepository.findOne({
84+
where: { id: user.id },
85+
});
7286
if (freshUser) {
7387
const today = new Date();
74-
const lastDateStr = freshUser.last_usage_date ? freshUser.last_usage_date.toDateString() : null;
88+
const lastDateStr = freshUser.last_usage_date
89+
? freshUser.last_usage_date.toDateString()
90+
: null;
7591

7692
if (lastDateStr !== today.toDateString()) {
7793
// First use of the day
78-
await this.userRepository.update({ id: user.id }, { usage_count: 1, last_usage_date: today });
94+
await this.userRepository.update(
95+
{ id: user.id },
96+
{ usage_count: 1, last_usage_date: today },
97+
);
7998
} else {
8099
// Same day, just increment
81-
await this.userRepository.increment({ id: user.id }, 'usage_count', 1);
100+
await this.userRepository.increment(
101+
{ id: user.id },
102+
'usage_count',
103+
1,
104+
);
82105
// Ensure date is current (though distinct days are handled above, keeping it fresh is fine)
83-
await this.userRepository.update({ id: user.id }, { last_usage_date: today });
106+
await this.userRepository.update(
107+
{ id: user.id },
108+
{ last_usage_date: today },
109+
);
84110
}
85111
}
86112
}

backend/src/main.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,20 @@ import { AppModule } from './app.module';
77
async function bootstrap() {
88
const app = await NestFactory.create(AppModule);
99
app.enableCors({
10-
origin: (origin: string, callback: (err: Error | null, allow?: boolean) => void) => {
11-
const allowedOrigins = (process.env.FRONTEND_URL || '').split(',').map(url => url.trim());
10+
origin: (
11+
origin: string,
12+
callback: (err: Error | null, allow?: boolean) => void,
13+
) => {
14+
const allowedOrigins = (process.env.FRONTEND_URL || '')
15+
.split(',')
16+
.map((url) => url.trim());
1217
allowedOrigins.push('http://localhost:3000');
1318

14-
if (!origin || allowedOrigins.includes(origin) || /\.vercel\.app$/.test(origin)) {
19+
if (
20+
!origin ||
21+
allowedOrigins.includes(origin) ||
22+
/\.vercel\.app$/.test(origin)
23+
) {
1524
callback(null, true);
1625
} else {
1726
console.warn(`Blocked CORS for origin: ${origin}`);

0 commit comments

Comments
 (0)