Skip to content

Instantly share code, notes, and snippets.

@navarroaxel
Created November 19, 2020 12:31
Show Gist options
  • Save navarroaxel/a4547fbd055ae81345c26def12603f8c to your computer and use it in GitHub Desktop.
Save navarroaxel/a4547fbd055ae81345c26def12603f8c to your computer and use it in GitHub Desktop.
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