Skip to content

Commit

Permalink
Add klamm service and controller for requesting rule info.
Browse files Browse the repository at this point in the history
  • Loading branch information
brysonjbest committed Dec 12, 2024
1 parent 81d187e commit 54efd88
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/api/klamm/klamm.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ describe('KlammController', () => {
useValue: {
getKlammBREFields: jest.fn(() => Promise.resolve('expected result')),
getKlammBREFieldFromName: jest.fn((fieldName) => Promise.resolve(`result for ${fieldName}`)), // Mock implementation
getKlammBRERules: jest.fn(() => Promise.resolve('rules result')),
},
},
],
Expand All @@ -35,4 +36,9 @@ describe('KlammController', () => {
expect(service.getKlammBREFieldFromName).toHaveBeenCalledWith(fieldName);
expect(service.getKlammBREFieldFromName).toHaveBeenCalledTimes(1);
});

it('should call getKlammBRERules and return expected result', async () => {
expect(await controller.getKlammBRERules()).toBe('rules result');
expect(service.getKlammBRERules).toHaveBeenCalledTimes(1);
});
});
5 changes: 5 additions & 0 deletions src/api/klamm/klamm.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ export class KlammController {
return await this.klammService.getKlammBREFields(searchText);
}

@Get('/brerules')
async getKlammBRERules() {
return await this.klammService.getKlammBRERules();
}

@Get('/brefield/:fieldName')
async getKlammBREFieldFromName(@Param('fieldName') fieldName: string): Promise<KlammField[]> {
return await this.klammService.getKlammBREFieldFromName(fieldName);
Expand Down
16 changes: 16 additions & 0 deletions src/api/klamm/klamm.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,22 @@ describe('KlammService', () => {
await expect(service.getKlammBREFieldFromName(fieldName)).rejects.toThrow('Error fetching from Klamm');
});

it('should get Klamm BRE rules correctly', async () => {
const mockData = ['rule1', 'rule2'];
jest.spyOn(service.axiosKlammInstance, 'get').mockResolvedValue({ data: mockData });

const result = await service.getKlammBRERules();

expect(service.axiosKlammInstance.get).toHaveBeenCalledWith(`${process.env.KLAMM_API_URL}/api/brerules`);
expect(result).toEqual(mockData);
});

it('should handle error in getKlammBRERules', async () => {
jest.spyOn(service.axiosKlammInstance, 'get').mockRejectedValue(new Error('Error'));

await expect(service.getKlammBRERules()).rejects.toThrow('Error fetching from Klamm');
});

it('should throw InvalidFieldRequest error if field name does not exist', async () => {
const fieldName = 'field1';
jest.spyOn(service.axiosKlammInstance, 'get').mockResolvedValue({ data: { data: [] } });
Expand Down
9 changes: 9 additions & 0 deletions src/api/klamm/klamm.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ export class KlammService {
}
}

async getKlammBRERules(): Promise<string[]> {
try {
const { data } = await this.axiosKlammInstance.get(`${process.env.KLAMM_API_URL}/api/brerules`);
return data;
} catch (err) {
throw new HttpException('Error fetching from Klamm', HttpStatus.INTERNAL_SERVER_ERROR);
}
}

async getKlammBREFieldFromName(fieldName: string): Promise<KlammField[]> {
try {
const sanitizedFieldName = encodeURIComponent(fieldName.trim());
Expand Down

0 comments on commit 54efd88

Please sign in to comment.