diff --git a/backend/src/modules/users/services/users.service.ts b/backend/src/modules/users/services/users.service.ts index 9ed5026..91f0f01 100644 --- a/backend/src/modules/users/services/users.service.ts +++ b/backend/src/modules/users/services/users.service.ts @@ -1,5 +1,5 @@ import { Injectable, NotFoundException, Inject } from '@nestjs/common'; -import { eq } from 'drizzle-orm'; +import { eq, inArray } from 'drizzle-orm'; import { DRIZZLE } from '../../../database/database.module'; import * as schema from '../../../database/schema'; import { CreateUserDto } from '../dto/create-user.dto'; @@ -111,17 +111,59 @@ export class UsersService { */ async exportUserData(id: string) { const user = await this.findById(id); + + // Get all projects owned by the user const projects = await this.db .select() .from(schema.projects) .where(eq(schema.projects.ownerId, id)); - // Add empty groups and persons arrays for compatibility with tests + // Get all project IDs + const projectIds = projects.map(project => project.id); + + // Get all persons in user's projects + const persons = projectIds.length > 0 + ? await this.db + .select() + .from(schema.persons) + .where(inArray(schema.persons.projectId, projectIds)) + : []; + + // Get all groups in user's projects + const groups = projectIds.length > 0 + ? await this.db + .select() + .from(schema.groups) + .where(inArray(schema.groups.projectId, projectIds)) + : []; + + // Get all project collaborations where the user is a collaborator + const collaborations = await this.db + .select({ + collaboration: schema.projectCollaborators, + project: schema.projects + }) + .from(schema.projectCollaborators) + .innerJoin( + schema.projects, + eq(schema.projectCollaborators.projectId, schema.projects.id) + ) + .where(eq(schema.projectCollaborators.userId, id)); + return { user, projects, - groups: [], - persons: [] + groups, + persons, + collaborations: collaborations.map(c => ({ + id: c.collaboration.id, + projectId: c.collaboration.projectId, + project: { + id: c.project.id, + name: c.project.name, + description: c.project.description + } + })) }; } }