返回 AiToEarn
oauth2-credential.schema.ts
根目录 / project / aitoearn-backend / libs / mongodb / src / schemas / oauth2-credential.schema.ts
1 import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'
2 import { AccountType } from '@yikart/common'
3 import { Schema as MongooseSchema } from 'mongoose'
4 import { DEFAULT_SCHEMA_OPTIONS } from '../mongodb.constants'
5 import { WithTimestampSchema } from './timestamp.schema'
6
7 @Schema({ ...DEFAULT_SCHEMA_OPTIONS, collection: 'oauth2Credential' })
8 export class OAuth2Credential extends WithTimestampSchema {
9 id: string
10
11 @Prop({
12 required: true,
13 type: String,
14 })
15 accountId: string
16
17 @Prop({
18 required: true,
19 enum: AccountType,
20 })
21 platform: AccountType
22
23 @Prop({
24 required: true,
25 type: String,
26 default: '',
27 })
28 accessToken: string
29
30 @Prop({
31 required: false,
32 type: String,
33 })
34 refreshToken: string
35
36 @Prop({
37 required: false,
38 type: Number,
39 })
40 accessTokenExpiresAt?: number
41
42 @Prop({
43 required: false,
44 type: Number,
45 })
46 refreshTokenExpiresAt?: number
47
48 @Prop({
49 required: false,
50 type: String,
51 })
52 scope?: string
53
54 @Prop({
55 required: false,
56 type: MongooseSchema.Types.Mixed,
57 })
58 raw?: unknown
59
60 get isExpired() {
61 const now = Math.floor(Date.now() / 1000)
62 if (this.refreshTokenExpiresAt) {
63 return this.refreshTokenExpiresAt <= now
64 }
65 return this.accessTokenExpiresAt ? this.accessTokenExpiresAt <= now : false
66 }
67 }
68
69 export const OAuth2CredentialSchema = SchemaFactory.createForClass(OAuth2Credential)
70 OAuth2CredentialSchema.index(
71 { accountId: 1 },
72 {
73 unique: true,
74 name: 'accountId_1',
75 },
76 )
77
77 lines TYPESCRIPT