From 4448c13684f32e0289c2afbd3f4a1a64bd5c2f80 Mon Sep 17 00:00:00 2001 From: Ruben Talstra Date: Mon, 10 Mar 2025 15:19:52 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20refactor:=20Update=20Group=20Sch?= =?UTF-8?q?ema=20to=20use=20TypeScript=20and=20import=20from=20data-schema?= =?UTF-8?q?s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/models/Group.js | 2 +- api/models/schema/groupSchema.js | 41 ----------------------- packages/data-schemas/src/schema/group.ts | 39 +++++++++++++++++++++ packages/data-schemas/src/schema/user.ts | 8 ++++- 4 files changed, 47 insertions(+), 43 deletions(-) delete mode 100644 api/models/schema/groupSchema.js create mode 100644 packages/data-schemas/src/schema/group.ts diff --git a/api/models/Group.js b/api/models/Group.js index 69a8a53a6e..6d55f2b527 100644 --- a/api/models/Group.js +++ b/api/models/Group.js @@ -1,5 +1,5 @@ const mongoose = require('mongoose'); -const groupSchema = require('~/models/schema/groupSchema'); +const { groupSchema } = require('@librechat/data-schemas'); const Group = mongoose.model('Group', groupSchema); diff --git a/api/models/schema/groupSchema.js b/api/models/schema/groupSchema.js deleted file mode 100644 index acbf5ac000..0000000000 --- a/api/models/schema/groupSchema.js +++ /dev/null @@ -1,41 +0,0 @@ -const mongoose = require('mongoose'); - -/** - * @typedef {Object} MongoGroup - * @property {ObjectId} [_id] - MongoDB Document ID - * @property {string} name - The group's name - * @property {string} [description] - A brief description of the group - * @property {string} [externalId] - External identifier for the group (required for non-local groups) - * @property {string} provider - The provider of the group. Defaults to 'local'. For external groups (e.g., 'openid') the externalId is required. - * @property {Date} [createdAt] - Date when the group was created (added by timestamps) - * @property {Date} [updatedAt] - Date when the group was last updated (added by timestamps) - */ -const groupSchema = mongoose.Schema( - { - name: { - type: String, - required: true, - unique: true, - }, - description: { - type: String, - }, - externalId: { - type: String, - unique: true, - required: function () { - return this.provider !== 'local'; - }, - }, - provider: { - type: String, - required: true, - default: 'local', - enum: ['local', 'openid'], - }, - }, - - { timestamps: true }, -); - -module.exports = groupSchema; \ No newline at end of file diff --git a/packages/data-schemas/src/schema/group.ts b/packages/data-schemas/src/schema/group.ts new file mode 100644 index 0000000000..c7b392a711 --- /dev/null +++ b/packages/data-schemas/src/schema/group.ts @@ -0,0 +1,39 @@ +import { Schema, Document } from 'mongoose'; + +export interface IGroup extends Document { + name: string; + description?: string; + externalId?: string; + provider: 'local' | 'openid'; + createdAt?: Date; + updatedAt?: Date; +} + +const groupSchema = new Schema( + { + name: { + type: String, + required: true, + unique: true, + }, + description: { + type: String, + }, + externalId: { + type: String, + unique: true, + required: function (this: IGroup) { + return this.provider !== 'local'; + }, + }, + provider: { + type: String, + required: true, + default: 'local', + enum: ['local', 'openid'], + }, + }, + { timestamps: true }, +); + +export default groupSchema; diff --git a/packages/data-schemas/src/schema/user.ts b/packages/data-schemas/src/schema/user.ts index eb25b735ab..d05892ac23 100644 --- a/packages/data-schemas/src/schema/user.ts +++ b/packages/data-schemas/src/schema/user.ts @@ -1,4 +1,4 @@ -import { Schema, Document } from 'mongoose'; +import { Schema, Document, Types } from 'mongoose'; import { SystemRoles } from 'librechat-data-provider'; export interface IUser extends Document { @@ -18,6 +18,7 @@ export interface IUser extends Document { discordId?: string; appleId?: string; plugins?: unknown[]; + groups?: Types.ObjectId[]; twoFactorEnabled?: boolean; totpSecret?: string; backupCodes?: Array<{ @@ -135,6 +136,11 @@ const User = new Schema( plugins: { type: Array, }, + groups: { + type: [Schema.Types.ObjectId], + ref: 'Group', + default: [], + }, twoFactorEnabled: { type: Boolean, default: false,