From b43ed1204fc109fc2c6e8cda3c5103d24b4a450c Mon Sep 17 00:00:00 2001 From: Alex Date: Sat, 28 Nov 2020 19:17:23 -0600 Subject: [PATCH] updated GET /api/ticket/id to return CombinedDTO --- api/src/ticket/ticket.service.ts | 81 ++++++++++++++++++++++++++++++-- 1 file changed, 77 insertions(+), 4 deletions(-) diff --git a/api/src/ticket/ticket.service.ts b/api/src/ticket/ticket.service.ts index ca257b3..332951b 100644 --- a/api/src/ticket/ticket.service.ts +++ b/api/src/ticket/ticket.service.ts @@ -300,30 +300,103 @@ export class TicketService { /* WORKING implementation */ async findOne(ticket_id: number) { + let response: CombinedDTO; + /* Query ticket by ticket_id */ - const query: QueryConfig = { + const ticketQuery: QueryConfig = { name: 'select_ticket_by_ticket_id', text: 'SELECT * FROM ticket WHERE ticket_id = $1', values: [ticket_id], }; try { - const res = await this.connection.query(query); + const res = await this.connection.query(ticketQuery); /* If no ticket found return empty object*/ if (res.rows.length === 0) { return {}; } + /* Accumulate ticket into response */ + const ticket = res.rows[0]; + response = { ...response, ...ticket }; + } catch (error) { + throw new HttpException( + { + query: ticketQuery, + error: error, + }, + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } - return res.rows[0]; + /* Query user by ticket_id */ + const userQuery: QueryConfig = { + name: 'select_user_by_lsu_id', + text: 'SELECT * FROM "user" WHERE lsu_id = $1', + values: [response.lsu_id], + }; + try { + const res = await this.connection.query(userQuery); + + /* If no ticket throw custom error*/ + if (res.rows.length === 0) { + throw new Error('BAD_REQUEST'); + } + /* Accumulate ticket into response */ + const user = res.rows[0]; + response = { ...response, ...user }; + } catch (error) { + if ((error.message = 'BAD_REQUEST')) { + throw new HttpException( + { + query: ticketQuery, + error: `Could not find user with lsu_id: ${userQuery.values[0]}`, + }, + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + throw new HttpException( + { + query: ticketQuery, + error: error, + }, + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + /* Query device by ticket_id */ + const deviceQuery: QueryConfig = { + name: 'select_device_by_ticket_id', + text: 'SELECT * FROM "device" WHERE ticket_id = $1', + values: [ticket_id], + }; + try { + const res = await this.connection.query(deviceQuery); + + /* If no device found throw custom error*/ + if (res.rows.length === 0) { + throw new Error('BAD_REQUEST'); + } + /* Accumulate device into response */ + const device = res.rows[0]; + response = { ...response, ...device }; } catch (error) { + if (error.message === 'BAD_REQUEST') { + throw new HttpException( + { + query: deviceQuery, + error: `Could not find device with ticket_id: ${deviceQuery.values[0]}`, + }, + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } throw new HttpException( { - query: query, + query: ticketQuery, error: error, }, HttpStatus.INTERNAL_SERVER_ERROR, ); } + return response; } /* WORKING Implementation */