Skip to content

Commit

Permalink
Feat: ORV2-1892 Rename Company Endpoints (#1110)
Browse files Browse the repository at this point in the history
Co-authored-by: praju-aot <[email protected]>
  • Loading branch information
erikataot and praju-aot authored Jan 26, 2024
1 parent f7dcfcd commit bf9e589
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 35 deletions.
2 changes: 1 addition & 1 deletion dops/src/modules/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class AuthService {
@LogAsyncMethodExecution()
async getCompaniesForUser(accessToken: string): Promise<AxiosResponse> {
return lastValueFrom(
this.httpService.get(process.env.ACCESS_API_URL + '/companies', {
this.httpService.get(process.env.ACCESS_API_URL + '/companies/meta-data', {
headers: {
Authorization: accessToken,
'Content-Type': 'application/json',
Expand Down
4 changes: 2 additions & 2 deletions vehicles/src/common/dto/paginate/page-meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export class PageMetaDto {
readonly hasNextPage: boolean;

constructor({ pageOptionsDto, totalItems }: PageMetaDtoParameters) {
this.page = pageOptionsDto.page;
this.take = pageOptionsDto.take;
this.page = pageOptionsDto?.page || 1;
this.take = pageOptionsDto?.take || 10;
this.totalItems = totalItems;
this.pageCount = Math.ceil(this.totalItems / this.take);
this.hasPreviousPage = this.page > 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,32 +116,7 @@ export class CompanyController {
}

/**
* A GET method defined with the @Get(':companyId') decorator and a route of
* /company/:companyId that retrieves a company by its id.
*
* @param companyId The company Id.
*
* @returns The company details with response object {@link ReadCompanyDto}.
*/
@ApiOkResponse({
description: 'The Company Resource',
type: ReadCompanyDto,
})
@Roles(Role.READ_ORG)
@Get(':companyId')
async get(
@Req() request: Request,
@Param('companyId') companyId: number,
): Promise<ReadCompanyDto> {
const company = await this.companyService.findOne(companyId);
if (!company) {
throw new DataNotFoundException();
}
return company;
}

/**
* A GET method defined with the @Get() decorator and a route of /companies
* A GET method defined with the @Get() decorator and a route of /meta-data
* that retrieves a company metadata by userGuid. If userGUID is not provided,
* the guid will be grabbed from the token.
*
Expand All @@ -156,7 +131,7 @@ export class CompanyController {
})
@ApiQuery({ name: 'userGUID', required: false })
@Roles(Role.READ_ORG)
@Get()
@Get('meta-data')
async getCompanyMetadata(
@Req() request: Request,
@Query('userGUID') userGUID?: string,
Expand All @@ -178,6 +153,31 @@ export class CompanyController {
return company;
}

/**
* A GET method defined with the @Get(':companyId') decorator and a route of
* /company/:companyId that retrieves a company by its id.
*
* @param companyId The company Id.
*
* @returns The company details with response object {@link ReadCompanyDto}.
*/
@ApiOkResponse({
description: 'The Company Resource',
type: ReadCompanyDto,
})
@Roles(Role.READ_ORG)
@Get(':companyId')
async get(
@Req() request: Request,
@Param('companyId') companyId: number,
): Promise<ReadCompanyDto> {
const company = await this.companyService.findOne(companyId);
if (!company) {
throw new DataNotFoundException();
}
return company;
}

/**
* A PUT method defined with the @Put(':companyId') decorator and a route of
* /company/:companyId that updates a company by its ID.
Expand Down
16 changes: 11 additions & 5 deletions vehicles/test/e2e/company.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as request from 'supertest';
import { Test } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';

import { getRepositoryToken } from '@nestjs/typeorm';
import { classes } from '@automapper/classes';
Expand Down Expand Up @@ -38,6 +37,7 @@ import { EmailService } from '../../src/modules/email/email.service';
import { HttpService } from '@nestjs/axios';
import { EmailModule } from '../../src/modules/email/email.module';
import { App } from 'supertest/types';
import { INestApplication } from '@nestjs/common';

let repo: DeepMocked<Repository<Company>>;
let emailService: DeepMocked<EmailService>;
Expand Down Expand Up @@ -114,19 +114,22 @@ describe('Company (e2e)', () => {
findCompanywithParams(PARAMS);

const response = await request(app.getHttpServer() as unknown as App)
.get('/companies')
.get('/companies/meta-data')
.expect(200);

expect(response.body).toContainEqual(readRedCompanyMetadataDtoMock);
});
it('should throw a forbidden exception when user is not staff and userGUID is passed as Query Param', async () => {
it('should throw a forbidden exception when user is not READ_ORG and userGUID is passed as Query Param', async () => {
const PARAMS = { userGUID: constants.RED_COMPANY_ADMIN_USER_GUID };
findCompanywithParams(PARAMS);

TestUserMiddleware.testUser = redCompanyAdminUserJWTMock;

await request(app.getHttpServer() as unknown as App)
.get('/companies?userGUID=' + constants.RED_COMPANY_ADMIN_USER_GUID)
.get(
'/companies/meta-data?userGUID=' +
constants.RED_COMPANY_ADMIN_USER_GUID,
)
.expect(403);
});
it('should return an array of company metadata associated with the userGUID Query Param when logged in as Staff', async () => {
Expand All @@ -136,7 +139,10 @@ describe('Company (e2e)', () => {
TestUserMiddleware.testUser = sysAdminStaffUserJWTMock;

const response = await request(app.getHttpServer() as unknown as App)
.get('/companies?userGUID=' + constants.RED_COMPANY_ADMIN_USER_GUID)
.get(
'/companies/meta-data?userGUID=' +
constants.RED_COMPANY_ADMIN_USER_GUID,
)
.expect(200);

expect(response.body).toContainEqual(readRedCompanyMetadataDtoMock);
Expand Down

0 comments on commit bf9e589

Please sign in to comment.