From d83300e141e0a5e44479fdb6c3860baa87e5cff2 Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Fri, 19 May 2023 01:20:47 +0800 Subject: [PATCH] feat: add group data get and save action in group.extra --- .../services/core/group/groupExtra.service.ts | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/server/services/core/group/groupExtra.service.ts b/server/services/core/group/groupExtra.service.ts index 890ec858..6d3d8c6e 100644 --- a/server/services/core/group/groupExtra.service.ts +++ b/server/services/core/group/groupExtra.service.ts @@ -24,6 +24,23 @@ class GroupExtraService extends TcService { onInit(): void { this.registerLocalDb(require('../../../models/group/group-extra').default); + this.registerAction('getGroupData', this.getGroupData, { + params: { + groupId: 'string', + name: 'string', + }, + cache: { + keys: ['groupId', 'name'], + ttl: 60 * 60, // 1 hour + }, + }); + this.registerAction('saveGroupData', this.saveGroupData, { + params: { + groupId: 'string', + name: 'string', + data: 'string', + }, + }); this.registerAction('getPanelData', this.getPanelData, { params: { groupId: 'string', @@ -45,6 +62,61 @@ class GroupExtraService extends TcService { }); } + async getGroupData( + ctx: TcContext<{ + groupId: string; + name: string; + }> + ) { + const { groupId, name } = ctx.params; + + const res = await this.adapter.findOne({ + groupId, + panelId: null, + name, + }); + + return { data: res?.data ?? null }; + } + + async saveGroupData( + ctx: TcContext<{ + groupId: string; + name: string; + data: string; + }> + ) { + const { groupId, name, data } = ctx.params; + const userId = ctx.meta.userId; + + const [hasPermission] = await call(ctx).checkUserPermissions( + groupId, + userId, + [PERMISSION.core.groupConfig] + ); + if (!hasPermission) { + throw new NoPermissionError(t('没有操作权限')); + } + + await this.adapter.model.findOneAndUpdate( + { + groupId, + panelId: null, + name, + }, + { + data: String(data), + }, + { + upsert: true, // Create if not exist + } + ); + + await this.cleanGroupDataCache(groupId, name); + + return true; + } + async getPanelData( ctx: TcContext<{ groupId: string; @@ -102,6 +174,10 @@ class GroupExtraService extends TcService { return true; } + private cleanGroupDataCache(groupId: string, name: string) { + return this.cleanActionCache('getGroupData', [groupId, name]); + } + private cleanGroupPanelDataCache( groupId: string, panelId: string,