Skip to content

Commit

Permalink
'.'
Browse files Browse the repository at this point in the history
  • Loading branch information
ManucherKM committed Oct 1, 2023
1 parent 8213460 commit fb218e3
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 71 deletions.
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Module } from '@nestjs/common'
import { ConfigModule } from '@nestjs/config'
import { MongooseModule } from '@nestjs/mongoose'
import { ActivationModule } from './activation/activation.module'
import { ArchiveModule } from './archive/archive.module'
import { AuthModule } from './auth/auth.module'
import { FileModule } from './file/file.module'

Expand All @@ -23,6 +24,7 @@ import { FileModule } from './file/file.module'
AuthModule,
ActivationModule,
FileModule,
ArchiveModule,
],
})
export class AppModule {}
35 changes: 35 additions & 0 deletions src/archive/archive.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { JwtAuthGuard } from '@/guard/jwt-auth.guard'
import { Body, Controller, Post, UseGuards } from '@nestjs/common'
import { ApiBearerAuth, ApiBody, ApiTags } from '@nestjs/swagger'
import { ArchiveService } from './archive.service'
import { CreateArchiveDto } from './dto/create-archive.dto'

@ApiTags('Archive')
@Controller('archive')
export class ArchiveController {
constructor(private readonly archiveService: ArchiveService) {}

@ApiBody({
schema: {
type: 'object',
properties: {
userId: {
default: 'YOUR_ID',
},
fileIds: {
type: 'array',
items: {
type: 'string',
},
default: ['YOUR_FILE_ID'],
},
},
},
})
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
@Post()
create(@Body() createArchiveDto: CreateArchiveDto) {
return this.archiveService.create(createArchiveDto)
}
}
18 changes: 18 additions & 0 deletions src/archive/archive.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { FileModule } from '@/file/file.module'
import { JwtModule } from '@/jwt/jwt.module'
import { Module } from '@nestjs/common'
import { MongooseModule } from '@nestjs/mongoose'
import { ArchiveController } from './archive.controller'
import { ArchiveService } from './archive.service'
import { Archive, ArchiveSchema } from './entities/archive.entity'

@Module({
imports: [
MongooseModule.forFeature([{ name: Archive.name, schema: ArchiveSchema }]),
FileModule,
JwtModule,
],
controllers: [ArchiveController],
providers: [ArchiveService],
})
export class ArchiveModule {}
49 changes: 49 additions & 0 deletions src/archive/archive.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { FileService } from '@/file/file.service'
import { BadRequestException, Injectable } from '@nestjs/common'
import { InjectModel } from '@nestjs/mongoose'
import { Model } from 'mongoose'
import { CreateArchiveDto } from './dto/create-archive.dto'
import { Archive } from './entities/archive.entity'

@Injectable()
export class ArchiveService {
constructor(
@InjectModel(Archive.name)
private readonly archiveModel: Model<Archive>,
private readonly fileService: FileService,
) {}

async create({ userId, fileIds }: CreateArchiveDto) {
const isFilesExist = await this.isFilesExist(fileIds)

if (!isFilesExist) {
throw new BadRequestException('The file could not be found.')
}

const createdArchive = await this.archiveModel.create({
userId,
fileIds,
})

return { id: createdArchive._id }
}

private async isFilesExist(fileIds: string[]) {
const filePromises = []

for (const id of fileIds) {
const filePromise = this.fileService.findById(id)
filePromises.push(filePromise)
}

const files = await Promise.all(filePromises)

const isFileNotFound = files.some(file => file === null)

return !isFileNotFound
}

async findByUserId(userId: string) {
return await this.archiveModel.findOne({ userId })
}
}
10 changes: 10 additions & 0 deletions src/archive/dto/create-archive.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { IsNotEmpty, IsString } from 'class-validator'

export class CreateArchiveDto {
@IsString()
@IsNotEmpty()
userId: string

@IsString({ each: true })
fileIds: string[]
}
4 changes: 4 additions & 0 deletions src/archive/dto/update-archive.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { PartialType } from '@nestjs/swagger'
import { CreateArchiveDto } from './create-archive.dto'

export class UpdateArchiveDto extends PartialType(CreateArchiveDto) {}
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'
import { HydratedDocument, SchemaTypes } from 'mongoose'

export type FileDocument = HydratedDocument<FileArchive>
export type ArchiveDocument = HydratedDocument<Archive>

