Skip to content
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ workflows:
- pm-1955_2
- re-try-failed-jobs
- pm-2539
- pm-2177_user_detais

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[❗❗ correctness]
There is a typo in the branch name pm-2177_user_detais. It should likely be pm-2177_user_details. This could lead to confusion or errors when triggering workflows based on branch names.



- 'build-prod':
Expand Down
70 changes: 69 additions & 1 deletion src/api/ai-workflow/ai-workflow.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { UserRole } from 'src/shared/enums/userRole.enum';
import { ChallengeStatus } from 'src/shared/enums/challengeStatus.enum';
import { LoggerService } from 'src/shared/modules/global/logger.service';
import { GiteaService } from 'src/shared/modules/global/gitea.service';
import { MemberPrismaService } from 'src/shared/modules/global/member-prisma.service';
import { VoteType } from '@prisma/client';

@Injectable()
Expand All @@ -35,6 +36,7 @@ export class AiWorkflowService {

constructor(
private readonly prisma: PrismaService,
private readonly memberPrisma: MemberPrismaService,
private readonly challengeApiService: ChallengeApiService,
private readonly resourceApiService: ResourceApiService,
private readonly giteaService: GiteaService,
Expand Down Expand Up @@ -824,7 +826,73 @@ export class AiWorkflowService {
},
});

return items;
const createdByList = items
.map((item) => item.comments)
.flat()
.map((item) => item.createdBy as string);

const members = await this.memberPrisma.member.findMany({
where: { userId: { in: createdByList.map((id) => BigInt(id)) } },

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[❗❗ correctness]
Using BigInt to convert userId might cause issues if createdByList contains non-numeric strings. Consider validating the input before conversion to ensure all entries are valid numeric strings.

select: {
userId: true,
handle: true,
maxRating: { select: { ratingColor: true } },
},
});

// eslint-disable-next-line @typescript-eslint/no-unsafe-return
const normalized = members.map((m: any) => ({

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[⚠️ maintainability]
The use of any type for members and commentsById can lead to runtime errors due to lack of type safety. Consider defining a specific type or interface for these objects to improve type safety and maintainability.

...m,
userId: m.userId.toString(),
ratingColor: m.maxRating?.ratingColor,
}));

const membersMap = normalized.reduce(
(acc, item) => {
if (item.userId) {
acc[item.userId] = item;
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return acc;
},
{} as Record<string, (typeof members)[0]>,
);

// Reconstruct comments with child comments nested in parent's "comments" property
const commentsById: Record<string, any> = {};

for (const item of items) {
for (const comment of item.comments) {
commentsById[comment.id] = {
...comment,
createdUser: membersMap[comment.createdBy as string],
comments: [],
};
}
}

for (const item of items) {
for (const comment of item.comments) {
if (comment.parentId) {
const parent = commentsById[comment.parentId];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[⚠️ performance]
The nested loop structure for reconstructing comments could be optimized. Consider using a single pass to build the commentsById map and nest child comments, which would improve performance by reducing the complexity from O(n^2) to O(n).

if (parent) {
parent.comments.push(commentsById[comment.id]);
}
}
}
}

for (const item of items) {
item.comments = item.comments
.filter((comment) => !comment.parentId)
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
.map((comment) => commentsById[comment.id]);
}

return items.map((item) => ({
...item,
comments: item.comments,
}));
}

async updateRunItem(
Expand Down
Loading