Skip to content

Commit

Permalink
PUT /ticket/{ticket_id} now updates user,ticket,device
Browse files Browse the repository at this point in the history
  • Loading branch information
alxford45 committed Nov 29, 2020
1 parent eb94b4a commit 7ca8e0d
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 27 deletions.
17 changes: 3 additions & 14 deletions api/src/ticket/ticket.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { ApiTags } from '@nestjs/swagger';
import { CreateCombinedDTO } from './dto/create-combined.dto';
import { TicketType } from './dto/ticket.dto';
import { UpdateCombinedDTO } from './dto/update-combined.dto';
import { UpdateTicketDto } from './dto/update-ticket.dto';
import { TicketService } from './ticket.service';

Expand Down Expand Up @@ -68,20 +69,8 @@ export class TicketController {
@Put(':ticket_id')
update(
@Param('ticket_id') ticket_id: number,
@Body() updateTicketDto: UpdateTicketDto,
@Body() updateCombinedDTO: UpdateCombinedDTO,
) {
Logger.log(
{
req: {
http: `PUT /api/ticket/${ticket_id}`,
params: ticket_id,
body: updateTicketDto,
},
},
'TicketController.update',
false,
);

return this.ticketService.update(ticket_id, updateTicketDto);
return this.ticketService.update(ticket_id, updateCombinedDTO);
}
}
148 changes: 135 additions & 13 deletions api/src/ticket/ticket.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ import { DeviceDTO } from './dto/device.dto';
import { TicketDTO, TicketType } from './dto/ticket.dto';
import { UpdateTicketDto } from './dto/update-ticket.dto';
import { createDate } from 'src/util';
import { UpdateCombinedDTO } from './dto/update-combined.dto';
import { UpdateUserDTO } from 'src/user/dto/update-user.dto';
import { UpdateDeviceDTO } from './dto/update-device.dto';
import { response } from 'express';

@Injectable()
export class TicketService {
Expand Down Expand Up @@ -160,19 +164,21 @@ export class TicketService {
model,
operating_system,
operating_system_version,
component,
} = createDeviceDTO;

/* Insert Device into db */
const query: QueryConfig = {
name: 'insert_device',
text:
'INSERT INTO device(ticket_id, manufacturer, model, operating_system, operating_system_version) VALUES ($1, $2, $3, $4, $5) RETURNING *',
'INSERT INTO device(ticket_id, manufacturer, model, operating_system, operating_system_version, component) VALUES ($1, $2, $3, $4, $5 $6) RETURNING *',
values: [
ticket_id,
manufacturer,
model,
operating_system,
operating_system_version,
component,
],
};
try {
Expand Down Expand Up @@ -400,14 +406,9 @@ export class TicketService {
}

/* WORKING Implementation */
async update(ticket_id: number, updateTicketDto: UpdateTicketDto) {
const {
core_issue,
description,
problem_category,
status,
priority,
} = updateTicketDto;
async update(ticket_id: number, updateCombinedDTO: UpdateCombinedDTO) {
let response: CombinedDTO;
let old_lsu_id;

/* Check to see if ticket exist */
const findQuery: QueryConfig = {
Expand All @@ -422,6 +423,7 @@ export class TicketService {
if (res.rows.length < 1) {
throw new Error('BAD_REQUEST');
}
old_lsu_id = res.rows[0].lsu_id;
} catch (error) {
/* catch custom error for ticket not found */
if (error.message === 'BAD_REQUEST') {
Expand All @@ -442,9 +444,66 @@ export class TicketService {
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
/* Update User */
const updateUser: UpdateUserDTO = { ...updateCombinedDTO };
const {
lsu_id,
email,
first_name,
last_name,
department,
phone_number,
} = updateUser;
const updateUserQuery: QueryConfig = {
name: 'update_user',
text:
'UPDATE "user" SET lsu_id = $1, email = $2, first_name = $3, last_name = $4, department = $5, phone_number = $6 WHERE lsu_id = $7 RETURNING *',
values: [
lsu_id,
email,
first_name,
last_name,
department,
phone_number,
old_lsu_id,
],
};
try {
const res = await this.connection.query<UserDTO>(updateUserQuery);
if (res.rows.length < 1) {
throw new Error('BAD_REQUEST');
}
const user = res.rows[0];
response = { ...response, ...user };
} catch (error) {
if (error.message === 'BAD_REQUEST') {
throw new HttpException(
{
query: updateUserQuery,
error: `Update query could not find existing user to update`,
},
HttpStatus.BAD_REQUEST,
);
}
throw new HttpException(
{
query: updateUserQuery,
error: error,
},
HttpStatus.INTERNAL_SERVER_ERROR,
);
}

/* Update ticket */
const updateQuery = {
const updateTicket: UpdateTicketDto = { ...updateCombinedDTO };
const {
core_issue,
description,
problem_category,
status,
priority,
} = updateTicket;
const updateTicketQuery = {
name: 'update_ticket',
text:
'UPDATE ticket SET core_issue = $1, description = $2, problem_category = $3, status = $4, priority = $5 WHERE ticket_id = $6 RETURNING *',
Expand All @@ -458,16 +517,79 @@ export class TicketService {
],
};
try {
const res = await this.connection.query(updateQuery);
return res.rows[0];
const res = await this.connection.query<TicketDTO>(updateTicketQuery);
if (res.rows.length < 1) {
throw new Error('BAD_REQUEST');
}
const ticket = res.rows[0];
response = { ...response, ...ticket };
} catch (error) {
if (error.message === 'BAD_REQUEST') {
throw new HttpException(
{
query: updateUserQuery,
error: `Update query could not find existing user to update`,
},
HttpStatus.BAD_REQUEST,
);
}
throw new HttpException(
{
query: updateQuery,
query: updateTicketQuery,
error: error,
},
HttpStatus.INTERNAL_SERVER_ERROR,
);
}

/* Update device */
const updateDevice: UpdateDeviceDTO = { ...updateCombinedDTO };
const {
manufacturer,
model,
operating_system,
operating_system_version,
component,
} = updateDevice;
const updateDeviceQuery = {
name: 'update_device',
text:
'UPDATE device SET manufacturer = $1, model = $2, operating_system = $3, operating_system_version = $4, component = $5 WHERE ticket_id = $6 RETURNING *',
values: [
manufacturer,
model,
operating_system,
operating_system_version,
component,
ticket_id,
],
};
try {
const res = await this.connection.query<DeviceDTO>(updateDeviceQuery);
if (res.rows.length < 1) {
throw new Error('BAD_REQUEST');
}
const device = res.rows[0];
response = { ...response, ...device };
} catch (error) {
if (error.message === 'BAD_REQUEST') {
throw new HttpException(
{
query: updateDeviceQuery,
error: `Update query could not find existing device to update`,
},
HttpStatus.BAD_REQUEST,
);
}
throw new HttpException(
{
query: updateDeviceQuery,
error: error,
},
HttpStatus.INTERNAL_SERVER_ERROR,
);
}

return response;
}
}

0 comments on commit 7ca8e0d

Please sign in to comment.