From 190b6906018668e03e27c6b22c198fa496b4c7dc Mon Sep 17 00:00:00 2001 From: Hentry Martin Date: Mon, 22 Sep 2025 22:23:24 +0200 Subject: [PATCH 01/10] fix: foreign key constraint error --- .circleci/config.yml | 2 +- src/api/ai-workflow/ai-workflow.service.ts | 25 +++++++++++++--------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 4956c7f..9a427ba 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -75,7 +75,7 @@ workflows: only: - develop - feat/ai-workflows - - pm-1957 + - pm-1955_2 - 'build-prod': diff --git a/src/api/ai-workflow/ai-workflow.service.ts b/src/api/ai-workflow/ai-workflow.service.ts index 165e7bb..66b3b51 100644 --- a/src/api/ai-workflow/ai-workflow.service.ts +++ b/src/api/ai-workflow/ai-workflow.service.ts @@ -162,16 +162,21 @@ export class AiWorkflowService { throw new BadRequestException(`User id is not available`); } - const createdComment = await this.prisma.aiWorkflowRunItemComment.create({ - data: { - workflowRunItemId: itemId, - content: body.content, - parentId: body.parentId ?? null, - userId: user.userId.toString(), - }, - }); - - return createdComment; + try { + const createdComment = await this.prisma.aiWorkflowRunItemComment.create({ + data: { + workflowRunItemId: itemId, + content: body.content, + parentId: body.parentId ?? null, + userId: user.userId.toString(), + }, + }); + return createdComment; + } catch (e) { + if (e.code === 'P2003') { + this.logger.debug(e.meta); + } + } } async scorecardExists(scorecardId: string): Promise { From ddd1f8421fb7af72fc96f062171f7f031de51f80 Mon Sep 17 00:00:00 2001 From: Hentry Martin Date: Mon, 22 Sep 2025 23:11:13 +0200 Subject: [PATCH 02/10] fix: foreign key constraint error --- src/api/ai-workflow/ai-workflow.service.ts | 36 +++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/src/api/ai-workflow/ai-workflow.service.ts b/src/api/ai-workflow/ai-workflow.service.ts index 66b3b51..9e8ac4a 100644 --- a/src/api/ai-workflow/ai-workflow.service.ts +++ b/src/api/ai-workflow/ai-workflow.service.ts @@ -174,7 +174,11 @@ export class AiWorkflowService { return createdComment; } catch (e) { if (e.code === 'P2003') { - this.logger.debug(e.meta); + if (e.meta.field_name === 'aiWorkflowRunItemComment_parentId_fkey (index)') { + throw new BadRequestException( + `Invalid workflow id provided! Workflow with id ${workflowId} does not exist!`, + ); + } } } } @@ -375,6 +379,36 @@ export class AiWorkflowService { async createWorkflowRun(workflowId: string, runData: CreateAiWorkflowRunDto) { try { + const submission = runData.submissionId + ? await this.prisma.submission.findUnique({ + where: { id: runData.submissionId }, + }) + : null; + const challengeId = submission?.challengeId; + + if (!challengeId) { + this.logger.error( + `Challenge ID not found for submission ${runData.submissionId}`, + ); + throw new InternalServerErrorException( + `Challenge ID not found for submission ${runData.submissionId}`, + ); + } + + const challenge: ChallengeData = + await this.challengeApiService.getChallengeDetail(challengeId); + + if (!challenge) { + throw new InternalServerErrorException( + `Challenge with id ${challengeId} was not found!`, + ); + } + + this.logger.debug(challenge); + this.logger.debug("Challenge debug") + + // if ([ChallengeStatus.COMPLETED, ChallengeStatus.ACTIVE]) + return await this.prisma.aiWorkflowRun.create({ data: { ...runData, From 61862721353faef4c3eb7fa3501fd58e37872f38 Mon Sep 17 00:00:00 2001 From: Hentry Martin Date: Mon, 22 Sep 2025 23:17:38 +0200 Subject: [PATCH 03/10] fix: foreign key constraint error --- src/api/ai-workflow/ai-workflow.service.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/api/ai-workflow/ai-workflow.service.ts b/src/api/ai-workflow/ai-workflow.service.ts index 9e8ac4a..d750220 100644 --- a/src/api/ai-workflow/ai-workflow.service.ts +++ b/src/api/ai-workflow/ai-workflow.service.ts @@ -174,7 +174,9 @@ export class AiWorkflowService { return createdComment; } catch (e) { if (e.code === 'P2003') { - if (e.meta.field_name === 'aiWorkflowRunItemComment_parentId_fkey (index)') { + if ( + e.meta.field_name === 'aiWorkflowRunItemComment_parentId_fkey (index)' + ) { throw new BadRequestException( `Invalid workflow id provided! Workflow with id ${workflowId} does not exist!`, ); @@ -405,7 +407,7 @@ export class AiWorkflowService { } this.logger.debug(challenge); - this.logger.debug("Challenge debug") + this.logger.debug('Challenge debug'); // if ([ChallengeStatus.COMPLETED, ChallengeStatus.ACTIVE]) From 7726cec4d9652859cc50da0c5046d78b3a44387b Mon Sep 17 00:00:00 2001 From: Hentry Martin Date: Mon, 22 Sep 2025 23:40:27 +0200 Subject: [PATCH 04/10] fix: foreign key constraint error --- src/api/ai-workflow/ai-workflow.service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api/ai-workflow/ai-workflow.service.ts b/src/api/ai-workflow/ai-workflow.service.ts index d750220..363cfc4 100644 --- a/src/api/ai-workflow/ai-workflow.service.ts +++ b/src/api/ai-workflow/ai-workflow.service.ts @@ -178,7 +178,7 @@ export class AiWorkflowService { e.meta.field_name === 'aiWorkflowRunItemComment_parentId_fkey (index)' ) { throw new BadRequestException( - `Invalid workflow id provided! Workflow with id ${workflowId} does not exist!`, + `Invalid parent id provided! Parent comment with id ${body.parentId} does not exist!`, ); } } From 0916f599ec0e457f593f46941dfe947ae87dfb2f Mon Sep 17 00:00:00 2001 From: Hentry Martin Date: Tue, 23 Sep 2025 17:00:57 +0200 Subject: [PATCH 05/10] fix: all workflow run to be created only on submissions which are in review or submission phase --- src/api/ai-workflow/ai-workflow.service.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/api/ai-workflow/ai-workflow.service.ts b/src/api/ai-workflow/ai-workflow.service.ts index 363cfc4..20b91df 100644 --- a/src/api/ai-workflow/ai-workflow.service.ts +++ b/src/api/ai-workflow/ai-workflow.service.ts @@ -406,10 +406,13 @@ export class AiWorkflowService { ); } - this.logger.debug(challenge); - this.logger.debug('Challenge debug'); - - // if ([ChallengeStatus.COMPLETED, ChallengeStatus.ACTIVE]) + const aiWorkflowAllowedPhases = ['Submission', 'Review', 'Iterative Review']; + const isNotInSubmissionOrReviewPhases = challenge.phases?.some(item => aiWorkflowAllowedPhases.includes(item.name)); + if (challenge.status !== ChallengeStatus.COMPLETED || isNotInSubmissionOrReviewPhases) { + throw new InternalServerErrorException( + `Challenge is either not completed or its not in one of these phases ${aiWorkflowAllowedPhases.join(',')}`, + ); + } return await this.prisma.aiWorkflowRun.create({ data: { From 3f4d9519ce54cebd53d3682405b04fa81f750f89 Mon Sep 17 00:00:00 2001 From: Hentry Martin Date: Tue, 23 Sep 2025 17:02:58 +0200 Subject: [PATCH 06/10] fix: all workflow run to be created only on submissions which are in review or submission phase --- src/api/ai-workflow/ai-workflow.service.ts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/api/ai-workflow/ai-workflow.service.ts b/src/api/ai-workflow/ai-workflow.service.ts index 20b91df..b2f8739 100644 --- a/src/api/ai-workflow/ai-workflow.service.ts +++ b/src/api/ai-workflow/ai-workflow.service.ts @@ -406,9 +406,18 @@ export class AiWorkflowService { ); } - const aiWorkflowAllowedPhases = ['Submission', 'Review', 'Iterative Review']; - const isNotInSubmissionOrReviewPhases = challenge.phases?.some(item => aiWorkflowAllowedPhases.includes(item.name)); - if (challenge.status !== ChallengeStatus.COMPLETED || isNotInSubmissionOrReviewPhases) { + const aiWorkflowAllowedPhases = [ + 'Submission', + 'Review', + 'Iterative Review', + ]; + const isNotInSubmissionOrReviewPhases = challenge.phases?.some((item) => + aiWorkflowAllowedPhases.includes(item.name), + ); + if ( + challenge.status !== ChallengeStatus.COMPLETED || + isNotInSubmissionOrReviewPhases + ) { throw new InternalServerErrorException( `Challenge is either not completed or its not in one of these phases ${aiWorkflowAllowedPhases.join(',')}`, ); From 914acf87450121d2d1968cc0c559f043ff5e23a7 Mon Sep 17 00:00:00 2001 From: Hentry Martin Date: Tue, 23 Sep 2025 17:40:15 +0200 Subject: [PATCH 07/10] fix: all workflow run to be created only on submissions which are in review or submission phase --- src/api/ai-workflow/ai-workflow.service.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/api/ai-workflow/ai-workflow.service.ts b/src/api/ai-workflow/ai-workflow.service.ts index b2f8739..a99be84 100644 --- a/src/api/ai-workflow/ai-workflow.service.ts +++ b/src/api/ai-workflow/ai-workflow.service.ts @@ -411,9 +411,9 @@ export class AiWorkflowService { 'Review', 'Iterative Review', ]; - const isNotInSubmissionOrReviewPhases = challenge.phases?.some((item) => - aiWorkflowAllowedPhases.includes(item.name), - ); + const isNotInSubmissionOrReviewPhases = challenge.phases + ?.filter((item) => item.isOpen) + .some((item) => aiWorkflowAllowedPhases.includes(item.name)); if ( challenge.status !== ChallengeStatus.COMPLETED || isNotInSubmissionOrReviewPhases From ac74e6a1d35562dcebdb03e9735a4fdcffb51bbc Mon Sep 17 00:00:00 2001 From: Hentry Martin Date: Tue, 23 Sep 2025 17:50:12 +0200 Subject: [PATCH 08/10] fix: all workflow run to be created only on submissions which are in review or submission phase --- src/api/ai-workflow/ai-workflow.service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api/ai-workflow/ai-workflow.service.ts b/src/api/ai-workflow/ai-workflow.service.ts index a99be84..fd447eb 100644 --- a/src/api/ai-workflow/ai-workflow.service.ts +++ b/src/api/ai-workflow/ai-workflow.service.ts @@ -415,7 +415,7 @@ export class AiWorkflowService { ?.filter((item) => item.isOpen) .some((item) => aiWorkflowAllowedPhases.includes(item.name)); if ( - challenge.status !== ChallengeStatus.COMPLETED || + challenge.status !== ChallengeStatus.COMPLETED && isNotInSubmissionOrReviewPhases ) { throw new InternalServerErrorException( From 26c3abdda1955596109816cf3ee2e910cf15abcd Mon Sep 17 00:00:00 2001 From: Hentry Martin Date: Tue, 23 Sep 2025 20:04:24 +0200 Subject: [PATCH 09/10] fix: all workflow run to be created only on submissions which are in review or submission phase --- src/api/ai-workflow/ai-workflow.service.ts | 27 ++++++++++------------ 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/src/api/ai-workflow/ai-workflow.service.ts b/src/api/ai-workflow/ai-workflow.service.ts index fd447eb..691c199 100644 --- a/src/api/ai-workflow/ai-workflow.service.ts +++ b/src/api/ai-workflow/ai-workflow.service.ts @@ -406,21 +406,18 @@ export class AiWorkflowService { ); } - const aiWorkflowAllowedPhases = [ - 'Submission', - 'Review', - 'Iterative Review', - ]; - const isNotInSubmissionOrReviewPhases = challenge.phases - ?.filter((item) => item.isOpen) - .some((item) => aiWorkflowAllowedPhases.includes(item.name)); - if ( - challenge.status !== ChallengeStatus.COMPLETED && - isNotInSubmissionOrReviewPhases - ) { - throw new InternalServerErrorException( - `Challenge is either not completed or its not in one of these phases ${aiWorkflowAllowedPhases.join(',')}`, - ); + const allowedPhases = ['Submission', 'Review', 'Iterative Review']; + const phases = challenge.phases || []; + const isInAllowedPhase = phases.some( + (phase) => allowedPhases.includes(phase.name) && phase.isOpen, + ); + + if (!isInAllowedPhase) { + if (challenge.status !== 'COMPLETED') { + throw new InternalServerErrorException( + `Challenge ${submission.challengeId} is not in an allowed phase and is not completed.`, + ); + } } return await this.prisma.aiWorkflowRun.create({ From 64e044f8080c1931a9cdeff9e153f8a8927c8fa6 Mon Sep 17 00:00:00 2001 From: Hentry Martin Date: Tue, 23 Sep 2025 20:21:56 +0200 Subject: [PATCH 10/10] fix: lint --- src/api/ai-workflow/ai-workflow.service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api/ai-workflow/ai-workflow.service.ts b/src/api/ai-workflow/ai-workflow.service.ts index 691c199..f20c588 100644 --- a/src/api/ai-workflow/ai-workflow.service.ts +++ b/src/api/ai-workflow/ai-workflow.service.ts @@ -413,7 +413,7 @@ export class AiWorkflowService { ); if (!isInAllowedPhase) { - if (challenge.status !== 'COMPLETED') { + if (challenge.status !== ChallengeStatus.COMPLETED) { throw new InternalServerErrorException( `Challenge ${submission.challengeId} is not in an allowed phase and is not completed.`, );