mirror of
https://github.com/labring/FastGPT.git
synced 2026-05-03 01:02:15 +08:00
57 lines
1.4 KiB
TypeScript
57 lines
1.4 KiB
TypeScript
import { addDays } from 'date-fns';
|
|
import { connectionMongo, getMongoModel } from '../../../common/mongo';
|
|
const { Schema } = connectionMongo;
|
|
import type { TeamCouponSchemaType } from '@fastgpt/global/support/wallet/sub/coupon/type';
|
|
import { TeamCollectionName } from '@fastgpt/global/support/user/team/constant';
|
|
import { CouponTypeEnum } from '@fastgpt/global/support/wallet/sub/coupon/constants';
|
|
import { getLogger, LogCategories } from '../../../common/logger';
|
|
|
|
export const couponCollectionName = 'team_sub_coupons';
|
|
|
|
const CouponSchema = new Schema({
|
|
key: {
|
|
type: String,
|
|
required: true
|
|
},
|
|
type: {
|
|
type: String,
|
|
enum: Object.values(CouponTypeEnum)
|
|
},
|
|
price: Number,
|
|
paidAmount: Number,
|
|
transactionId: String,
|
|
description: String,
|
|
subscriptions: {
|
|
type: [Object],
|
|
required: true
|
|
},
|
|
createdAt: {
|
|
type: Date,
|
|
default: () => new Date()
|
|
},
|
|
redeemedAt: {
|
|
type: Date,
|
|
default: undefined
|
|
},
|
|
redeemedTeamId: {
|
|
type: Schema.Types.ObjectId,
|
|
ref: TeamCollectionName
|
|
},
|
|
expiredAt: {
|
|
type: Date,
|
|
default: () => addDays(new Date(), 7)
|
|
}
|
|
});
|
|
|
|
try {
|
|
CouponSchema.index({ key: 1 }, { unique: true });
|
|
} catch (error) {
|
|
const logger = getLogger(LogCategories.INFRA.MONGO);
|
|
logger.error('Failed to build coupon indexes', { error });
|
|
}
|
|
|
|
export const MongoTeamCoupon = getMongoModel<TeamCouponSchemaType>(
|
|
couponCollectionName,
|
|
CouponSchema
|
|
);
|