universo-platform-3d

Форк
0
/
paginated-search-asset.dto.ts 
182 строки · 4.5 Кб
1
import { SORT_DIRECTION } from './../../util/pagination/pagination.interface'
2
import { ApiProperty } from '@nestjs/swagger'
3
import { Transform } from 'class-transformer'
4
import {
5
  IsOptional,
6
  IsString,
7
  IsEnum,
8
  IsArray,
9
  ValidateIf,
10
  IsNotEmpty,
11
  IsIn
12
} from 'class-validator'
13
import { ASSET_TYPE } from '../../option-sets/asset-type'
14
import { PopulateField } from '../../util/pagination/pagination.service'
15
import { IsSortDirection } from '../../util/validators/sort-directions.validator'
16
import { TAG_TYPES } from '../../tag/models/tag-types.enum'
17
import { ApiArrayQuery } from '../../util/decorators/api-array-query.decorator'
18

19
export class PaginatedSearchAssetDto {
20
  @IsOptional()
21
  @IsString()
22
  @ApiProperty()
23
  field: string
24

25
  @IsOptional()
26
  @IsString()
27
  @ApiProperty()
28
  search: string
29

30
  @IsOptional()
31
  @IsString()
32
  @ApiProperty({
33
    description: `Default is updatedAt: desc`
34
  })
35
  sortKey: string
36

37
  @IsOptional()
38
  @ApiProperty({
39
    description: `Default is updatedAt: desc`
40
  })
41
  @IsSortDirection()
42
  sortDirection: SORT_DIRECTION
43

44
  @IsOptional()
45
  @ApiProperty()
46
  page: number
47

48
  @IsOptional()
49
  @ApiProperty()
50
  perPage: number
51

52
  @IsOptional()
53
  @IsString()
54
  @ApiProperty({
55
    required: false
56
  })
57
  startItem: number
58

59
  @IsOptional()
60
  @IsString()
61
  @ApiProperty({ required: false })
62
  numberOfItems: number
63

64
  /**
65
   * @deprecated use assetType instead to line up with MongoDB property. This will be overriden by assetType if both are provided
66
   */
67
  @IsOptional()
68
  @IsString()
69
  @ApiProperty({
70
    description:
71
      'DEPRECATED: use assetType instead to line up with MongoDB property. This will be overriden by assetType if both are provided'
72
  })
73
  type: ASSET_TYPE
74

75
  /**
76
   * @deprecated use assetTypes (plural), below
77
   */
78
  @IsOptional()
79
  @IsEnum(ASSET_TYPE, {
80
    message: `assetType must be one of: ${Object.values(ASSET_TYPE).join(
81
      ', '
82
    )} (case sensitive)`
83
  })
84
  @ApiProperty({
85
    enum: ASSET_TYPE,
86
    description: `
87
    Filter by assetType. Options: ${Object.values(ASSET_TYPE).join(', ')}.
88
      `
89
  })
90
  assetType: ASSET_TYPE
91

92
  @IsOptional()
93
  @IsArray()
94
  @Transform(({ value }) => value?.toString().split(',').map(String)) // This transforms the comma-separated strings into an array of strings
95
  @IsEnum(ASSET_TYPE, {
96
    message: `assetTypes must be one of: ${Object.values(ASSET_TYPE).join(
97
      ', '
98
    )} (case sensitive)`,
99
    each: true
100
  })
101
  @ApiProperty({
102
    enum: ASSET_TYPE,
103
    description: `
104
    Filter by assetType as array. Options: ${Object.values(ASSET_TYPE).join(
105
      ', '
106
    )}.
107
      `
108
  })
109
  assetTypes: ASSET_TYPE[]
110

111
  @IsOptional()
112
  @ApiArrayQuery([String])
113
  @Transform(({ value }) => (Array.isArray(value) ? value : [value]))
114
  tag?: string[]
115

116
  @ValidateIf((o) => o.tag)
117
  @IsNotEmpty()
118
  @IsEnum(TAG_TYPES)
119
  @ApiProperty({ enum: () => TAG_TYPES })
120
  tagType?: TAG_TYPES
121
}
122

123
/**
124
 * @description This adds the populate property
125
 * @date 2023-07-20 07:57
126
 */
127
export class PaginatedSearchAssetDtoV2 extends PaginatedSearchAssetDto {
128
  @IsOptional()
129
  @IsArray()
130
  @Transform(({ value }) => value?.toString().split(',').map(String)) // This transforms the comma-separated strings into an array of strings
131
  @ApiProperty({
132
    description: 'Comma-separated list of fields to populate',
133
    examples: ['creator', 'owner', 'tagsV2', 'creator']
134
  })
135
  populate: string[]
136

137
  @IsOptional()
138
  @ApiProperty({ required: false })
139
  @Transform(({ value }) => value === 'true' || value === true)
140
  includeSoftDeleted?: boolean
141

142
  @IsOptional()
143
  @ApiProperty({ required: false })
144
  @IsIn(['1', '0', '-1'])
145
  mirrorAssetManagerUserSortKey?: string
146

147
  @IsOptional()
148
  @ApiProperty({ required: false, default: false })
149
  includeAssetPackAssets = false
150
}
151

152
export function getPopulateFieldsFromPaginatedSearchAssetDto(searchAssetDto) {
153
  const populateFields: PopulateField[] = []
154

155
  searchAssetDto?.populate?.includes('creator') &&
156
    populateFields.push({
157
      localField: 'creator', // TODO: filter out properties for user so that not all are passed back
158
      from: 'users',
159
      unwind: true,
160
      // use this to filter properties
161
      project: {
162
        displayName: 1
163
      }
164
    })
165
  searchAssetDto?.populate?.includes('owner') &&
166
    populateFields.push({
167
      localField: 'owner',
168
      from: 'users',
169
      unwind: true,
170
      // use this to filter properties
171
      project: {
172
        displayName: 1
173
      }
174
    })
175
  searchAssetDto?.populate?.includes('customData') &&
176
    populateFields.push({
177
      localField: 'customData',
178
      from: 'customdatas',
179
      unwind: true
180
    })
181
  return populateFields
182
}
183

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

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

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

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