adding pagination to logbook table (#112)
This commit was merged in pull request #112.
This commit is contained in:
@@ -13,7 +13,7 @@ describe('LogController', () => {
|
||||
create: jest.fn(),
|
||||
delete: jest.fn(),
|
||||
find: jest.fn(),
|
||||
findAll: jest.fn(),
|
||||
findLogsWithCount: jest.fn(),
|
||||
update: jest.fn()
|
||||
}
|
||||
|
||||
@@ -69,32 +69,32 @@ describe('LogController', () => {
|
||||
}
|
||||
})
|
||||
|
||||
it('findAll => should find all logs', async () => {
|
||||
it('findLogsWithCount => should find all logs', async () => {
|
||||
const id: string = '95834f84-0a02-44d3-884e-a20237adeca0';
|
||||
const log = {
|
||||
|
||||
} as LogEntity;
|
||||
const logs = [log]
|
||||
|
||||
jest.spyOn(mockLogService, 'findAll').mockReturnValue(logs);
|
||||
jest.spyOn(mockLogService, 'findLogsWithCount').mockReturnValue(logs);
|
||||
|
||||
const result = await controller.findAll();
|
||||
const result = await controller.findLogsWithCount();
|
||||
|
||||
expect(result).toEqual(logs);
|
||||
expect(mockLogService.findAll).toHaveBeenCalled();
|
||||
expect(mockLogService.findLogsWithCount).toHaveBeenCalled();
|
||||
})
|
||||
|
||||
it('findAll => should fail to find all logs', async () => {
|
||||
jest.spyOn(mockLogService, 'findAll').mockRejectedValue(new Error('Logs not found'))
|
||||
it('findLogsWithCount => should fail to find logs with count', async () => {
|
||||
jest.spyOn(mockLogService, 'findLogsWithCount').mockRejectedValue(new Error('Logs not found'))
|
||||
|
||||
try {
|
||||
await controller.findAll();
|
||||
await controller.findLogsWithCount();
|
||||
|
||||
fail('findAll did not throw error');
|
||||
fail('findLogsWithCount did not throw error');
|
||||
} catch (error) {
|
||||
expect(error).toBeInstanceOf(Error);
|
||||
expect(mockLogService.findAll).toHaveBeenCalled();
|
||||
expect(mockLogService.findAll).rejects.toThrow('Logs not found')
|
||||
expect(mockLogService.findLogsWithCount).toHaveBeenCalled();
|
||||
expect(mockLogService.findLogsWithCount).rejects.toThrow('Logs not found')
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ import {
|
||||
Param,
|
||||
Post,
|
||||
Put,
|
||||
Query,
|
||||
UseGuards,
|
||||
UseInterceptors
|
||||
} from '@nestjs/common';
|
||||
@@ -47,13 +48,13 @@ export class LogController {
|
||||
|
||||
@Get()
|
||||
@Public()
|
||||
async findAll(): Promise<LogEntity[]> {
|
||||
async findLogsWithCount(@Query('skip') skip?, @Query('take') take?: number,): Promise<{ entities: LogEntity[], total: number, hasNextPage: boolean }> {
|
||||
try {
|
||||
return await this.logService.findAll();
|
||||
return await this.logService.findLogsWithCount(skip, take)
|
||||
} catch (error) {
|
||||
const customError = error as CustomError;
|
||||
|
||||
throw new HttpException(customError.message, customError.statusCode);
|
||||
throw new HttpException(customError.message, customError.statusCode)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -15,10 +15,10 @@ export class LogInterceptor implements NestInterceptor {
|
||||
]);
|
||||
|
||||
return handler.handle().pipe(
|
||||
map((data: LogEntity[]) => {
|
||||
map((data: { entities: LogEntity[], total: number, hasNextPage: boolean }) => {
|
||||
const req = context.switchToHttp().getRequest();
|
||||
const limitData = (data) => {
|
||||
return data.map((log: LogEntity) => {
|
||||
const limitData = (entities: LogEntity[]) => {
|
||||
return entities.map((log: LogEntity) => {
|
||||
return {
|
||||
id: log.id,
|
||||
pilot: {
|
||||
@@ -41,17 +41,25 @@ export class LogInterceptor implements NestInterceptor {
|
||||
const rolesKeyName = Object.keys(jwtPayload).find((key) => key.includes('roles'));
|
||||
|
||||
if (jwtPayload[rolesKeyName].includes('Flying.Read')) {
|
||||
const logs = limitData(data);
|
||||
const logs = limitData(data.entities);
|
||||
|
||||
return logs;
|
||||
return {
|
||||
entities: logs,
|
||||
total: data.total,
|
||||
hasNextPage: data.hasNextPage
|
||||
};
|
||||
} else {
|
||||
return data;
|
||||
}
|
||||
} else if (!req.headers.authorization && isPublic) {
|
||||
const publicData = limitData(data)
|
||||
const publicData = limitData(data.entities)
|
||||
const logs = publicData.slice(0, 5)
|
||||
|
||||
return logs;
|
||||
return {
|
||||
entities: logs,
|
||||
total: data.total,
|
||||
hasNextPage: data.hasNextPage
|
||||
};
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
@@ -17,7 +17,7 @@ describe('LogService', () => {
|
||||
|
||||
const mockLogRepository = {
|
||||
delete: jest.fn(),
|
||||
find: jest.fn(),
|
||||
findAndCount: jest.fn(),
|
||||
findOne: jest.fn(),
|
||||
findOneBy: jest.fn(),
|
||||
save: jest.fn(),
|
||||
@@ -181,7 +181,7 @@ describe('LogService', () => {
|
||||
})
|
||||
})
|
||||
|
||||
it('findAll => should find all log entries', async () => {
|
||||
it('findLogsWithCount => should find log entries with count', async () => {
|
||||
const log = {
|
||||
id: 'd685f1ca-28e0-40b9-8713-74467db12965',
|
||||
pilotId: '7c4bae6b-9ec4-469e-8e16-fcbf0b940936',
|
||||
@@ -209,14 +209,18 @@ describe('LogService', () => {
|
||||
notes: 'This is a test',
|
||||
tracks: []
|
||||
} as LogEntity;
|
||||
const logs = [log]
|
||||
const logs = [log];
|
||||
const count = 1
|
||||
const morePages = false
|
||||
|
||||
jest.spyOn(mockLogRepository, 'findAndCount').mockReturnValue([logs, count, morePages]);
|
||||
|
||||
jest.spyOn(mockLogRepository, 'find').mockReturnValue(logs);
|
||||
const {entities, total, hasNextPage} = await service.findLogsWithCount();
|
||||
|
||||
const result = await service.findAll();
|
||||
|
||||
expect(result).toEqual(logs);
|
||||
expect(mockLogRepository.find).toHaveBeenCalled();
|
||||
expect(entities).toEqual(logs);
|
||||
expect(count).toEqual(total);
|
||||
expect(hasNextPage).toEqual(morePages);
|
||||
expect(mockLogRepository.findAndCount).toHaveBeenCalled();
|
||||
})
|
||||
|
||||
it('update => should update a log entry', async () => {
|
||||
|
||||
@@ -25,10 +25,18 @@ export class LogService {
|
||||
return logEntity;
|
||||
}
|
||||
|
||||
async findAll(): Promise<LogEntity[]> {
|
||||
return await this.logRepository.find({
|
||||
async findLogsWithCount(skip?: number, take?: number): Promise<{entities: LogEntity[], total: number, hasNextPage: boolean}> {
|
||||
const [entities, total] = await this.logRepository.findAndCount({
|
||||
take,
|
||||
skip,
|
||||
relations: ['pilot', 'tracks']
|
||||
});
|
||||
})
|
||||
|
||||
return {
|
||||
entities,
|
||||
total,
|
||||
hasNextPage: skip + take < total
|
||||
}
|
||||
}
|
||||
|
||||
async create(logDto: LogDto): Promise<LogDto> {
|
||||
|
||||
Reference in New Issue
Block a user