Skip to content

Commit 97d7daf

Browse files
authored
feat: list detected duplicates in warning message (#142)
1 parent d1bb332 commit 97d7daf

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

src/api/checks.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import config from '../config'
55
import { query } from '../db/client'
66
import { ApiSubmission, ValidatedSubmission } from '../types/submission'
77
import { runCatching } from '../utils/request'
8+
import { Submission } from '@prisma/client'
89

910
interface RequiredValuesOk {
1011
author: GuildMember
@@ -75,13 +76,18 @@ export async function runNonCriticalChecks (
7576
): Promise<boolean> {
7677
let result = true
7778

78-
const isDuplicate = await checkForDuplicate(submission)
79+
const duplicateSubmissions = await listDuplicates(submission)
80+
const isDuplicate = duplicateSubmissions.length > 0
7981

8082
if (isDuplicate) {
83+
let duplicateContent = 'Possible duplicate submission detected, matched submission links. Matches:\n'
84+
for (const duplicate of duplicateSubmissions) {
85+
duplicateContent += ` ${duplicate.name} [${duplicate.sourceLinks}] (<#${duplicate.reviewThreadId}>)\n`
86+
}
87+
8188
genericLog.warning({
8289
type: 'text',
83-
content:
84-
'Possible duplicate submission detected, matched submission links.',
90+
content: duplicateContent,
8591
ctx: submission.reviewThread
8692
})
8793

@@ -111,11 +117,11 @@ export async function runNonCriticalChecks (
111117
return result
112118
}
113119

114-
async function checkForDuplicate (
120+
async function listDuplicates (
115121
submission: ValidatedSubmission
116-
): Promise<boolean> {
117-
const count = await query(async (db) =>
118-
await db.submission.count({
122+
): Promise<Submission[]> {
123+
const duplicates = await query(async (db) =>
124+
await db.submission.findMany({
119125
where: {
120126
AND: [
121127
{
@@ -129,13 +135,16 @@ async function checkForDuplicate (
129135
}
130136
}
131137
]
138+
},
139+
// Order by the submission date
140+
orderBy: {
141+
submittedAt: 'desc'
132142
}
133143
})
134144
)
135145

136-
// We insert the submission before this code runs, so we count how many match
137-
// and if it is > 1, that means there's a duplicate somewhere
138-
return count > 1
146+
// We insert the submission before this code runs, so we slice the first element off.
147+
return duplicates.slice(1)
139148
}
140149

141150
const ghClient = new GraphQLClient('https://api.github.com/graphql')

0 commit comments

Comments
 (0)