//Schema
const mongoose = require('mongoose')
const Schema = mongoose.Schema
const timestamps = require('mongoose-timestamps')
const findOrCreate = require('mongoose-findorcreate')
const ProjectSchema = new Schema({
name: {
type: String,
required: true
},
tasks: [{ type: Schema.Types.ObjectId, ref: 'Task' }]
})
ProjectSchema.plugin(timestamps)
ProjectSchema.plugin(findOrCreate)
mongoose.model('Project', ProjectSchema)
//Task Create function
async taskParser (message, args, flags) {
try {
const task = new Task({
title: 'feowfmwe',
user_id: message.author.username
})
// Find or Update a Project and push the task in it
const project = await Project.findOneAndUpdate(
{ name: message.channel.name },
{ $push: { tasks: { _id: task._id } } }
)
// Save the project
const newProject = await project.save()
// read project with tasks
const tasks = await Project.find().populate('tasks').exec()
console.log(JSON.stringify(tasks))
} catch (error) {
console.log(error)
}
}