This repository has been archived by the owner on Mar 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
get module imports and dependencies aligned so that system starts
- Loading branch information
Showing
10 changed files
with
125 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
SENTRY_DSN= | ||
PORT= | ||
DATABASE_URL=http://127.0.0.1:5984 | ||
DATABASE_ADMIN=admin | ||
DATABASE_USER=admin | ||
DATABASE_PASSWORD=admin | ||
QUERY_URL=http://127.0.0.1:4984 | ||
SCHEMA_CONFIG_ID=_design/sqlite:config | ||
REPORT_DATABASE_URL=http://127.0.0.1:5984 | ||
REPORT_DATABASE_NAME=app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
/** | ||
* Monitor all changes to the application database and check if they affect any report's results. | ||
*/ | ||
|
||
export interface ReportChangesService {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,18 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { CouchdbReportChangesService } from './core/couchdb-report-changes.service'; | ||
import { CouchdbChangesRepositoryService } from './repository/couchdb-changes-repository.service'; | ||
import { NotificationModule } from '../notification/notification.module'; | ||
import { ReportModule } from '../report/report.module'; | ||
import { CouchDbClient } from '../couchdb/couch-db-client.service'; | ||
import { HttpModule } from '@nestjs/axios'; | ||
|
||
@Module({ | ||
providers: [CouchdbReportChangesService], | ||
imports: [NotificationModule, ReportModule, HttpModule], | ||
providers: [ | ||
CouchdbReportChangesService, | ||
CouchdbChangesRepositoryService, | ||
CouchDbClient, // TODO: pack this into a CouchDbModule together with HttpModule import etc. | ||
], | ||
exports: [CouchdbReportChangesService], | ||
}) | ||
export class ReportChangesModule {} |
33 changes: 33 additions & 0 deletions
33
src/report-changes/repository/couchdb-changes-repository.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { CouchdbChangesRepositoryService } from './couchdb-changes-repository.service'; | ||
import { HttpModule } from '@nestjs/axios'; | ||
import { ConfigService } from '@nestjs/config'; | ||
|
||
describe('CouchdbChangesRepositoryService', () => { | ||
let service: CouchdbChangesRepositoryService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
imports: [HttpModule], | ||
providers: [ | ||
CouchdbChangesRepositoryService, | ||
{ | ||
provide: ConfigService, | ||
useValue: { | ||
getOrThrow: jest.fn((key) => { | ||
return 'foo'; | ||
}), | ||
}, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
service = module.get<CouchdbChangesRepositoryService>( | ||
CouchdbChangesRepositoryService, | ||
); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
}); |
60 changes: 60 additions & 0 deletions
60
src/report-changes/repository/couchdb-changes-repository.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { | ||
ForbiddenException, | ||
Injectable, | ||
NotFoundException, | ||
UnauthorizedException, | ||
} from '@nestjs/common'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { catchError, Observable } from 'rxjs'; | ||
import { CouchDbClient } from '../../couchdb/couch-db-client.service'; | ||
import { CouchDbChangesResponse } from '../../couchdb/dtos'; | ||
|
||
@Injectable() | ||
export class CouchdbChangesRepositoryService { | ||
// TODO: centralize this config by refactoring couchdbClient and providing configured clients through DI | ||
// TODO: check if this is the correct db for our changes from app | ||
private dbUrl: string = this.configService.getOrThrow('DATABASE_URL'); | ||
private databaseName: string = 'app'; // TODO: move to config and clean up .env, clarifying different DBs there | ||
private databaseUser: string = this.configService.getOrThrow('DATABASE_USER'); | ||
private databasePassword: string = | ||
this.configService.getOrThrow('DATABASE_PASSWORD'); | ||
|
||
private authHeaderValue: string; | ||
|
||
constructor( | ||
private couchdbClient: CouchDbClient, | ||
private configService: ConfigService, | ||
) { | ||
const authHeader = Buffer.from( | ||
`${this.databaseUser}:${this.databasePassword}`, | ||
).toString('base64'); | ||
this.authHeaderValue = `Basic ${authHeader}`; | ||
} | ||
|
||
fetchChanges(): Observable<CouchDbChangesResponse> { | ||
return this.couchdbClient | ||
.changes(this.dbUrl, this.databaseName, { | ||
headers: { | ||
Authorization: this.authHeaderValue, | ||
}, | ||
}) | ||
.pipe( | ||
catchError((err, caught) => { | ||
this.handleError(err); | ||
throw caught; | ||
}), | ||
); | ||
} | ||
|
||
private handleError(err: any) { | ||
if (err.response.status === 401) { | ||
throw new UnauthorizedException(); | ||
} | ||
if (err.response.status === 403) { | ||
throw new ForbiddenException(); | ||
} | ||
if (err.response.status === 404) { | ||
throw new NotFoundException(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters