aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/app/models/pPost.js
blob: d138c8f732ccde7e0047b8399d52cb691e424a6a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75

/**
 * Module dependencies
 */

var mongoose = require('mongoose')
  , Schema = mongoose.Schema;

var pPostSchema = new Schema({
    user:           { type: Schema.ObjectId, ref: 'User' }
  , for:            { type: Schema.ObjectId, ref: 'User' }
  , project:        { type: Schema.ObjectId, ref: 'Project' }
  , what:           { type: String, default: '', trim: true }
  , comment:        { type: String, default: '', trim: true }
  , participants:   []
  , value:          { type: Number, default: 0 }
  , file:           { type: String, default: '', trim: true }
  , currency:       { type: String, default: 'kr', trim: true }
  , created:        { type: Date, default: Date.now }
  , updated:        { type: Date, default: Date.now }
  , when:           { type: Date, default: Date.now }
});




pPostSchema.statics = {

   /**
    * Find post by id
    *
    * @param {ObjectId} id
    * @param {Function} callback
    */

    load: function(id, callback) {
        this.findOne({ _id: id })
          .populate({ path: 'user', select: '_id, name'}) // include name of the user who posted it
          .exec(callback);
    },


          /**
    * Find all posts that belong to a project, by project id
    *
    * @param {ObjectId} project
    * @param {Function} callback
    */

    loadProject: function(project, callback) {
        this.find({ project: project })
          .populate({ path: 'user', select: '_id, name'})
          .sort({ 'when': -1, 'created': -1 })
          .exec(callback);
    },


   /**
    * Find all posts that belongs to projects provided in an array
    *
    * @param {Array[ObjectId]} projects
    * @param {Function} callback
    */

    loadByProjects: function(projects, callback) {
        this.find({ project: { $in: projects } })
          .populate({ path: 'user', select: '_id name' })
          .populate({ path: 'project', select: 'name shortURL' })
          .sort({ 'when': -1, 'created': -1 })
          .exec(callback);
    }

}

mongoose.model('pPost', pPostSchema);