Skip to content

Commit

Permalink
Merge pull request #33 from DevoteamNL/fix/validation-futures-reserva…
Browse files Browse the repository at this point in the history
…tion

Unique user
  • Loading branch information
hardik-id authored Mar 7, 2024
2 parents cd2238d + 71e6243 commit 6a04485
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
11 changes: 9 additions & 2 deletions src/users/entities/user.entity.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import { Entity, PrimaryGeneratedColumn, Column, OneToMany } from 'typeorm';
import {
Entity,
PrimaryGeneratedColumn,
Column,
OneToMany,
Unique,
} from 'typeorm';
import { IsEmail } from 'class-validator';
import { Thread } from '../../thread/entities/thread.entity';
import { Feedback } from '../../message/feedback/entities/feedback.entity';

@Entity() // Use the Entity decorator
@Entity()
@Unique('UQ_USERNAME_PROVIDER', ['username', 'providerId'])
export class User {
@PrimaryGeneratedColumn()
id: number;
Expand Down
23 changes: 19 additions & 4 deletions src/users/users.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { Injectable, Logger, NotFoundException } from '@nestjs/common';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { InjectRepository } from '@nestjs/typeorm'; // Import InjectRepository
Expand All @@ -8,6 +8,7 @@ import { Thread } from '../thread/entities/thread.entity'; // Import Repository

@Injectable()
export class UsersService {
private readonly logger = new Logger(UsersService.name);
constructor(
@InjectRepository(User)
private readonly usersRepository: Repository<User>, // Use Repository<User>
Expand All @@ -16,9 +17,23 @@ export class UsersService {
) {}

async create(createUserDto: CreateUserDto): Promise<User> {
const user = this.usersRepository.create(createUserDto);
await this.usersRepository.save(user);
return user;
try {
const user = this.usersRepository.create(createUserDto);
await this.usersRepository.save(user);
return user;
} catch (error) {
if (error.code === '23505') {
const existingUser = await this.findByProviderId(
createUserDto.providerId,
);
if (existingUser) {
return existingUser;
}
throw new Error('User already exists with this provider ID');
}
this.logger.error(`Failed to create user: ${error.message}`);
throw error;
}
}

async findAll(): Promise<User[]> {
Expand Down

0 comments on commit 6a04485

Please sign in to comment.