Skip to content

Commit

Permalink
feat: list detected duplicates in warning message (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
nullishamy committed Feb 5, 2024
1 parent d1bb332 commit 97d7daf
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions src/api/checks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import config from '../config'
import { query } from '../db/client'
import { ApiSubmission, ValidatedSubmission } from '../types/submission'
import { runCatching } from '../utils/request'
import { Submission } from '@prisma/client'

interface RequiredValuesOk {
author: GuildMember
Expand Down Expand Up @@ -75,13 +76,18 @@ export async function runNonCriticalChecks (
): Promise<boolean> {
let result = true

const isDuplicate = await checkForDuplicate(submission)
const duplicateSubmissions = await listDuplicates(submission)
const isDuplicate = duplicateSubmissions.length > 0

if (isDuplicate) {
let duplicateContent = 'Possible duplicate submission detected, matched submission links. Matches:\n'
for (const duplicate of duplicateSubmissions) {
duplicateContent += ` ${duplicate.name} [${duplicate.sourceLinks}] (<#${duplicate.reviewThreadId}>)\n`
}

genericLog.warning({
type: 'text',
content:
'Possible duplicate submission detected, matched submission links.',
content: duplicateContent,
ctx: submission.reviewThread
})

Expand Down Expand Up @@ -111,11 +117,11 @@ export async function runNonCriticalChecks (
return result
}

async function checkForDuplicate (
async function listDuplicates (
submission: ValidatedSubmission
): Promise<boolean> {
const count = await query(async (db) =>
await db.submission.count({
): Promise<Submission[]> {
const duplicates = await query(async (db) =>
await db.submission.findMany({
where: {
AND: [
{
Expand All @@ -129,13 +135,16 @@ async function checkForDuplicate (
}
}
]
},
// Order by the submission date
orderBy: {
submittedAt: 'desc'
}
})
)

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

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

0 comments on commit 97d7daf

Please sign in to comment.