feat(users): enhance exportUserData with projects, groups, persons, and collaborations

This commit is contained in:
Mathis HERRIOT 2025-05-17 10:33:24 +02:00
parent c1a74d712b
commit 4028cebb63
No known key found for this signature in database
GPG Key ID: E7EB4A211D8D4907

View File

@ -1,5 +1,5 @@
import { Injectable, NotFoundException, Inject } from '@nestjs/common'; 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 { DRIZZLE } from '../../../database/database.module';
import * as schema from '../../../database/schema'; import * as schema from '../../../database/schema';
import { CreateUserDto } from '../dto/create-user.dto'; import { CreateUserDto } from '../dto/create-user.dto';
@ -111,17 +111,59 @@ export class UsersService {
*/ */
async exportUserData(id: string) { async exportUserData(id: string) {
const user = await this.findById(id); const user = await this.findById(id);
// Get all projects owned by the user
const projects = await this.db const projects = await this.db
.select() .select()
.from(schema.projects) .from(schema.projects)
.where(eq(schema.projects.ownerId, id)); .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 { return {
user, user,
projects, projects,
groups: [], groups,
persons: [] 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
}
}))
}; };
} }
} }