universo-platform-3d

Форк
0
/
user-entity-action.schema.ts 
103 строки · 2.6 Кб
1
import * as mongoose from 'mongoose'
2
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'
3
import { ApiProperty } from '@nestjs/swagger'
4
import { User } from '../user.schema'
5

6
export class UserEntityActionPublicData {
7
  @ApiProperty() // @ApiProperty must be included to be exposed by the API and flow to FE codegen
8
  _id = '' // Must not be undefined
9
  @ApiProperty()
10
  createdAt = new Date()
11
  @ApiProperty()
12
  updatedAt = new Date()
13
  @ApiProperty()
14
  actionType = ''
15
  @ApiProperty()
16
  entityType = ''
17
  @ApiProperty()
18
  forEntity = ''
19
  @ApiProperty()
20
  creator = ''
21
  @ApiProperty()
22
  rating = 5
23
}
24

25
export enum ENTITY_TYPE {
26
  USER = 'USER',
27
  SPACE = 'SPACE',
28
  ASSET = 'ASSET'
29
}
30

31
/**
32
* @description Business logic:
33
// Assets: Can be liked
34
// Spaces: Can be rated
35
// User: Can be followed
36
// should we remove save, like? Seems like that functionality could be covered by tags/folders.
37
*/
38
export enum USER_ENTITY_ACTION_TYPE {
39
  LIKE = 'LIKE',
40
  RATING = 'RATING',
41
  SAVE = 'SAVE',
42
  FOLLOW = 'FOLLOW'
43
}
44

45
export type UserEntityActionDocument = UserEntityAction & Document
46

47
/**
48
* @description Likes, ratings, saves, etc. This is a generic class for an action that a user takes on another entity. 
49
I wasn't feeling certain that we want to call it a "like", so this keeps it general.
50
Walkthrough: https://www.loom.com/share/76a6644fd0264ec696281285bcfbd22e
51
* @date 2023-06-14 15:31
52
*/
53
@Schema({
54
  timestamps: true,
55
  toJSON: {
56
    virtuals: true
57
  }
58
})
59
export class UserEntityAction {
60
  @Prop({
61
    required: true,
62
    enum: USER_ENTITY_ACTION_TYPE
63
  })
64
  actionType: USER_ENTITY_ACTION_TYPE
65

66
  @Prop({
67
    required: true,
68
    enum: ENTITY_TYPE
69
  })
70
  @ApiProperty()
71
  entityType: ENTITY_TYPE
72

73
  @Prop({
74
    type: mongoose.Types.ObjectId,
75
    refPath: 'entityType',
76
    required: true
77
  })
78
  @ApiProperty()
79
  forEntity: mongoose.Types.ObjectId
80

81
  @Prop({ type: mongoose.Schema.Types.ObjectId, ref: 'User' })
82
  @ApiProperty()
83
  creator: User
84

85
  @Prop({
86
    required: function () {
87
      return this.actionType === USER_ENTITY_ACTION_TYPE.RATING
88
    },
89
    min: 1,
90
    max: 5
91
  })
92
  @ApiProperty()
93
  rating?: number // for rate actions
94
}
95

96
export const UserEntityActionSchema =
97
  SchemaFactory.createForClass(UserEntityAction)
98

99
// Enforce uniqueness: a user can only take 1 action on an entity with the same actionType (e.g. a user can only like an entity once, BUT could rate it once as well. This allows us flexibility on future business logic, but we for sure know that a user would only be able to like once, rate once, etc.)
100
UserEntityActionSchema.index(
101
  { creator: 1, forEntity: 1, actionType: 1 },
102
  { unique: true }
103
)
104

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.