Readme

Price Tracker

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
41
42
43
44
45
46
47
48
49
50
import { email } from "https://esm.town/v/std/email";
const productUrl = "https://www.emma-sleep.co.uk/mattresses/emma-luxe-cooling-mattress/";
const apiUrl = "https://api.ecom.emma-sleep.com/ecommerce-api-gateway/virtual-carts/emma_gb_webshop";
const product = {
cartDraft: {
"country": "GB",
"currency": "GBP",
"locale": "en-GB",
"storeKey": "emma_gb_webshop",
"lineItems": [
{
"sku": "EMALC150200AAB",
"quantity": 1,
"custom": {
"fields": {
"bundleName": "Emma Luxe Cooling Mattress",
// "bundleId": "bundleName=Emma Luxe Cooling Mattress&lineItem=sku:EMALC150200AAB;quantity:1",
},
},
},
],
},
};
export const checkPrice = async () => {
const res = await fetch(apiUrl, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(product),
});
const { lineItems } = await res.json();
if (!lineItems) {
console.log("Failed to get price");
return;
}
const { price, totalPrice } = lineItems[0];
const fullPrice = ${price.centAmount / 100}`;
const actualPrice = ${totalPrice.centAmount / 100}`;
const discount = ${((price.centAmount - totalPrice.centAmount) / 100).toFixed(2)}`;
const reduction = `${((price.centAmount - totalPrice.centAmount) / price.centAmount) * 100}%`;
const subject = `Mattress is ${actualPrice}, ${reduction} off`;
const text = productUrl;
console.log(subject, lineItems);
if (price.centAmount < 100000) {
await email({ subject, text });
}
};
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!
September 7, 2024