-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: automatically trigger clear_local when permissions changed
see #7
- Loading branch information
Showing
8 changed files
with
155 additions
and
60 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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { AdminController } from './admin.controller'; | ||
import { CouchdbService } from '../couchdb/couchdb.service'; | ||
import { authGuardMockProviders } from '../auth/auth-guard-mock.providers'; | ||
import { AdminService } from './admin.service'; | ||
|
||
describe('AdminController', () => { | ||
let controller: AdminController; | ||
let mockAdminService: CouchdbService; | ||
|
||
beforeEach(async () => { | ||
mockAdminService = { | ||
clearLocal: () => Promise.resolve(), | ||
} as any; | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [AdminController], | ||
providers: [ | ||
...authGuardMockProviders, | ||
{ provide: AdminService, useValue: mockAdminService }, | ||
], | ||
}).compile(); | ||
|
||
controller = module.get(AdminController); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(controller).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,30 @@ | ||
import { Controller, Param, Post, UseGuards } from '@nestjs/common'; | ||
import { CombinedAuthGuard } from '../auth/guards/combined-auth/combined-auth.guard'; | ||
import { OnlyAuthenticated } from '../auth/only-authenticated.decorator'; | ||
import { AdminService } from './admin.service'; | ||
|
||
/** | ||
* This service provides some general administrativ endpoints. | ||
*/ | ||
@OnlyAuthenticated() | ||
@UseGuards(CombinedAuthGuard) | ||
@Controller('admin') | ||
export class AdminController { | ||
constructor(private adminService: AdminService) {} | ||
|
||
/** | ||
* Deletes all local documents of the remote database. | ||
* These document hold meta-information about the replication process. | ||
* Deleting them forces clients to re-run sync and check which documents are different. | ||
* See {@link https://docs.couchdb.org/en/stable/replication/protocol.html#retrieve-replication-logs-from-source-and-target} | ||
* | ||
* @param db name of the database where the local documents should be deleted from | ||
* | ||
* This function should be called whenever the permissions change to re-trigger sync | ||
*/ | ||
@Post('/clear_local/:db') | ||
async clearLocal(@Param('db') db: string): Promise<any> { | ||
await this.adminService.clearLocal(db); | ||
return true; | ||
} | ||
} |
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,11 +1,13 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { AdminController } from './admin/admin.controller'; | ||
import { AdminController } from './admin.controller'; | ||
import { PermissionModule } from '../permissions/permission.module'; | ||
import { CouchdbModule } from '../couchdb/couchdb.module'; | ||
import { AuthModule } from '../auth/auth.module'; | ||
import { AdminService } from './admin.service'; | ||
|
||
@Module({ | ||
controllers: [AdminController], | ||
imports: [PermissionModule, CouchdbModule, AuthModule], | ||
providers: [AdminService], | ||
}) | ||
export class AdminModule {} |
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,27 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { firstValueFrom } from 'rxjs'; | ||
import { AllDocsResponse } from '../restricted-endpoints/replication/bulk-document/couchdb-dtos/all-docs.dto'; | ||
import { CouchdbService } from '../couchdb/couchdb.service'; | ||
|
||
@Injectable() | ||
export class AdminService { | ||
constructor(private couchdbService: CouchdbService) {} | ||
|
||
async clearLocal(db: string) { | ||
const localDocsResponse = await firstValueFrom( | ||
this.couchdbService.get<AllDocsResponse>(db, '_local_docs'), | ||
); | ||
|
||
// Get IDs of the replication checkpoints | ||
const ids = localDocsResponse.rows | ||
.map((doc) => doc.id) | ||
.filter( | ||
(id) => !id.includes('purge-mrview') && !id.includes('shard-sync'), | ||
); | ||
const deletePromises = ids.map((id) => | ||
firstValueFrom(this.couchdbService.delete(db, id)), | ||
); | ||
|
||
await Promise.all(deletePromises); | ||
} | ||
} |
This file was deleted.
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