Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

new skeleton of the extension update route

Yevhenii Kulisidi321479f64d5c
diff --git a/package.json b/package.json
index f290c4d46..10e0db1d3 100644
--- a/package.json
+++ b/package.json
@@ -29,6 +29,7 @@
"@nestjs/config": "^2.2.0",
"@nestjs/core": "^9.2.1",
"@nestjs/jwt": "^10.0.1",
+ "@nestjs/mapped-types": "^1.2.2",
"@nestjs/passport": "^9.0.0",
"@nestjs/platform-express": "^9.2.1",
"@nestjs/schedule": "^2.1.0",
diff --git a/src/marketplace/extensions/dtos/create-extension.dto.ts b/src/marketplace/extensions/dtos/create-extension.dto.ts
index 7f4d86a80..c376d6a30 100644
--- a/src/marketplace/extensions/dtos/create-extension.dto.ts
+++ b/src/marketplace/extensions/dtos/create-extension.dto.ts
@@ -1,4 +1,5 @@
import {
+ IsEnum,
IsNotEmpty,
IsNumberString,
IsOptional,
@@ -40,3 +41,15 @@ export class CreateExtensionBodyDto {
@HasMimeType(['text/javascript', 'application/javascript'])
readonly extensionScript?: Express.Multer.File
}
+
+export enum ExtensionVersionType {
+ MAJOR = 'major',
+ MINOR = 'minor',
+ PATCH = 'patch',
+}
+
+export class AdditionalExtensionInfo {
+ @IsNotEmpty()
+ @IsEnum(ExtensionVersionType)
+ readonly version: ExtensionVersionType
+}
diff --git a/src/marketplace/extensions/dtos/index.ts b/src/marketplace/extensions/dtos/index.ts
index 0335bdfdf..df8608c0d 100644
--- a/src/marketplace/extensions/dtos/index.ts
+++ b/src/marketplace/extensions/dtos/index.ts
@@ -1 +1,2 @@
export * from './create-extension.dto'
+export * from './update-extension.dto'
diff --git a/src/marketplace/extensions/dtos/update-extension.dto.ts b/src/marketplace/extensions/dtos/update-extension.dto.ts
index 1baa179ef..697f4625b 100644
--- a/src/marketplace/extensions/dtos/update-extension.dto.ts
+++ b/src/marketplace/extensions/dtos/update-extension.dto.ts
@@ -1,87 +1,16 @@
-import { ApiProperty } from '@nestjs/swagger'
+import { IntersectionType, PartialType } from '@nestjs/mapped-types'
+import { IsUUID } from 'class-validator'
import {
- IsNumber,
- IsOptional,
- IsString,
- Matches,
- MaxLength,
- MinLength,
-} from 'class-validator'
+ CreateExtensionBodyDto,
+ AdditionalExtensionInfo,
+} from './create-extension.dto'
-export class UpdateExtension {
- @ApiProperty({
- description: 'Extension name',
- example: '', // TODO: Add example
- maxLength: 255,
- minLength: 1,
- type: String,
- })
- @IsString()
- @IsOptional()
- @MinLength(1)
- @MaxLength(255)
- readonly name!: string
-
- @ApiProperty({
- default: null,
- description: 'Extension description',
- example: '', // Add example
- maxLength: 1024,
- minLength: 1,
- nullable: true,
- required: false,
- type: String,
- })
- @IsString()
- @IsOptional()
- @MinLength(1)
- @MaxLength(1024)
- readonly description?: string | null
-
- @ApiProperty({
- default: 'patch',
- description: 'Extension version',
- example: 'major',
- enum: ['major', 'minor', 'patch', ''],
- required: false,
- type: String,
- })
- @IsString()
- @IsOptional()
- @Matches(/^(major|minor|patch|)$/)
- readonly version?: string
-
- @ApiProperty({
- default: 0,
- description: 'Extension price',
- example: 100,
- required: false,
- type: Number,
- })
- @IsNumber()
- @IsOptional()
- readonly price?: number
-
- @ApiProperty({
- default: [],
- description: 'Extension category ID',
- example: 1,
- required: false,
- type: Array<number>,
- })
- @IsOptional()
- readonly categoryID?: number
-
- @ApiProperty({
- default: [],
- description: 'Extension additional images',
- example: [
- 'https://example.com/image1.png',
- 'https://example.com/image2.png',
- ],
- required: false,
- type: Array<string>,
- })
- @IsOptional()
- readonly additionalImagesCdn?: string[]
+export class UpdateExtensionParamsDto {
+ @IsUUID()
+ readonly extensionId: string
}
+
+export class UpdateExtensionBodyDto extends IntersectionType(
+ PartialType(CreateExtensionBodyDto),
+ PartialType(AdditionalExtensionInfo),
+) {}
diff --git a/src/marketplace/extensions/extensions.controller.ts b/src/marketplace/extensions/extensions.controller.ts
index 3520ef9c0..a03804bbe 100644
--- a/src/marketplace/extensions/extensions.controller.ts
+++ b/src/marketplace/extensions/extensions.controller.ts
@@ -5,33 +5,26 @@ import {
Delete,
ForbiddenException,
Get,
- InternalServerErrorException,
Logger,
NotFoundException,
Param,
Patch,
Post,
Query,
- UploadedFiles,
UseGuards,
- UseInterceptors,
UsePipes,
ValidationPipe,
} from '@nestjs/common'
-import { FileFieldsInterceptor } from '@nestjs/platform-express'
import { ApiParam, ApiQuery, ApiTags } from '@nestjs/swagger'
import { getRepository, Like } from 'typeorm'
import * as _map from 'lodash/map'
import * as _size from 'lodash/size'
-import * as _isEmpty from 'lodash/isEmpty'
import { DeleteExtensionParams } from './dtos/delete-extension-params.dto'
import { GetExtensionParams } from './dtos/get-extension-params.dto'
import { GetAllExtensionsQueries } from './dtos/get-all-extensions-queries.dto'
import { ExtensionsService } from './extensions.service'
import { UserService } from '../../user/user.service'
-import { ISaveExtension } from './interfaces/save-extension.interface'
import { UpdateExtensionParams } from './dtos/update-extension-params.dto'
-import { UpdateExtension } from './dtos/update-extension.dto'
import { SearchExtensionQueries } from './dtos/search-extension-queries.dto'
import { CategoriesService } from '../categories/categories.service'
import { SortByExtension } from './enums/sort-by-extension.enum'
@@ -50,7 +43,7 @@ import { UserType } from '../../user/entities/user.entity'
import { ExtensionStatus } from './enums/extension-status.enum'
import { JwtAccessTokenGuard } from 'src/auth/guards'
import { Auth } from 'src/auth/decorators'
-import { CreateExtensionBodyDto } from './dtos'
+import { CreateExtensionBodyDto, UpdateExtensionBodyDto, UpdateExtensionParamsDto } from './dtos'
import { FormDataRequest } from 'nestjs-form-data'
@ApiTags('extensions')
@@ -424,144 +417,15 @@ export class ExtensionsController {
})
}
- @ApiParam({
- name: 'extensionId',
- description: 'Extension ID',
- example: 'de025965-3221-4d09-ba35-a09da59793a6',
- type: String,
- })
- @UseInterceptors(
- FileFieldsInterceptor([
- { name: 'mainImage', maxCount: 1 },
- { name: 'additionalImages', maxCount: 5 },
- { name: 'file', maxCount: 1 },
- ]),
- )
- @UseGuards(JwtAccessTokenGuard, RolesGuard)
- @Roles(UserType.ADMIN, UserType.CUSTOMER)
@Patch(':extensionId')
+ @Auth([UserType.CUSTOMER, UserType.ADMIN])
+ @FormDataRequest()
async updateExtension(
- @Param() params: UpdateExtensionParams,
- @Body() body: UpdateExtension,
+ @Param() params: UpdateExtensionParamsDto,
+ @Body() body: UpdateExtensionBodyDto,
@CurrentUserId() userId: string,
- @UploadedFiles()
- files: {
- mainImage?: Express.Multer.File
- additionalImages?: Express.Multer.File[]
- file?: Express.Multer.File
- },
- ): Promise<ISaveExtension & Extension> {
- this.extensionsService.allowedToManage(userId, params.extensionId)
- const extension = await this.extensionsService.findOne({
- where: {
- id: params.extensionId,
- owner: userId,
- },
- })
-
- if (!extension) {
- throw new NotFoundException('Extension not found.')
- }
-
- const additionalImagesConcat = [
- ...(files.additionalImages || []),
- ...(body.additionalImagesCdn || []),
- ]
-
- const additionalImageFilenames = []
-
- try {
- if (additionalImagesConcat) {
- await Promise.all(
- additionalImagesConcat.map(async additionalImage => {
- if (typeof additionalImage === 'string') {
- additionalImageFilenames.push(additionalImage)
- } else {
- additionalImageFilenames.push(
- (await this.cdnService.uploadFile(additionalImage))?.filename,
- )
- }
- }),
- )
- }
- } catch (e) {
- throw new InternalServerErrorException(
- 'Failed to upload additional images to the CDN.',
- )
- }
-
- let fileURL
- let mainImageURL
- let statusInfo
-
- try {
- fileURL =
- files.file &&
- (await this.cdnService.uploadFile(files.file[0]))?.filename
- } catch (e) {
- throw new InternalServerErrorException(
- 'Failed to upload extension to the CDN.',
- )
- }
-
- try {
- mainImageURL =
- files.mainImage &&
- (await this.cdnService.uploadFile(files.mainImage[0]))?.filename
- } catch (e) {
- throw new InternalServerErrorException(
- 'Failed to upload main image to the CDN.',
- )
- }
-
- let updateVersion
-
- if (body.version && fileURL) {
- switch (body.version) {
- case 'major':
- updateVersion = extension.version.split('.')
- updateVersion[0] = (parseInt(updateVersion[0]) + 1).toString()
- updateVersion[1] = '0'
- updateVersion[2] = '0'
- break
- case 'minor':
- updateVersion = extension.version.split('.')
- updateVersion[1] = (parseInt(updateVersion[1]) + 1).toString()
- updateVersion[2] = '0'
- break
- case 'patch':
- updateVersion = extension.version.split('.')
- updateVersion[2] = (parseInt(updateVersion[2]) + 1).toString()
- break
- default:
- updateVersion = extension.version.split('.')
- updateVersion[2] = (parseInt(updateVersion[2]) + 1).toString()
- }
- updateVersion = updateVersion.join('.')
- }
-
- if (fileURL) {
- statusInfo = ExtensionStatus.PENDING
- }
-
- const extensionInstance = this.extensionsService.create({
- ...extension,
- name: body.name || extension.name,
- description: body.description || extension.description,
- version: updateVersion || extension.version,
- price: body.price || extension.price,
- status: statusInfo,
- mainImage: mainImageURL || extension.mainImage,
- additionalImages: _isEmpty(additionalImageFilenames)
- ? extension.additionalImages
- : additionalImageFilenames,
- fileURL: fileURL || extension.fileURL,
- category: body.categoryID
- ? await this.categoriesService.findById(body.categoryID)
- : extension.category,
- })
-
- return await this.extensionsService.save(extensionInstance)
+ ): Promise<unknown> {
+ return
}
@ApiParam({