-
Notifications
You must be signed in to change notification settings - Fork 43
feat: collection management #1726
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
Merged
+878
−66
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,64 @@ | ||
| // Copyright (c) 2025 The Linux Foundation and each contributor. | ||
| // SPDX-License-Identifier: MIT | ||
| import type { Pool } from 'pg'; | ||
| import { CommunityCollectionRepository } from '~~/server/repo/communityCollection.repo'; | ||
| import { InsightsSsoUserRepository } from '~~/server/repo/insightsSsoUser.repo'; | ||
| import type { DecodedOidcToken } from '~~/types/auth/auth-jwt.types'; | ||
|
|
||
| /** | ||
| * API Endpoint: DELETE /api/collection/community/:id | ||
| * Description: Soft-deletes a community collection owned by the authenticated user. | ||
| * | ||
| * URL Parameters: | ||
| * - id (string, required): Collection ID | ||
| * | ||
| * Response: | ||
| * - 200: Success | ||
| * - 401: Unauthorized | ||
| * - 403: Forbidden (not the owner) | ||
| * - 404: Collection not found | ||
| * - 500: Internal Server Error | ||
| */ | ||
| export default defineEventHandler(async (event): Promise<{ success: boolean } | Error> => { | ||
| const user = event.context.user as DecodedOidcToken | undefined; | ||
|
|
||
| if (!user?.sub) { | ||
| throw createError({ statusCode: 401, statusMessage: 'Unauthorized' }); | ||
| } | ||
|
|
||
| const { id } = event.context.params as Record<string, string>; | ||
|
|
||
| if (!id) { | ||
| throw createError({ statusCode: 400, statusMessage: 'Collection ID is required' }); | ||
| } | ||
|
|
||
| const cmDbPool = event.context.cmDbPool as Pool | undefined; | ||
|
|
||
| if (!cmDbPool) { | ||
| throw createError({ statusCode: 503, statusMessage: 'Database not available' }); | ||
| } | ||
|
|
||
| try { | ||
| const username = user.sub.includes('|') ? user.sub.split('|').pop()! : user.sub; | ||
|
|
||
| const ssoUserRepo = new InsightsSsoUserRepository(cmDbPool); | ||
| const ssoUser = await ssoUserRepo.upsert({ | ||
| id: user.sub, | ||
| displayName: user.name, | ||
| avatarUrl: user.picture, | ||
| email: user.email, | ||
| username, | ||
| }); | ||
|
|
||
| const repo = new CommunityCollectionRepository(cmDbPool); | ||
| await repo.destroy(id, ssoUser.id); | ||
|
|
||
| return { success: true }; | ||
| } catch (error: unknown) { | ||
| if (error && typeof error === 'object' && 'statusCode' in error) { | ||
| throw error; | ||
| } | ||
| console.error('Error deleting community collection:', error); | ||
| throw createError({ statusCode: 500, statusMessage: 'Internal Server Error' }); | ||
| } | ||
| }); |
This file contains hidden or 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,79 @@ | ||
| // Copyright (c) 2025 The Linux Foundation and each contributor. | ||
| // SPDX-License-Identifier: MIT | ||
| import type { Pool } from 'pg'; | ||
| import { | ||
| type CommunityCollection, | ||
| CommunityCollectionRepository, | ||
| type UpdateCommunityCollectionInput, | ||
| } from '~~/server/repo/communityCollection.repo'; | ||
| import { InsightsSsoUserRepository } from '~~/server/repo/insightsSsoUser.repo'; | ||
| import type { DecodedOidcToken } from '~~/types/auth/auth-jwt.types'; | ||
|
|
||
| /** | ||
| * API Endpoint: PUT /api/collection/community/:id | ||
| * Description: Updates a community collection owned by the authenticated user. | ||
| * | ||
| * URL Parameters: | ||
| * - id (string, required): Collection ID | ||
| * | ||
| * Request Body: | ||
| * - name (string, optional): Collection name | ||
| * - description (string, optional): Collection description | ||
| * - isPrivate (boolean, optional): Whether the collection is private | ||
| * - projects (string[], optional): List of project IDs | ||
| * | ||
| * Response: | ||
| * - 200: Updated collection | ||
| * - 401: Unauthorized | ||
| * - 403: Forbidden (not the owner) | ||
| * - 404: Collection not found | ||
| * - 500: Internal Server Error | ||
| */ | ||
| export default defineEventHandler(async (event): Promise<CommunityCollection | Error> => { | ||
| const user = event.context.user as DecodedOidcToken | undefined; | ||
|
|
||
| if (!user?.sub) { | ||
| throw createError({ statusCode: 401, statusMessage: 'Unauthorized' }); | ||
| } | ||
|
|
||
| const { id } = event.context.params as Record<string, string>; | ||
|
|
||
| if (!id) { | ||
| throw createError({ statusCode: 400, statusMessage: 'Collection ID is required' }); | ||
| } | ||
|
|
||
| const body = await readBody<Partial<UpdateCommunityCollectionInput>>(event); | ||
|
|
||
| const cmDbPool = event.context.cmDbPool as Pool | undefined; | ||
|
|
||
| if (!cmDbPool) { | ||
| throw createError({ statusCode: 503, statusMessage: 'Database not available' }); | ||
| } | ||
|
|
||
| try { | ||
| const username = user.sub.includes('|') ? user.sub.split('|').pop()! : user.sub; | ||
|
|
||
| const ssoUserRepo = new InsightsSsoUserRepository(cmDbPool); | ||
| const ssoUser = await ssoUserRepo.upsert({ | ||
| id: user.sub, | ||
| displayName: user.name, | ||
| avatarUrl: user.picture, | ||
| email: user.email, | ||
| username, | ||
| }); | ||
|
|
||
| const repo = new CommunityCollectionRepository(cmDbPool); | ||
| return await repo.update(id, ssoUser.id, { | ||
| name: body.name?.trim(), | ||
| description: body.description?.trim(), | ||
| isPrivate: body.isPrivate, | ||
| projects: body.projects, | ||
| }); | ||
| } catch (error: unknown) { | ||
| if (error && typeof error === 'object' && 'statusCode' in error) { | ||
| throw error; | ||
| } | ||
| console.error('Error updating community collection:', error); | ||
| throw createError({ statusCode: 500, statusMessage: 'Internal Server Error' }); | ||
| } | ||
| }); | ||
This file contains hidden or 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,79 @@ | ||
| // Copyright (c) 2025 The Linux Foundation and each contributor. | ||
| // SPDX-License-Identifier: MIT | ||
| import type { Pool } from 'pg'; | ||
| import { | ||
| CommunityCollectionRepository, | ||
| type CommunityCollection, | ||
| type CreateCommunityCollectionInput, | ||
| } from '~~/server/repo/communityCollection.repo'; | ||
| import { InsightsSsoUserRepository } from '~~/server/repo/insightsSsoUser.repo'; | ||
| import type { DecodedOidcToken } from '~~/types/auth/auth-jwt.types'; | ||
|
|
||
| /** | ||
| * API Endpoint: POST /api/collection/community | ||
| * Description: Creates a new community collection for the authenticated user. | ||
| * | ||
| * Request Body: | ||
| * - name (string, required): Collection name | ||
| * - description (string, optional): Collection description | ||
| * - isPrivate (boolean, optional): Whether the collection is private (default: false) | ||
| * - projects (string[], optional): List of project IDs | ||
| * | ||
| * Response: | ||
| * - 201: Created collection | ||
| * - 400: Validation error | ||
| * - 401: Unauthorized | ||
| * - 500: Internal Server Error | ||
| */ | ||
| export default defineEventHandler(async (event): Promise<CommunityCollection | Error> => { | ||
| const user = event.context.user as DecodedOidcToken | undefined; | ||
|
|
||
| if (!user?.sub) { | ||
| throw createError({ statusCode: 401, statusMessage: 'Unauthorized' }); | ||
| } | ||
|
|
||
| const body = await readBody<Partial<CreateCommunityCollectionInput>>(event); | ||
|
|
||
| if (!body?.name?.trim()) { | ||
| throw createError({ statusCode: 400, statusMessage: 'Name is required' }); | ||
| } | ||
|
|
||
| const cmDbPool = event.context.cmDbPool as Pool | undefined; | ||
|
|
||
| if (!cmDbPool) { | ||
| throw createError({ statusCode: 503, statusMessage: 'Database not available' }); | ||
| } | ||
|
|
||
| try { | ||
| // Derive username from Auth0 sub (e.g. "auth0|abc123" -> "abc123") | ||
| const username = user.sub.includes('|') ? user.sub.split('|').pop()! : user.sub; | ||
|
|
||
| // Upsert SSO user | ||
| const ssoUserRepo = new InsightsSsoUserRepository(cmDbPool); | ||
| const ssoUser = await ssoUserRepo.upsert({ | ||
| id: user.sub, | ||
| displayName: user.name, | ||
| avatarUrl: user.picture, | ||
| email: user.email, | ||
| username, | ||
| }); | ||
gaspergrom marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const repo = new CommunityCollectionRepository(cmDbPool); | ||
| const collection = await repo.create({ | ||
| name: body.name.trim(), | ||
| description: body.description?.trim(), | ||
| isPrivate: body.isPrivate, | ||
| ssoUserId: ssoUser.id, | ||
| projects: body.projects, | ||
| }); | ||
|
|
||
| setResponseStatus(event, 201); | ||
| return collection; | ||
| } catch (error: unknown) { | ||
| if (error && typeof error === 'object' && 'statusCode' in error) { | ||
| throw error; | ||
| } | ||
| console.error('Error creating community collection:', error); | ||
| throw createError({ statusCode: 500, statusMessage: 'Internal Server Error' }); | ||
| } | ||
| }); | ||
This file contains hidden or 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,49 @@ | ||
| // Copyright (c) 2025 The Linux Foundation and each contributor. | ||
| // SPDX-License-Identifier: MIT | ||
| import type { Pool } from 'pg'; | ||
| import { | ||
| CommunityCollectionRepository, | ||
| type CommunityCollection, | ||
| } from '~~/server/repo/communityCollection.repo'; | ||
| import { InsightsSsoUserRepository } from '~~/server/repo/insightsSsoUser.repo'; | ||
| import type { DecodedOidcToken } from '~~/types/auth/auth-jwt.types'; | ||
|
|
||
| /** | ||
| * API Endpoint: GET /api/collection/community/my | ||
| * Description: Returns all community collections owned by the authenticated user. | ||
| * | ||
| * Response: | ||
| * - 200: List of collections | ||
| * - 401: Unauthorized | ||
| * - 500: Internal Server Error | ||
| */ | ||
| export default defineEventHandler(async (event): Promise<CommunityCollection[] | Error> => { | ||
joanagmaia marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const user = event.context.user as DecodedOidcToken | undefined; | ||
|
|
||
| if (!user?.sub) { | ||
| throw createError({ statusCode: 401, statusMessage: 'Unauthorized' }); | ||
| } | ||
|
|
||
| const cmDbPool = event.context.cmDbPool as Pool | undefined; | ||
|
|
||
| if (!cmDbPool) { | ||
| throw createError({ statusCode: 503, statusMessage: 'Database not available' }); | ||
| } | ||
|
|
||
| try { | ||
| const username = user.sub.includes('|') ? user.sub.split('|').pop()! : user.sub; | ||
|
|
||
| const ssoUserRepo = new InsightsSsoUserRepository(cmDbPool); | ||
| const ssoUser = await ssoUserRepo.findByUsername(username); | ||
|
|
||
| if (!ssoUser) { | ||
| return []; | ||
| } | ||
|
|
||
| const repo = new CommunityCollectionRepository(cmDbPool); | ||
| return await repo.findBySsoUserId(ssoUser.id); | ||
| } catch (error) { | ||
| console.error('Error fetching user collections:', error); | ||
| throw createError({ statusCode: 500, statusMessage: 'Internal Server Error' }); | ||
| } | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When updating,
name: body.name?.trim()will turn a whitespace-only value into an empty string, which then updates the collection name/slug to''. Ifnameis provided, validate it after trimming (non-empty), or treat empty/whitespace-only input as "no update" (i.e.undefined).