Created
November 19, 2020 12:31
-
-
Save navarroaxel/a4547fbd055ae81345c26def12603f8c 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
const save = (surveys = [], user) => { | |
const promises = surveys.map(async (survey) => { | |
const {user: currentUser} = await this.knex | |
.select('user') | |
.from('addresses') | |
.where({ | |
user: user.id, | |
id: survey.address, | |
}) | |
.first(); | |
if (currentUser) { | |
let situation = survey.situation; | |
if (survey.readyToSync) { | |
situation = situations.FINISHED_IN_FIELD; | |
} else { | |
if (survey.situation === situations.ASSIGNED) { | |
situation = situations.SYNCHRONIZED; | |
} | |
if (user.roles[0] === roles.POLLSTER && survey.visits) { | |
situation = situations.SURVEYING; | |
} | |
if (user.roles[0] === roles.TEAM_LEADER && survey.visits) { | |
situation = situations.IN_RECOVERY; | |
} | |
if (survey.closed) { | |
situation = situations.CLOSED; | |
} | |
} | |
return [ | |
this.knex.update({data: survey}).from('surveys').where({id: survey.id}), | |
this.knex | |
.update({situation}) | |
.from('addresses') | |
.where({id: survey.address}), | |
]; | |
} | |
}); | |
return Promise.all(promises.flat()); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment