pyc-website

main website for pyc inc.

git clone https://9o.is/git/pyc-website.git

AtmSnip.scala

(4319B)


      1 package inc.pyc
      2 package snippet
      3 
      4 import lib._
      5 import config._
      6 import model._
      7 import field._
      8 import xml._
      9 import net.liftweb._
     10 import common._
     11 import json._
     12 import JsonDSL._
     13 import util._
     14 import Helpers._
     15 import http._
     16 import js._
     17 import JsCmds._
     18 import SHtml._
     19 import JE.JsVar
     20 import net.liftmodules.mongoauth.MongoAuth
     21 import dispatch._, Defaults._
     22 import net.liftmodules.extras.SnippetHelper
     23 
     24 
     25 class AtmApplication extends AngularSnippet {
     26   
     27   def roundTrips: List[RoundTripInfo] = List("submit" -> submit _)
     28     
     29   def submit(model: JValue): JValue = {
     30     val rec = AtmApplication.createRecord
     31     rec.setFieldsFromJValue(model)
     32     
     33     rec.validate match {
     34       case Nil =>
     35         rec.save(false)
     36         notifyByEmail(rec)
     37         
     38         NgAlert.success(Text(S ? "atm_app_received" format rec.name.get))
     39       case errors =>
     40         NgAlert.danger(Text(S ? "Invalid_submission"), errors)
     41     }
     42   }
     43   
     44   /** 
     45    *  Notify us of the new applicant by email, 
     46    *  so we can begin reviewing immediately.
     47    */
     48   def notifyByEmail(rec: model.AtmApplication): Unit = {
     49     import Mailer._
     50     val msgTxt =
     51       s"""
     52         Business Asking for a Bitcoin ATM!
     53         Name: ${rec.name.get}
     54         Website: ${rec.website.get}
     55         Address: 
     56         ${rec.address.get}
     57         ${rec.city.get}, ${rec.state.get}
     58         Email: ${rec.email.get}
     59         Phone: ${rec.phone.get}
     60         Best Time: ${rec.bestTime.get}
     61       """
     62         
     63     sendMail(
     64       From(MongoAuth.systemFancyEmail),
     65       Subject("PYC: Business wants a Bitcoin ATM"),
     66       To(Emails.atmBusiness),
     67       PlainMailBodyType(msgTxt)
     68     )
     69   }
     70 }
     71 
     72 
     73 class FindAtm extends AngularSnippet with Logger {
     74   
     75   var location: Option[SearchedLocation] = None
     76   
     77   def roundTrips: List[RoundTripInfo] = List(
     78       "submit" -> submit _, 
     79       "notifyEmail" -> notifyEmail _,
     80       "init" -> init _)
     81   
     82   /** Distance to check for nearby ATMs */
     83   val distance = 100 // kilometers (~62 miles)
     84   
     85   def submit(model: JValue): JValue =
     86     for {
     87       JString(address) <- model \ "address"
     88     } yield 
     89       Geocode.geolocation(address)() match {
     90         case Right(geo) => 
     91           
     92           location = Some(SearchedLocation.findByName(address) openOr {
     93             SearchedLocation.createRecord
     94           }) map {
     95             _.name(address).loc(geo).save()
     96           }
     97           
     98           val atms = Atm.nearby(geo, distance)
     99           
    100           if(atms.isEmpty) 
    101             NgAlert.danger(("loc" -> 
    102             	("latitude" -> geo.lat) ~ 
    103             	("longitude" -> geo.lng)
    104             ))
    105           else
    106             NgAlert.success(JArray(atms.zipWithIndex map {
    107               case (rec, i) =>
    108                 rec.asJValue merge
    109                 	("id" -> (i+1)) ~
    110                 	("showWindow" -> (i==0))
    111             }))
    112 
    113         case Left(err) =>
    114           
    115           //TODO notify me
    116           NgAlert.danger
    117       }
    118     
    119   def notifyEmail(model: JValue): JValue =
    120     for (JString(e) <- model \ "email") 
    121       yield location map {
    122         loc => 
    123           val email = e.toLowerCase.trim
    124     	  loc.emails.add(email).update
    125 
    126     	  NgAlert.success(Text(S ? "atm_notify"))
    127       } getOrElse JNull
    128 
    129     
    130   /* Set featured ATM on map */
    131   def init(o: JValue): JValue = {
    132     
    133     //* Don't show any windows by default (for now)
    134     FeaturedAtm.atm.asJValue merge 
    135     ("id" -> 1) ~ 
    136     ("showWindow" -> false)
    137     //*/
    138 
    139     
    140     
    141     val all = Atm.findAll
    142     val featured = all.filter(_.name.get == FeaturedAtm.name)
    143     val rest = all.filterNot(_.name.get == FeaturedAtm.name)
    144     val combined = featured ++ rest
    145     
    146     val atms = combined.zipWithIndex map {
    147       case (rec, i) =>
    148         val featured = rec.name.get == FeaturedAtm.name
    149 
    150         rec.asJValue merge
    151           ("id" -> (i + 1)) ~
    152           ("showWindow" -> featured)
    153     }
    154     
    155     JArray(atms)
    156   }
    157 }
    158 
    159 
    160 
    161 sealed trait AtmSnip extends SnippetHelper with Logger {
    162   protected def atm: Box[Atm]
    163   
    164   protected def serve(snip: Atm => JValue): JValue = 
    165     for (atm <- atm ?~ "Bitcoin ATM not found") yield snip(atm)
    166 }
    167 
    168 class AtmProfile extends AtmSnip with AngularSnippet {
    169   
    170   override def atm = Site.atmProfileLoc.currentValue
    171   
    172   def roundTrips: List[RoundTripInfo] = List("init" -> init _)
    173   
    174   def init(o: JValue): JValue = serve {
    175     atm => atm.asJValue
    176   }
    177 }
    178 
    179