Skip to content
This repository has been archived by the owner on Mar 14, 2024. It is now read-only.

Commit

Permalink
feat(report): implement report api endpoints (#2)
Browse files Browse the repository at this point in the history
closes #7
  • Loading branch information
tomwwinter authored Feb 13, 2024
1 parent 0d2a0d3 commit 055ea69
Show file tree
Hide file tree
Showing 36 changed files with 1,552 additions and 106 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/pr-update.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ jobs:
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Build and test image
- name: Build, test and publish test image
uses: docker/build-push-action@v3
with:
context: ./
file: ./build/Dockerfile
builder: ${{ steps.buildx.outputs.name }}
push: true
tags: aamdigital/query-ms:pr-${{ github.event.number }}
cache-from: type=gha
cache-to: type=gha,mode=max
build-args: |
Expand Down
4 changes: 1 addition & 3 deletions nest-cli.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"plugins": ["@nestjs/swagger"]
}
"compilerOptions": {}
}
126 changes: 79 additions & 47 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@
"@nestjs/config": "^3.1.1",
"@nestjs/core": "^9.3.10",
"@nestjs/platform-express": "^9.3.10",
"@nestjs/swagger": "^6.3.0",
"@nestjs/schedule": "4.0.0",
"@ntegral/nestjs-sentry": "^4.0.0",
"@sentry/node": "^7.38.0",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"rxjs": "^7.8.0"
"rxjs": "^7.8.0",
"uuid": "9.0.1"
},
"devDependencies": {
"@nestjs/cli": "^9.2.0",
Expand All @@ -41,6 +42,7 @@
"@types/jest": "^29.4.0",
"@types/node": "^18.13.0",
"@types/supertest": "^2.0.12",
"@types/uuid": "9.0.8",
"@typescript-eslint/eslint-plugin": "^5.52.0",
"@typescript-eslint/parser": "^5.52.0",
"eslint": "^8.34.0",
Expand Down
21 changes: 2 additions & 19 deletions src/app.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('AppController', () => {
get: jest.fn().mockReturnValue(of({ data: undefined })),
};
const mockConfigService = {
getOrThrow: (key) => {
getOrThrow: (key: any) => {
switch (key) {
case 'DATABASE_URL':
return dbUrl;
Expand Down Expand Up @@ -168,24 +168,7 @@ describe('AppController', () => {
it('should throw error trying to query a non-sql report', (done) => {
const report: SqlReport = {
mode: 'exporting' as any,
aggregationDefinitions: undefined,
};
mockHttp.get.mockReturnValue(of({ data: report }));

controller
.queryData('ReportConfig:some-id', 'app', 'valid token')
.subscribe({
error: (err) => {
expect(err).toBeInstanceOf(BadRequestException);
done();
},
});
});

it('should throw sql query is not defined', (done) => {
const report: SqlReport = {
mode: 'sql',
aggregationDefinitions: undefined,
aggregationDefinitions: [],
};
mockHttp.get.mockReturnValue(of({ data: report }));

Expand Down
21 changes: 7 additions & 14 deletions src/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import {
} from '@nestjs/common';
import { HttpService } from '@nestjs/axios';
import { ConfigService } from '@nestjs/config';
import { ApiHeader, ApiOperation, ApiParam } from '@nestjs/swagger';
import { catchError, concat, map, mergeMap, toArray } from 'rxjs';
import { catchError, concat, map, mergeMap, Observable, toArray } from 'rxjs';
import { SqlReport } from './sql-report';
import { QueryBody } from './query-body.dto';

Expand All @@ -26,16 +25,6 @@ export class AppController {
private configService: ConfigService,
) {}

@ApiOperation({
description: `Get the results for the report with the given ID. User needs 'read' access for the requested report entity.`,
})
@ApiParam({ name: 'id', description: '(full) ID of the report entity' })
@ApiParam({ name: 'db', example: 'app', description: 'name of database' })
@ApiHeader({
name: 'Authorization',
required: false,
description: 'request needs to be authenticated',
})
@Post(':db/:id')
queryData(
@Param('id') reportId: string,
Expand Down Expand Up @@ -73,11 +62,15 @@ export class AppController {
).pipe(
// combine results of each request
toArray(),
map((res) => [].concat(...res)),
map((res) => res.flat()),
);
}

private getQueryResult(query: string, args: QueryBody, db: string) {
private getQueryResult(
query: string,
args: QueryBody | undefined,
db: string,
): Observable<any[]> {
const data: SqsRequest = { query: query };
// There needs to be the same amount of "?" in the query as elements in "args"
if (args?.from && args?.to && query.match(/\?/g)?.length === 2) {
Expand Down
Loading

0 comments on commit 055ea69

Please sign in to comment.