Skip to content

Instantly share code, notes, and snippets.

@anirudhamahale
Created May 17, 2019 05:26
Show Gist options
  • Save anirudhamahale/8e8ee2707e3a2870a0f9fa1b95ef1d69 to your computer and use it in GitHub Desktop.
Save anirudhamahale/8e8ee2707e3a2870a0f9fa1b95ef1d69 to your computer and use it in GitHub Desktop.
public createTask(req: Request, res: Response): Observable<mongoose.Document> {
const body = req.body
const self = this
const task = body.task
const projectId = body.projectId
const moduleId = body.moduleId
const timeRequired = body.timeRequired
return Observable.create(function(observer: Observer<mongoose.Document>) {
if ((!task) || !(typeof task === 'string')) {
observer.error('Task name is required.')
} else if (!projectId) {
observer.error('Project id is required.')
} else if (!mongoose.Types.ObjectId.isValid(projectId)) {
observer.error('Invalide Project id.')
} else if (!moduleId) {
observer.error('Module id is required.')
} else if (!mongoose.Types.ObjectId.isValid(moduleId)) {
observer.error('Invalide Module id.')
} else if ((!timeRequired) || !(typeof timeRequired === 'number')) {
observer.error('Time is required.')
} else {
// Check if project and module id is correct.
console.log('zipping the query.')
zip(project.projectExists(projectId), module.moduleExists(moduleId, projectId), (proj, mod) => {
if (!proj) {
observer.error('Invalide Project id.')
} else if (!mod) {
observer.error('Invalide Module id.')
} else {
return true
}
}).subscribe({
next: (value) => {
// Save Work
const newTask = new self.taskModule({ task: task, projectId: projectId, moduleId: moduleId, requiredTime: timeRequired })
self.save(newTask).subscribe({
next: (value) => {
observer.next(value)
observer.complete()
},
error: (err) => observer.error(err)
})
},
error: (err) => observer.error(err)
})
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment