mirror of
				https://github.com/paperless-ngx/paperless-ngx.git
				synced 2025-10-30 03:56:23 -05:00 
			
		
		
		
	Fix: get highest ASN regardless of user (#4326)
This commit is contained in:
		| @@ -30,39 +30,9 @@ describe('NumberComponent', () => { | |||||||
|     input = component.inputField.nativeElement |     input = component.inputField.nativeElement | ||||||
|   }) |   }) | ||||||
|  |  | ||||||
|   // TODO: why doesnt this work? |  | ||||||
|   // it('should support use of input field', () => { |  | ||||||
|   //   expect(component.value).toBeUndefined() |  | ||||||
|   //   input.stepUp() |  | ||||||
|   //   console.log(input.value); |  | ||||||
|  |  | ||||||
|   //   input.dispatchEvent(new Event('change')) |  | ||||||
|   //   fixture.detectChanges() |  | ||||||
|   //   expect(component.value).toEqual('3') |  | ||||||
|   // }) |  | ||||||
|  |  | ||||||
|   it('should support +1 ASN', () => { |   it('should support +1 ASN', () => { | ||||||
|     const listAllSpy = jest.spyOn(documentService, 'listFiltered') |     const nextAsnSpy = jest.spyOn(documentService, 'getNextAsn') | ||||||
|     listAllSpy |     nextAsnSpy.mockReturnValueOnce(of(1001)).mockReturnValueOnce(of(1)) | ||||||
|       .mockReturnValueOnce( |  | ||||||
|         of({ |  | ||||||
|           count: 1, |  | ||||||
|           all: [1], |  | ||||||
|           results: [ |  | ||||||
|             { |  | ||||||
|               id: 1, |  | ||||||
|               archive_serial_number: 1000, |  | ||||||
|             }, |  | ||||||
|           ], |  | ||||||
|         }) |  | ||||||
|       ) |  | ||||||
|       .mockReturnValueOnce( |  | ||||||
|         of({ |  | ||||||
|           count: 0, |  | ||||||
|           all: [], |  | ||||||
|           results: [], |  | ||||||
|         }) |  | ||||||
|       ) |  | ||||||
|     expect(component.value).toBeUndefined() |     expect(component.value).toBeUndefined() | ||||||
|     component.nextAsn() |     component.nextAsn() | ||||||
|     expect(component.value).toEqual(1001) |     expect(component.value).toEqual(1001) | ||||||
|   | |||||||
| @@ -1,6 +1,5 @@ | |||||||
| import { Component, forwardRef, Input } from '@angular/core' | import { Component, forwardRef, Input } from '@angular/core' | ||||||
| import { NG_VALUE_ACCESSOR } from '@angular/forms' | import { NG_VALUE_ACCESSOR } from '@angular/forms' | ||||||
| import { FILTER_ASN_ISNULL } from 'src/app/data/filter-rule-type' |  | ||||||
| import { DocumentService } from 'src/app/services/rest/document.service' | import { DocumentService } from 'src/app/services/rest/document.service' | ||||||
| import { AbstractInputComponent } from '../abstract-input' | import { AbstractInputComponent } from '../abstract-input' | ||||||
|  |  | ||||||
| @@ -28,17 +27,9 @@ export class NumberComponent extends AbstractInputComponent<number> { | |||||||
|     if (this.value) { |     if (this.value) { | ||||||
|       return |       return | ||||||
|     } |     } | ||||||
|     this.documentService |     this.documentService.getNextAsn().subscribe((nextAsn) => { | ||||||
|       .listFiltered(1, 1, 'archive_serial_number', true, [ |       this.value = nextAsn | ||||||
|         { rule_type: FILTER_ASN_ISNULL, value: 'false' }, |       this.onChange(this.value) | ||||||
|       ]) |     }) | ||||||
|       .subscribe((results) => { |  | ||||||
|         if (results.count > 0) { |  | ||||||
|           this.value = results.results[0].archive_serial_number + 1 |  | ||||||
|         } else { |  | ||||||
|           this.value = 1 |  | ||||||
|         } |  | ||||||
|         this.onChange(this.value) |  | ||||||
|       }) |  | ||||||
|   } |   } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -229,6 +229,14 @@ describe(`DocumentService`, () => { | |||||||
|       `${environment.apiBaseUrl}${endpoint}/${documents[0].id}/preview/#search="${searchQuery}"` |       `${environment.apiBaseUrl}${endpoint}/${documents[0].id}/preview/#search="${searchQuery}"` | ||||||
|     ) |     ) | ||||||
|   }) |   }) | ||||||
|  |  | ||||||
|  |   it('should support get next asn', () => { | ||||||
|  |     subscription = service.getNextAsn().subscribe((asn) => asn) | ||||||
|  |     const req = httpTestingController.expectOne( | ||||||
|  |       `${environment.apiBaseUrl}${endpoint}/next_asn/` | ||||||
|  |     ) | ||||||
|  |     expect(req.request.method).toEqual('GET') | ||||||
|  |   }) | ||||||
| }) | }) | ||||||
|  |  | ||||||
| beforeEach(() => { | beforeEach(() => { | ||||||
|   | |||||||
| @@ -143,6 +143,10 @@ export class DocumentService extends AbstractPaperlessService<PaperlessDocument> | |||||||
|     return url |     return url | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  |   getNextAsn(): Observable<number> { | ||||||
|  |     return this.http.get<number>(this.getResourceUrl(null, 'next_asn')) | ||||||
|  |   } | ||||||
|  |  | ||||||
|   update(o: PaperlessDocument): Observable<PaperlessDocument> { |   update(o: PaperlessDocument): Observable<PaperlessDocument> { | ||||||
|     // we want to only set created_date |     // we want to only set created_date | ||||||
|     o.created = undefined |     o.created = undefined | ||||||
|   | |||||||
| @@ -2689,6 +2689,50 @@ class TestDocumentApi(DirectoriesMixin, DocumentConsumeDelayMixin, APITestCase): | |||||||
|         ) |         ) | ||||||
|         self.assertEqual(resp.status_code, status.HTTP_200_OK) |         self.assertEqual(resp.status_code, status.HTTP_200_OK) | ||||||
|  |  | ||||||
|  |     def test_next_asn(self): | ||||||
|  |         """ | ||||||
|  |         GIVEN: | ||||||
|  |             - Existing documents with ASNs, highest owned by user2 | ||||||
|  |         WHEN: | ||||||
|  |             - API request is made by user1 to get next ASN | ||||||
|  |         THEN: | ||||||
|  |             - ASN +1 from user2's doc is returned for user1 | ||||||
|  |         """ | ||||||
|  |         user1 = User.objects.create_user(username="test1") | ||||||
|  |         user1.user_permissions.add(*Permission.objects.all()) | ||||||
|  |         user1.save() | ||||||
|  |  | ||||||
|  |         user2 = User.objects.create_user(username="test2") | ||||||
|  |         user2.save() | ||||||
|  |  | ||||||
|  |         doc1 = Document.objects.create( | ||||||
|  |             title="test", | ||||||
|  |             mime_type="application/pdf", | ||||||
|  |             content="this is a document 1", | ||||||
|  |             checksum="1", | ||||||
|  |             archive_serial_number=998, | ||||||
|  |         ) | ||||||
|  |         doc1.owner = user1 | ||||||
|  |         doc1.save() | ||||||
|  |  | ||||||
|  |         doc2 = Document.objects.create( | ||||||
|  |             title="test2", | ||||||
|  |             mime_type="application/pdf", | ||||||
|  |             content="this is a document 2 with higher ASN", | ||||||
|  |             checksum="2", | ||||||
|  |             archive_serial_number=999, | ||||||
|  |         ) | ||||||
|  |         doc2.owner = user2 | ||||||
|  |         doc2.save() | ||||||
|  |  | ||||||
|  |         self.client.force_authenticate(user1) | ||||||
|  |  | ||||||
|  |         resp = self.client.get( | ||||||
|  |             "/api/documents/next_asn/", | ||||||
|  |         ) | ||||||
|  |         self.assertEqual(resp.status_code, status.HTTP_200_OK) | ||||||
|  |         self.assertEqual(resp.content, b"1000") | ||||||
|  |  | ||||||
|  |  | ||||||
| class TestDocumentApiV2(DirectoriesMixin, APITestCase): | class TestDocumentApiV2(DirectoriesMixin, APITestCase): | ||||||
|     def setUp(self): |     def setUp(self): | ||||||
|   | |||||||
| @@ -661,6 +661,19 @@ class UnifiedSearchViewSet(DocumentViewSet): | |||||||
|         else: |         else: | ||||||
|             return super().list(request) |             return super().list(request) | ||||||
|  |  | ||||||
|  |     @action(detail=False, methods=["GET"], name="Get Next ASN") | ||||||
|  |     def next_asn(self, request, *args, **kwargs): | ||||||
|  |         return Response( | ||||||
|  |             ( | ||||||
|  |                 Document.objects.filter(archive_serial_number__gte=0) | ||||||
|  |                 .order_by("archive_serial_number") | ||||||
|  |                 .last() | ||||||
|  |                 .archive_serial_number | ||||||
|  |                 or 0 | ||||||
|  |             ) | ||||||
|  |             + 1, | ||||||
|  |         ) | ||||||
|  |  | ||||||
|  |  | ||||||
| class LogViewSet(ViewSet): | class LogViewSet(ViewSet): | ||||||
|     permission_classes = (IsAuthenticated, PaperlessAdminPermissions) |     permission_classes = (IsAuthenticated, PaperlessAdminPermissions) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 shamoon
					shamoon