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
37
38
39
40
import { set } from "https://esm.town/v/std/set?v=11";
import { email } from "https://esm.town/v/std/email?v=9";
import { coffee } from "https://esm.town/v/davidmchan/coffee";
export const updateCoffee = async (req: express.Request, res: express.Response) => {
let data = req.query.type;
const d = new Date();
const marked = await import("npm:marked");
if (
data != coffee.last_update ||
d.getTime() - coffee.last_update_time > (5 * 60 * 1000)
) {
coffee.last_update_time = d.getTime();
coffee.last_update_str = d.toLocaleString("en-US", {
timeZone: "America/Los_Angeles",
});
if (data === "decaf") {
coffee.decaf -= 1;
coffee.last_update = "decaf";
}
else if (data === "normal") {
coffee.normal -= 1;
coffee.last_update = "normal";
}
// Email me if there's no more coffee!
await email({
text: "Current Coffee Status:\n" +
"Decaf: " + coffee.decaf + "\n" +
"Normal: " + coffee.normal + "\n",
subject: "Coffee Update!!",
});
}
await set("coffee", coffee);
res.send(marked.parse(`
Thanks for refilling the Coffee!
========================
Bags of Decaf Remaining: ${coffee.decaf}
Bags of Normal Remaining: ${coffee.normal}
`));
};
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!
October 23, 2023