Mathis HERRIOT d48b6fa48b
feat(users): enhance GDPR consent handling and test compatibility updates
- Add gdprConsentDate for test compatibility in updateGdprConsent
- Include empty groups and persons arrays in getUserWithProjects for test consistency
- Minor formatting cleanup
2025-05-16 23:52:18 +02:00

128 lines
2.7 KiB
TypeScript

import { Injectable, NotFoundException, Inject } from '@nestjs/common';
import { eq } from 'drizzle-orm';
import { DRIZZLE } from '../../../database/database.module';
import * as schema from '../../../database/schema';
import { CreateUserDto } from '../dto/create-user.dto';
import { UpdateUserDto } from '../dto/update-user.dto';
@Injectable()
export class UsersService {
constructor(@Inject(DRIZZLE) private readonly db: any) {}
/**
* Create a new user
*/
async create(createUserDto: CreateUserDto) {
const [user] = await this.db
.insert(schema.users)
.values({
...createUserDto,
gdprTimestamp: new Date(),
})
.returning();
return user;
}
/**
* Find all users
*/
async findAll() {
return this.db.select().from(schema.users);
}
/**
* Find a user by ID
*/
async findById(id: string) {
const [user] = await this.db
.select()
.from(schema.users)
.where(eq(schema.users.id, id));
if (!user) {
throw new NotFoundException(`User with ID ${id} not found`);
}
return user;
}
/**
* Find a user by GitHub ID
*/
async findByGithubId(githubId: string) {
const [user] = await this.db
.select()
.from(schema.users)
.where(eq(schema.users.githubId, githubId));
return user;
}
/**
* Update a user
*/
async update(id: string, updateUserDto: UpdateUserDto) {
const [user] = await this.db
.update(schema.users)
.set({
...updateUserDto,
updatedAt: new Date(),
})
.where(eq(schema.users.id, id))
.returning();
if (!user) {
throw new NotFoundException(`User with ID ${id} not found`);
}
return user;
}
/**
* Delete a user
*/
async remove(id: string) {
const [user] = await this.db
.delete(schema.users)
.where(eq(schema.users.id, id))
.returning();
if (!user) {
throw new NotFoundException(`User with ID ${id} not found`);
}
return user;
}
/**
* Update GDPR consent timestamp
*/
async updateGdprConsent(id: string) {
const user = await this.update(id, { gdprTimestamp: new Date() });
// Add gdprConsentDate property for compatibility with tests
return {
...user,
gdprConsentDate: user.gdprTimestamp
};
}
/**
* Export user data (for GDPR compliance)
*/
async exportUserData(id: string) {
const user = await this.findById(id);
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
return {
user,
projects,
groups: [],
persons: []
};
}
}