@@ -15,13 +15,34 @@ import { UserInsert, UsersService } from "./UsersEntityService.js";
1515export type CollectionSelect = Selectable < DataDatabase [ "collections" ] > ;
1616export type CollectionInsert = Insertable < DataDatabase [ "collections" ] > ;
1717
18+ /**
19+ * Service for managing collection entities in the database.
20+ * Handles CRUD operations and relationships for collections, including hypercerts, blueprints, and admins.
21+ *
22+ * Features:
23+ * - Fetch collections with filtering and pagination
24+ * - Manage collection contents (hypercerts and blueprints)
25+ * - Handle collection administrators
26+ * - Support for complex queries with JSON aggregation
27+ * - Transaction support for data integrity
28+ *
29+ * @injectable Marks the class as injectable for dependency injection with tsyringe
30+ */
1831@injectable ( )
1932export class CollectionService {
2033 private entityService : EntityService <
2134 DataDatabase [ "collections" ] ,
2235 GetCollectionsArgs
2336 > ;
2437
38+ /**
39+ * Creates a new instance of CollectionService.
40+ *
41+ * @param hypercertsService - Service for hypercert-related operations
42+ * @param dbService - Service for database operations
43+ * @param blueprintsService - Service for blueprint-related operations
44+ * @param usersService - Service for user-related operations
45+ */
2546 constructor (
2647 @inject ( HypercertsService )
2748 private hypercertsService : HypercertsService ,
@@ -39,15 +60,37 @@ export class CollectionService {
3960 > ( "collections" , "CollectionEntityService" , kyselyData ) ;
4061 }
4162
42- //TODO can we programatically generate these?
63+ /**
64+ * Retrieves multiple collections based on provided arguments.
65+ *
66+ * @param args - Query arguments for filtering and pagination
67+ * @returns A promise resolving to an object containing:
68+ * - data: Array of collections matching the query
69+ * - count: Total number of matching collections
70+ * @throws {Error } If the database query fails
71+ */
4372 async getCollections ( args : GetCollectionsArgs ) {
4473 return this . entityService . getMany ( args ) ;
4574 }
4675
76+ /**
77+ * Retrieves a single collection based on provided arguments.
78+ *
79+ * @param args - Query arguments for filtering
80+ * @returns A promise resolving to a single collection or undefined if not found
81+ * @throws {Error } If the database query fails
82+ */
4783 async getCollection ( args : GetCollectionsArgs ) {
4884 return this . entityService . getSingle ( args ) ;
4985 }
5086
87+ /**
88+ * Retrieves blueprint IDs associated with a collection.
89+ *
90+ * @param collectionId - ID of the collection
91+ * @returns Promise resolving to an array of blueprint IDs
92+ * @throws {Error } If the database query fails
93+ */
5194 async getCollectionBlueprintIds ( collectionId : string ) {
5295 return await this . dbService
5396 . getConnection ( )
@@ -57,10 +100,16 @@ export class CollectionService {
57100 . execute ( ) ;
58101 }
59102
103+ /**
104+ * Retrieves all blueprints associated with a collection.
105+ *
106+ * @param collectionId - ID of the collection
107+ * @returns Promise resolving to an array of blueprints
108+ * @throws {Error } If the database query fails
109+ */
60110 async getCollectionBlueprints ( collectionId : string ) {
61111 const collectionBlueprintIds =
62112 await this . getCollectionBlueprintIds ( collectionId ) ;
63-
64113 const blueprintIds = collectionBlueprintIds . map (
65114 ( blueprint ) => blueprint . blueprint_id ,
66115 ) ;
@@ -70,6 +119,13 @@ export class CollectionService {
70119 } ) ;
71120 }
72121
122+ /**
123+ * Retrieves hypercert IDs associated with a collection.
124+ *
125+ * @param collectionId - ID of the collection
126+ * @returns Promise resolving to an array of hypercert IDs
127+ * @throws {Error } If the database query fails
128+ */
73129 async getCollectionHypercertIds ( collectionId : string ) {
74130 return await this . dbService
75131 . getConnection ( )
@@ -79,16 +135,29 @@ export class CollectionService {
79135 . execute ( ) ;
80136 }
81137
138+ /**
139+ * Retrieves all hypercerts associated with a collection.
140+ *
141+ * @param collectionId - ID of the collection
142+ * @returns Promise resolving to an array of hypercerts
143+ * @throws {Error } If the database query fails
144+ */
82145 async getCollectionHypercerts ( collectionId : string ) {
83146 const hypercerts = await this . getCollectionHypercertIds ( collectionId ) ;
84-
85147 const hypercertIds = hypercerts . map ( ( hypercert ) => hypercert . hypercert_id ) ;
86148
87149 return this . hypercertsService . getHypercerts ( {
88150 where : { hypercert_id : { in : hypercertIds } } ,
89151 } ) ;
90152 }
91153
154+ /**
155+ * Retrieves all administrators of a collection.
156+ *
157+ * @param collectionId - ID of the collection
158+ * @returns Promise resolving to an array of users who are admins
159+ * @throws {Error } If the database query fails
160+ */
92161 async getCollectionAdmins ( collectionId : string ) {
93162 return await this . dbService
94163 . getConnection ( )
@@ -104,6 +173,14 @@ export class CollectionService {
104173 . execute ( ) ;
105174 }
106175
176+ /**
177+ * Retrieves detailed collection information including admins.
178+ * Uses JSON aggregation for efficient data retrieval.
179+ *
180+ * @param collectionId - ID of the collection
181+ * @returns Promise resolving to the collection with admin details
182+ * @throws {Error } If the database query fails
183+ */
107184 // TODO this type and query can be cleaner. Do we need a view?
108185 async getCollectionById ( collectionId : string ) {
109186 return await this . dbService
@@ -130,7 +207,13 @@ export class CollectionService {
130207 . executeTakeFirst ( ) ;
131208 }
132209
133- // Mutations
210+ /**
211+ * Creates or updates multiple collections.
212+ *
213+ * @param collections - Array of collection data to upsert
214+ * @returns Promise resolving to the result of the upsert operation
215+ * @throws {Error } If the database operation fails
216+ */
134217 async upsertCollections ( collections : CollectionInsert [ ] ) {
135218 return this . dbService
136219 . getConnection ( )
@@ -148,6 +231,13 @@ export class CollectionService {
148231 . execute ( ) ;
149232 }
150233
234+ /**
235+ * Removes all hypercerts from a collection.
236+ *
237+ * @param collectionId - ID of the collection
238+ * @returns Promise resolving to the result of the delete operation
239+ * @throws {Error } If the database operation fails
240+ */
151241 async deleteAllHypercertsFromCollection ( collectionId : string ) {
152242 return this . dbService
153243 . getConnection ( )
@@ -156,6 +246,13 @@ export class CollectionService {
156246 . execute ( ) ;
157247 }
158248
249+ /**
250+ * Removes all blueprints from a collection.
251+ *
252+ * @param collectionId - ID of the collection
253+ * @returns Promise resolving to the result of the delete operation
254+ * @throws {Error } If the database operation fails
255+ */
159256 async deleteAllBlueprintsFromCollection ( collectionId : string ) {
160257 return this . dbService
161258 . getConnection ( )
@@ -164,6 +261,13 @@ export class CollectionService {
164261 . execute ( ) ;
165262 }
166263
264+ /**
265+ * Associates hypercerts with collections.
266+ *
267+ * @param hypercerts - Array of hypercert-collection associations to create or update
268+ * @returns Promise resolving to the result of the upsert operation
269+ * @throws {Error } If the database operation fails
270+ */
167271 async upsertHypercertCollections (
168272 hypercerts : Insertable < DataDatabase [ "hypercerts" ] > [ ] ,
169273 ) {
@@ -180,6 +284,14 @@ export class CollectionService {
180284 . execute ( ) ;
181285 }
182286
287+ /**
288+ * Adds an administrator to a collection.
289+ *
290+ * @param collectionId - ID of the collection
291+ * @param admin - User data for the new admin
292+ * @returns Promise resolving to the result of the operation
293+ * @throws {Error } If the database operation fails
294+ */
183295 async addAdminToCollection ( collectionId : string , admin : UserInsert ) {
184296 const user = await this . usersService . getOrCreateUser ( admin ) ;
185297 return this . dbService
@@ -200,6 +312,13 @@ export class CollectionService {
200312 . executeTakeFirst ( ) ;
201313 }
202314
315+ /**
316+ * Associates blueprints with a collection.
317+ *
318+ * @param values - Array of blueprint-collection associations to create
319+ * @returns Promise resolving to the result of the insert operation
320+ * @throws {Error } If the database operation fails
321+ */
203322 async addBlueprintsToCollection (
204323 values : Insertable < DataDatabase [ "collection_blueprints" ] > [ ] ,
205324 ) {
0 commit comments