@@ -8,10 +8,11 @@ import {
88} from '@nestjs/common' ;
99import { InjectRepository } from '@nestjs/typeorm' ;
1010import { Cache } from 'cache-manager' ;
11- import { isUUID } from 'class-validator' ;
1211import { TypedJSON } from 'typedjson' ;
1312import { Repository } from 'typeorm' ;
1413import { BenchmarkService } from '../benchmarks/benchmark.service' ;
14+ import { User } from '../users/user.entity' ;
15+ import { FindLastSubmissionByLanguageDTO } from './dto/find-last-submission-by-language.dto' ;
1516import { FindSubmissionDTO } from './dto/find-submission.dto' ;
1617import { InsertSubmissionDTO } from './dto/insert-submission-dto' ;
1718import { JobStatusDTO } from './dto/job-status.dto' ;
@@ -31,21 +32,13 @@ export class SubmissionsService {
3132 const submission = new Submission ( insertSubmissionDTO ) ;
3233 submission . status = 'waiting' ;
3334
34- if ( ! isUUID ( insertSubmissionDTO . benchmarkId ) ) {
35- throw new BadRequestException (
36- `Invalid benchmark id: ${ insertSubmissionDTO . benchmarkId } ` ,
37- ) ;
38- }
39- const benchmark = await this . benchmarkService . findOne ( {
40- id : insertSubmissionDTO . benchmarkId ,
41- } ) ;
35+ const benchmark = await this . benchmarkService . findOne (
36+ insertSubmissionDTO . benchmarkId ,
37+ ) ;
4238
43- if ( ! benchmark ) {
44- throw new BadRequestException (
45- `Could not find benchmark: ${ insertSubmissionDTO . benchmarkId } ` ,
46- ) ;
39+ if ( benchmark ) {
40+ submission . benchmark = benchmark ;
4741 }
48- submission . benchmark = benchmark ;
4942
5043 return submission . save ( ) ;
5144 }
@@ -124,4 +117,36 @@ export class SubmissionsService {
124117 }
125118 }
126119 }
120+
121+ async findLastByLanguage (
122+ filter : FindLastSubmissionByLanguageDTO ,
123+ requestUser : User ,
124+ ) : Promise < Submission | undefined > {
125+ const bench = await this . benchmarkService . findOne ( filter . benchmarkId ) ;
126+ const matchedLanguage = await this . languageMatcher ( filter . language ) ;
127+
128+ if ( ! matchedLanguage ) {
129+ throw new BadRequestException ( `Invalid language: ${ filter . language } ` ) ;
130+ }
131+
132+ return this . submissionsRepository . findOne ( {
133+ where : [
134+ {
135+ user : requestUser ,
136+ benchmark : bench ,
137+ language : matchedLanguage ,
138+ } ,
139+ ] ,
140+ order : { createdAt : 'DESC' } ,
141+ } ) ;
142+ }
143+
144+ async languageMatcher ( language : string ) : Promise < string | undefined > {
145+ switch ( language ) {
146+ case 'python' :
147+ return 'cpython3' ;
148+ default :
149+ return undefined ;
150+ }
151+ }
127152}
0 commit comments