From a8c474b34672477394730f562138ee2006651f38 Mon Sep 17 00:00:00 2001 From: hyh123a Date: Tue, 19 Apr 2022 10:39:49 +0800 Subject: [PATCH] complete the ticket intervention function --- myems-api/app.py | 15 +- myems-api/reports/ticket.py | 228 +++++++++++++++++- .../MyEMS/Ticket/TicketApplication.js | 2 +- .../MyEMS/Ticket/TicketIntervention.js | 8 +- .../src/components/MyEMS/Ticket/TicketList.js | 18 +- 5 files changed, 249 insertions(+), 22 deletions(-) diff --git a/myems-api/app.py b/myems-api/app.py index d7737818..c8d18951 100644 --- a/myems-api/app.py +++ b/myems-api/app.py @@ -104,9 +104,9 @@ api = falcon.App(middleware=[cors.middleware, MultipartMiddleware()]) # Get Ticket Type api.add_route('/ticket/types', ticket.TicketTypeCollection()) -# Get Ticket List: all -api.add_route('/ticket/list', - ticket.TicketListCollection()) +# Get Ticket List: My Apply +api.add_route('/ticket/list/apply', + ticket.TicketApplicationListCollection()) # Apply One Ticket api.add_route('/ticket/apply/{id_}', @@ -116,6 +116,15 @@ api.add_route('/ticket/apply/{id_}', api.add_route('/ticket/list/agent', ticket.TicketAgentListCollection()) + +# Get Ticket List: My Relation +api.add_route('/ticket/list/completed', + ticket.TicketCompletedListCollection()) + +# Get Ticket List: Has Intervention Auth And My Relation: Only admin +api.add_route('/ticket/list/intervention', + ticket.TicketInterventionListCollection()) + ######################################################################################################################## # Routes for System Core ######################################################################################################################## diff --git a/myems-api/reports/ticket.py b/myems-api/reports/ticket.py index 959638bd..f7af6836 100644 --- a/myems-api/reports/ticket.py +++ b/myems-api/reports/ticket.py @@ -66,7 +66,7 @@ class TicketTypeCollection: resp.text = json.dumps(result) -class TicketListCollection: +class TicketApplicationListCollection: @staticmethod def __init__(): """"Initializes ContactCollection""" @@ -137,7 +137,7 @@ class TicketListCollection: " FROM ticket_ticketrecord " " WHERE creator = %s " " ORDER BY id DESC") - cursor.execute(query, (user_name, )) + cursor.execute(query, (user_name,)) rows = cursor.fetchall() cursor.close() cnx.close() @@ -437,6 +437,230 @@ class TicketAgentListCollection: resp.text = json.dumps(result) +class TicketCompletedListCollection: + @staticmethod + def __init__(): + """"Initializes ContactCollection""" + pass + + @staticmethod + def on_options(req, resp): + resp.status = falcon.HTTP_200 + + @staticmethod + def on_post(req, resp): + + try: + raw_json = req.stream.read().decode('utf-8') + except Exception as ex: + raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', description=ex) + """ + { + "user_name": "admin", + }""" + new_values = json.loads(raw_json) + + checked_fields = [ + { + "name": "user_name", + "type": str + }, + ] + + for field in checked_fields: + _name = field['name'] + _type = field['type'] + if _name not in new_values.keys() or \ + not isinstance(new_values[_name], _type) or \ + len(str(new_values[_name])) == 0: + raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', + description='API.INVALID_FIELD' + _name) + + payload = new_values + user_name = payload['user_name'] + + cnx = mysql.connector.connect(**config.loonflow) + cursor = cnx.cursor() + + # Get Ticket ids: Completed + query = (" SELECT id, ticket_id " + " FROM ticket_ticketuser " + " WHERE username = %s" + " ORDER BY id DESC ") + cursor.execute(query, (user_name,)) + rows = cursor.fetchall() + + ticket_ids = [] + if rows is not None and len(rows) > 0: + for row in rows: + ticket_id = row[1] + if ticket_id not in ticket_ids: + ticket_ids.append(ticket_id) + + query = (" SELECT id, name " + " FROM workflow_workflow " + " ORDER BY id ") + cursor.execute(query) + workflow_rows = cursor.fetchall() + + workflow_result = {} + if workflow_rows is not None and len(workflow_rows) > 0: + for row in workflow_rows: + workflow_result[row[0]] = row[1] + + query = (" SELECT id, name " + " FROM workflow_state " + " ORDER BY id ") + cursor.execute(query) + workflow_state_rows = cursor.fetchall() + + workflow_state_result = {} + if workflow_state_rows is not None and len(workflow_state_rows) > 0: + for row in workflow_state_rows: + workflow_state_result[row[0]] = row[1] + + query = (" SELECT id, title, workflow_id, sn, state_id, creator, gmt_created, gmt_modified " + " FROM ticket_ticketrecord " + " ORDER BY id DESC ") + cursor.execute(query, ) + rows = cursor.fetchall() + cursor.close() + cnx.close() + + result = list() + if rows is not None and len(rows) > 0: + for row in rows: + _id = row[0] + if _id not in ticket_ids: + continue + meta_result = {"id": row[0], + "title": row[1], + "ticket_type": workflow_result.get(row[2]), + "sn": row[3], + "state": workflow_state_result.get(row[4]), + "creator": row[5], + "gmt_created": str(row[6]), + "gmt_modified": str(row[7]) + } + result.append(meta_result) + + resp.text = json.dumps(result) + + +class TicketInterventionListCollection: + @staticmethod + def __init__(): + """"Initializes ContactCollection""" + pass + + @staticmethod + def on_options(req, resp): + resp.status = falcon.HTTP_200 + + @staticmethod + def on_post(req, resp): + + try: + raw_json = req.stream.read().decode('utf-8') + except Exception as ex: + raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', description=ex) + """ + { + "user_name": "admin", + }""" + new_values = json.loads(raw_json) + + checked_fields = [ + { + "name": "user_name", + "type": str + }, + ] + + for field in checked_fields: + _name = field['name'] + _type = field['type'] + if _name not in new_values.keys() or \ + not isinstance(new_values[_name], _type) or \ + len(str(new_values[_name])) == 0: + raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', + description='API.INVALID_FIELD' + _name) + + payload = new_values + user_name = payload['user_name'] + + if user_name != 'admin': + raise falcon.HTTPError(falcon.HTTP_401, title='API.UNAUTHENTICATED', + description='API.INVALID_FIELD' + user_name) + + cnx = mysql.connector.connect(**config.loonflow) + cursor = cnx.cursor() + + # Get Ticket ids: Completed + query = (" SELECT id, ticket_id " + " FROM ticket_ticketuser " + " WHERE username = %s " + " ORDER BY id DESC ") + cursor.execute(query, (user_name,)) + rows = cursor.fetchall() + + ticket_ids = [] + if rows is not None and len(rows) > 0: + for row in rows: + ticket_id = row[1] + if ticket_id not in ticket_ids: + ticket_ids.append(ticket_id) + + query = (" SELECT id, name " + " FROM workflow_workflow " + " ORDER BY id ") + cursor.execute(query) + workflow_rows = cursor.fetchall() + + workflow_result = {} + if workflow_rows is not None and len(workflow_rows) > 0: + for row in workflow_rows: + workflow_result[row[0]] = row[1] + + query = (" SELECT id, name " + " FROM workflow_state " + " ORDER BY id ") + cursor.execute(query) + workflow_state_rows = cursor.fetchall() + + workflow_state_result = {} + if workflow_state_rows is not None and len(workflow_state_rows) > 0: + for row in workflow_state_rows: + workflow_state_result[row[0]] = row[1] + + query = (" SELECT id, title, workflow_id, sn, state_id, creator, gmt_created, gmt_modified " + " FROM ticket_ticketrecord " + " ORDER BY id DESC ") + cursor.execute(query, ) + rows = cursor.fetchall() + cursor.close() + cnx.close() + + result = list() + if rows is not None and len(rows) > 0: + for row in rows: + _id = row[0] + if _id not in ticket_ids: + continue + meta_result = {"id": row[0], + "title": row[1], + "ticket_type": workflow_result.get(row[2]), + "sn": row[3], + "state": workflow_state_result.get(row[4]), + "creator": row[5], + "gmt_created": str(row[6]), + "gmt_modified": str(row[7]) + } + result.append(meta_result) + + resp.text = json.dumps(result) + + class ContactItem: @staticmethod def __init__(): diff --git a/myems-web/src/components/MyEMS/Ticket/TicketApplication.js b/myems-web/src/components/MyEMS/Ticket/TicketApplication.js index 98083fe6..e080b8e7 100644 --- a/myems-web/src/components/MyEMS/Ticket/TicketApplication.js +++ b/myems-web/src/components/MyEMS/Ticket/TicketApplication.js @@ -109,7 +109,7 @@ const TicketApplication = ({ setRedirect, setRedirectUrl, t }) => { useEffect(() => { let isResponseOK = false; // Get Ticket List - fetch(APIBaseURL + '/ticket/list', { + fetch(APIBaseURL + '/ticket/list/apply', { method: 'POST', headers: { 'Content-type': 'application/json' diff --git a/myems-web/src/components/MyEMS/Ticket/TicketIntervention.js b/myems-web/src/components/MyEMS/Ticket/TicketIntervention.js index ffe603bc..02725587 100644 --- a/myems-web/src/components/MyEMS/Ticket/TicketIntervention.js +++ b/myems-web/src/components/MyEMS/Ticket/TicketIntervention.js @@ -100,12 +100,14 @@ const TicketIntervention = ({ setRedirect, setRedirectUrl, t }) => { useEffect(() => { let isResponseOK = false; // Get Ticket List - fetch(APIBaseURL + '/ticket/list', { - method: 'GET', + fetch(APIBaseURL + '/ticket/list/intervention', { + method: 'POST', headers: { 'Content-type': 'application/json' }, - body: null + body: JSON.stringify({ + user_name: 'admin' + }) }) .then(response => { console.log(response); diff --git a/myems-web/src/components/MyEMS/Ticket/TicketList.js b/myems-web/src/components/MyEMS/Ticket/TicketList.js index a452b3ab..7dff4fc1 100644 --- a/myems-web/src/components/MyEMS/Ticket/TicketList.js +++ b/myems-web/src/components/MyEMS/Ticket/TicketList.js @@ -100,12 +100,14 @@ const TicketList = ({ setRedirect, setRedirectUrl, t }) => { useEffect(() => { let isResponseOK = false; // Get Ticket List - fetch(APIBaseURL + '/ticket/list', { - method: 'GET', + fetch(APIBaseURL + '/ticket/list/completed', { + method: 'POST', headers: { 'Content-type': 'application/json' }, - body: null + body: JSON.stringify({ + user_name: 'admin' + }) }) .then(response => { console.log(response); @@ -129,16 +131,6 @@ const TicketList = ({ setRedirect, setRedirectUrl, t }) => { const DetailedDataTable = loadable(() => import('../common/DetailedDataTable')); - const nameFormatter = (dataField, { name }) => ( - - - -
{name}
-
-
- - ); - const actionFormatter = (dataField, { id }) => ( // Control your row with this id