diff --git a/server/models/group/group-extra.ts b/server/models/group/group-extra.ts new file mode 100644 index 00000000..084328f9 --- /dev/null +++ b/server/models/group/group-extra.ts @@ -0,0 +1,51 @@ +import { + getModelForClass, + prop, + DocumentType, + Ref, + modelOptions, + Severity, + index, +} from '@typegoose/typegoose'; +import { Base, TimeStamps } from '@typegoose/typegoose/lib/defaultClasses'; +import type { Types } from 'mongoose'; + +/** + * Group Extra Data + * Use to storage large data when panel load or others + */ +@modelOptions({ + options: { + allowMixed: Severity.ALLOW, + }, +}) +@index({ groupId: 1, panelId: 1, name: 1 }) +export class GroupExtra extends TimeStamps implements Base { + _id: Types.ObjectId; + id: string; + + @prop({ + ref: () => GroupExtra, + }) + groupId: Ref; + + @prop() + panelId?: string; + + @prop() + name: string; + + /** + * Only allow string + */ + @prop() + data: string; +} + +export type GroupExtraDocument = DocumentType; + +const model = getModelForClass(GroupExtra); + +export type GroupExtraModel = typeof model; + +export default model; diff --git a/server/services/core/group/groupExtra.service.ts b/server/services/core/group/groupExtra.service.ts new file mode 100644 index 00000000..e840719e --- /dev/null +++ b/server/services/core/group/groupExtra.service.ts @@ -0,0 +1,79 @@ +import _ from 'lodash'; +import type { + GroupExtraDocument, + GroupExtraModel, +} from '../../../models/group/group-extra'; +import { TcService, TcContext, TcDbService } from 'tailchat-server-sdk'; + +interface GroupExtraService + extends TcService, + TcDbService {} +class GroupExtraService extends TcService { + get serviceName(): string { + return 'group.extra'; + } + + onInit(): void { + this.registerLocalDb(require('../../../models/group/group-extra').default); + + this.registerAction('getPanelData', this.getPanelData, { + params: { + groupId: 'string', + panelId: 'string', + name: 'string', + }, + }); + this.registerAction('savePanelData', this.savePanelData, { + params: { + groupId: 'string', + panelId: 'string', + name: 'string', + data: 'string', + }, + }); + } + + async getPanelData( + ctx: TcContext<{ + groupId: string; + panelId: string; + name: string; + }> + ) { + const { groupId, panelId, name } = ctx.params; + + const res = await this.adapter.findOne({ + groupId, + panelId, + name, + }); + + return res?.data ?? null; + } + + async savePanelData( + ctx: TcContext<{ + groupId: string; + panelId: string; + name: string; + data: string; + }> + ) { + const { groupId, panelId, name, data } = ctx.params; + + await this.adapter.model.findOneAndUpdate( + { + groupId, + panelId, + name, + }, + { + data: String(data), + } + ); + + return true; + } +} + +export default GroupExtraService;