-
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.
- Loading branch information
1 parent
e760f1f
commit ce9fbe9
Showing
12 changed files
with
875 additions
and
108 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,10 +1,14 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { CouchdbAdminController } from './couchdb-admin/couchdb-admin.controller'; | ||
import { CouchdbAdminController } from './couchdb/couchdb-admin.controller'; | ||
import { HttpModule } from '@nestjs/axios'; | ||
import { ConfigModule } from '@nestjs/config'; | ||
import { CouchdbService } from './couchdb/couchdb.service'; | ||
import { KeycloakService } from './couchdb/keycloak.service'; | ||
import { MigrationController } from './couchdb/migration.controller'; | ||
|
||
@Module({ | ||
imports: [HttpModule, ConfigModule.forRoot({ isGlobal: true })], | ||
controllers: [CouchdbAdminController], | ||
controllers: [CouchdbAdminController, MigrationController], | ||
providers: [CouchdbService, KeycloakService], | ||
}) | ||
export class AppModule {} |
File renamed without changes.
File renamed without changes.
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { CouchdbService } from './couchdb.service'; | ||
|
||
describe('CouchdbService', () => { | ||
let service: CouchdbService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [CouchdbService], | ||
}).compile(); | ||
|
||
service = module.get<CouchdbService>(CouchdbService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
}); |
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,59 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { catchError, firstValueFrom, map } from 'rxjs'; | ||
import { HttpService } from '@nestjs/axios'; | ||
import { ConfigService } from '@nestjs/config'; | ||
|
||
@Injectable() | ||
export class CouchdbService { | ||
private domain = this.configService.get('DOMAIN'); | ||
|
||
constructor( | ||
private http: HttpService, | ||
private configService: ConfigService, | ||
) {} | ||
|
||
get(org: string, path: string, password: string) { | ||
const auth = { username: 'admin', password }; | ||
const url = `https://${org}.${this.domain}/db`; | ||
return firstValueFrom( | ||
this.http.get(`${url}/couchdb${path}`, { auth }).pipe( | ||
catchError(() => this.http.get(`${url}${path}`, { auth })), | ||
map((res) => res.data.rows ?? res.data), | ||
), | ||
); | ||
} | ||
|
||
put( | ||
org: string, | ||
path: string, | ||
data, | ||
password: string, | ||
headers?: any, | ||
): Promise<any> { | ||
const auth = { username: 'admin', password }; | ||
const url = `https://${org}.${this.domain}/db`; | ||
return firstValueFrom( | ||
this.http.put(`${url}/couchdb${path}`, data, { auth, headers }).pipe( | ||
catchError(() => this.http.put(`${url}${path}`, data, { auth })), | ||
map(({ data }) => (data?.docs ? data.docs : data)), | ||
), | ||
); | ||
} | ||
|
||
post( | ||
org: string, | ||
path: string, | ||
data, | ||
password: string, | ||
headers?: any, | ||
): Promise<any> { | ||
const auth = { username: 'admin', password }; | ||
const url = `https://${org}.${this.domain}/db`; | ||
return firstValueFrom( | ||
this.http.post(`${url}/couchdb${path}`, data, { auth, headers }).pipe( | ||
catchError(() => this.http.post(`${url}${path}`, data, { auth })), | ||
map(({ data }) => (data?.docs ? data.docs : data)), | ||
), | ||
); | ||
} | ||
} |
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,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { KeycloakService } from './keycloak.service'; | ||
|
||
describe('KeycloakService', () => { | ||
let service: KeycloakService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [KeycloakService], | ||
}).compile(); | ||
|
||
service = module.get<KeycloakService>(KeycloakService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
}); |
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,41 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { firstValueFrom, map } from 'rxjs'; | ||
import { HttpService } from '@nestjs/axios'; | ||
import { ConfigService } from '@nestjs/config'; | ||
|
||
@Injectable() | ||
export class KeycloakService { | ||
private keycloakPassword = this.configService.get('KEYCLOAK_ADMIN_PASSWORD'); | ||
private keycloakUrl = this.configService.get('KEYCLOAK_URL'); | ||
constructor( | ||
private http: HttpService, | ||
private configService: ConfigService, | ||
) {} | ||
|
||
getKeycloakToken(): Promise<string> { | ||
const body = new URLSearchParams(); | ||
body.set('username', 'admin'); | ||
body.set('password', this.keycloakPassword); | ||
body.set('grant_type', 'password'); | ||
body.set('client_id', 'admin-cli'); | ||
return firstValueFrom( | ||
this.http | ||
.post<{ access_token: string }>( | ||
`${this.keycloakUrl}/realms/master/protocol/openid-connect/token`, | ||
body.toString(), | ||
) | ||
.pipe(map((res) => res.data.access_token)), | ||
); | ||
} | ||
|
||
getUsersFromKeycloak(org: string, token: string) { | ||
return firstValueFrom( | ||
this.http | ||
.get<{ access_token: string }>( | ||
`${this.keycloakUrl}/admin/realms/${org}/users?enabled=true`, | ||
{ headers: { Authorization: `Bearer ${token}` } }, | ||
) | ||
.pipe(map((res) => res.data)), | ||
); | ||
} | ||
} |
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,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { MigrationController } from './migration.controller'; | ||
|
||
describe('MigrationController', () => { | ||
let controller: MigrationController; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [MigrationController], | ||
}).compile(); | ||
|
||
controller = module.get<MigrationController>(MigrationController); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(controller).toBeDefined(); | ||
}); | ||
}); |
Oops, something went wrong.