Created
July 6, 2017 18:11
-
-
Save GlauberF/87dfe53f310acac2af83fdcfbdf0328b to your computer and use it in GitHub Desktop.
scope nodejs
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
.then((daAtualizado) => { | |
pessoas | |
.forge({ | |
cli_id: person.attributes.id | |
}) | |
.fetch() | |
.then((d) => { | |
// instancio o objeto que irá receber os valores | |
let respSuccess = {}; | |
// recebe novo posição e valor | |
respSuccess.pessoas = d; | |
// tb endereço | |
if (objEndereco.endcli_logradouro || objEndereco.endcli_cep || objEndereco.endcli_bairro || | |
objEndereco.endcli_pais || objEndereco.endcli_cidade || objEndereco.endcli_estado || | |
objEndereco.endcli_numero || objEndereco.endcli_complemento || objEndereco.endcli_latitude || | |
objEndereco.endcli_longitude) { | |
knex('tb_Endereco_Cliente') | |
.insert(objEndereco) | |
.then((endId) => { | |
knex.select('endcli_id', 'fk_cli_id', 'endcli_logradouro', 'endcli_numero', 'endcli_complemento', | |
'endcli_cep', 'endcli_bairro', 'endcli_pais', 'endcli_cidade', 'endcli_estado', | |
'endcli_latitude', 'endcli_longitude') | |
.from('tb_Endereco_Cliente') | |
.where({ endcli_id: endId['0'] }) | |
.then((end) => { | |
// recebe novo posição e valor | |
respSuccess.endereco = end; | |
}) | |
}) | |
.catch((err) => { | |
return res | |
.status(500) | |
.json({ | |
code: 500, | |
data: {} | |
}); | |
}); | |
} | |
// tb email | |
if (objListaEmail.fk_emt_id) { | |
knex('tb_Email_Cliente') | |
.insert(objListaEmail) | |
.then((emailR) => { | |
knex.select('emc_id', 'fk_emt_id', 'fk_cli_id', 'emc_email') | |
.from('tb_Email_Cliente') | |
.where({ emc_id: emailR['0'] }) | |
.then((end) => { | |
// recebe novo posição e valor | |
respSuccess.lista_email = end; | |
}); | |
}) | |
.catch((err) => { | |
return res | |
.status(500) | |
.json({ | |
code: 500, | |
data: {} | |
}); | |
}); | |
} | |
// tb social networks | |
if (objRedesSociais.fk_sn_id) { | |
knex('tb_Social_Network_Cliente') | |
.insert(objRedesSociais) | |
.then((socialNet) => { | |
knex.select('snc_id', 'snc_url', 'fk_sn_id', 'fk_cli_id') | |
.from('tb_Social_Network_Cliente') | |
.where({ snc_id: socialNet['0'] }) | |
.then((end) => { | |
// recebe novo posição e valor | |
respSuccess.redes_sociais = end; | |
}) | |
}) | |
.catch((err) => { | |
return res | |
.status(500) | |
.json({ | |
code: 500, | |
data: {} | |
}); | |
}); | |
} | |
// tb tags | |
if (objTag.fk_tag_id) { | |
knex('tb_Tag_Cliente') | |
.insert(objTag) | |
.then((tagR) => { | |
knex.select('tagcli_id', 'fk_cli_id', 'fk_tag_id', 'tagcli_origem') | |
.from('tb_Tag_Cliente') | |
.where({ tagcli_id: tagR['0'] }) | |
.then((end) => { | |
// recebe novo posição e valor | |
respSuccess.tag = end; | |
}) | |
}) | |
.catch((err) => { | |
return res | |
.status(500) | |
.json({ | |
code: 500, | |
data: {} | |
}); | |
}); | |
} | |
// tb atendimento | |
if (objAtendimentos.fk_user_id) { | |
knex('tb_Atendimento_Cliente') | |
.insert(objAtendimentos) | |
.then((atendR) => { | |
knex.select('atdcli_id', 'atdcli_descricao', 'fk_cli_id', 'fk_user_id') | |
.from('tb_Atendimento_Cliente') | |
.where({ atdcli_id: atendR['0'] }) | |
.then((end) => { | |
// recebe novo posição e valor | |
respSuccess.atendimentos = end; | |
}) | |
}) | |
.catch((err) => { | |
return res | |
.status(500) | |
.json({ | |
code: 500, | |
data: {} | |
}); | |
}); | |
} | |
// console.log(d) | |
res | |
.status(200) | |
.json({ | |
code: 200, | |
data: respSuccess | |
}); | |
}) | |
.catch((err) => { | |
return res | |
.status(500) | |
.json({ | |
code: 500, | |
data: {} | |
}); | |
}); | |
}) | |
.catch((err) => { | |
return res | |
.status(500) | |
.json({ | |
code: 500, | |
data: { | |
erro: 'Não foi possível gravar no banco de dados' | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Manooo cada insert q vc dá em cada if diferente cria uma closure interna e como elas são assíncronas elas podem terminar em ordens diferentes, nesse seu caso vc deveria utilizar um encadeamento de Promises um Promise.all pra executar todas na sequência.