@Schema({
timestamps: true,
})
export class FileArchive {
export class Archive {
@Prop({ required: true, type: SchemaTypes.ObjectId, ref: 'User' })
userId: string

@Prop({ required: true })
fileIds: string[]
}

export const FileArchiveSchema = SchemaFactory.createForClass(FileArchive)
export const ArchiveSchema = SchemaFactory.createForClass(Archive)
8 changes: 0 additions & 8 deletions src/file/dto/share-file.dto.ts

This file was deleted.

16 changes: 0 additions & 16 deletions src/file/file.controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { GetUserIdByToken } from '@/decorators/GetUserIdByToken'
import { JwtAuthGuard } from '@/guard/jwt-auth.guard'
import {
Body,
Controller,
Delete,
Get,
Expand All @@ -15,7 +14,6 @@ import {
} from '@nestjs/common'
import { FileInterceptor } from '@nestjs/platform-express'
import { ApiBearerAuth, ApiBody, ApiConsumes, ApiTags } from '@nestjs/swagger'
import { ShareFileDto } from './dto/share-file.dto'
import { FileService } from './file.service'
import { fileStorage } from './storage'

Expand Down Expand Up @@ -103,18 +101,4 @@ export class FileController {
throw new HttpException({ message: e.message }, HttpStatus.BAD_REQUEST)
}
}

@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
@Post('fileArchive')
async shareFiles(
@GetUserIdByToken() userId: string,
@Body() shareFileDto: ShareFileDto,
) {
try {
return await this.fileService.shareFiles(userId, shareFileDto)
} catch (e) {
throw new HttpException({ message: e.message }, HttpStatus.BAD_REQUEST)
}
}
}
5 changes: 1 addition & 4 deletions src/file/file.module.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
import { JwtModule } from '@/jwt/jwt.module'
import { Module } from '@nestjs/common'
import { MongooseModule } from '@nestjs/mongoose'
import { FileArchive, FileArchiveSchema } from './entities/file-archive.entity'
import { File, FileSchema } from './entities/file.entity'
import { FileController } from './file.controller'
import { FileService } from './file.service'

@Module({
imports: [
MongooseModule.forFeature([{ name: File.name, schema: FileSchema }]),
MongooseModule.forFeature([
{ name: FileArchive.name, schema: FileArchiveSchema },
]),
JwtModule,
],
controllers: [FileController],
providers: [FileService],
exports: [FileService],
})
export class FileModule {}
41 changes: 1 addition & 40 deletions src/file/file.service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { BadRequestException, Injectable } from '@nestjs/common'
import { InjectModel } from '@nestjs/mongoose'
import { promises } from 'fs'
import { Model, Types } from 'mongoose'
import { SendFileDto } from './dto/send-file.dto'
import { ShareFileDto } from './dto/share-file.dto'
import { FileArchive } from './entities/file-archive.entity'
import { File } from './entities/file.entity'

export type TFileModel = File & {
Expand All @@ -17,8 +14,6 @@ export type TFileModel = File & {
export class FileService {
constructor(
@InjectModel(File.name) private readonly fileModel: Model<File>,
@InjectModel(FileArchive.name)
private readonly fileArchiveModel: Model<FileArchive>,
) {}

async create(userId: string, file: Express.Multer.File) {
Expand Down Expand Up @@ -48,7 +43,7 @@ export class FileService {
return res.map(item => this.formatFileModel(item.toObject()))
}

async findById(id: string | Types.ObjectId) {
async findById(id: string) {
return await this.fileModel.findById({ _id: id })
}

Expand Down Expand Up @@ -100,38 +95,4 @@ export class FileService {

return await foundFile.save()
}

async isFilesExist(fileIds: string | Types.ObjectId[]) {
const filePromises: Promise<TFileModel>[] = []

for (const id of fileIds) {
const filePromise = this.findById(id)
filePromises.push(filePromise)
}

const files = await Promise.all(filePromises)

const isFileNotFound = files.some(file => file === null)

return !isFileNotFound
}

async shareFiles(userId: string, shareFileDto: ShareFileDto) {
const { fileIds } = shareFileDto

const isExist = this.isFilesExist(fileIds)

if (!isExist) {
throw new BadRequestException('The file could not be found.')
}

const createdArchiveFiles = await this.fileArchiveModel.create({
userId,
fileIds,
})

return {
archiveId: createdArchiveFiles._id,
}
}
}

0 comments on commit fb218e3

Please sign in to comment.