This repository has been archived by the owner on Oct 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from gtaEPIC/tickets
Base Ticket system
- Loading branch information
Showing
10 changed files
with
1,099 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Database setup | ||
const mongoose = require('mongoose'); | ||
require('dotenv').config(); | ||
|
||
module.exports = function(){ | ||
|
||
mongoose.connect(process.env.ATLASDB); | ||
|
||
let mongodb = mongoose.connection; | ||
|
||
mongodb.on('error', console.error.bind(console, 'Connection Error: ')); | ||
mongodb.once('open', ()=>{ | ||
console.log("====> Connected to MongoDB."); | ||
}) | ||
|
||
return mongodb; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
const ticketModel = require('../models/ticket'); | ||
|
||
// TODO: Add proper authentication to all routes | ||
|
||
// List all tickets | ||
module.exports.list = async function (req, res, next) { | ||
try { | ||
let list = await ticketModel.find({}); // .populate('User'); | ||
res.json({ success: true, list: list }); | ||
} catch (error) { | ||
console.log(error); | ||
next(error); | ||
} | ||
} | ||
|
||
// Get a ticket by record | ||
module.exports.getTicketByRecord = async function (req, res, next) { | ||
try { | ||
let ticket = await ticketModel.findOne({ record: req.params.id }); | ||
if (!ticket) | ||
throw new Error('Ticket not found. Are you sure it exists?') | ||
|
||
res.json({ success: true, ticket: ticket }); | ||
} catch (error) { | ||
console.log(error); | ||
next(error); | ||
} | ||
} | ||
|
||
// Create a ticket | ||
module.exports.createTicket = async function (req, res, next) { | ||
try { | ||
let ticket = await ticketModel.create({ | ||
title: req.body.title, | ||
description: req.body.description, | ||
status: ticketModel.TicketStatus.Open, | ||
priority: 1, | ||
record: ticketModel.FormatDateToRecord(new Date()) + '-' + await ticketModel.MakeRecordDigits(ticketModel), | ||
dateCreated: new Date(), | ||
updated: new Date(), | ||
//user: req.body.user, | ||
|
||
}); | ||
res.json({ success: true, ticket: ticket }); | ||
} catch (error) { | ||
console.log(error); | ||
next(error); | ||
} | ||
} | ||
|
||
// Update a ticket | ||
module.exports.updateTicket = async function (req, res, next) { | ||
try { | ||
let ticket = await ticketModel.findOne({ record: req.params.id }); | ||
if (!ticket) | ||
throw new Error('Ticket not found. Are you sure it exists?') | ||
|
||
let updatedTicket = ticketModel(req.body); | ||
updatedTicket._id = ticket._id; | ||
updatedTicket.record = ticket.record; | ||
updatedTicket.dateCreated = ticket.dateCreated; | ||
updatedTicket.updated = new Date(); | ||
|
||
let result = await ticketModel.updateOne({ _id: ticket._id }, updatedTicket); | ||
console.log(result); | ||
if (result.modifiedCount > 0) { | ||
res.json( | ||
{ | ||
success: true, | ||
message: "Ticket updated successfully." | ||
} | ||
); | ||
} | ||
else { | ||
// Express will catch this on its own. | ||
throw new Error('Ticket not updated. Are you sure it exists?') | ||
} | ||
} catch (error) { | ||
console.log(error); | ||
next(error) | ||
} | ||
} | ||
|
||
// Disable a ticket | ||
module.exports.disableTicket = async function (req, res, next) { | ||
try { | ||
let ticket = await ticketModel.findOne({ record: req.params.id }); | ||
if (!ticket) throw new Error('Ticket not found. Are you sure it exists?') | ||
|
||
// TODO: Create an iteration | ||
let updatedTicket = ticketModel({ | ||
_id: ticket._id, | ||
title: ticket.title, | ||
description: ticket.description, | ||
status: ticketModel.TicketStatus.Cancelled, | ||
priority: ticket.priority, | ||
record: ticket.record, | ||
dateCreated: ticket.dateCreated, | ||
updated: ticket.updated, | ||
//user: ticket.user, | ||
}); | ||
let result = await ticketModel.updateOne({ _id: ticket._id }, updatedTicket); | ||
console.log(result); | ||
|
||
if (result.modifiedCount > 0) { | ||
res.json( | ||
{ | ||
success: true, | ||
message: "Ticket disabled successfully." | ||
} | ||
); | ||
} | ||
else { | ||
// Express will catch this on its own. | ||
throw new Error('Ticket not disabled. Are you sure it exists?') | ||
} | ||
} catch (error) { | ||
console.log(error); | ||
next(error); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
const mongoose = require('mongoose'); | ||
const Schema = mongoose.Schema; | ||
|
||
// Ticket status "enum" | ||
// TODO: Add other values for proper ticket workflow | ||
const TicketStatus = { | ||
Open: 'Open', | ||
InProgress: 'In Progress', | ||
Closed: 'Closed', | ||
Cancelled: 'Cancelled' | ||
} | ||
|
||
const TicketSchema = new Schema({ | ||
title: {type: String, required: true}, // Title of the ticket | ||
description: String, // Description of the ticket | ||
status: {type: String, default: 'Open', required: true}, // Status of the ticket | ||
priority: {type: Number, default: 1, required: true}, // Priority of the ticket | ||
record: {type: String, required: true, unique: true}, // Record of the ticket (Format to be "YYYYMMDD-XXXX") | ||
dateCreated: {type: Date, default: Date.now, required: true}, // Date the ticket was created. | ||
updated: {type: Date, default: Date.now, required: true}, // Date the ticket was last updated. | ||
//TODO: Finish adding User | ||
//user: {type: Schema.Types.ObjectId, ref: 'User', required: true}, // User that created the ticket | ||
//TODO: Add iteration field | ||
}); | ||
|
||
TicketSchema.set('toJSON', { | ||
virtuals: true, | ||
versionKey: false, | ||
transform: function (doc, ret) { | ||
// remove these props when object is serialized | ||
delete ret._id; | ||
delete ret.id; | ||
} | ||
}); | ||
|
||
module.exports = mongoose.model('Ticket', TicketSchema); | ||
module.exports.TicketStatus = TicketStatus; | ||
module.exports.FormatDateToRecord = function(date) { | ||
return date.getFullYear() + | ||
('0' + (date.getMonth() + 1)).slice(-2) + | ||
('0' + date.getDate()).slice(-2); | ||
} | ||
module.exports.MakeRecordDigits = function(ticketModel) { | ||
return ticketModel.countDocuments().then(function (count) { | ||
return ('000' + (count + 1)).slice(-4); | ||
}); | ||
} |
Oops, something went wrong.