-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from knovator/valid-invalid-data_save
Valid Invalid data save
- Loading branch information
Showing
16 changed files
with
185 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,48 @@ | ||
import { Controller, Get, Param, UseGuards } from '@nestjs/common'; | ||
import { BadRequestException, Controller, Get, Param, UseGuards } from '@nestjs/common'; | ||
import { ApiOperation, ApiTags, ApiSecurity } from '@nestjs/swagger'; | ||
import { FileEntity } from '@impler/dal'; | ||
import { UploadStatusEnum } from '@impler/shared'; | ||
import { APIMessages } from '../shared/constants'; | ||
import { APIKeyGuard } from '../shared/framework/auth.gaurd'; | ||
import { validateUploadStatus } from '../shared/helpers/upload.helpers'; | ||
import { DoReview } from './usecases/do-review/do-review.usecase'; | ||
import { GetUploadInvalidData } from './usecases/get-upload-invalid-data/get-upload-invalid-data.usecase'; | ||
import { SaveReviewData } from './usecases/save-review-data/save-review-data.usecase'; | ||
import { GetFileInvalidData } from './usecases/get-file-invalid-data/get-file-invalid-data.usecase'; | ||
|
||
@Controller('/review') | ||
@ApiTags('Review') | ||
@ApiSecurity('ACCESS_KEY') | ||
@UseGuards(APIKeyGuard) | ||
export class ReviewController { | ||
constructor(private doReview: DoReview) {} | ||
constructor( | ||
private doReview: DoReview, | ||
private saveReviewData: SaveReviewData, | ||
private getFileInvalidData: GetFileInvalidData, | ||
private getUploadInvalidData: GetUploadInvalidData | ||
) {} | ||
|
||
@Get(':uploadId') | ||
@ApiOperation({ | ||
summary: 'Get Review data for uploaded file', | ||
}) | ||
async getReview(@Param('uploadId') uploadId: string) { | ||
return await this.doReview.execute(uploadId); | ||
async getReview(@Param('uploadId') _uploadId: string) { | ||
const uploadData = await this.getUploadInvalidData.execute(_uploadId); | ||
if (!uploadData) throw new BadRequestException(APIMessages.UPLOAD_NOT_FOUND); | ||
|
||
// Only Mapped & Reviewing status are allowed | ||
validateUploadStatus(uploadData.status as UploadStatusEnum, [UploadStatusEnum.MAPPED, UploadStatusEnum.REVIEWING]); | ||
|
||
if (uploadData.status === UploadStatusEnum.MAPPED) { | ||
// uploaded file is mapped, do review | ||
const reviewData = await this.doReview.execute(_uploadId); | ||
// save invalid data to storage | ||
this.saveReviewData.execute(_uploadId, reviewData.invalid, reviewData.valid); | ||
|
||
return reviewData.invalid; | ||
} else { | ||
// Uploaded file is already reviewed, return reviewed data | ||
return this.getFileInvalidData.execute((uploadData._invalidDataFileId as unknown as FileEntity).path); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
apps/api/src/app/review/usecases/get-file-invalid-data/get-file-invalid-data.usecase.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { FileEncodingsEnum } from '@impler/shared'; | ||
import { StorageService } from '../../../shared/storage/storage.service'; | ||
|
||
@Injectable() | ||
export class GetFileInvalidData { | ||
constructor(private storageService: StorageService) {} | ||
|
||
async execute(path: string) { | ||
const stringContent = await this.storageService.getFileContent(path, FileEncodingsEnum.JSON); | ||
|
||
return JSON.parse(stringContent); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
apps/api/src/app/review/usecases/get-upload-invalid-data/get-upload-invalid-data.usecase.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { UploadRepository } from '@impler/dal'; | ||
|
||
@Injectable() | ||
export class GetUploadInvalidData { | ||
constructor(private uploadRepository: UploadRepository) {} | ||
|
||
async execute(_uploadId: string) { | ||
return this.uploadRepository.getUploadInvalidDataInformation(_uploadId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,12 @@ | ||
import { DoReview } from './do-review/do-review.usecase'; | ||
import { SaveReviewData } from './save-review-data/save-review-data.usecase'; | ||
import { GetUploadInvalidData } from './get-upload-invalid-data/get-upload-invalid-data.usecase'; | ||
import { GetFileInvalidData } from './get-file-invalid-data/get-file-invalid-data.usecase'; | ||
|
||
export const USE_CASES = [ | ||
DoReview, | ||
SaveReviewData, | ||
GetFileInvalidData, | ||
GetUploadInvalidData, | ||
// | ||
]; |
58 changes: 58 additions & 0 deletions
58
apps/api/src/app/review/usecases/save-review-data/save-review-data.usecase.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { FileRepository, UploadRepository } from '@impler/dal'; | ||
import { FileMimeTypesEnum } from '@impler/shared'; | ||
import { FileNameService } from '../../../shared/file/name.service'; | ||
import { StorageService } from '../../../shared/storage/storage.service'; | ||
|
||
@Injectable() | ||
export class SaveReviewData { | ||
constructor( | ||
private fileNameService: FileNameService, | ||
private storageService: StorageService, | ||
private fileRepository: FileRepository, | ||
private uploadRepository: UploadRepository | ||
) {} | ||
|
||
async execute(_uploadId: string, invalidData: any[], validData: any[]) { | ||
const _invalidDataFileId = await this.storeInvalidFile(_uploadId, invalidData); | ||
const _validDataFileId = await this.storeValidFile(_uploadId, validData); | ||
await this.uploadRepository.update({ _id: _uploadId }, { _invalidDataFileId, _validDataFileId }); | ||
} | ||
|
||
private async storeValidFile(_uploadId: string, validData: any[]): Promise<string> { | ||
if (validData.length < 1) return null; | ||
|
||
const strinValidData = JSON.stringify(validData); | ||
const validFilePath = this.fileNameService.getValidDataFilePath(_uploadId); | ||
await this.storeFile(validFilePath, strinValidData); | ||
const validFileName = this.fileNameService.getValidDataFileName(); | ||
const entry = await this.makeFileEntry(validFileName, validFilePath); | ||
|
||
return entry._id; | ||
} | ||
|
||
private async storeInvalidFile(_uploadId: string, invalidData: any[]): Promise<string> { | ||
if (invalidData.length < 1) return null; | ||
|
||
const stringInvalidData = JSON.stringify(invalidData); | ||
const invalidFilePath = this.fileNameService.getInvalidDataFilePath(_uploadId); | ||
await this.storeFile(invalidFilePath, stringInvalidData); | ||
const invalidFileName = this.fileNameService.getInvalidDataFileName(); | ||
const entry = await this.makeFileEntry(invalidFileName, invalidFilePath); | ||
|
||
return entry._id; | ||
} | ||
|
||
private async storeFile(invalidFilePath: string, data: string) { | ||
await this.storageService.uploadFile(invalidFilePath, data, FileMimeTypesEnum.JSON); | ||
} | ||
|
||
private async makeFileEntry(fileName: string, filePath: string) { | ||
return await this.fileRepository.create({ | ||
mimeType: FileMimeTypesEnum.JSON, | ||
name: fileName, | ||
originalName: fileName, | ||
path: filePath, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { UploadStatusEnum } from '@impler/shared'; | ||
import { BadRequestException } from '@nestjs/common'; | ||
import { APIMessages } from '../constants'; | ||
|
||
export function validateUploadStatus(currentStatus: UploadStatusEnum, expectedStatus: UploadStatusEnum[]): boolean { | ||
if (expectedStatus.includes(currentStatus)) return true; | ||
else { | ||
if (currentStatus === UploadStatusEnum.UPLOADED) { | ||
throw new BadRequestException(APIMessages.DO_MAPPING_FIRST); | ||
} else if (currentStatus === UploadStatusEnum.MAPPED) { | ||
throw new BadRequestException(APIMessages.DO_REVIEW_FIRST); | ||
} else if (currentStatus === UploadStatusEnum.REVIEWED) { | ||
throw new BadRequestException(APIMessages.DO_CONFIRM_FIRST); | ||
} else if (currentStatus === UploadStatusEnum.CONFIRMED) { | ||
throw new BadRequestException(APIMessages.ALREADY_CONFIRMED); | ||
} | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
apps/api/src/app/shared/validations/valid-import-file.validation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters