Skip to content

Commit

Permalink
fix(tests): status updates
Browse files Browse the repository at this point in the history
  • Loading branch information
polonel committed Jun 22, 2023
1 parent ece5b34 commit a089792
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 70 deletions.
28 changes: 17 additions & 11 deletions src/models/ticket.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ const commentSchema = require('./comment')
const noteSchema = require('./note')
const attachmentSchema = require('./attachment')
const historySchema = require('./history')
const statusSchema = require('./ticketStatus')
require('./tag')
require('./ticketpriority')
require('./tickettype')
require('./ticketStatus')

const COLLECTION = 'tickets'

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

self.closedDate = status.isResolved ? new Date() : null
self.closedDate = statusModel.isResolved ? new Date() : null
self.status = status

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

const self = this

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

return q.exec(callback)
const unresolvedStatusesIds = statuses.map(i => i._id)

const q = self
.model(COLLECTION)
.find({ assignee: userId, deleted: false, status: { $in: unresolvedStatusesIds } })
.populate(
'owner assignee comments.owner notes.owner subscribers history.owner',
'username fullname email role image title'
)
.populate('type tags status group')

return q.exec(callback)
})
}

/**
Expand Down
12 changes: 12 additions & 0 deletions test/0_database.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ before(function (done) {
var typeSchema = require('../src/models/tickettype')
typeSchema.insertMany([{ name: 'Task' }, { name: 'Issue' }], cb)
},
function (cb) {
var statusSchema = require('../src/models/ticketStatus')
statusSchema.insertMany(
[
{ name: 'New', uid: 0, isLocked: true },
{ name: 'Open', uid: 1, isLocked: true },
{ name: 'Pending', uid: 2, isLocked: true },
{ name: 'Closed', uid: 3, isLocked: true, isResolved: true }
],
cb
)
},
function (cb) {
require('../src/settings/defaults').init(cb)
},
Expand Down
136 changes: 77 additions & 59 deletions test/models/ticket.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var m = require('mongoose')
var ticketSchema = require('../../src/models/ticket')
var groupSchema = require('../../src/models/group')
var prioritySchema = require('../../src/models/ticketpriority')
var statusSchema = require('../../src/models/ticketStatus')

describe('ticket.js', function () {
// it('should clear collections.', function(done) {
Expand All @@ -21,77 +22,90 @@ describe('ticket.js', function () {
prioritySchema.findOne({ default: true }).exec(function (err, p) {
expect(err).to.not.exist
expect(p).to.be.a('object')

ticketSchema.create(
{
owner: m.Types.ObjectId(),
group: m.Types.ObjectId(),
status: 0,
tags: [],
date: new Date(),
subject: 'Dummy Test Subject',
issue: 'Dummy Test Issue',
priority: p._id,
type: m.Types.ObjectId(),
history: []
},
function (err, t) {
expect(err).to.not.exist
expect(t).to.be.a('object')
expect(t._doc).to.include.keys(
'_id',
'uid',
'owner',
'group',
'status',
'tags',
'date',
'subject',
'issue',
'priority',
'type',
'history',
'attachments',
'comments',
'deleted'
)

expect(t.uid).to.equal(1000)

done()
}
)
statusSchema.findOne({ uid: 0 }).exec(function (err, status) {
expect(err).to.not.exist
expect(status).to.be.a('object')
ticketSchema.create(
{
owner: m.Types.ObjectId(),
group: m.Types.ObjectId(),
status: status._id,
tags: [],
date: new Date(),
subject: 'Dummy Test Subject',
issue: 'Dummy Test Issue',
priority: p._id,
type: m.Types.ObjectId(),
history: []
},
function (err, t) {
expect(err).to.not.exist
expect(t).to.be.a('object')
expect(t._doc).to.include.keys(
'_id',
'uid',
'owner',
'group',
'status',
'tags',
'date',
'subject',
'issue',
'priority',
'type',
'history',
'attachments',
'comments',
'deleted'
)

expect(t.uid).to.equal(1000)

done()
}
)
})
})
})

it('should set the ticket status to closed then to open', function (done) {
async.series(
[
function (cb) {
ticketSchema.getTicketByUid(1000, function (err, ticket) {
statusSchema.findOne({ uid: 3 }, function (err, status) {
expect(err).to.not.exist
expect(ticket).to.be.a('object')
expect(status).to.be.a('object')

ticket.setStatus(m.Types.ObjectId(), 3, function (err, ticket) {
ticketSchema.getTicketByUid(1000, function (err, ticket) {
expect(err).to.not.exist
expect(ticket.status).to.equal(3)
expect(ticket.closedDate).to.exist
expect(ticket).to.be.a('object')

cb()
ticket.setStatus(ticket._id, status._id, function (err, ticket) {
expect(err).to.not.exist
expect(ticket.status).to.equal(status._id)
expect(ticket.closedDate).to.exist

cb()
})
})
})
},
function (cb) {
ticketSchema.getTicketByUid(1000, function (err, ticket) {
statusSchema.findOne({ uid: 1 }, function (err, status) {
expect(err).to.not.exist
expect(ticket).to.be.a('object')

ticket.setStatus(m.Types.ObjectId(), 1, function (err, ticket) {
expect(status).to.be.a('object')
ticketSchema.getTicketByUid(1000, function (err, ticket) {
expect(err).to.not.exist
expect(ticket.status).to.equal(1)
expect(ticket.closedDate).to.not.exist
expect(ticket).to.be.a('object')

cb()
ticket.setStatus(ticket._id, status._id, function (err, ticket) {
expect(err).to.not.exist
console.log(ticket)
expect(ticket.status).to.equal(status._id)
expect(ticket.closedDate).to.not.exist

cb()
})
})
})
}
Expand Down Expand Up @@ -393,22 +407,22 @@ describe('ticket.js', function () {
async.parallel(
[
function (cb) {
ticketSchema.getTicketsByStatus([m.Types.ObjectId()], 0, function (err, tickets) {
ticketSchema.getTicketsByStatus([m.Types.ObjectId()], m.Types.ObjectId(), function (err, tickets) {
expect(err).to.not.exist
expect(tickets).to.have.length(0)

cb()
})
},
function (cb) {
ticketSchema.getTicketsByStatus(undefined, 0, function (err, tickets) {
ticketSchema.getTicketsByStatus(undefined, m.Types.ObjectId(), function (err, tickets) {
expect(err).to.exist

cb()
})
},
function (cb) {
ticketSchema.getTicketsByStatus(m.Types.ObjectId(), 0, function (err, tickets) {
ticketSchema.getTicketsByStatus(m.Types.ObjectId(), m.Types.ObjectId(), function (err, tickets) {
expect(err).to.exist

cb()
Expand All @@ -422,12 +436,16 @@ describe('ticket.js', function () {
})

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

expect(tickets).to.have.length(1)
ticketSchema.getAllByStatus(status._id, function (err, tickets) {
expect(err).to.not.exist

done()
expect(tickets).to.have.length(1)

done()
})
})
})

Expand Down

0 comments on commit a089792

Please sign in to comment.