summaryrefslogtreecommitdiff
path: root/lib.fnl
diff options
context:
space:
mode:
authorunwox <me@unwox.com>2025-10-03 11:56:37 +0600
committerunwox <me@unwox.com>2025-10-13 23:11:01 +0600
commit3f5ade2e7a139bb4405437e8fc5546aafc7b05ef (patch)
tree77c437958d74b591f11ec207d16749cf207a51e3 /lib.fnl
parentf5a70e6a446e00969adb866ef2e2d10bf33bc4a8 (diff)
WIP shop
Diffstat (limited to 'lib.fnl')
-rw-r--r--lib.fnl123
1 files changed, 74 insertions, 49 deletions
diff --git a/lib.fnl b/lib.fnl
index cb437a2..f5b860c 100644
--- a/lib.fnl
+++ b/lib.fnl
@@ -1,3 +1,5 @@
+(local secrets (require :secrets))
+
(fn now []
(os.date "%Y-%m-%d %H:%M:%S"))
@@ -81,11 +83,6 @@
final-name))
-(fn order-id [request]
- (let [cookies-header (. request.headers :Cookie)
- cookies (if cookies-header (parse-values cookies-header) {})]
- cookies.order))
-
(fn with-tx [db f]
(let [tx (_G.must (luna.db.begin db))]
(local (ok? result) (pcall f tx))
@@ -151,52 +148,79 @@
k1 v1 (pairs t1) &until (not res)]
(and (. t2 k1) (= v1 (. t2 k1))))))
-(fn basket [db order-id]
- (local items
- (_G.must
- (luna.db.query-assoc
- db
- "SELECT order_lines.id,
- products.name,
- products.title,
- products.price_per AS \"price-per\",
- STRING_AGG(product_images.name, ',') AS \"images\",
- order_lines.quantity
- FROM order_lines
- INNER JOIN products ON products.name = order_lines.product_name
- LEFT JOIN product_images ON products.name = product_images.product_name
- WHERE order_lines.order_id = ?
- GROUP BY order_lines.id
- ORDER BY product_images.position"
- [order-id])))
- (if (and (. items 1) (not (empty-table? (. items 1))))
- (icollect [_ item (ipairs items)]
- (do
- (when (. item :images)
- (tset item :images (split (. item :images) ",")))
- item))
- []))
-
(fn string->number [str]
(if str
(tonumber (pick-values 1 (str:gsub "[^0-9.]" "")))
nil))
-(fn authenticate-request [db request]
- (let [cookies-header (. request.headers :Cookie)
- cookies (if cookies-header (parse-values cookies-header) {})
- session-id cookies.auth]
- (if (not (empty? session-id))
- (let [sessions
- (_G.must
- (luna.db.query-assoc
- db
- "SELECT id FROM auth_sessions
- WHERE id = ?
- AND expires_at > STRFTIME('%Y-%m-%d %H:%M:%S', DATETIME('now'))"
- [session-id]))]
- (< 0 (# sessions)))
- false)))
+(fn insert [str substr pos]
+ (.. (str:sub 1 pos) substr (str:sub (+ 1 pos))))
+
+(fn format-price [price]
+ (var price-str (tostring price))
+ (local dot-position (price-str:find "%."))
+ (local price-len (if dot-position
+ (- (pick-values 1 dot-position) 1)
+ (# price-str)))
+ (var cursor (- price-len 3))
+ (while (< 0 cursor)
+ (set price-str (insert price-str " " cursor))
+ (set cursor (- cursor 3)))
+ price-str)
+
+(fn group-by [lines fields]
+ (fn contains? [array needle]
+ (var found? false)
+ (each [_ v (ipairs array) &until found?]
+ (when (= v needle) (set found? true)))
+ found?)
+
+ (if (= 0 (# lines))
+ []
+ (do
+ (local result [])
+ (var grouping-line (. lines 1))
+ (tset grouping-line :rest [])
+
+ (each [_ line (ipairs lines)]
+ (each [_ field (ipairs fields) &until (= line grouping-line)]
+ (when (~= (. grouping-line field) (. line field))
+ (do
+ (table.insert result grouping-line)
+ (set grouping-line line)
+ (tset grouping-line :rest []))))
+
+ (var rest {})
+ (each [key value (pairs line)]
+ (when (not (contains? fields key))
+ (tset rest key value)))
+ (table.insert grouping-line.rest rest))
+
+ (table.insert result grouping-line)
+ result)))
+
+(fn encode-url-values [vals]
+ (table.concat
+ (accumulate [result [] k v (pairs vals)]
+ (do
+ (table.insert result (.. k "=" (_G.must (luna.http.encode-url v))))
+ result))
+ "&"))
+
+(fn notify [message]
+ (local tg-api-url
+ (.. "https://api.telegram.org/bot" secrets.telegram-bot-token "/sendMessage"))
+
+ ;; TODO: test for non-200 responses and log them.
+ (local response
+ (_G.must
+ (luna.http.request
+ "POST" tg-api-url
+ {:Content-Type "application/x-www-form-urlencoded"}
+ (encode-url-values
+ {:chat_id secrets.telegram-notification-user-id
+ :text message
+ :parse_mode "html"})))))
{: improve-typography
: starts-with?
@@ -206,7 +230,6 @@
: trim-right
: file-exists?
: parse-values
- : order-id
: handle-upload
: with-tx
: append
@@ -217,6 +240,8 @@
: merge
: empty?
: now
- : basket
: string->number
- : authenticate-request}
+ : format-price
+ : group-by
+ : encode-url-values
+ : notify}