Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API endpoint for ticket cancellation #140

Merged
merged 1 commit into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/controllers/Ticket.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,21 @@ export class TicketController {
) {
return await this.ticketService.update(id, update);
}

@ContentType("application/json")
@Post("/cancel")
@Summary("Cancel ticket")
@Description("Returns updated ticket from databse or null.")
@Returns(200, TicketModel)
@Returns(400).Description("Did not send id")
@Returns(400).Description("Did not send email")
@Returns(404).Description("Ticket not found in database")
@Returns(400).Description("User send bad email.")
@Returns(409).Description("Ticket already cancelled.")
async cancelById(
@BodyParams("id") id: string,
@BodyParams("email") email: string
) {
return await this.ticketService.cancel(id, email);
}
}
52 changes: 51 additions & 1 deletion src/services/Ticket.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Inject, Injectable } from "@tsed/di";
import { BadRequest } from "@tsed/exceptions";
import { BadRequest, Conflict, NotFound } from "@tsed/exceptions";
import { MongooseModel } from "@tsed/mongoose";
import moment from "moment";
import { QRCode, QRSvg } from 'sexy-qr';
Expand Down Expand Up @@ -308,4 +308,54 @@ export class TicketService {
return csvData;
}

async cancel(
id: string,
email: string
) {
if (!id) {
throw new BadRequest("Did not send id");
}

if (!email) {
throw new BadRequest("Did not send email");
}

// Remove whitespaces
email = email.trim();

// Find ticket in database
let obj = await this.findById(id);

if (!obj) {
throw new NotFound("Ticket not found in database");
}

// Check given email and ticket email
if (obj.email.toLowerCase() != email.toLocaleLowerCase()) {
throw new BadRequest("User send bad email.");

}

if (obj.status == 'cancelled') {
throw new Conflict("Ticket already cancelled.");
}

// Cancel ticket
obj.status = 'cancelled';
obj.statusChanges.push({
date: new Date(),
status: 'cancelled'
});

// Save to database
await obj.save();

// Get saved ticket from database
let res = await obj.populate(["year", "time"]);

// Send ticket to clients
this.wss.broadcast("update-ticket", res);
this.wss.broadcast("cancelled-ticket", res);
return res;
}
}