Created
July 25, 2013 09:35
-
-
Save imsickofmaps/6078266 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| var vumigo = require("vumigo_v01"); | |
| var jed = require("jed"); | |
| if (typeof api === "undefined") { | |
| // testing hook (supplies api when it is not passed in by the real sandbox) | |
| var api = this.api = new vumigo.dummy_api.DummyApi(); | |
| } | |
| var Promise = vumigo.promise.Promise; | |
| var success = vumigo.promise.success; | |
| var Choice = vumigo.states.Choice; | |
| var ChoiceState = vumigo.states.ChoiceState; | |
| var FreeText = vumigo.states.FreeText; | |
| var EndState = vumigo.states.EndState; | |
| var BookletState = vumigo.states.BookletState; | |
| var InteractionMachine = vumigo.state_machine.InteractionMachine; | |
| var StateCreator = vumigo.state_machine.StateCreator; | |
| function GoNikeGHRError(msg) { | |
| var self = this; | |
| self.msg = msg; | |
| self.toString = function() { | |
| return "<GoNikeGHRError: " + self.msg + ">"; | |
| }; | |
| } | |
| function GoNikeGHR() { | |
| var self = this; | |
| self.post_headers = { | |
| 'Content-Type': ['application/x-www-form-urlencoded'] | |
| }; | |
| // The first state to enter | |
| StateCreator.call(self, 'initial_state'); | |
| self.get_today = function(im) { | |
| if (im.config.testing) { | |
| return new Date(im.config.testing_mock_today[0], | |
| im.config.testing_mock_today[1], | |
| im.config.testing_mock_today[2], | |
| im.config.testing_mock_today[3], | |
| im.config.testing_mock_today[4]); | |
| } else { | |
| return new Date(); | |
| } | |
| }; | |
| self.error_state = function() { | |
| return new EndState( | |
| "end_state_error", | |
| "Sorry! Something went wrong. Please redial and try again.", | |
| "initial_state" | |
| ); | |
| }; | |
| self.get_contact = function(im){ | |
| var p = im.api_request('contacts.get_or_create', { | |
| delivery_class: 'ussd', | |
| addr: im.user_addr | |
| }); | |
| return p; | |
| }; | |
| self.make_main_menu = function(){ | |
| return new ChoiceState( | |
| "main_menu", | |
| function(choice) { | |
| return choice.value; | |
| }, | |
| "", | |
| [ | |
| new Choice("articles", "Articles"), | |
| new Choice("opinions", "Opinions"), | |
| new Choice("wwnd", "What would Ndabaga do?"), | |
| new Choice("quiz_start", "Weekly quiz"), | |
| new Choice("directory_start", "Directory") | |
| ] | |
| ); | |
| }; | |
| self.add_creator('initial_state', function(state_name, im) { | |
| // Check if they've already registered | |
| var p = self.get_contact(im); | |
| p.add_callback(function(result) { | |
| // This callback creates extras if first time visitor - or just passes through | |
| if (result.contact["extras-ghr_reg_complete"] === undefined){ | |
| // First visit - create extras | |
| var today = self.get_today(im); | |
| var fields = { | |
| "ghr_reg_complete": "false", | |
| "ghr_reg_started": today.toISOString(), | |
| "ghr_questions": JSON.stringify([]), | |
| "ghr_gender": "", | |
| "ghr_age": "", | |
| "ghr_sector": "", | |
| "ghr_terms_accepted": "false" | |
| }; | |
| // Run the extras update | |
| return im.api_request('contacts.update_extras', { | |
| key: result.contact.key, | |
| fields: fields | |
| }); | |
| } else { | |
| // Not first so just pass previous callback result on | |
| return result; | |
| } | |
| }); | |
| p.add_callback(function(result) { | |
| // This callback generates the state the user sees | |
| if (result.success){ | |
| if (result.contact["extras-ghr_reg_complete"] == "false"){ | |
| // Did not finish registration and session state not found | |
| return new ChoiceState( | |
| state_name, | |
| function(choice) { | |
| return choice.value; | |
| }, | |
| "To proceed with registration, do you accept the Terms " + | |
| "and Conditions of Ni Nyampinga - " + im.config.terms_url + ":", | |
| [ | |
| new Choice("reg_gender", "Yes"), | |
| new Choice("reg_noterms", "No") | |
| ] | |
| ); | |
| } else { | |
| // Registration complete so check for questions | |
| // Check all question sets have been answered | |
| return self.make_main_menu(); | |
| } | |
| } else { | |
| // Something went wrong saving the extras | |
| return self.error_state(); | |
| } | |
| }); | |
| return p; // return the promise | |
| }); | |
| self.add_state(new EndState( | |
| "reg_noterms", | |
| "Sorry but we can't proceed with your registration unless you accept the " + | |
| "Terms & Conditions. Please redial if you change your mind. Thanks!", | |
| "initial_state" | |
| )); | |
| self.add_state(new EndState( | |
| "end_state", | |
| "Thank you and bye bye!", | |
| "first_state" | |
| )); | |
| } | |
| // launch app | |
| var states = new GoNikeGHR(); | |
| var im = new InteractionMachine(api, states); | |
| im.attach(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment