Skip to content

Commit

Permalink
Merge pull request #15 from freemocap/jon/new-user-bug
Browse files Browse the repository at this point in the history
Fix bug with MongoDB creating duplicate entries
  • Loading branch information
jonmatthis authored Jan 13, 2024
2 parents c412733 + 09126de commit 0e5efb7
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 13 deletions.
29 changes: 29 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@slack/bolt": "^3.17.0",
"bson": "^6.2.0",
"bufferutil": "^4.0.8",
"class-validator": "^0.14.1",
"discord.js": "^14.14.1",
"langchain": "^0.0.209",
"mongodb": "^5.9.2",
Expand Down
32 changes: 30 additions & 2 deletions src/core/database/schema/users/user.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,32 @@
import { IS_UUID, IsOptional, IsString, IsUUID } from 'class-validator';

class DiscordIdentifierDto {
@IsString()
@IsOptional()
id?: string;

@IsString()
@IsOptional()
username?: string;
}

class SlackIdentifierDto {
@IsString()
@IsOptional()
id?: string;

@IsString()
@IsOptional()
username?: string;
}

export class UserDto {
readonly identifiers?: object;
readonly metadata?: object;
@IsOptional()
identifiers?: {
discord?: DiscordIdentifierDto;
slack?: SlackIdentifierDto;
};

@IsOptional()
metadata?: Record<string, unknown>;
}
14 changes: 10 additions & 4 deletions src/core/database/schema/users/user.schema.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { IsUUID } from 'class-validator';

@Schema({ timestamps: true })
export class User {
@Prop({ required: true, unique: true })
@IsUUID()
uuid: string;

@Prop({ type: Object })
identifiers?: {
discordId?: string;
discordUsername?: string;
slackId?: string;
slackUsername?: string;
discord?: {
id: string;
username: string;
};
slack?: {
id: string;
username: string;
};
};

@Prop({ type: Object })
Expand Down
14 changes: 7 additions & 7 deletions src/core/database/schema/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,31 @@ export class UsersService {

async findOne(userDto: UserDto): Promise<User> {
let user: User;
if ('discordID' in userDto.identifiers && userDto.identifiers.discordID) {
if ('discord' in userDto.identifiers && userDto.identifiers.discord.id) {
user = await this.userModel
.findOne({ discordID: userDto.identifiers.discordID })
.findOne({ 'identifiers.discord.id': userDto.identifiers.discord.id })
.exec();
}
if ('slackID' in userDto.identifiers && userDto.identifiers.slackID) {
if ('slack' in userDto.identifiers && userDto.identifiers.slack.id) {
user = await this.userModel
.findOne({ slackID: userDto.identifiers.slackID })
.findOne({ 'identifiers.slack.id': userDto.identifiers.slack.id })
.exec();
}

return user;
}

async getOrCreateUser(userDto: UserDto): Promise<User> {
public async getOrCreateUser(userDto: UserDto): Promise<User> {
const existingUser = await this.findOne(userDto);

if (existingUser) {
return existingUser;
}

return this.createUser(userDto);
return this._createUser(userDto);
}

async createUser(userDto: UserDto): Promise<User> {
async _createUser(userDto: UserDto): Promise<User> {
if (await this.findOne(userDto)) {
throw new HttpException('User ID already exists', HttpStatus.CONFLICT);
}
Expand Down

0 comments on commit 0e5efb7

Please sign in to comment.