-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
123 changed files
with
3,878 additions
and
301 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
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
24 changes: 19 additions & 5 deletions
24
apps/api/src/app/common/usecases/get-import-config/get-import-config.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 |
---|---|---|
@@ -1,20 +1,34 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { UserRepository } from '@impler/dal'; | ||
import { BadRequestException, Injectable } from '@nestjs/common'; | ||
import { UserRepository, TemplateRepository, TemplateEntity } from '@impler/dal'; | ||
import { IImportConfig } from '@impler/shared'; | ||
import { PaymentAPIService } from '@impler/services'; | ||
import { APIMessages } from '@shared/constants'; | ||
|
||
@Injectable() | ||
export class GetImportConfig { | ||
constructor( | ||
private userRepository: UserRepository, | ||
private paymentAPIService: PaymentAPIService | ||
private paymentAPIService: PaymentAPIService, | ||
private templateRepository: TemplateRepository | ||
) {} | ||
|
||
async execute(projectId: string): Promise<IImportConfig> { | ||
async execute(projectId: string, templateId?: string): Promise<IImportConfig> { | ||
const userEmail = await this.userRepository.findUserEmailFromProjectId(projectId); | ||
|
||
const removeBrandingAvailable = await this.paymentAPIService.checkEvent(userEmail, 'REMOVE_BRANDING'); | ||
|
||
return { showBranding: !removeBrandingAvailable }; | ||
let template: TemplateEntity; | ||
if (templateId) { | ||
template = await this.templateRepository.findOne({ | ||
_projectId: projectId, | ||
_id: templateId, | ||
}); | ||
|
||
if (!template) { | ||
throw new BadRequestException(APIMessages.TEMPLATE_NOT_FOUND); | ||
} | ||
} | ||
|
||
return { showBranding: !removeBrandingAvailable, mode: template?.mode, title: template?.name }; | ||
} | ||
} |
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,15 @@ | ||
import { IsNotEmpty, IsOptional, IsString } from 'class-validator'; | ||
|
||
export class CreateUserJobDto { | ||
@IsString() | ||
@IsNotEmpty() | ||
url: string; | ||
|
||
@IsString() | ||
@IsOptional() | ||
externalUserId?: string; | ||
|
||
@IsString() | ||
@IsOptional() | ||
extra?: string; | ||
} |
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,3 @@ | ||
export * from './create-userjob.dto'; | ||
export * from './update-userjob.dto'; | ||
export * from './update-jobmapping.dto'; |
18 changes: 18 additions & 0 deletions
18
apps/api/src/app/import-jobs/dtos/update-jobmapping.dto.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,18 @@ | ||
import { IsBoolean, IsString } from 'class-validator'; | ||
|
||
export class UpdateJobMappingDto { | ||
@IsString() | ||
key: string; | ||
|
||
@IsString() | ||
name: string; | ||
|
||
@IsBoolean() | ||
isRequired: boolean; | ||
|
||
@IsString() | ||
path: string; | ||
|
||
@IsString() | ||
_jobId: string; | ||
} |
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,27 @@ | ||
import { IsArray, IsOptional, IsString } from 'class-validator'; | ||
|
||
export class UpdateJobDto { | ||
@IsString() | ||
@IsOptional() | ||
url: string; | ||
|
||
@IsString() | ||
@IsOptional() | ||
_templateId: string; | ||
|
||
@IsString() | ||
@IsOptional() | ||
cron: string; | ||
|
||
@IsArray() | ||
@IsOptional() | ||
headings: string[]; | ||
|
||
@IsString() | ||
@IsOptional() | ||
status: string; | ||
|
||
@IsString() | ||
@IsOptional() | ||
externalUserId: string; | ||
} |
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,101 @@ | ||
import { ApiTags, ApiSecurity, ApiOperation } from '@nestjs/swagger'; | ||
import { Body, Controller, Delete, Get, Param, ParseArrayPipe, Post, Put, UseGuards } from '@nestjs/common'; | ||
import { | ||
CreateUserJob, | ||
GetColumnSchemaMapping, | ||
CreateJobMapping, | ||
UpdateUserJob, | ||
GetUserJob, | ||
UserJobPause, | ||
UserJobResume, | ||
UserJobTerminate, | ||
UserJobDelete, | ||
} from './usecase'; | ||
import { ACCESS_KEY_NAME } from '@impler/shared'; | ||
import { JwtAuthGuard } from '@shared/framework/auth.gaurd'; | ||
import { UpdateJobDto, CreateUserJobDto, UpdateJobMappingDto } from './dtos'; | ||
|
||
@ApiTags('Import-Jobs') | ||
@Controller('/import-jobs') | ||
@UseGuards(JwtAuthGuard) | ||
@ApiSecurity(ACCESS_KEY_NAME) | ||
export class ImportJobsController { | ||
constructor( | ||
private createUserJob: CreateUserJob, | ||
private updateJobMapping: CreateJobMapping, | ||
private getColumnSchemaMapping: GetColumnSchemaMapping, | ||
private getUserJob: GetUserJob, | ||
private updateUserJob: UpdateUserJob, | ||
private userJobPause: UserJobPause, | ||
private userJobResume: UserJobResume, | ||
private userJobDelete: UserJobDelete, | ||
private userJobTerminate: UserJobTerminate | ||
) {} | ||
|
||
@Post(':templateId') | ||
@ApiOperation({ summary: 'Create User-Job' }) | ||
@ApiSecurity(ACCESS_KEY_NAME) | ||
async createUserJobRoute(@Param('templateId') templateId: string, @Body() createUserJobData: CreateUserJobDto) { | ||
return this.createUserJob.execute({ | ||
_templateId: templateId, | ||
url: createUserJobData.url, | ||
}); | ||
} | ||
|
||
@Get(':jobId/mappings') | ||
@ApiOperation({ summary: 'Fetch the User-Job Mapping Information based on jobId' }) | ||
@UseGuards(JwtAuthGuard) | ||
@ApiSecurity(ACCESS_KEY_NAME) | ||
async getImportJobInfoRoute(@Param('jobId') _jobId: string) { | ||
return this.getColumnSchemaMapping.execute(_jobId); | ||
} | ||
|
||
@Put(':jobId/mappings') | ||
@ApiOperation({ summary: 'Update User-Job Mappings' }) | ||
@UseGuards(JwtAuthGuard) | ||
async updateJobMappingRoute(@Body(new ParseArrayPipe({ items: UpdateJobMappingDto })) body: UpdateJobMappingDto[]) { | ||
return this.updateJobMapping.execute(body); | ||
} | ||
|
||
@Put(':jobId') | ||
@ApiOperation({ summary: 'Update User-Job Fields' }) | ||
@UseGuards(JwtAuthGuard) | ||
async updateUserJobRoute(@Param('jobId') _jobId: string, @Body() userJobData: UpdateJobDto) { | ||
return this.updateUserJob.execute(_jobId, userJobData); | ||
} | ||
|
||
@Get('/user/:externalUserId') | ||
@ApiOperation({ summary: 'Get User Jobs' }) | ||
@UseGuards(JwtAuthGuard) | ||
async getUserJobs(@Param('externalUserId') externalUserId: string) { | ||
return this.getUserJob.execute(externalUserId); | ||
} | ||
|
||
@Put('/user/:jobId/pause') | ||
@ApiOperation({ summary: 'Pause User-Job from Running' }) | ||
@UseGuards(JwtAuthGuard) | ||
async pauseCronJob(@Param('jobId') jobId: string) { | ||
return await this.userJobPause.execute(jobId); | ||
} | ||
|
||
@Put('/user/:jobId/resume') | ||
@ApiOperation({ summary: 'Resume stopped User-Job' }) | ||
@UseGuards(JwtAuthGuard) | ||
async resumeUserJobRoute(@Param('jobId') _jobId: string) { | ||
return await this.userJobResume.execute(_jobId); | ||
} | ||
|
||
@Delete('/user/:externalUserId/:jobId') | ||
@ApiOperation({ summary: 'Delete User-Job' }) | ||
@UseGuards(JwtAuthGuard) | ||
async deleteJob(@Param('externalUserId') externalUserId: string, @Param('jobId') _jobId: string) { | ||
return await this.userJobDelete.execute({ externalUserId, _jobId }); | ||
} | ||
|
||
@Delete(':jobId') | ||
@ApiOperation({ summary: 'Delete User-Job and Update the status of UserJob to TERMINATED' }) | ||
@UseGuards(JwtAuthGuard) | ||
async deleteJobRoute(@Param('jobId') _jobId: string) { | ||
return await this.userJobTerminate.execute(_jobId); | ||
} | ||
} |
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 { Module } from '@nestjs/common'; | ||
import { SharedModule } from '@shared/shared.module'; | ||
import { USECASES } from './usecase'; | ||
import { ImportJobsController } from './import-jobs.controller'; | ||
|
||
@Module({ | ||
imports: [SharedModule], | ||
providers: [...USECASES], | ||
controllers: [ImportJobsController], | ||
}) | ||
export class ImportJobsModule {} |
11 changes: 11 additions & 0 deletions
11
apps/api/src/app/import-jobs/usecase/create-jobmapping/create-jobmapping.command.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 @@ | ||
export class CreateJobMappingCommand { | ||
key: string; | ||
|
||
name: string; | ||
|
||
isRequired: boolean; | ||
|
||
path: string; | ||
|
||
_jobId: string; | ||
} |
20 changes: 20 additions & 0 deletions
20
apps/api/src/app/import-jobs/usecase/create-jobmapping/create-jobmapping.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,20 @@ | ||
import { JobMappingRepository } from '@impler/dal'; | ||
import { BadRequestException, Injectable } from '@nestjs/common'; | ||
import { CreateJobMappingCommand } from './create-jobmapping.command'; | ||
|
||
@Injectable() | ||
export class CreateJobMapping { | ||
constructor(private readonly jobMappingRepository: JobMappingRepository) {} | ||
|
||
async execute(jobMappingCommand: CreateJobMappingCommand[]) { | ||
jobMappingCommand.filter((command) => !!command.key).map((command) => command.key); | ||
|
||
for (const mappingCommand of jobMappingCommand) { | ||
if (mappingCommand.isRequired && !mappingCommand.path) { | ||
throw new BadRequestException(`${mappingCommand.name} is required`); | ||
} | ||
} | ||
|
||
return this.jobMappingRepository.createMany(jobMappingCommand); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
apps/api/src/app/import-jobs/usecase/create-userjob/create-userjob.command.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,6 @@ | ||
export class CreateUserJobCommand { | ||
url: string; | ||
_templateId: string; | ||
externalUserId?: string; | ||
extra?: string; | ||
} |
35 changes: 35 additions & 0 deletions
35
apps/api/src/app/import-jobs/usecase/create-userjob/create-userjob.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,35 @@ | ||
import { BadRequestException, Injectable } from '@nestjs/common'; | ||
import { FileMimeTypesEnum } from '@impler/shared'; | ||
import { APIMessages } from '@shared/constants'; | ||
import { UserJobEntity, UserJobRepository } from '@impler/dal'; | ||
import { RSSService } from '@shared/services'; | ||
import { CreateUserJobCommand } from './create-userjob.command'; | ||
|
||
@Injectable() | ||
export class CreateUserJob { | ||
constructor( | ||
private readonly rssService: RSSService, | ||
private readonly userJobRepository: UserJobRepository | ||
) {} | ||
|
||
async execute({ _templateId, url, externalUserId, extra }: CreateUserJobCommand): Promise<UserJobEntity> { | ||
const mimeType = await this.rssService.getMimeType(url); | ||
if (mimeType === FileMimeTypesEnum.XML || mimeType === FileMimeTypesEnum.TEXTXML) { | ||
const { rssKeyHeading } = await this.rssService.parseRssFeed(url); | ||
let formattedExtra = extra || '{}'; | ||
try { | ||
formattedExtra = JSON.parse(extra); | ||
} catch (_) {} | ||
|
||
return await this.userJobRepository.create({ | ||
url, | ||
headings: rssKeyHeading, | ||
_templateId: _templateId, | ||
extra, | ||
externalUserId: externalUserId || (formattedExtra as unknown as Record<string, any>)?.externalUserId, | ||
}); | ||
} else { | ||
throw new BadRequestException(APIMessages.INVALID_RSS_URL); | ||
} | ||
} | ||
} |
Oops, something went wrong.