Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backend fixes #118

Merged
merged 3 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/permissions/rules/rules.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,24 @@ export class RulesService {
since: this.lastSeq,
include_docs: true,
doc_ids: JSON.stringify([Permission.DOC_ID]),
// requests somehow time out after 1 minute
timeout: 50000,
}),
);
const before = new Date().getTime();

return getParams
.pipe(
concatMap((params) =>
this.couchdbService.get<ChangesResponse>(db, '_changes', params),
),
concatMap((params) => {
const date = new Date().getTime();
const diff = (date - before) / 1000;
console.log(diff.toFixed(2), 'restarting');
TheSlimvReal marked this conversation as resolved.
Show resolved Hide resolved
return this.couchdbService.get<ChangesResponse>(
db,
'_changes',
params,
);
}),
catchError((err) => {
console.error('LOAD RULES ERROR:', err);
throw err;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,20 @@ describe('ChangesController', () => {
]);
});

it('should return last sequence number if no more matching changes were found', async () => {
// Not allowed to read anything
getRulesSpy.mockReturnValue([]);
getSpy.mockReturnValueOnce(createChanges([schoolDoc, childDoc], 0));

const lastSeq = docToChange(childDoc).seq;

const res = await controller.changes('some-db', user, { limit: 3 });

expect(res.pending).toBe(0);
expect(res.last_seq).toBe(lastSeq);
expect(res.results).toEqual([]);
});

it('should not return docs of deleted documents that still have other properties', async () => {
const deletedWithoutProps: DatabaseDocument = {
_id: 'School:deletedWithout',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,12 @@ export class ChangesController {
// overflow changes of this request
const discarded = Math.max(res.results.length - missing, 0);
change.results.push(...res.results.slice(0, missing));
if (change.results.length > 0) {
if (discarded > 0) {
// not all requested changes are used
change.last_seq = change.results[change.results.length - 1].seq;
} else {
// all changes were used
change.last_seq = res.last_seq;
}
change.pending = res.pending + discarded;
if (
Expand Down