Skip to content

Commit

Permalink
PUSH
Browse files Browse the repository at this point in the history
-> Ticket system list
-> Ticket system create ticket
-> Ticket system translation!
NaysKutzu committed Jan 21, 2025
1 parent e9d2477 commit c4a2594
Showing 22 changed files with 1,564 additions and 1,200 deletions.
19 changes: 16 additions & 3 deletions backend/app/Api/User/Ticket/Create.php
Original file line number Diff line number Diff line change
@@ -48,12 +48,14 @@
if (isset($_POST['department_id']) && $_POST['department_id'] != '') {
$departmentId = $_POST['department_id'];
if (Departments::exists((int) $departmentId)) {
/**
* Make that every info needed is provided.
*/
if (isset($_POST['service_id']) && $_POST['service_id'] != '') {
$serviceId = $_POST['service_id'];
} else {
$serviceId = null;
}

if (isset($_POST['subject']) && $_POST['subject'] != '') {
$subject = $_POST['subject'];
} else {
@@ -73,8 +75,19 @@
}

// TODO: Check if service exists
// TODO: Limit to 3 open tickets user

/**
* Check if the user has more than 3 open tickets.
*/
$userTickets = Tickets::getAllTicketsByUser($session->getInfo(UserColumns::UUID, false), 150);
$openTickets = array_filter($userTickets, function ($ticket) {
return in_array($ticket['status'], ['open', 'waiting', 'replied', 'inprogress']);
});
if (count($openTickets) >= 3) {
$appInstance->BadRequest('You have reached the limit of 3 open tickets!', ['error_code' => 'LIMIT_REACHED']);
}
/**
* Create the ticket.
*/
$ticketId = Tickets::create($session->getInfo(UserColumns::UUID, false), $departmentId, $serviceId, $subject, $message, $priority);
if ($ticketId == 0) {
$appInstance->BadRequest('Failed to create ticket!', ['error_code' => 'FAILED_TO_CREATE_TICKET']);
31 changes: 31 additions & 0 deletions backend/app/Api/User/Ticket/List.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/*
* This file is part of MythicalClient.
* Please view the LICENSE file that was distributed with this source code.
*
* # MythicalSystems License v2.0
*
* ## Copyright (c) 2021–2025 MythicalSystems and Cassian Gherman
*
* Breaking any of the following rules will result in a permanent ban from the MythicalSystems community and all of its services.
*/

use MythicalClient\App;
use MythicalClient\Chat\Session;
use MythicalClient\Chat\Tickets;
use MythicalClient\Chat\columns\UserColumns;

$router->get('/api/user/ticket/list', function () {
App::init();
$appInstance = App::getInstance(true);
$appInstance->allowOnlyGET();
$s = new Session($appInstance);

$tickets = Tickets::getAllTicketsByUser($s->getInfo(UserColumns::UUID, false), 150);

$appInstance->OK('Tickets', [
'tickets' => $tickets,
]);

});
12 changes: 6 additions & 6 deletions backend/app/Chat/Departments.php
Original file line number Diff line number Diff line change
@@ -47,7 +47,7 @@ public static function update(
string $close,
): void {
try {
if (self::exists($id)) {
if (!self::exists($id)) {
App::getInstance(true)->getLogger()->warning('Department does not exist but tried to update it.', true);

return;
@@ -72,7 +72,7 @@ public static function exists(int $id): bool
$con = self::getPdoConnection();
$sql = 'SELECT id FROM ' . self::TABLE_NAME . ' WHERE id = :id';
$stmt = $con->prepare($sql);
$stmt->bindParam(':id', $id);
$stmt->bindParam(':id', $id, \PDO::PARAM_INT);
$stmt->execute();

return $stmt->rowCount() > 0;
@@ -86,8 +86,8 @@ public static function exists(int $id): bool
public static function delete(int $id): bool
{
try {
if (self::exists($id)) {
App::getInstance(true)->getLogger()->warning('Department does not exist but tried to update it.', true);
if (!self::exists($id)) {
App::getInstance(true)->getLogger()->warning('Department does not exist but tried to delete it.', true);

return false;
}
@@ -124,8 +124,8 @@ public static function getAll(): array
public static function get(int $id): array
{
try {
if (self::exists($id)) {
App::getInstance(true)->getLogger()->warning('Department does not exist but tried to update it.', true);
if (!self::exists($id)) {
App::getInstance(true)->getLogger()->warning('Department does not exist but tried to get it: ' . $id . '.', true);

return [];
}
38 changes: 38 additions & 0 deletions backend/app/Chat/Tickets.php
Original file line number Diff line number Diff line change
@@ -96,6 +96,44 @@ public static function getAllTickets(int $limit = 150): array
}
}

public static function getAllTicketsByUser(string $uuid, int $limit = 150): array
{
try {
$con = self::getPdoConnection();
$sql = 'SELECT * FROM ' . self::TABLE_NAME . ' WHERE user = :uuid ORDER BY id DESC LIMIT ' . $limit;
$stmt = $con->prepare($sql);
$stmt->bindParam('uuid', $uuid, \PDO::PARAM_STR);
$stmt->execute();

$tickets = $stmt->fetchAll(\PDO::FETCH_ASSOC);

foreach ($tickets as $key => $ticket) {
$tickets[$key]['department_id'] = $ticket['department'];
$tickets[$key]['department'] = Departments::get((int) $ticket['department']);

if (empty($tickets[$key]['department'])) {
$tickets[$key]['department'] = [
'id' => 0,
'name' => 'Deleted Department',
'description' => 'This department has been deleted.',
'time_open' => '08:30',
'time_close' => '17:30',
'enabled' => 'true',
'deleted' => 'false',
'locked' => 'false',
'date' => '2024-12-25 22:25:09',
];
}
}

return $tickets;
} catch (\Exception $e) {
self::db_Error('Failed to get all tickets by user: ' . $e->getMessage());

return [];
}
}

public static function getTicket(int $id): array
{
try {
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE `mythicalclient_tickets` ADD `status` ENUM('open','closed','waiting','replied','inprogress') NOT NULL DEFAULT 'open' AFTER `priority`;
Loading

0 comments on commit c4a2594

Please sign in to comment.