blob: 7d9450bdfc985b0703344142d58984b7d4c22040 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
import { sql } from '@vercel/postgres';
import { CURRENT_CHALLENGES } from './_tables';
export default async function setCurrentChallenge(userId: string, challenge: string) {
try {
const insert = `
INSERT INTO ${CURRENT_CHALLENGES.name} (
${CURRENT_CHALLENGES.fields.userId},
${CURRENT_CHALLENGES.fields.currentChallenge}
) VALUES (
${userId},
${challenge}
);
`);
const update = `
UPDATE TABLE ${CURRENT_CHALLENGES.name}
SET ${CURRENT_CHALLENGES.fields.currentChallenge} = ${challenge}
WHERE ${CURRENT_CHALLENGES.fields.userId} = '${userId}';
`);
await sql.query(`
BEGIN
IF EXISTS (
SELECT ${CURRENT_CHALLENGES.fields.userId} FROM ${CURRENT_CHALLENGES.name}
WHERE ${CURRENT_CHALLENGES.fields.userId} = ${userId}
)
${update};
ELSE
${insert};
`
} catch (err) {
throw new Error(`Failed to set current challenge for user with ID ${userId} in database.`, err);
}
}
|