Skip to content

Commit a089792

Browse files
committed
fix(tests): status updates
1 parent ece5b34 commit a089792

File tree

3 files changed

+106
-70
lines changed

3 files changed

+106
-70
lines changed

src/models/ticket.js

+17-11
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ const commentSchema = require('./comment')
2929
const noteSchema = require('./note')
3030
const attachmentSchema = require('./attachment')
3131
const historySchema = require('./history')
32+
const statusSchema = require('./ticketStatus')
3233
require('./tag')
3334
require('./ticketpriority')
3435
require('./tickettype')
35-
require('./ticketStatus')
3636

3737
const COLLECTION = 'tickets'
3838

@@ -250,7 +250,7 @@ ticketSchema.methods.setStatus = function (ownerId, status, callback) {
250250
return reject(new Error('Invalid Status'))
251251
}
252252

253-
self.closedDate = status.isResolved ? new Date() : null
253+
self.closedDate = statusModel.isResolved ? new Date() : null
254254
self.status = status
255255

256256
const historyItem = {
@@ -1516,16 +1516,22 @@ ticketSchema.statics.getAssigned = function (userId, callback) {
15161516

15171517
const self = this
15181518

1519-
const q = self
1520-
.model(COLLECTION)
1521-
.find({ assignee: userId, deleted: false, status: { $ne: 3 } })
1522-
.populate(
1523-
'owner assignee comments.owner notes.owner subscribers history.owner',
1524-
'username fullname email role image title'
1525-
)
1526-
.populate('type tags status group')
1519+
statusSchema.find({ isResolved: false }, function (err, statuses) {
1520+
if (err) return callback(err)
15271521

1528-
return q.exec(callback)
1522+
const unresolvedStatusesIds = statuses.map(i => i._id)
1523+
1524+
const q = self
1525+
.model(COLLECTION)
1526+
.find({ assignee: userId, deleted: false, status: { $in: unresolvedStatusesIds } })
1527+
.populate(
1528+
'owner assignee comments.owner notes.owner subscribers history.owner',
1529+
'username fullname email role image title'
1530+
)
1531+
.populate('type tags status group')
1532+
1533+
return q.exec(callback)
1534+
})
15291535
}
15301536

15311537
/**

test/0_database.js

+12
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,18 @@ before(function (done) {
5252
var typeSchema = require('../src/models/tickettype')
5353
typeSchema.insertMany([{ name: 'Task' }, { name: 'Issue' }], cb)
5454
},
55+
function (cb) {
56+
var statusSchema = require('../src/models/ticketStatus')
57+
statusSchema.insertMany(
58+
[
59+
{ name: 'New', uid: 0, isLocked: true },
60+
{ name: 'Open', uid: 1, isLocked: true },
61+
{ name: 'Pending', uid: 2, isLocked: true },
62+
{ name: 'Closed', uid: 3, isLocked: true, isResolved: true }
63+
],
64+
cb
65+
)
66+
},
5567
function (cb) {
5668
require('../src/settings/defaults').init(cb)
5769
},

test/models/ticket.js

+77-59
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var m = require('mongoose')
55
var ticketSchema = require('../../src/models/ticket')
66
var groupSchema = require('../../src/models/group')
77
var prioritySchema = require('../../src/models/ticketpriority')
8+
var statusSchema = require('../../src/models/ticketStatus')
89

910
describe('ticket.js', function () {
1011
// it('should clear collections.', function(done) {
@@ -21,77 +22,90 @@ describe('ticket.js', function () {
2122
prioritySchema.findOne({ default: true }).exec(function (err, p) {
2223
expect(err).to.not.exist
2324
expect(p).to.be.a('object')
24-
25-
ticketSchema.create(
26-
{
27-
owner: m.Types.ObjectId(),
28-
group: m.Types.ObjectId(),
29-
status: 0,
30-
tags: [],
31-
date: new Date(),
32-
subject: 'Dummy Test Subject',
33-
issue: 'Dummy Test Issue',
34-
priority: p._id,
35-
type: m.Types.ObjectId(),
36-
history: []
37-
},
38-
function (err, t) {
39-
expect(err).to.not.exist
40-
expect(t).to.be.a('object')
41-
expect(t._doc).to.include.keys(
42-
'_id',
43-
'uid',
44-
'owner',
45-
'group',
46-
'status',
47-
'tags',
48-
'date',
49-
'subject',
50-
'issue',
51-
'priority',
52-
'type',
53-
'history',
54-
'attachments',
55-
'comments',
56-
'deleted'
57-
)
58-
59-
expect(t.uid).to.equal(1000)
60-
61-
done()
62-
}
63-
)
25+
statusSchema.findOne({ uid: 0 }).exec(function (err, status) {
26+
expect(err).to.not.exist
27+
expect(status).to.be.a('object')
28+
ticketSchema.create(
29+
{
30+
owner: m.Types.ObjectId(),
31+
group: m.Types.ObjectId(),
32+
status: status._id,
33+
tags: [],
34+
date: new Date(),
35+
subject: 'Dummy Test Subject',
36+
issue: 'Dummy Test Issue',
37+
priority: p._id,
38+
type: m.Types.ObjectId(),
39+
history: []
40+
},
41+
function (err, t) {
42+
expect(err).to.not.exist
43+
expect(t).to.be.a('object')
44+
expect(t._doc).to.include.keys(
45+
'_id',
46+
'uid',
47+
'owner',
48+
'group',
49+
'status',
50+
'tags',
51+
'date',
52+
'subject',
53+
'issue',
54+
'priority',
55+
'type',
56+
'history',
57+
'attachments',
58+
'comments',
59+
'deleted'
60+
)
61+
62+
expect(t.uid).to.equal(1000)
63+
64+
done()
65+
}
66+
)
67+
})
6468
})
6569
})
6670

6771
it('should set the ticket status to closed then to open', function (done) {
6872
async.series(
6973
[
7074
function (cb) {
71-
ticketSchema.getTicketByUid(1000, function (err, ticket) {
75+
statusSchema.findOne({ uid: 3 }, function (err, status) {
7276
expect(err).to.not.exist
73-
expect(ticket).to.be.a('object')
77+
expect(status).to.be.a('object')
7478

75-
ticket.setStatus(m.Types.ObjectId(), 3, function (err, ticket) {
79+
ticketSchema.getTicketByUid(1000, function (err, ticket) {
7680
expect(err).to.not.exist
77-
expect(ticket.status).to.equal(3)
78-
expect(ticket.closedDate).to.exist
81+
expect(ticket).to.be.a('object')
7982

80-
cb()
83+
ticket.setStatus(ticket._id, status._id, function (err, ticket) {
84+
expect(err).to.not.exist
85+
expect(ticket.status).to.equal(status._id)
86+
expect(ticket.closedDate).to.exist
87+
88+
cb()
89+
})
8190
})
8291
})
8392
},
8493
function (cb) {
85-
ticketSchema.getTicketByUid(1000, function (err, ticket) {
94+
statusSchema.findOne({ uid: 1 }, function (err, status) {
8695
expect(err).to.not.exist
87-
expect(ticket).to.be.a('object')
88-
89-
ticket.setStatus(m.Types.ObjectId(), 1, function (err, ticket) {
96+
expect(status).to.be.a('object')
97+
ticketSchema.getTicketByUid(1000, function (err, ticket) {
9098
expect(err).to.not.exist
91-
expect(ticket.status).to.equal(1)
92-
expect(ticket.closedDate).to.not.exist
99+
expect(ticket).to.be.a('object')
93100

94-
cb()
101+
ticket.setStatus(ticket._id, status._id, function (err, ticket) {
102+
expect(err).to.not.exist
103+
console.log(ticket)
104+
expect(ticket.status).to.equal(status._id)
105+
expect(ticket.closedDate).to.not.exist
106+
107+
cb()
108+
})
95109
})
96110
})
97111
}
@@ -393,22 +407,22 @@ describe('ticket.js', function () {
393407
async.parallel(
394408
[
395409
function (cb) {
396-
ticketSchema.getTicketsByStatus([m.Types.ObjectId()], 0, function (err, tickets) {
410+
ticketSchema.getTicketsByStatus([m.Types.ObjectId()], m.Types.ObjectId(), function (err, tickets) {
397411
expect(err).to.not.exist
398412
expect(tickets).to.have.length(0)
399413

400414
cb()
401415
})
402416
},
403417
function (cb) {
404-
ticketSchema.getTicketsByStatus(undefined, 0, function (err, tickets) {
418+
ticketSchema.getTicketsByStatus(undefined, m.Types.ObjectId(), function (err, tickets) {
405419
expect(err).to.exist
406420

407421
cb()
408422
})
409423
},
410424
function (cb) {
411-
ticketSchema.getTicketsByStatus(m.Types.ObjectId(), 0, function (err, tickets) {
425+
ticketSchema.getTicketsByStatus(m.Types.ObjectId(), m.Types.ObjectId(), function (err, tickets) {
412426
expect(err).to.exist
413427

414428
cb()
@@ -422,12 +436,16 @@ describe('ticket.js', function () {
422436
})
423437

424438
it('should get all tickets by status', function (done) {
425-
ticketSchema.getAllByStatus(0, function (err, tickets) {
439+
statusSchema.findOne({ uid: 0 }, function (err, status) {
426440
expect(err).to.not.exist
427441

428-
expect(tickets).to.have.length(1)
442+
ticketSchema.getAllByStatus(status._id, function (err, tickets) {
443+
expect(err).to.not.exist
429444

430-
done()
445+
expect(tickets).to.have.length(1)
446+
447+
done()
448+
})
431449
})
432450
})
433451

0 commit comments

Comments
 (0)