Skip to content

Commit

Permalink
feat: ORV2-1951 Staff User edits company user (#1211)
Browse files Browse the repository at this point in the history
  • Loading branch information
krishnan-aot authored Feb 16, 2024
1 parent a88552f commit 28afbaa
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,11 @@ export const deleteCompanyUsers = (userName: string) => {
export const getCompanyUserByUserGUID = (
userGUID: string,
): Promise<ReadUserInformationResponse> => {
return httpGETRequest(`${VEHICLES_URL}/users/${userGUID}`).then(
(response) => response.data,
);
return httpGETRequest(
`${
MANAGE_PROFILE_API.COMPANIES
}/${getCompanyIdFromSession()}/users/${userGUID}`,
).then((response) => response.data);
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { CustomActionLink } from "../../../../../common/components/links/CustomA
import { SnackBarContext } from "../../../../../App";
import { formatPhoneNumber } from "../../../../../common/components/form/subFormComponents/PhoneNumberInput";
import { requiredMessage } from "../../../../../common/helpers/validationMessages";
import { PROFILE_ROUTES } from "../../../../../routes/constants";
import { ERROR_ROUTES, PROFILE_ROUTES } from "../../../../../routes/constants";
import { BC_COLOURS } from "../../../../../themes/bcGovStyles";
import { updateUserInfo } from "../../../apiManager/manageProfileAPI";
import {
Expand Down Expand Up @@ -74,12 +74,7 @@ export const EditUserForm = memo(
const updateUserInfoMutation = useMutation({
mutationFn: updateUserInfo,
onError: () => {
setSnackBar({
message: "An unexpected error occurred.",
showSnackbar: true,
setShowSnackbar: () => true,
alertType: "error",
});
navigate(ERROR_ROUTES.UNEXPECTED);
},
onSuccess: (response) => {
if (response.status === 200) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { Role } from '../../../common/enum/roles.enum';
import { UpdateUserDto } from './dto/request/update-user.dto';
import { UpdateUserStatusDto } from './dto/request/update-user-status.dto';
import { GetCompanyUserQueryParamsDto } from './dto/request/queryParam/getCompanyUser.query-params.dto';
import { GetCompanyUserByUserGUIDPathParamsDto } from './dto/request/pathParam/getCompanyUserByUserGUID.path-params.dto';

@ApiTags('Company and User Management - Company User')
@ApiBadRequestResponse({
Expand Down Expand Up @@ -102,14 +103,40 @@ export class CompanyUsersController {
return await this.userService.create(createUserDto, companyId, currentUser);
}

/**
* A GET method defined with the @Get(':userGUID') decorator and a route of
* /companies/:companyId/users/:userGUID that retrieves a user details by its GUID.
*
* @param companyId The company Id.
* @param userGUID The GUID of the user.
*
* @returns The user details with response object {@link ReadUserDto}.
*/
@ApiOkResponse({
description: 'The User Resource',
type: ReadUserDto,
})
@Roles(Role.READ_USER)
@Get(':userGUID')
async get(
@Param() params: GetCompanyUserByUserGUIDPathParamsDto,
): Promise<ReadUserDto> {
const { companyId, userGUID } = params;
const users = await this.userService.findUsersDto(userGUID, [companyId]);
if (!users?.length) {
throw new DataNotFoundException();
}
return users[0];
}

/**
* A PUT method defined with the @Put(':userGUID') decorator and a route of
* /companies/:companyId/users/:userGUID that updates a user details by its GUID.
*
* @param companyId The company Id.
* @param userGUID The GUID of the user.
*
* @returns The updated user deails with response object {@link ReadUserDto}.
* @returns The updated user details with response object {@link ReadUserDto}.
*/
@ApiOkResponse({
description: 'The User Resource',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { ApiProperty } from '@nestjs/swagger';
import { Type } from 'class-transformer';
import { IsInt, IsString } from 'class-validator';

export class GetCompanyUserByUserGUIDPathParamsDto {
@ApiProperty({
description: `Id of the company the user is associated with.`,
example: 74,
required: true,
})
@Type(() => Number)
@IsInt()
companyId: number;

@ApiProperty({
description: `The user GUID.`,
example: '6F9619FF8B86D011B42D00C04FC964FF',
required: true,
})
@IsString()
userGUID: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ export class UsersService {
// Find user entities based on the provided filtering criteria
const userDetails = await this.findUsersEntity(userGUID, companyId);
let pendingUsersList: ReadUserDto[] = [];
if (pendingUser?.valueOf() && companyId?.length) {
if (pendingUser && companyId?.length) {
const pendingUser = await this.pendingUsersService.findPendingUsersDto(
undefined,
companyId?.at(0),
Expand Down Expand Up @@ -599,12 +599,12 @@ export class UsersService {
}

/**
* The getCompaniesForUser() method finds and returns a {@link number[]} object
* The getCompaniesForUser() method finds and returns a {@link ReadCompanyMetadataDto[]} object
* for a user with a specific userGUID.
*
* @param userGUID The user GUID.
*
* @returns The associated companies as a promise of type {@link number[]}
* @returns The associated companies as a promise of type {@link ReadCompanyMetadataDto[]}
*/
@LogAsyncMethodExecution()
async getCompaniesForUser(
Expand Down
30 changes: 30 additions & 0 deletions vehicles/test/unit/users/company-users.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,36 @@ describe('CompanyUsersController', () => {
});
});

describe('get user by user guid function', () => {
it('should return a user whose user guid matches', async () => {
userService.findUsersDto.mockResolvedValue([
readRedCompanyCvClientUserDtoMock,
]);
const retUser = await controller.get({
companyId: constants.RED_COMPANY_ID,
userGUID: constants.RED_COMPANY_CVCLIENT_USER_GUID,
});

expect(typeof retUser).toBe('object');
expect(retUser).toEqual(readRedCompanyCvClientUserDtoMock);
expect(userService.findUsersDto).toHaveBeenCalledWith(
constants.RED_COMPANY_CVCLIENT_USER_GUID,
[constants.RED_COMPANY_ID],
);
});

it('should throw DataNotFoundException when user not found', async () => {
userService.findUsersDto.mockResolvedValue([]);

await expect(async () => {
await controller.get({
companyId: constants.RED_COMPANY_ID,
userGUID: constants.RED_COMPANY_CVCLIENT_USER_GUID,
});
}).rejects.toThrow(DataNotFoundException);
});
});

describe('Users controller update function', () => {
it('should return the updated user', async () => {
const request = createMock<Request>();
Expand Down

0 comments on commit 28afbaa

Please sign in to comment.