mirror of https://github.com/msgbyte/tailchat
feat: add action group.extra for storage group extra data
parent
32875f8a72
commit
8aa5d93eea
@ -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<GroupExtra>;
|
||||
|
||||
@prop()
|
||||
panelId?: string;
|
||||
|
||||
@prop()
|
||||
name: string;
|
||||
|
||||
/**
|
||||
* Only allow string
|
||||
*/
|
||||
@prop()
|
||||
data: string;
|
||||
}
|
||||
|
||||
export type GroupExtraDocument = DocumentType<GroupExtra>;
|
||||
|
||||
const model = getModelForClass(GroupExtra);
|
||||
|
||||
export type GroupExtraModel = typeof model;
|
||||
|
||||
export default model;
|
@ -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<GroupExtraDocument, GroupExtraModel> {}
|
||||
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;
|
Loading…
Reference in New Issue