Commit e56d9c9d authored by Yechang's avatar Yechang
Browse files

feat(submission): reject dued submissions

parent 7560baed
Loading
Loading
Loading
Loading
Loading
+28 −0
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@ import _ from 'lodash';
import { z } from 'zod';
import { redis } from '../redis';
import { judgementArgsSchema } from '../oj/args';
import moment from 'moment';

type CreateSubmissionSchema = {
	problemId: number;
@@ -11,6 +12,33 @@ type CreateSubmissionSchema = {
	volumeId: string;
};

export const canSubmit = async (submitterId: number, problemId: number): Promise<boolean> => {
	const problem = await db.problem.findUnique({ where: { id: problemId } })
	if (_.isNil(problem)) {
		return false
	}
	const rel = await db.classesOnUsers.findUnique({
		where: {
			classId_userId: {
				classId: problem.classId,
				userId: submitterId
			}
		}
	})
	if (_.isNil(rel)) {
		return false
	}

	if (rel.role === 'STUDENT') {
		const cur = moment()
		if (cur.isAfter(problem.end) || cur.isBefore(problem.start)) {
			return false
		}
	}

	return true
}

export const createSubmission = async ({
	problemId,
	submitterId,
+8 −1
Original line number Diff line number Diff line
import { submissionFormSchema } from '$lib/components/problem/SubmitCard.svelte';
import { db } from '$lib/server/db';
import { createSubmission } from '$lib/server/db/submission';
import { canSubmit, createSubmission } from '$lib/server/db/submission';
import { redis } from '$lib/server/redis';
import { error, fail, redirect, type Actions, type ServerLoad } from '@sveltejs/kit';
import _ from 'lodash';
@@ -62,6 +62,13 @@ export const actions: Actions = {
			});
		}

		const available = await canSubmit(user.id, problemId);
		if (!available) {
			return message(form, 'You cannot submit it now.', {
				status: 403
			});
		}

		const submission = await createSubmission({ problemId, submitterId: user.id, volumeId: key });
		if (_.isNil(submission)) {
			return fail(500, {