diff --git a/api/README.md b/api/README.md index c8821c6..67bc834 100644 --- a/api/README.md +++ b/api/README.md @@ -110,11 +110,12 @@ req: none res: body: {CombineDTO} ``` -Update ticket +Updates one ticket by ticket_id; Does NOT update user or device ``` -/* NOT WORKING */ -PUT /api​/ticket​/{id} +PUT /api​/ticket​/{ticket_id} +req: body: {UpdateTicketDTO} +res: body: {TicketDTO} ``` ## User @@ -155,11 +156,12 @@ GET /api​/user/{lsu_id} res: body: {UserDTO} ``` -Update user +Update user by lsu_id ``` -/* NOT WORKING */ PUT /api/user/{lsu_id} +req: body: {UpdateUserDTO} +res: body: {UserDTO} ``` ## Assign diff --git a/api/src/main.ts b/api/src/main.ts index 99e07ee..ffa443f 100644 --- a/api/src/main.ts +++ b/api/src/main.ts @@ -30,7 +30,7 @@ async function bootstrap() { .setVersion('0.1.0') .setExternalDoc( 'additional documentation', - `http://${HOST}:${PORT}/api/docs/more`, + `https://github.com/cdalton713/CSC_4402_DB_Project/blob/master/api/README.md`, ) .build(); const document = SwaggerModule.createDocument(app, options); diff --git a/api/src/ticket/dto/combined.dto.ts b/api/src/ticket/dto/combined.dto.ts index 50d1870..19a9be4 100644 --- a/api/src/ticket/dto/combined.dto.ts +++ b/api/src/ticket/dto/combined.dto.ts @@ -1,8 +1,11 @@ import { IntersectionType } from '@nestjs/swagger'; -import { Ticket } from './ticket.dto'; -import { User } from 'src/user/dto/user.dto'; -import { Device } from './device.dto'; +import { TicketDTO } from './ticket.dto'; +import { UserDTO } from 'src/user/dto/user.dto'; +import { DeviceDTO } from './device.dto'; -class TicketAndDevice extends IntersectionType(Ticket, Device) {} +class TicketAndDeviceDTO extends IntersectionType(TicketDTO, DeviceDTO) {} -export class Combined extends IntersectionType(TicketAndDevice, User) {} +export class CombinedDTO extends IntersectionType( + TicketAndDeviceDTO, + UserDTO, +) {} diff --git a/api/src/ticket/dto/create-combined.dto.ts b/api/src/ticket/dto/create-combined.dto.ts index 2abca50..c4a5807 100644 --- a/api/src/ticket/dto/create-combined.dto.ts +++ b/api/src/ticket/dto/create-combined.dto.ts @@ -1,14 +1,14 @@ import { IntersectionType, OmitType as Omit } from '@nestjs/swagger'; -import { CreateUser } from 'src/user/dto/create-user.dto'; -import { CreateTicket } from './create-ticket.dto'; -import { CreateDevice } from './create-device.dto'; +import { CreateUserDTO } from 'src/user/dto/create-user.dto'; +import { CreateTicketDTO } from './create-ticket.dto'; +import { CreateDeviceDTO } from './create-device.dto'; -class createTicketAndDevice extends IntersectionType( - CreateTicket, - Omit(CreateDevice, ['ticket_id']), +class createTicketAndDeviceDTO extends IntersectionType( + CreateTicketDTO, + Omit(CreateDeviceDTO, ['ticket_id']), ) {} -export class CreateCombined extends IntersectionType( - createTicketAndDevice, - Omit(CreateUser, ['admin']), +export class CreateCombinedDTO extends IntersectionType( + createTicketAndDeviceDTO, + Omit(CreateUserDTO, ['admin']), ) {} diff --git a/api/src/ticket/dto/create-device.dto.ts b/api/src/ticket/dto/create-device.dto.ts index 490c3f0..ee9b3ea 100644 --- a/api/src/ticket/dto/create-device.dto.ts +++ b/api/src/ticket/dto/create-device.dto.ts @@ -1,4 +1,4 @@ import { OmitType as Omit } from '@nestjs/swagger'; -import { Device } from './device.dto'; +import { DeviceDTO } from './device.dto'; -export class CreateDevice extends Omit(Device, ['device_id']) {} +export class CreateDeviceDTO extends Omit(DeviceDTO, ['device_id']) {} diff --git a/api/src/ticket/dto/create-ticket.dto.ts b/api/src/ticket/dto/create-ticket.dto.ts index cd893c9..a8adb5f 100644 --- a/api/src/ticket/dto/create-ticket.dto.ts +++ b/api/src/ticket/dto/create-ticket.dto.ts @@ -1,4 +1,8 @@ -import { Ticket } from './ticket.dto'; +import { TicketDTO } from './ticket.dto'; import { OmitType as Omit } from '@nestjs/swagger'; -export class CreateTicket extends Omit(Ticket, ['ticket_id', 'status']) {} +export class CreateTicketDTO extends Omit(TicketDTO, [ + 'ticket_id', + 'status', + 'submission_date', +]) {} diff --git a/api/src/ticket/dto/device.dto.ts b/api/src/ticket/dto/device.dto.ts index fcba3f0..022de62 100644 --- a/api/src/ticket/dto/device.dto.ts +++ b/api/src/ticket/dto/device.dto.ts @@ -1,6 +1,6 @@ import { ApiProperty } from '@nestjs/swagger'; -export class Device { +export class DeviceDTO { @ApiProperty({ description: 'id of device', example: 1 }) device_id: number; diff --git a/api/src/ticket/dto/ticket.dto.ts b/api/src/ticket/dto/ticket.dto.ts index a62c001..4d5fd08 100644 --- a/api/src/ticket/dto/ticket.dto.ts +++ b/api/src/ticket/dto/ticket.dto.ts @@ -6,7 +6,7 @@ export enum TicketType { 'ANY', } -export class Ticket { +export class TicketDTO { @ApiProperty({ readOnly: true }) ticket_id: number; diff --git a/api/src/ticket/dto/update-ticket.dto.ts b/api/src/ticket/dto/update-ticket.dto.ts index 3f2cbad..0da1bdd 100644 --- a/api/src/ticket/dto/update-ticket.dto.ts +++ b/api/src/ticket/dto/update-ticket.dto.ts @@ -1,4 +1,10 @@ import { PartialType } from '@nestjs/swagger'; -import { Ticket } from './ticket.dto'; +import { CreateTicketDTO } from './create-ticket.dto'; +import { TicketDTO } from './ticket.dto'; +import { OmitType as Omit } from '@nestjs/swagger'; -export class UpdateTicketDto extends PartialType(Ticket) {} +export class UpdateTicketDto extends Omit(TicketDTO, [ + 'ticket_id', + 'submission_date', + 'lsu_id', +]) {} diff --git a/api/src/ticket/ticket.controller.ts b/api/src/ticket/ticket.controller.ts index 2521d3e..a93d7f6 100644 --- a/api/src/ticket/ticket.controller.ts +++ b/api/src/ticket/ticket.controller.ts @@ -8,7 +8,7 @@ import { Put, } from '@nestjs/common'; import { ApiTags } from '@nestjs/swagger'; -import { CreateCombined } from './dto/create-combined.dto'; +import { CreateCombinedDTO } from './dto/create-combined.dto'; import { TicketType } from './dto/ticket.dto'; import { UpdateTicketDto } from './dto/update-ticket.dto'; import { TicketService } from './ticket.service'; @@ -18,61 +18,63 @@ import { TicketService } from './ticket.service'; export class TicketController { constructor(private readonly ticketService: TicketService) {} - /* TODO: test implementation */ + /* WORKING Implementation */ @Post() - create(@Body() createCombined: CreateCombined) { + create(@Body() createCombinedDTO: CreateCombinedDTO) { Logger.log( { req: { http: 'POST /api/ticket', params: 'none', - body: createCombined, + body: createCombinedDTO, }, }, 'TicketController.create', false, ); - return this.ticketService.create(createCombined); + return this.ticketService.create(createCombinedDTO); } - /* Working implementation */ + /* WORKING Implementation */ @Get() findAll() { return this.ticketService.findAll(TicketType.ANY); } - /* Working implementation */ + /* WORKING Implementation */ @Get('/opened') findAllOpened() { return this.ticketService.findAll(TicketType.OPENED); } - /* Working implementation */ + /* WORKING Implementation */ @Get('/closed') findAllClosed() { return this.ticketService.findAll(TicketType.CLOSED); } - /* Working implementation */ + /* WORKING Implementation */ @Get('/user/:lsu_id') findAllByLsuId(@Param('lsu_id') lsu_id: number) { return this.ticketService.findAll(lsu_id); } - /* Working implementation */ + /* WORKING Implementation */ @Get(':ticket_id') findOne(@Param('ticket_id') ticket_id: number) { return this.ticketService.findOne(+ticket_id); } - /* TODO: FIX */ - /* NOT WORKING */ - @Put(':id') - update(@Param('id') id: number, @Body() updateTicketDto: UpdateTicketDto) { + /* WORKING Implementation */ + @Put(':ticket_id') + update( + @Param('ticket_id') ticket_id: number, + @Body() updateTicketDto: UpdateTicketDto, + ) { Logger.log( { req: { - http: `PUT /api/ticket/${id}`, - params: id, + http: `PUT /api/ticket/${ticket_id}`, + params: ticket_id, body: updateTicketDto, }, }, @@ -80,6 +82,6 @@ export class TicketController { false, ); - return this.ticketService.update(id, updateTicketDto); + return this.ticketService.update(ticket_id, updateTicketDto); } } diff --git a/api/src/ticket/ticket.service.ts b/api/src/ticket/ticket.service.ts index cc3c40d..b63aed3 100644 --- a/api/src/ticket/ticket.service.ts +++ b/api/src/ticket/ticket.service.ts @@ -7,14 +7,14 @@ import { } from '@nestjs/common'; import { Pool, QueryConfig } from 'pg'; import { PG_CONNECTION } from 'src/connection'; -import { CreateUser } from 'src/user/dto/create-user.dto'; -import { User } from 'src/user/dto/user.dto'; -import { Combined } from './dto/combined.dto'; -import { CreateCombined } from './dto/create-combined.dto'; -import { CreateDevice } from './dto/create-device.dto'; -import { CreateTicket } from './dto/create-ticket.dto'; -import { Device } from './dto/device.dto'; -import { Ticket, TicketType } from './dto/ticket.dto'; +import { CreateUserDTO } from 'src/user/dto/create-user.dto'; +import { UserDTO } from 'src/user/dto/user.dto'; +import { CombinedDTO } from './dto/combined.dto'; +import { CreateCombinedDTO } from './dto/create-combined.dto'; +import { CreateDeviceDTO } from './dto/create-device.dto'; +import { CreateTicketDTO } from './dto/create-ticket.dto'; +import { DeviceDTO } from './dto/device.dto'; +import { TicketDTO, TicketType } from './dto/ticket.dto'; import { UpdateTicketDto } from './dto/update-ticket.dto'; @Injectable() @@ -24,10 +24,10 @@ export class TicketService { /** * Queries and inserts user if user does not exists * - * @param createUser + * @param createUserDTO * @returns User */ - private async createUser(createUser: CreateUser) { + private async createUser(createUserDTO: CreateUserDTO) { const { lsu_id, email, @@ -36,29 +36,33 @@ export class TicketService { phone_number, department, admin, - } = createUser; + } = createUserDTO; + /* Check to see if user exists */ const findQuery: QueryConfig = { name: 'select_user_by_id_or_email', text: 'SELECT * FROM "user" WHERE lsu_id = $1 OR email = $2', values: [lsu_id, email], }; - /* Query User by lsuid or email */ try { - const res = await this.connection.query(findQuery); - /* Test to see if student exists */ + const res = await this.connection.query(findQuery); + + /* If user exists abort insert operation by returning early */ if (res.rows.length > 0) { return res.rows[0]; } } catch (error) { throw new HttpException( - { query: findQuery, error: error }, + { + query: findQuery, + error: error, + }, HttpStatus.INTERNAL_SERVER_ERROR, ); } - /* Insert new student into db */ + /* Insert new user into db */ const insertQuery: QueryConfig = { name: 'insert_user', @@ -75,11 +79,16 @@ export class TicketService { ], }; try { - const res = await this.connection.query(insertQuery); + const res = await this.connection.query(insertQuery); + + /* Return newly inserted user */ return res.rows[0]; } catch (error) { throw new HttpException( - { query: insertQuery, error: error }, + { + query: insertQuery, + error: error, + }, HttpStatus.INTERNAL_SERVER_ERROR, ); } @@ -88,21 +97,25 @@ export class TicketService { /** * Inserts ticket into ticket table * - * @param createTicket + * @param createTicketDTO * @returns Ticket */ - private async createTicket(createTicket: CreateTicket) { + private async createTicket(createTicketDTO: CreateTicketDTO) { const { lsu_id, core_issue, description, problem_category, priority, - } = createTicket; + } = createTicketDTO; + /* Status set to open for new ticket */ const status = 'OPEN'; + + /* Submission date set to current server time */ const submission_date = this.createDate(); + /* Insert new ticket into db */ const query: QueryConfig = { name: 'insert_ticket', text: @@ -119,12 +132,14 @@ export class TicketService { }; try { - const res = await this.connection.query(query); + const res = await this.connection.query(query); + + /* Return newly inserted ticket */ return res.rows[0]; } catch (error) { throw new HttpException( { - message: query, + query: query, error: error, }, HttpStatus.INTERNAL_SERVER_ERROR, @@ -134,17 +149,19 @@ export class TicketService { /** * Inserts new device into device table - * @param createDevice + * @param createDeviceDTO * @returns Device */ - private async createDevice(createDevice: CreateDevice) { + private async createDevice(createDeviceDTO: CreateDeviceDTO) { const { ticket_id, manufacturer, model, operating_system, operating_system_version, - } = createDevice; + } = createDeviceDTO; + + /* Insert Device into db */ const query: QueryConfig = { name: 'insert_device', text: @@ -158,12 +175,14 @@ export class TicketService { ], }; try { - const res = await this.connection.query(query); + const res = await this.connection.query(query); + + /* Return newly inserted device */ return res.rows[0]; } catch (error) { throw new HttpException( { - message: query, + query: query, error: error, }, HttpStatus.INTERNAL_SERVER_ERROR, @@ -193,19 +212,22 @@ export class TicketService { [d.getHours(), d.getMinutes(), d.getSeconds()].join(':'); return date; } - /* TODO: test implementation */ - async create(createCombined: CreateCombined) { + + /* WORKING Implementation */ + async create(createCombinedDTO: CreateCombinedDTO) { /* Response Accumulator */ - let response: Combined; + let response: CombinedDTO; - let device: Device; - let ticket: Ticket; - let user: User; + let device: DeviceDTO; + let ticket: TicketDTO; + let user: UserDTO; /* Insert or Retrieve User */ - const createUser: CreateUser = { ...createCombined, admin: false }; // tickets are created by students so admin is false + const createUser: CreateUserDTO = { ...createCombinedDTO, admin: false }; // tickets are created by students so admin is false try { user = await this.createUser(createUser); + + /* Accumulate user to response */ response = { ...response, ...user }; } catch (error) { Logger.error(error); @@ -213,9 +235,11 @@ export class TicketService { } /* Create new Ticket */ - const createTicket: CreateTicket = { ...createCombined }; + const createTicket: CreateTicketDTO = { ...createCombinedDTO }; try { ticket = await this.createTicket(createTicket); + + /* Accumulate ticket to response */ response = { ...response, ...ticket }; } catch (error) { Logger.error(error); @@ -223,18 +247,21 @@ export class TicketService { } /* Create new Device */ - const createDevice: CreateDevice = { - ...createCombined, + const createDevice: CreateDeviceDTO = { + ...createCombinedDTO, ticket_id: ticket.ticket_id, }; try { device = await this.createDevice(createDevice); + + /* Accumulate device to response */ response = { ...response, ...device }; } catch (error) { Logger.error(error); return error; } + /* Return flat object with the intersection of properties from user, ticket, and deivce */ return response; } @@ -274,9 +301,9 @@ export class TicketService { break; } try { - const queryRes = await this.connection.query(query); + const queryRes = await this.connection.query(query); - /* If no tickets */ + /* If no tickets return empty array */ if (queryRes.rows.length === 0) { return []; } @@ -285,7 +312,7 @@ export class TicketService { } catch (error) { throw new HttpException( { - message: query, + query: query, error: error, }, HttpStatus.INTERNAL_SERVER_ERROR, @@ -295,24 +322,25 @@ export class TicketService { /* WORKING implementation */ async findOne(ticket_id: number) { + /* Query ticket by ticket_id */ const query: QueryConfig = { name: 'select_ticket_by_ticket_id', text: 'SELECT * FROM ticket WHERE ticket_id = $1', values: [ticket_id], }; try { - const queryRes = await this.connection.query(query); + const res = await this.connection.query(query); - /* If no ticket found */ - if (queryRes.rows.length === 0) { + /* If no ticket found return empty object*/ + if (res.rows.length === 0) { return {}; } - return queryRes.rows[0]; + return res.rows[0]; } catch (error) { throw new HttpException( { - message: query, + query: query, error: error, }, HttpStatus.INTERNAL_SERVER_ERROR, @@ -320,35 +348,71 @@ export class TicketService { } } - /* NOT WORKING implementation */ - async update(id: number, updateTicketDto: UpdateTicketDto) { - id = Number(id); - let keys = []; - let values = []; - - /* unknown properties to be updated */ - for (let key in updateTicketDto) { - if (!!updateTicketDto[key]) { - keys = [...keys, key]; - values = [...values, updateTicketDto[key]]; + /* WORKING Implementation */ + async update(ticket_id: number, updateTicketDto: UpdateTicketDto) { + const { + core_issue, + description, + problem_category, + status, + priority, + } = updateTicketDto; + + /* Check to see if ticket exist */ + const findQuery: 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(findQuery); + + /* If no ticket found throw custom error*/ + if (res.rows.length < 1) { + throw new Error('BAD_REQUEST'); } + } catch (error) { + /* catch custom error for ticket not found */ + if (error.message === 'BAD_REQUEST') { + /* Throw HttpException for ticket not found */ + throw new HttpException( + { + message: `Ticket with ticket_id: ${ticket_id} DOES NOT EXIST`, + }, + HttpStatus.BAD_REQUEST, + ); + } + /* Throw HttpException for postgres errors */ + throw new HttpException( + { + query: findQuery, + error: error, + }, + HttpStatus.INTERNAL_SERVER_ERROR, + ); } - /* keys variable defined by interface NOT user so avoids sql injection */ - const columns = keys.map((val, idx) => `${val} = $${idx + 1}`).join(', '); - let txt = String.raw`UPDATE ticket SET ${columns} WHERE ticket_id = $${keys.length}`; - const query = { + /* Update ticket */ + const updateQuery = { name: 'update_ticket', - text: txt, - values: [...values, id], + text: + 'UPDATE ticket SET core_issue = $1, description = $2, problem_category = $3, status = $4, priority = $5 WHERE ticket_id = $6 RETURNING *', + values: [ + core_issue, + description, + problem_category, + status, + priority, + ticket_id, + ], }; try { - const queryRes = await this.connection.query(query, [...values, id]); - return queryRes.rows; + const res = await this.connection.query(updateQuery); + return res.rows[0]; } catch (error) { throw new HttpException( { - message: query, + query: updateQuery, error: error, }, HttpStatus.INTERNAL_SERVER_ERROR, diff --git a/api/src/user/dto/create-user.dto.ts b/api/src/user/dto/create-user.dto.ts index 9190090..3ed9592 100644 --- a/api/src/user/dto/create-user.dto.ts +++ b/api/src/user/dto/create-user.dto.ts @@ -1,3 +1,3 @@ -import { User } from './user.dto'; +import { UserDTO } from './user.dto'; -export class CreateUser extends User {} +export class CreateUserDTO extends UserDTO {} diff --git a/api/src/user/dto/update-user.dto.ts b/api/src/user/dto/update-user.dto.ts new file mode 100644 index 0000000..f4c017e --- /dev/null +++ b/api/src/user/dto/update-user.dto.ts @@ -0,0 +1,3 @@ +import { UserDTO } from './user.dto'; + +export class UpdateUserDTO extends UserDTO {} diff --git a/api/src/user/dto/user.dto.ts b/api/src/user/dto/user.dto.ts index 6701000..8d43540 100644 --- a/api/src/user/dto/user.dto.ts +++ b/api/src/user/dto/user.dto.ts @@ -4,7 +4,7 @@ export enum UserType { 'STUDENT', 'USER', } -export class User { +export class UserDTO { @ApiProperty({ minLength: 9, maxLength: 9, diff --git a/api/src/user/user.controller.ts b/api/src/user/user.controller.ts index 1df3f96..0a5bfda 100644 --- a/api/src/user/user.controller.ts +++ b/api/src/user/user.controller.ts @@ -8,9 +8,10 @@ import { Delete, } from '@nestjs/common'; import { UserService } from './user.service'; -import { CreateUser } from './dto/create-user.dto'; +import { CreateUserDTO } from './dto/create-user.dto'; import { UserType } from './dto/user.dto'; import { ApiTags } from '@nestjs/swagger'; +import { UpdateUserDTO } from './dto/update-user.dto'; @ApiTags('user') @Controller('/api/user') @@ -18,7 +19,7 @@ export class UserController { constructor(private readonly userService: UserService) {} /* WORKING implementation */ @Post() - async create(@Body() createUserDto: CreateUser) { + async create(@Body() createUserDto: CreateUserDTO) { return await this.userService.create(createUserDto); } /* WORKING implementation */ @@ -44,4 +45,13 @@ export class UserController { findOne(@Param('lsu_id') lsu_id: number) { return this.userService.findOne(lsu_id); } + + /* WORKING implementation */ + @Put(':lsu_id') + update( + @Param('lsu_id') lsu_id: number, + @Body() updateUserDTO: UpdateUserDTO, + ) { + return this.userService.update(lsu_id, updateUserDTO); + } } diff --git a/api/src/user/user.service.ts b/api/src/user/user.service.ts index 4ad1efc..a7d7ad5 100644 --- a/api/src/user/user.service.ts +++ b/api/src/user/user.service.ts @@ -7,15 +7,16 @@ import { } from '@nestjs/common'; import { Pool, QueryConfig } from 'pg'; import { PG_CONNECTION } from 'src/connection'; -import { CreateUser } from './dto/create-user.dto'; -import { User, UserType } from './dto/user.dto'; +import { CreateUserDTO } from './dto/create-user.dto'; +import { UpdateUserDTO } from './dto/update-user.dto'; +import { UserDTO, UserType } from './dto/user.dto'; @Injectable() export class UserService { constructor(@Inject(PG_CONNECTION) private connection: Pool) {} /* WORKING implementation */ - async create(createUser: CreateUser) { + async create(createUserDTO: CreateUserDTO) { const { lsu_id, email, @@ -24,7 +25,7 @@ export class UserService { phone_number, department, admin, - } = createUser; + } = createUserDTO; const findQuery: QueryConfig = { name: 'select_user_by_id_or_email', @@ -79,7 +80,7 @@ export class UserService { ], }; try { - const res = await this.connection.query(insertQuery); + const res = await this.connection.query(insertQuery); return res.rows[0]; } catch (error) { throw new HttpException( @@ -113,7 +114,7 @@ export class UserService { } try { - const queryRes = await this.connection.query(query); + const queryRes = await this.connection.query(query); /* If no users found return empty array */ if (queryRes.rows.length < 1) { return []; @@ -136,7 +137,7 @@ export class UserService { text: 'SELECT * FROM "user" WHERE "lsu_id" = $1', }; try { - const queryRes = await this.connection.query(query, [lsu_id]); + const queryRes = await this.connection.query(query, [lsu_id]); /* If customer not found return empty object */ if (queryRes.rows.length < 1) { return {}; @@ -153,4 +154,83 @@ export class UserService { ); } } + + /* WORKING implementation */ + async update(old_lsu_id: number, updateUserDTO: UpdateUserDTO) { + const { + lsu_id, + email, + first_name, + last_name, + phone_number, + department, + admin, + } = updateUserDTO; + + /* Find user by old_lsu_id supplied in route i.e. /api/user/{lsu_id} */ + const findQuery: QueryConfig = { + name: 'select_user_by_id', + text: 'SELECT lsu_id FROM "user" WHERE lsu_id = $1', + values: [old_lsu_id], + }; + try { + const res = await this.connection.query(findQuery); + + /* Test to see if user exists */ + if (res.rows.length < 1) { + /*Throw custom error to be handled in catch*/ + throw new Error('BAD_REQUEST'); + } + } catch (error) { + /* Catch custom error if user does not exist + Then throw Bad Request HttpException + */ + if (error.message === 'BAD_REQUEST') { + throw new HttpException( + { + error: `User with lsu_id ${lsu_id} DOES NOT EXIST`, + }, + HttpStatus.BAD_REQUEST, + ); + } + /* Catch postgres error and throw Internal Error HttpException */ + throw new HttpException( + { + message: findQuery, + error: error, + }, + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + + /* Update user in db */ + const updateQuery: QueryConfig = { + name: 'update_user', + + text: + 'UPDATE "user" SET lsu_id = $1, email = $2, first_name = $3, last_name = $4, phone_number = $5, department = $6, admin = $7 WHERE lsu_id = $8 RETURNING *', + values: [ + lsu_id, + email, + first_name, + last_name, + phone_number, + department, + admin, + old_lsu_id, + ], + }; + try { + const res = await this.connection.query(updateQuery); + return res.rows[0]; + } catch (error) { + throw new HttpException( + { + message: updateQuery, + error: error, + }, + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + } }