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
36
import { sqlite } from "https://esm.town/v/std/sqlite?v=4";
interface UpsertEmailSubscriberParams {
name: string;
email: string;
token: string;
}
/*
* Upsert covers three cases:
* 1. Never subscribed
* 2. Subscribed but not verified
* 3. Resubscribing
*
* Case #3 is somewhat ambiguous w/r/t user intent.
* E.g. if someone is on lesson 6 and they resubscribe,
* do they want to start over from lesson 1?
* Or was resubscribing an accident?
* The current behavior is to reset to lesson 1.
*/
export async function upsertStudent({ name, email, token }: UpsertEmailSubscriberParams) {
return sqlite.execute({
sql: `
INSERT INTO students (name, email, verification_token)
VALUES (?, ?, ?)
ON CONFLICT(email)
DO UPDATE SET
name = excluded.name,
verification_token = excluded.verification_token,
subscribed_at = CURRENT_TIMESTAMP,
current_lesson = 0,
has_completed_current_lesson = 0;
`,
args: [name, email, token],
});
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
January 20, 2024