* New pay (#2484)

* remove sub status

* feat: new pay mode

* fix: ts

* limit
This commit is contained in:
Archer
2024-08-26 09:52:09 +08:00
committed by GitHub
parent a4c19fbd0a
commit c1d08c0ccc
17 changed files with 170 additions and 440 deletions

View File

@@ -7,10 +7,9 @@ import { connectionMongo, getMongoModel } from '../../../common/mongo';
const { Schema } = connectionMongo;
import { TeamCollectionName } from '@fastgpt/global/support/user/team/constant';
import {
standardSubLevelMap,
subModeMap,
subStatusMap,
subTypeMap
StandardSubLevelEnum,
SubModeEnum,
SubTypeEnum
} from '@fastgpt/global/support/wallet/sub/constants';
import type { TeamSubSchema } from '@fastgpt/global/support/wallet/sub/type';
@@ -24,12 +23,7 @@ const SubSchema = new Schema({
},
type: {
type: String,
enum: Object.keys(subTypeMap),
required: true
},
status: {
type: String,
enum: Object.keys(subStatusMap),
enum: Object.values(SubTypeEnum),
required: true
},
startTime: {
@@ -40,38 +34,29 @@ const SubSchema = new Schema({
type: Date,
required: true
},
price: {
// last sub pay price(total price)
type: Number,
required: true
},
// standard sub
currentMode: {
type: String,
enum: Object.keys(subModeMap)
enum: Object.values(SubModeEnum)
},
nextMode: {
type: String,
enum: Object.keys(subModeMap)
enum: Object.values(SubModeEnum)
},
currentSubLevel: {
type: String,
enum: Object.keys(standardSubLevelMap)
enum: Object.values(StandardSubLevelEnum)
},
nextSubLevel: {
type: String,
enum: Object.keys(standardSubLevelMap)
enum: Object.values(StandardSubLevelEnum)
},
// stand sub and extra points sub. Plan total points
totalPoints: {
type: Number
},
pointPrice: {
// stand level point total price
type: Number
},
surplusPoints: {
// plan surplus points
type: Number

View File

@@ -1,26 +1,45 @@
import {
StandardSubLevelEnum,
SubModeEnum,
SubStatusEnum,
SubTypeEnum
SubTypeEnum,
standardSubLevelMap
} from '@fastgpt/global/support/wallet/sub/constants';
import { MongoTeamSub } from './schema';
import { FeTeamPlanStatusType } from '@fastgpt/global/support/wallet/sub/type.d';
import { FeTeamPlanStatusType, TeamSubSchema } from '@fastgpt/global/support/wallet/sub/type.d';
import { getVectorCountByTeamId } from '../../../common/vectorStore/controller';
import dayjs from 'dayjs';
import { ClientSession } from '../../../common/mongo';
import { addMonths } from 'date-fns';
import { readFromSecondary } from '../../../common/mongo/utils';
export const getStandardPlans = () => {
export const getStandardPlansConfig = () => {
return global?.subPlans?.standard;
};
export const getStandardPlan = (level: `${StandardSubLevelEnum}`) => {
export const getStandardPlanConfig = (level: `${StandardSubLevelEnum}`) => {
return global.subPlans?.standard?.[level];
};
export const sortStandPlans = (plans: TeamSubSchema[]) => {
return plans.sort(
(a, b) =>
standardSubLevelMap[b.currentSubLevel].weight - standardSubLevelMap[a.currentSubLevel].weight
);
};
export const getTeamStandPlan = async ({ teamId }: { teamId: string }) => {
const plans = await MongoTeamSub.find(
{
teamId,
type: SubTypeEnum.standard
},
undefined,
{
...readFromSecondary
}
);
sortStandPlans(plans);
const standardPlans = global.subPlans?.standard;
const standard = await MongoTeamSub.findOne({ teamId, type: SubTypeEnum.standard }).lean();
const standard = plans[0];
return {
[SubTypeEnum.standard]: standard,
@@ -38,12 +57,11 @@ export const initTeamStandardPlan2Free = async ({
teamId: string;
session?: ClientSession;
}) => {
const freePoints = global?.subPlans?.standard?.free?.totalPoints || 100;
const freePoints = global?.subPlans?.standard?.[StandardSubLevelEnum.free]?.totalPoints || 100;
const teamStandardSub = await MongoTeamSub.findOne({ teamId, type: SubTypeEnum.standard });
if (teamStandardSub) {
teamStandardSub.status = SubStatusEnum.active;
teamStandardSub.currentMode = SubModeEnum.month;
teamStandardSub.nextMode = SubModeEnum.month;
teamStandardSub.startTime = new Date();
@@ -52,9 +70,6 @@ export const initTeamStandardPlan2Free = async ({
teamStandardSub.currentSubLevel = StandardSubLevelEnum.free;
teamStandardSub.nextSubLevel = StandardSubLevelEnum.free;
teamStandardSub.price = 0;
teamStandardSub.pointPrice = 0;
teamStandardSub.totalPoints = freePoints;
teamStandardSub.surplusPoints =
teamStandardSub.surplusPoints && teamStandardSub.surplusPoints < 0
@@ -68,13 +83,10 @@ export const initTeamStandardPlan2Free = async ({
{
teamId,
type: SubTypeEnum.standard,
status: SubStatusEnum.active,
currentMode: SubModeEnum.month,
nextMode: SubModeEnum.month,
startTime: new Date(),
expiredTime: addMonths(new Date(), 1),
price: 0,
pointPrice: 0,
currentSubLevel: StandardSubLevelEnum.free,
nextSubLevel: StandardSubLevelEnum.free,
@@ -94,21 +106,27 @@ export const getTeamPlanStatus = async ({
}): Promise<FeTeamPlanStatusType> => {
const standardPlans = global.subPlans?.standard;
/* Get all plans and datasetSize */
const [plans, usedDatasetSize] = await Promise.all([
MongoTeamSub.find({ teamId }).lean(),
getVectorCountByTeamId(teamId)
]);
const standard = plans.find((plan) => plan.type === SubTypeEnum.standard);
/* Get all standardPlans and active standardPlan */
const teamStandardPlans = sortStandPlans(
plans.filter((plan) => plan.type === SubTypeEnum.standard)
);
const standardPlan = teamStandardPlans[0];
const extraDatasetSize = plans.filter((plan) => plan.type === SubTypeEnum.extraDatasetSize);
const extraPoints = plans.filter((plan) => plan.type === SubTypeEnum.extraPoints);
// Free user, first login after expiration. The free subscription plan will be reset
if (
standard &&
standard.expiredTime &&
standard.currentSubLevel === StandardSubLevelEnum.free &&
dayjs(standard.expiredTime).isBefore(new Date())
standardPlan &&
standardPlan.expiredTime &&
standardPlan.currentSubLevel === StandardSubLevelEnum.free &&
dayjs(standardPlan.expiredTime).isBefore(new Date())
) {
console.log('Init free stand plan', { teamId });
await initTeamStandardPlan2Free({ teamId });
@@ -116,26 +134,26 @@ export const getTeamPlanStatus = async ({
}
const totalPoints = standardPlans
? (standard?.totalPoints || 0) +
? (standardPlan?.totalPoints || 0) +
extraPoints.reduce((acc, cur) => acc + (cur.totalPoints || 0), 0)
: Infinity;
const surplusPoints =
(standard?.surplusPoints || 0) +
(standardPlan?.surplusPoints || 0) +
extraPoints.reduce((acc, cur) => acc + (cur.surplusPoints || 0), 0);
const standardMaxDatasetSize =
standard?.currentSubLevel && standardPlans
? standardPlans[standard.currentSubLevel]?.maxDatasetSize || Infinity
standardPlan?.currentSubLevel && standardPlans
? standardPlans[standardPlan.currentSubLevel]?.maxDatasetSize || Infinity
: Infinity;
const totalDatasetSize =
standardMaxDatasetSize +
extraDatasetSize.reduce((acc, cur) => acc + (cur.currentExtraDatasetSize || 0), 0);
return {
[SubTypeEnum.standard]: standard,
[SubTypeEnum.standard]: standardPlan,
standardConstants:
standard?.currentSubLevel && standardPlans
? standardPlans[standard.currentSubLevel]
standardPlan?.currentSubLevel && standardPlans
? standardPlans[standardPlan.currentSubLevel]
: undefined,
totalPoints,