Skip to content

Instantly share code, notes, and snippets.

@fomkin
Created September 6, 2015 10:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fomkin/6d2bfc4f61d83a95434f to your computer and use it in GitHub Desktop.
Save fomkin/6d2bfc4f61d83a95434f to your computer and use it in GitHub Desktop.
r.db("involve").table("authorization_codes").get(phoneNumber) update { doc ⇒
// Prepare query to SMSC
val sendSms = {
val params = s"phones=$phoneNumber&mes=$authorizationCode&fmt=3"
r.http(s"https://smsc.ru/sys/send.php?login=user&psw=******&$params")
}
// Check SMS limit is not reached
val timeIsOk = r.now().toEpochTime() - doc.getField("last_update").toEpochTime() > 3600
val countIsOk = doc.getField("count") < 3
// If is fine then execute request
// to SMSC and update document
r.branch(
condition = countIsOk || timeIsOk,
thn = r.branch(
// Update document only when SMS was realy sent
condition = sendSms.getField("error") === Null,
thn = document(
count = doc.getField("count") + 1,
last_update = r.now()
), els = document.empty
), els = document.empty
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment