Skip to content
This repository has been archived by the owner on Oct 16, 2024. It is now read-only.

Commit

Permalink
Merge pull request #20 from gtaEPIC/tickets
Browse files Browse the repository at this point in the history
Base Ticket system
  • Loading branch information
gtaEPIC authored Nov 10, 2023
2 parents 533d6b7 + fdf7bab commit a214958
Show file tree
Hide file tree
Showing 10 changed files with 1,099 additions and 98 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ these tickets and resolve them.
- [@Annie021 (Anmoljeet Kaur)](https://github.com/Annie021) - Database Programmer
- [@saiham019 (Saiham Salim Ullah)](https://github.com/saiham019) - Web Designer

## ⚠️ DATABASE SETUP ⚠️
The database library is mongoose. <br>
You will need to create a `.env` file in the root directory. <br>
Inside this file you should have the following variables: <br>
```
ATLASDB=<your atlas db connection string>
```
If you do not include this, your program will not work. <br>
**Do not surround with `"`, just use the text and make sure the password is correct.** <br>

## Branch Protection
The `main` branch is protected from being pushed to. <br>
All changes must be made with a branch and a pull request. <br>
Expand Down
4 changes: 3 additions & 1 deletion config/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ var logger = require('morgan');

var indexRouter = require('../routes/index');
var usersRouter = require('../routes/users');
let ticketRouter = require('../routes/ticketRoute');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('views', path.join(__dirname, '../views'));
app.set('view engine', 'ejs');

app.use(logger('dev'));
Expand All @@ -21,6 +22,7 @@ app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use('/ticket', ticketRouter);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
Expand Down
Empty file removed config/config.js
Empty file.
17 changes: 17 additions & 0 deletions config/db.js
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;
}
121 changes: 121 additions & 0 deletions controllers/ticketController.js
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);
}
}
47 changes: 47 additions & 0 deletions models/ticket.js
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);
});
}
Loading

0 comments on commit a214958

Please sign in to comment.