-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from freemocap/jon/users
Create User schema
- Loading branch information
Showing
49 changed files
with
331 additions
and
408 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
...hared/ai/openai/openai-secrets.service.ts → src/core/ai/openai/openai-secrets.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
8 changes: 4 additions & 4 deletions
8
src/shared/chatbot/chatbot.module.ts → src/core/bot/bot.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
import { Logger, Module } from '@nestjs/common'; | ||
import { LangchainModule } from '../ai/langchain/langchain.module'; | ||
import { ChatbotService } from './chatbot.service'; | ||
import { BotService } from './bot.service'; | ||
|
||
@Module({ | ||
imports: [LangchainModule], | ||
providers: [ChatbotService, Logger], | ||
exports: [ChatbotService], | ||
providers: [BotService, Logger], | ||
exports: [BotService], | ||
}) | ||
export class ChatbotModule {} | ||
export class BotModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { MongooseModule } from '@nestjs/mongoose'; | ||
import { GcpModule } from '../gcp/gcp.module'; | ||
import { UsersModule } from './schema/users/users.module'; | ||
import { DatabaseConnectionService } from './services/database-connection.service'; | ||
|
||
@Module({ | ||
imports: [ | ||
GcpModule, | ||
MongooseModule.forRoot('mongodb://localhost:27017/test'), | ||
// CatsModule, | ||
UsersModule, | ||
], | ||
providers: [], | ||
exports: [UsersModule], | ||
}) | ||
export class DatabaseModule {} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export class UserDto { | ||
readonly favoriteColor?: string; | ||
readonly slackID?: string; | ||
readonly discordId?: string; | ||
readonly metadata?: object; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'; | ||
|
||
@Schema({ timestamps: true }) | ||
export class User { | ||
@Prop({ required: true, unique: true }) | ||
uuid: string; | ||
|
||
@Prop({ required: true }) | ||
favoriteColor: string; | ||
|
||
@Prop({ unique: true }) | ||
discordId: string; | ||
|
||
@Prop({ unique: true }) | ||
slackId: string; | ||
|
||
@Prop({ type: Object }) | ||
metadata: Record<string, unknown>; | ||
} | ||
|
||
export const UserSchema = SchemaFactory.createForClass(User); | ||
|
||
function validateFavoriteColor(color: string) { | ||
const regex = /^([A-Fa-f0-9]{6})$/; | ||
return regex.test(`${color}`); | ||
} | ||
|
||
UserSchema.pre<User>('save', function (next) { | ||
if (!validateFavoriteColor(this.favoriteColor)) { | ||
throw new Error( | ||
'User.favoriteColor should be a valid 6 digit hex color code (e.g. FF00FF)', | ||
); | ||
} | ||
next(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { MongooseModule } from '@nestjs/mongoose'; | ||
import { UsersService } from './users.service'; | ||
import { User, UserSchema } from './user.schema'; | ||
|
||
@Module({ | ||
imports: [ | ||
MongooseModule.forFeature([{ name: User.name, schema: UserSchema }]), | ||
], | ||
providers: [UsersService], | ||
exports: [UsersService], | ||
}) | ||
export class UsersModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common'; | ||
import { InjectModel } from '@nestjs/mongoose'; | ||
import { Model } from 'mongoose'; | ||
import { User } from './user.schema'; | ||
import { UserDto } from './user.dto'; | ||
import { v4 as uuidv4 } from 'uuid'; | ||
|
||
@Injectable() | ||
export class UsersService { | ||
constructor( | ||
@InjectModel(User.name) | ||
private readonly userModel: Model<User>, | ||
) {} | ||
|
||
async findAll(): Promise<User[]> { | ||
return this.userModel.find().exec(); | ||
} | ||
|
||
async findOne(userDto: UserDto): Promise<User> { | ||
let user: User; | ||
if (userDto.discordId) { | ||
user = await this.userModel | ||
.findOne({ discordId: userDto.discordId }) | ||
.exec(); | ||
} else if (userDto.slackID) { | ||
user = await this.userModel.findOne({ slackID: userDto.slackID }).exec(); | ||
} | ||
|
||
return user; | ||
} | ||
|
||
async getOrCreate(userDto: UserDto): Promise<User> { | ||
const existingUser = await this.findOne(userDto); | ||
|
||
if (existingUser) { | ||
return existingUser; | ||
} | ||
|
||
return this.create(userDto); | ||
} | ||
|
||
_generateHexColorId() { | ||
const letters = '0123456789ABCDEF'; | ||
let color = ''; | ||
const digits = 6; | ||
for (let i = 0; i < digits; i++) { | ||
color += letters[Math.floor(Math.random() * 16)]; | ||
} | ||
return color; | ||
} | ||
|
||
async create(userDto: UserDto): Promise<User> { | ||
if (await this.findOne(userDto)) { | ||
throw new HttpException('User ID already exists', HttpStatus.CONFLICT); | ||
} | ||
|
||
const createdUser = new this.userModel({ | ||
...userDto, | ||
uuid: uuidv4(), | ||
favoriteColor: this._generateHexColorId() || userDto.favoriteColor, | ||
}); | ||
return createdUser.save(); | ||
} | ||
|
||
async update(id: string, userDto: UserDto) { | ||
const existingUser = await this.userModel.findOne({ id: id }).exec(); | ||
if (!existingUser) { | ||
throw new Error(`User with id ${id} not found`); | ||
} | ||
return this.userModel | ||
.findOneAndUpdate({ id: id }, userDto, { new: true }) | ||
.exec(); | ||
} | ||
|
||
async delete(id: string) { | ||
const deletedUser = await this.userModel | ||
.findOneAndDelete({ id: id }) | ||
.exec(); | ||
if (!deletedUser) { | ||
throw new Error(`User with id ${id} not found`); | ||
} | ||
return deletedUser; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { GcpSecretsService } from './gcp-secrets.service'; | ||
|
||
@Module({ | ||
providers: [GcpSecretsService], | ||
exports: [GcpSecretsService], | ||
}) | ||
export class GcpModule {} |
This file was deleted.
Oops, something went wrong.
40 changes: 0 additions & 40 deletions
40
src/interfaces/chat/discord/services/discordChat.service.ts
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.