@@ -9,15 +9,24 @@ import {
99 DatasetNotNumberedVersion ,
1010 getDataset
1111} from '../../../src/datasets'
12- import { deleteUnpublishedDatasetViaApi } from '../../testHelpers/datasets/datasetHelper'
12+ import {
13+ deletePublishedDatasetViaApi ,
14+ deleteUnpublishedDatasetViaApi ,
15+ publishDatasetViaApi ,
16+ waitForNoLocks
17+ } from '../../testHelpers/datasets/datasetHelper'
1318import {
1419 createCollectionViaApi ,
1520 deleteCollectionViaApi
1621} from '../../testHelpers/collections/collectionHelper'
1722import { CollectionPayload } from '../../../src/collections/infra/repositories/transformers/CollectionPayload'
23+ import { AccessRepository } from '../../../src/access/infra/repositories/AccessRepository'
24+ import { GuestbookResponseDTO } from '../../../src/access/domain/dtos/GuestbookResponseDTO'
25+ import { testTextFile1Name , uploadFileViaApi } from '../../testHelpers/files/filesHelper'
1826
1927describe ( 'GuestbooksRepository' , ( ) => {
2028 const sut = new GuestbooksRepository ( )
29+ const accessRepository = new AccessRepository ( )
2130 const testCollectionAlias = 'testGuestbooksRepository'
2231 let testCollectionId : number
2332 let createdGuestbookId : number
@@ -118,11 +127,100 @@ describe('GuestbooksRepository', () => {
118127 expect ( actual . some ( ( guestbook ) => guestbook . id === createdByAliasGuestbookId ) ) . toBe ( true )
119128 } )
120129
130+ test ( 'should list guestbooks for collection with stats' , async ( ) => {
131+ const createdGuestbookIdWithStats = await sut . createGuestbook (
132+ testCollectionAlias ,
133+ createGuestbookDTO
134+ )
135+ const actual = await sut . getGuestbooksByCollectionId ( testCollectionAlias , true )
136+ const createdGuestbookWithStats = actual . find (
137+ ( guestbook ) => guestbook . id === createdGuestbookIdWithStats
138+ )
139+
140+ expect ( createdGuestbookWithStats ) . toBeDefined ( )
141+ expect ( createdGuestbookWithStats ?. usageCount ) . toEqual ( expect . any ( Number ) )
142+ expect ( createdGuestbookWithStats ?. responseCount ) . toEqual ( expect . any ( Number ) )
143+ } )
144+
145+ test ( 'should increment usageCount when assigned to a dataset and responseCount when a response is submitted' , async ( ) => {
146+ let statsDatasetIds : CreatedDatasetIdentifiers | undefined
147+ let statsDatasetPublished = false
148+ const guestbookResponse : GuestbookResponseDTO = {
149+ guestbookResponse : {
150+ name : 'Guestbook Stats Test' ,
151+ email : 'guestbook-stats@example.edu'
152+ }
153+ }
154+ const statsGuestbookId = await sut . createGuestbook ( testCollectionAlias , {
155+ ...createGuestbookDTO ,
156+ name : 'guestbook stats test' ,
157+ customQuestions : [ ]
158+ } )
159+
160+ try {
161+ const initialStats = await getGuestbookStats ( statsGuestbookId )
162+ statsDatasetIds = await createDataset . execute (
163+ TestConstants . TEST_NEW_DATASET_DTO ,
164+ testCollectionAlias
165+ )
166+ await uploadFileViaApi ( statsDatasetIds . numericId , testTextFile1Name )
167+
168+ await sut . assignDatasetGuestbook ( statsDatasetIds . numericId , statsGuestbookId )
169+
170+ const statsAfterAssignment = await getGuestbookStats ( statsGuestbookId )
171+ expect ( statsAfterAssignment . usageCount ) . toBe ( ( initialStats . usageCount ?? 0 ) + 1 )
172+ expect ( statsAfterAssignment . responseCount ) . toBe ( initialStats . responseCount ?? 0 )
173+
174+ await publishDatasetViaApi ( statsDatasetIds . numericId )
175+ statsDatasetPublished = true
176+ await waitForNoLocks ( statsDatasetIds . numericId , 10 )
177+
178+ ApiConfig . init ( TestConstants . TEST_API_URL , DataverseApiAuthMechanism . API_KEY , undefined )
179+ await accessRepository . submitGuestbookForDatasetDownload (
180+ statsDatasetIds . numericId ,
181+ guestbookResponse
182+ )
183+
184+ ApiConfig . init (
185+ TestConstants . TEST_API_URL ,
186+ DataverseApiAuthMechanism . API_KEY ,
187+ process . env . TEST_API_KEY
188+ )
189+ const statsAfterResponse = await getGuestbookStats ( statsGuestbookId )
190+ expect ( statsAfterResponse . usageCount ) . toBe ( statsAfterAssignment . usageCount )
191+ expect ( statsAfterResponse . responseCount ) . toBe ( ( statsAfterAssignment . responseCount ?? 0 ) + 1 )
192+ } finally {
193+ ApiConfig . init (
194+ TestConstants . TEST_API_URL ,
195+ DataverseApiAuthMechanism . API_KEY ,
196+ process . env . TEST_API_KEY
197+ )
198+ if ( statsDatasetIds !== undefined ) {
199+ if ( statsDatasetPublished ) {
200+ await deletePublishedDatasetViaApi ( statsDatasetIds . persistentId )
201+ } else {
202+ await deleteUnpublishedDatasetViaApi ( statsDatasetIds . numericId )
203+ }
204+ }
205+ }
206+ } )
207+
121208 test ( 'should return error when collection does not exist' , async ( ) => {
122209 await expect ( sut . getGuestbooksByCollectionId ( 999999 ) ) . rejects . toThrow ( ReadError )
123210 } )
124211 } )
125212
213+ const getGuestbookStats = async ( guestbookId : number ) => {
214+ const guestbooks = await sut . getGuestbooksByCollectionId ( testCollectionAlias , true )
215+ const guestbook = guestbooks . find ( ( guestbook ) => guestbook . id === guestbookId )
216+
217+ if ( guestbook === undefined ) {
218+ throw new Error ( `Guestbook ${ guestbookId } was not found in collection stats.` )
219+ }
220+
221+ return guestbook
222+ }
223+
126224 describe ( 'getGuestbook' , ( ) => {
127225 test ( 'should get guestbook by id' , async ( ) => {
128226 createdGuestbookId = await sut . createGuestbook ( testCollectionId , createGuestbookDTO )
0 commit comments