feat: limit export kb

This commit is contained in:
archer
2023-05-22 14:14:06 +08:00
parent 5128bbcce4
commit 1c8db69a5a
4 changed files with 43 additions and 7 deletions

View File

@@ -1,6 +1,6 @@
import type { NextApiRequest, NextApiResponse } from 'next'; import type { NextApiRequest, NextApiResponse } from 'next';
import { jsonRes } from '@/service/response'; import { jsonRes } from '@/service/response';
import { connectToDatabase } from '@/service/mongo'; import { connectToDatabase, User } from '@/service/mongo';
import { authUser } from '@/service/utils/auth'; import { authUser } from '@/service/utils/auth';
import { PgClient } from '@/service/pg'; import { PgClient } from '@/service/pg';
@@ -14,10 +14,28 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
throw new Error('缺少参数'); throw new Error('缺少参数');
} }
await connectToDatabase();
// 凭证校验 // 凭证校验
const { userId } = await authUser({ req, authToken: true }); const { userId } = await authUser({ req, authToken: true });
await connectToDatabase(); const thirtyMinutesAgo = new Date(Date.now() - 30 * 60 * 1000);
// auth export times
const authTimes = await User.findOne(
{
_id: userId,
$or: [
{ 'limit.exportKbTime': { $exists: false } },
{ 'limit.exportKbTime': { $lte: thirtyMinutesAgo } }
]
},
'_id limit'
);
if (!authTimes) {
throw new Error('上次导出未到半小时,每半小时仅可导出一次。');
}
// 统计数据 // 统计数据
const count = await PgClient.count('modelData', { const count = await PgClient.count('modelData', {
@@ -36,6 +54,11 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
item.a.replace(/\n/g, '\\n') item.a.replace(/\n/g, '\\n')
]); ]);
// update export time
await User.findByIdAndUpdate(userId, {
'limit.exportKbTime': new Date()
});
jsonRes(res, { jsonRes(res, {
data data
}); });

View File

@@ -131,6 +131,10 @@ const DataCard = ({ kbId }: { kbId: string }) => {
type: 'text/csv', type: 'text/csv',
filename: 'data.csv' filename: 'data.csv'
}); });
toast({
title: '导出成功,下次导出需要半小时后',
status: 'success'
});
} catch (error) { } catch (error) {
error; error;
} }
@@ -164,10 +168,10 @@ const DataCard = ({ kbId }: { kbId: string }) => {
mr={2} mr={2}
size={'sm'} size={'sm'}
isLoading={isLoadingExport} isLoading={isLoadingExport}
title={'换行数据导出时,会进行格式转换'} title={'半小时仅能导出1次'}
onClick={() => onclickExport()} onClick={() => onclickExport()}
> >
csv
</Button> </Button>
<Menu autoSelect={false}> <Menu autoSelect={false}>
<MenuButton as={Button} size={'sm'}> <MenuButton as={Button} size={'sm'}>

View File

@@ -16,6 +16,10 @@ const UserSchema = new Schema({
get: (val: string) => hashPassword(val), get: (val: string) => hashPassword(val),
select: false select: false
}, },
createTime: {
type: Date,
default: () => new Date()
},
avatar: { avatar: {
type: String, type: String,
default: '/icon/human.png' default: '/icon/human.png'
@@ -41,9 +45,11 @@ const UserSchema = new Schema({
type: String, type: String,
default: '' default: ''
}, },
createTime: { limit: {
type: Date, exportKbTime: {
default: () => new Date() // Every half hour
type: Date
}
} }
}); });

View File

@@ -22,6 +22,9 @@ export interface UserModelSchema {
promotion: { promotion: {
rate: number; rate: number;
}; };
limit: {
exportKbTime?: Date;
};
} }
export interface AuthCodeSchema { export interface AuthCodeSchema {