Created
September 6, 2015 10:36
-
-
Save fomkin/6d2bfc4f61d83a95434f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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