Skip to content

Commit c6f7dfb

Browse files
committed
chore: Return created/updated records from mutations (#2627)
1 parent 715703d commit c6f7dfb

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

src/lib/services/contest_task_pairs.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,15 @@ export async function getContestTaskPair(
4242
* @param taskTableIndex - The table index of the task.
4343
* @param taskId - The ID of the task.
4444
*
45+
* @returns The created ContestTaskPair object or the existing one if it already exists.
46+
*
4547
* @throws Will throw an error if the creation fails.
4648
*/
4749
export async function createContestTaskPair(
4850
contestId: string,
4951
taskTableIndex: string,
5052
taskId: string,
51-
): Promise<void> {
53+
): Promise<ContestTaskPair> {
5254
try {
5355
const contestTaskPair = await db.contestTaskPair.create({
5456
data: {
@@ -59,10 +61,18 @@ export async function createContestTaskPair(
5961
});
6062

6163
console.log('Created ContestTaskPair:', contestTaskPair);
64+
65+
return contestTaskPair;
6266
} catch (error) {
6367
if (error instanceof Prisma.PrismaClientKnownRequestError && error.code === 'P2002') {
6468
console.log(`ContestTaskPair already exists: contestId=${contestId}, taskId=${taskId}`);
65-
return;
69+
const existingPair = await getContestTaskPair(contestId, taskId);
70+
71+
if (!existingPair) {
72+
throw new Error('Unexpected: record exists but cannot be fetched');
73+
}
74+
75+
return existingPair;
6676
}
6777

6878
console.error('Failed to create ContestTaskPair:', error);
@@ -77,13 +87,15 @@ export async function createContestTaskPair(
7787
* @param taskTableIndex: The table index of the task.
7888
* @param taskId: The ID of the task.
7989
*
90+
* @returns The updated ContestTaskPair object.
91+
*
8092
* @throws Will throw an error if the update fails or if the record does not exist.
8193
*/
8294
export async function updateContestTaskPair(
8395
contestId: string,
8496
taskTableIndex: string,
8597
taskId: string,
86-
): Promise<void> {
98+
): Promise<ContestTaskPair> {
8799
try {
88100
const updatedContestTaskPair = await db.contestTaskPair.update({
89101
where: {
@@ -98,9 +110,13 @@ export async function updateContestTaskPair(
98110
});
99111

100112
console.log('Updated ContestTaskPair:', updatedContestTaskPair);
113+
114+
return updatedContestTaskPair;
101115
} catch (error) {
102116
if (error instanceof Prisma.PrismaClientKnownRequestError && error.code === 'P2025') {
103-
console.error(`Not found ContestTaskPair: contestId=${contestId}, taskId=${taskId}`);
117+
const errorMessage = `Not found ContestTaskPair: contestId=${contestId}, taskId=${taskId}`;
118+
console.error(errorMessage);
119+
throw new Error(errorMessage);
104120
}
105121

106122
console.error('Failed to update ContestTaskPair:', error);

0 commit comments

Comments
 (0)