Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: participant tag rules #15

Merged
merged 2 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .yarn/versions/a22b338c.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
releases:
"@aoi-js/frontend": patch
"@aoi-js/server": patch
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@
:messages="hint"
/>
</div>
<VDivider />
<ContestStageTagRulesInput v-model="model.tagRules" />
</template>

<script setup lang="ts">
import type { IContestStage } from '@/types'
import { useI18n } from 'vue-i18n'
import ContestStageTagRulesInput from './ContestStageTagRulesInput.vue'

const { t } = useI18n()

Expand Down
21 changes: 21 additions & 0 deletions apps/frontend/src/components/contest/ContestStageTagRulesInput.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<template>
<VCardText>
<VBtn variant="tonal" v-if="!model" @click="model = {}">Enable Tag Rules</VBtn>
<div v-else>
<VTextField
v-model="model.copyVerifiedFields"
append-icon="mdi-delete"
@click:append="model.copyVerifiedFields = undefined"
label="Comma separated verified fields to copy"
placeholder="name,email,realname,telephone,school,studentGrade"
/>
<VBtn variant="tonal" color="error" @click="model = undefined">Disable Tag Rules</VBtn>
</div>
</VCardText>
</template>

<script setup lang="ts">
import type { IContestStage } from '@aoi-js/server'

const model = defineModel<IContestStage['settings']['tagRules']>({ required: true })
</script>
2 changes: 1 addition & 1 deletion apps/server/src/auth/mail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export class MailAuthProvider extends BaseAuthProvider {
{ _id: userId },
{
$set: { 'profile.email': value.mail, 'authSources.mail': value.mail },
$addToSet: { 'profile.verified': ['email'] }
$addToSet: { 'profile.verified': { $each: ['email'] } }
}
)
return {}
Expand Down
20 changes: 20 additions & 0 deletions apps/server/src/db/contest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
IContestStage
} from '../schemas/contest.js'
import { ISolution } from './solution.js'
import { IUser } from './user.js'
import { IUserProfile } from '../index.js'

export const ContestCapability = {
CAP_ACCESS: capabilityMask(0),
Expand Down Expand Up @@ -84,3 +86,21 @@ export function getCurrentContestStage(now: number, { stages }: IContest) {
}
return stages[0]
}

export async function evalTagRules(
stage: IContestStage,
user: IUser
): Promise<string[] | undefined> {
if (!stage.settings.tagRules) return undefined
const tags: string[] = []
const rules = stage.settings.tagRules
if (rules.copyVerifiedFields && user.profile.verified) {
const { verified } = user.profile
const fields = rules.copyVerifiedFields
.split(',')
.filter((field): field is keyof IUserProfile => verified.includes(field))
.map((field) => `${user.profile[field]}`)
tags.push(...fields)
}
return tags
}
31 changes: 17 additions & 14 deletions apps/server/src/routes/contest/scoped.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import {
OrgCapability,
contestParticipants,
contests,
getCurrentContestStage
evalTagRules,
getCurrentContestStage,
users
} from '../../db/index.js'
import { CAP_ALL, ensureCapability, hasCapability } from '../../utils/index.js'
import { contestAttachmentRoutes } from './attachment.js'
Expand Down Expand Up @@ -111,22 +113,23 @@ export const contestScopedRoutes = defineRoutes(async (s) => {
return rep.forbidden()
}

await contestParticipants.insertOne({
_id: new BSON.UUID(),
userId: req.user.userId,
contestId: ctx._contestId,
results: {},
updatedAt: Date.now()
})

// TODO: add to the corresponding contest
await contests.updateOne(
{ _id: ctx._contestId },
const user = await users.findOne({ _id: req.user.userId })
if (!user) return rep.notFound()
await contestParticipants.insertOne(
{
$inc: { participantCount: 1 }
}
_id: new BSON.UUID(),
userId: req.user.userId,
contestId: ctx._contestId,
results: {},
tags: await evalTagRules(ctx._contestStage, user),
updatedAt: Date.now()
},
{ ignoreUndefined: true }
)

// TODO: add to the corresponding contest
await contests.updateOne({ _id: ctx._contestId }, { $inc: { participantCount: 1 } })

return {}
}
)
Expand Down
27 changes: 19 additions & 8 deletions apps/server/src/routes/plan/contest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import {
SPlanContestSettings,
contestParticipants,
contests,
getCurrentContestStage
evalTagRules,
getCurrentContestStage,
users
} from '../../../index.js'
import { contestAdminRoutes } from './admin.js'
import { BSON } from 'mongodb'
Expand Down Expand Up @@ -98,13 +100,22 @@ const planContestViewRoutes = defineRoutes(async (s) => {
if (!contest) return rep.notFound()
const stage = getCurrentContestStage(Date.now(), contest)
if (!stage.settings.registrationEnabled) return rep.preconditionFailed(`Registration closed`)
await contestParticipants.insertOne({
_id: new BSON.UUID(),
userId: req.user.userId,
contestId: contestId,
results: {},
updatedAt: Date.now()
})
const user = await users.findOne({ _id: req.user.userId })
if (!user) return rep.notFound()
await contestParticipants.insertOne(
{
_id: new BSON.UUID(),
userId: req.user.userId,
contestId: contestId,
results: {},
tags: await evalTagRules(stage, user),
updatedAt: Date.now()
},
{ ignoreUndefined: true }
)

// TODO: see contest register
await contests.updateOne({ _id: contestId }, { $inc: { participantCount: 1 } })
return {}
}
)
Expand Down
10 changes: 9 additions & 1 deletion apps/server/src/schemas/contest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,15 @@ export const SContestStage = Type.StrictObject({
// Enable function function to participants
ranklistEnabled: Type.Boolean(),
// Show participants panel
participantEnabled: Type.Boolean()
participantEnabled: Type.Boolean(),
// Participant tag rules
tagRules: Type.Optional(
Type.Partial(
Type.StrictObject({
copyVerifiedFields: Type.String()
})
)
)
})
)
})
Expand Down