Created
January 31, 2019 19:45
-
-
Save Grohden/8c53f289633851f059c0c02b24d13874 to your computer and use it in GitHub Desktop.
Socorro
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
<- $ # module settings where | |
{ map, join, concat, filter, empty, fold, all, sort-by, any } = require 'prelude-ls' | |
response = JSON.parse """ | |
{ | |
"requestRepository":true, | |
"enterprises":[ | |
{ | |
"code":"T1", | |
"name":"Grupo 1", | |
"requestDatabase":false, | |
"orderDatabase":true, | |
"branches":[ | |
{ | |
"branch":"M SP 01 ", | |
"name":"São Paulo", | |
"facilities":true | |
}, | |
{ | |
"branch":"M SP 02 ", | |
"name":"Campinas", | |
"facilities":false | |
} | |
] | |
}, | |
{ | |
"code":"T4", | |
"name":"Grupo 3", | |
"requestDatabase":true, | |
"orderDatabase":false, | |
"branches":[ | |
{ | |
"branch":"M RJ 01 ", | |
"name":"Rio de Janeiro", | |
"facilities":false | |
}, | |
{ | |
"branch":"M RJ 02 ", | |
"name":"Santa Catarina", | |
"facilities":false | |
} | |
] | |
}, | |
{ | |
"code":"T5", | |
"name":"Grupo 4", | |
"requestDatabase":false, | |
"orderDatabase":false, | |
"branches":[ | |
{ | |
"branch":"M RJ 01", | |
"name":"Rio de Janeiro", | |
"facilities":true | |
} | |
] | |
} | |
] | |
} | |
""" | |
collect-invalid-branches = filter (.facilities) | |
has-only-invalid-branches = all (.facilities) | |
title-html-log = (text) -> | |
""" | |
<h4>#{text}</h4> | |
""" | |
is-request-ok = (request-rpo, enterprise) -> | |
yes | |
and enterprise.request-database | |
and not has-only-invalid-branches enterprise.branches | |
and request-rpo | |
is-order-ok = (enterprise) -> enterprise.order-database | |
branch-to-message = (branch) -> do | |
title: "#{branch.branch} - #{branch.name}" | |
type: \title | |
messages: [title: 'Facilities ativo' type: \error] | |
format-to-log = (enterprise) -> | |
invalid-branches = collect-invalid-branches enterprise.branches | |
validations = | |
* ((enterprise) -> not enterprise.order-database), do | |
title: 'Faltam de campos de usuário para O.S.' | |
type: \error | |
* ((enterprise) -> not enterprise.request-database), do | |
title: 'Faltam de campos de usuário para S.S.' | |
type: \error | |
* (-> not empty invalid-branches), map branch-to-message, invalid-branches | |
reducer = (errors, [predicate, error]) -> | |
switch predicate enterprise | |
| yes => [...errors].concat error | |
| otherwise errors | |
fold reducer, [], validations | |
make-icon = (type) -> | |
"""<i class="glyphicon colored glyphicon-#{type}"></i>""" | |
make-icon-for-type = (is-error) -> | |
glyph-type = if is-error then 'remove' else 'ok' | |
make-icon glyph-type | |
make-simple-title = (is-error, title) -> | |
""" | |
<ul class="list-unstyled"> | |
<li> | |
<span>#{make-icon-for-type is-error}</i>#{title}</span> | |
</li> | |
</ul> | |
""" | |
make-single-ul = (content) -> | |
""" | |
<ul class="list-unstyled">#{content}</ul> | |
""" | |
make-nested-ul = (content) -> | |
""" | |
<ul class="list-unstyled"> | |
<li> | |
<ul class="list-unstyled">#{content}</ul> | |
</li> | |
</ul> | |
""" | |
enterprise-to-log = (request-rpo, enterprise) --> | |
has-request-errors = !enterprise.is-request-ok | |
has-order-errors = !enterprise.is-order-ok | |
request-data = | |
position: if has-request-errors then 2 else 1 | |
html: if has-order-errors and has-request-errors | |
then '' | |
else make-simple-title has-request-errors, "Solicitações de Serviço" | |
order-data = | |
position: if has-order-errors then 2 else 1 | |
html: if has-order-errors and has-request-errors | |
then '' | |
else make-simple-title has-order-errors, "Ordens de Serviço" | |
rpo-html = | |
switch | |
| request-rpo => '' | |
| has-order-errors and has-request-errors => make-simple-title true, "RPO desatualizado" | |
| _ => make-single-ul (make-simple-title true, "RPO desatualizado") | |
error-html = if not empty enterprise.errors then | |
if has-order-errors and has-request-errors then | |
enterprise.errors | |
|> map process-message | |
|> join '\n' | |
|> make-single-ul | |
else | |
enterprise.errors | |
|> map process-message | |
|> join '\n' | |
|> make-nested-ul | |
else '' | |
success-html = [request-data, order-data] | |
|> sort-by (.position) | |
|> map (.html) | |
|> join '' | |
""" | |
<li> | |
<span>#{enterprise.name}</span> | |
#{success-html} | |
#{rpo-html} | |
#{error-html} | |
</li> | |
""" | |
process-message = (message) -> | |
icon = if message.type == \error then make-icon \remove else '' | |
messages = (message.messages or []) | |
|> map process-message | |
|> make-single-ul | |
""" | |
<li> | |
<span>#{icon}#{message.title}</span> | |
#{ if message.messages then messages else ''} | |
</li> | |
""" | |
has-any-errors = any (not) << empty << (.errors) | |
has-any-success = | |
any (message) -> message.is-request-ok or message.is-order-ok | |
evolve-enterprise = (request-rpo, enterprise) --> do | |
name: "#{enterprise.code} - #{enterprise.name}" | |
is-request-ok: is-request-ok request-rpo, enterprise | |
is-order-ok: is-order-ok enterprise | |
errors: format-to-log enterprise | |
deal-with-success = (res) -> | |
mapped = map (evolve-enterprise response.request-repository) response.enterprises | |
has-errors = has-any-errors mapped | |
has-success = has-any-success mapped | |
log-html = mapped | |
|> map (enterprise-to-log response.request-repository) | |
|> join '' | |
title = if has-errors | |
then "Algumas funcionalidades não poderão ser utilizadas." | |
else "Parabéns, todas as funcionalidades poderão ser utilizdas!" | |
|> title-html-log | |
swal do | |
html: yes | |
position: \center | |
close-on-confirm: yes | |
title: """<small id="log-title">#{title}</small>""" | |
text: """ | |
<div id="enviroment-checks-log" class="logs-area"> | |
<ul class="list-unstyled validations-tree"> | |
#{log-html} | |
</ul> | |
</div> | |
""" | |
!-> if has-success then ($ \#save-button).remove-attr \disabled | |
deal-with-error = (msg) -> | |
<- swal do | |
title: 'Oops!' | |
position: \center | |
type: \error | |
text: msg | |
close-on-confirm: yes | |
export check-enviroment = !-> | |
user <- credential-modal | |
return if not user.password | |
swal do | |
html: yes | |
position: \center | |
show-cancel-button: no | |
close-on-cancel: yes | |
close-on-confirm: no | |
title: """ | |
<small id="enviroment-checks-title">Realizando validações de ambiente</small> | |
""" | |
text: """ | |
<span id="enviroment-checks-loader" class="glyphicon big glyphicon-repeat spinning"></span> | |
""" | |
confirm-button-text: 'Ok!' | |
$.ajax do | |
url: '/Manage/CheckEnviroment/' + window.cnpj | |
data: | |
'Username': user.username | |
'Password': user.password | |
'Token': window.token | |
'url': ($ \#WebserviceAddress).val! | |
error: (res) !-> | |
res-json = res.response-JSON | |
deal-with-error <| res-json.message or res-json.Message or 'Erro desconhecido' | |
success: (res) !-> | |
console.log res | |
deal-with-success res | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
E tá errado?