package handler import ( "database/sql" "net/http" "time" "git.ekzyis.com/ekzyis/delphi.market/server/router/context" "git.ekzyis.com/ekzyis/delphi.market/server/router/pages/components" "git.ekzyis.com/ekzyis/delphi.market/types" "github.com/a-h/templ" "github.com/labstack/echo/v4" ) func HandleInvoice(sc context.Context) echo.HandlerFunc { return func(c echo.Context) error { var ( db = sc.Db ctx = c.Request().Context() hash = c.Param("hash") u = c.Get("session").(types.User) inv = types.Invoice{} expiresIn int paid bool qr templ.Component err error ) if err = db.QueryRowContext(ctx, ""+ "SELECT user_id, msats, COALESCE(msats_received, 0), expires_at, confirmed_at, bolt11 "+ "FROM invoices "+ "WHERE hash = $1", hash). Scan(&inv.UserId, &inv.Msats, &inv.MsatsReceived, &inv.ExpiresAt, &inv.ConfirmedAt, &inv.Bolt11); err != nil { if err == sql.ErrNoRows { return echo.NewHTTPError(http.StatusNotFound) } c.Logger().Error(err) return echo.NewHTTPError(http.StatusInternalServerError) } if u.Id != inv.UserId { return echo.NewHTTPError(http.StatusNotFound) } expiresIn = int(time.Until(inv.ExpiresAt).Seconds()) paid = inv.MsatsReceived >= inv.Msats qr = components.Invoice(hash, inv.Bolt11, int(inv.Msats), expiresIn, paid) return components.Modal(qr).Render(context.RenderContext(sc, c), c.Response().Writer) } }