Last active
May 27, 2018 02:04
-
-
Save octavioturra/a007d38ea93c1742b98f7ab0422d33ed to your computer and use it in GitHub Desktop.
Hook for beholder events
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
const actions = { | |
LOAD: 'load', | |
NEXT: 'next', | |
UPDATE: 'update', | |
ERROR: 'error', | |
INTEGRAGE: 'integrate' | |
} | |
const steps = { | |
HOME: 'home', | |
GREETINGS: 'greetings', | |
FINISH: 'finish' | |
} | |
function setIn(store, key, value) { | |
return new Promise((resolve, reject) => store.set(key, value, (err, result) => { | |
if (err) { | |
return reject(err); | |
} | |
return resolve(result); | |
})) | |
} | |
function getFrom(store, key) { | |
return new Promise((resolve, reject) => store.get(key, (err, result) => { | |
if (err) { | |
return reject(err); | |
} | |
return resolve(result); | |
})) | |
} | |
function deleteFrom(store, key) { | |
return new Promise((resolve, reject) => store.del(key, (err, result) => { | |
if (err) { | |
return reject(err); | |
} | |
return resolve(result); | |
})) | |
} | |
function response(action, data, code = "response") { | |
return { | |
name: `experience:${action}:${code}`, | |
payload: data, | |
version: 1 | |
} | |
} | |
function responseError(action, err) { | |
return response(action, err, "error") | |
} | |
function loadHome() { | |
return { | |
action: actions.LOAD, | |
step: steps.HOME, | |
blocks: { | |
login: { | |
email: { | |
value: 'testando@teste.com.br' | |
} | |
} | |
} | |
} | |
} | |
function nextHome(data) { | |
if (data.login.email.value === 'testando@teste.com.br') { | |
return { | |
action: actions.NEXT, | |
nextStep: steps.GREETINGS | |
} | |
} | |
return { | |
action: actions.ERROR, | |
step: steps.HOME, | |
error: { | |
code: "UNKNOW_EMAIL_FOR_USER", | |
message: `unknow email ${data.email} for registered user` | |
} | |
} | |
} | |
function loadGreetings() { | |
return { | |
action: actions.LOAD, | |
step: steps.GREETINGS, | |
blocks: { | |
address: { | |
street: {}, | |
zipCode: {}, | |
city: { | |
options: [] | |
}, | |
uf: { | |
options: ['SP', 'RJ'] | |
} | |
}, | |
income: { | |
total: { | |
value: 3000 | |
} | |
} | |
} | |
} | |
} | |
function updateUfGreetings(data) { | |
return { | |
action: actions.UPDATE, | |
step: steps.GREETINGS, | |
blocks: { | |
address: { | |
zipCode: { value: '00000000'}, | |
street: {value: 'adelante' }, | |
city: { | |
value: data.address.uf.value === 'SP'? 'Guarulhos' : 'Nitorói', | |
options: data.address.uf.value === 'SP'? ['São Paulo', 'Guarulhos']: ['Rio de Janeiro', 'Niterói'] | |
}, | |
uf: { | |
value: data.address.uf.value, | |
options: ['SP', 'RJ'] | |
} | |
} | |
} | |
} | |
} | |
function nextGreetings() { | |
return { | |
action: actions.NEXT, | |
nextStep: steps.FINISH, | |
back: false | |
} | |
} | |
function loadFinish() { | |
return { | |
action: actions.LOAD, | |
step: steps.FINISH, | |
blocks: { | |
feedback: {} | |
} | |
} | |
} | |
function forceBackTo(step) { | |
return { | |
action: actions.NEXT, | |
nextStep: step | |
}; | |
} | |
function notFound() { | |
return { | |
action: actions.ERROR, | |
message: 'Experience not found' | |
} | |
} | |
function isItInThat(it, ...that) { | |
return !!~that.indexOf(it); | |
} | |
function canLoadFrom(currentStep, step) { | |
switch(currentStep) { | |
case steps.GREETINGS: return isItInThat(step, currentStep, steps.HOME); | |
case steps.FINISH: return isItInThat(step, currentStep); | |
default: return isItInThat(step, currentStep, steps.HOME); | |
} | |
} | |
function router(user, step, data, action) { | |
if(user && !canLoadFrom(user.step, step)) { | |
return forceBackTo(user.step) | |
} | |
if (step === steps.HOME) { | |
if (action === actions.LOAD) return loadHome() | |
else if (action === actions.NEXT) return nextHome(data) | |
} else if (step === steps.GREETINGS) { | |
if (action === actions.LOAD) return loadGreetings() | |
else if (action === actions.UPDATE) return updateUfGreetings(data) | |
else if (action === actions.NEXT) return nextGreetings() | |
} else if (step === steps.FINISH) { | |
if (action === actions.LOAD) return loadFinish() | |
} else | |
return notFound() | |
} | |
function setUserData(store, uid, data) { | |
return setIn(store, uid, {...data, step: data.nextStep || data.step}) | |
} | |
function getUserData(store, uid) { | |
return getFrom(store, uid) | |
} | |
const events = { | |
'experience:uid:get': async (hook, store, uid) => hook.res.json(response(actions.LOAD, await getUserData(store, uid))), | |
'experience:uid:remove': async (hook, store, uid) => hook.res.json(response(actions.LOAD, await deleteFrom(store, uid))), | |
'user:integrate': async (hook, store, uid) => hook.res.json(response(actions.LOAD, { step: 'home', sessionToken: '15fa5fas-5fas5d5a-fasd5fa-' + hook.params.payload.integrationId })), | |
} | |
// A simple hello world microservice | |
// Service will respond to HTTP requests with a string | |
export default async function (hook) { | |
// hook.req is a Node.js http.IncomingMessage | |
const host = hook.req.host; | |
const store = hook.datastore; | |
// console.log(Object.keys(store)) | |
// hook.res is a Node.js httpServer.ServerResponse | |
// Respond to the request with a simple string | |
//hook.res.json(hook.params); | |
const uid = hook.params.auth.sessionToken; | |
const handler = events[hook.params.name] | |
if(handler) { | |
return await handler(hook, store, uid) | |
} | |
const {step, data, action} = hook.params.payload; | |
// console.log(step) | |
// console.log(data) | |
// console.log(action) | |
// console.log(uid) | |
try { | |
const user = await getUserData(store, uid) | |
console.log(user, 'user') | |
const route = router(user, step, data, action) | |
// console.log(route, 'route') | |
const result = await setUserData(store, uid, {...user, ...route}) | |
// console.log(result, 'result') | |
const res = response(action, route) | |
hook.res.json(res) | |
} catch(err) { | |
hook.res.json(responseError(action, { message: err.message })) | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment