-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
23 changed files
with
391 additions
and
38 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
46 changes: 46 additions & 0 deletions
46
apps/api/src/app/template/dtos/duplicate-template-request.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,46 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { IsDefined, IsString, IsOptional, IsBoolean } from 'class-validator'; | ||
|
||
export class DuplicateTemplateRequestDto { | ||
@ApiProperty({ | ||
description: 'Name of the template', | ||
}) | ||
@IsDefined() | ||
@IsString() | ||
name: string; | ||
|
||
@ApiProperty({ | ||
description: '_projectId where the template will be created', | ||
}) | ||
@IsString() | ||
@IsDefined() | ||
_projectId: string; | ||
|
||
@ApiProperty({ | ||
description: 'Whether to duplicate columns?', | ||
}) | ||
@IsBoolean() | ||
@IsOptional() | ||
duplicateColumns?: boolean; | ||
|
||
@ApiProperty({ | ||
description: 'Whether to duplicate output?', | ||
}) | ||
@IsBoolean() | ||
@IsOptional() | ||
duplicateOutput?: boolean; | ||
|
||
@ApiProperty({ | ||
description: 'Whether to duplicate webhook?', | ||
}) | ||
@IsBoolean() | ||
@IsOptional() | ||
duplicateWebhook?: boolean; | ||
|
||
@ApiProperty({ | ||
description: 'Whether to duplicate validation code?', | ||
}) | ||
@IsBoolean() | ||
@IsOptional() | ||
duplicateValidator?: boolean; | ||
} |
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
28 changes: 28 additions & 0 deletions
28
apps/api/src/app/template/usecases/duplicate-template/duplicate-template.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,28 @@ | ||
import { IsBoolean, IsDefined, IsOptional, IsString } from 'class-validator'; | ||
import { BaseCommand } from '@shared/commands/base.command'; | ||
|
||
export class DuplicateTemplateCommand extends BaseCommand { | ||
@IsDefined() | ||
@IsString() | ||
name: string; | ||
|
||
@IsString() | ||
@IsDefined() | ||
_projectId: string; | ||
|
||
@IsBoolean() | ||
@IsOptional() | ||
duplicateColumns?: boolean; | ||
|
||
@IsBoolean() | ||
@IsOptional() | ||
duplicateOutput?: boolean; | ||
|
||
@IsBoolean() | ||
@IsOptional() | ||
duplicateWebhook?: boolean; | ||
|
||
@IsBoolean() | ||
@IsOptional() | ||
duplicateValidator?: boolean; | ||
} |
79 changes: 79 additions & 0 deletions
79
apps/api/src/app/template/usecases/duplicate-template/duplicate-template.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,79 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { TemplateRepository, CustomizationRepository, ColumnRepository, ValidatorRepository } from '@impler/dal'; | ||
import { validateNotFound } from '@shared/helpers/common.helper'; | ||
import { DuplicateTemplateCommand } from './duplicate-template.command'; | ||
import { SaveSampleFile } from '@shared/usecases/save-sample-file/save-sample-file.usecase'; | ||
|
||
@Injectable() | ||
export class DuplicateTemplate { | ||
constructor( | ||
private saveSampleFile: SaveSampleFile, | ||
private columnRepository: ColumnRepository, | ||
private templateRepository: TemplateRepository, | ||
private validatorRepository: ValidatorRepository, | ||
private customizationRepository: CustomizationRepository | ||
) {} | ||
|
||
async execute(_templateId: string, command: DuplicateTemplateCommand) { | ||
const templateData = await this.templateRepository.findById( | ||
_templateId, | ||
'name callbackUrl chunkSize authHeaderName chunkSize' | ||
); | ||
|
||
validateNotFound(templateData, 'template'); | ||
|
||
const newTemplate = await this.templateRepository.create({ | ||
name: command.name, | ||
_projectId: command._projectId, | ||
chunkSize: templateData.chunkSize, | ||
...(command.duplicateWebhook && { | ||
callbackUrl: templateData.callbackUrl, | ||
authHeaderName: templateData.authHeaderName, | ||
}), | ||
}); | ||
|
||
if (command.duplicateColumns) { | ||
const columns = await this.columnRepository.find( | ||
{ | ||
_templateId, | ||
}, | ||
'-_id name key alternateKeys isRequired isUnique type regex regexDescription selectValues dateFormats sequence defaultValue' | ||
); | ||
|
||
await this.columnRepository.createMany( | ||
columns.map((column) => { | ||
return { | ||
...column, | ||
_templateId: newTemplate._id, | ||
}; | ||
}) | ||
); | ||
|
||
await this.saveSampleFile.execute(columns, newTemplate._id); | ||
} | ||
|
||
if (command.duplicateOutput) { | ||
const validation = await this.customizationRepository.findOne( | ||
{ | ||
_templateId, | ||
}, | ||
'-_id recordVariables chunkVariables recordFormat chunkFormat combinedFormat isRecordFormatUpdated isChunkFormatUpdated isCombinedFormatUpdated' | ||
); | ||
|
||
await this.customizationRepository.create({ | ||
_templateId: newTemplate._id, | ||
...validation, | ||
}); | ||
} | ||
|
||
if (command.duplicateValidator) { | ||
const validator = await this.validatorRepository.findOne({ _templateId }, '-_id onBatchInitialize'); | ||
await this.validatorRepository.create({ | ||
_templateId: newTemplate._id, | ||
...validator, | ||
}); | ||
} | ||
|
||
return newTemplate; | ||
} | ||
} |
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,64 @@ | ||
import { useForm } from 'react-hook-form'; | ||
import { useFocusTrap } from '@mantine/hooks'; | ||
import { SimpleGrid, Stack } from '@mantine/core'; | ||
|
||
import { Input } from '@ui/input'; | ||
import { Button } from '@ui/button'; | ||
import { Select } from '@ui/select'; | ||
import { Checkbox } from '@ui/checkbox'; | ||
|
||
import { IProjectPayload } from '@impler/shared'; | ||
|
||
interface DuplicateImportFormProps { | ||
profile?: IProfileData; | ||
projects?: IProjectPayload[]; | ||
onSubmit: (data: IDuplicateTemplateData) => void; | ||
} | ||
|
||
export function DuplicateImportForm({ onSubmit, profile, projects }: DuplicateImportFormProps) { | ||
const focusTrapRef = useFocusTrap(); | ||
const { | ||
register, | ||
handleSubmit, | ||
formState: { errors }, | ||
} = useForm<IDuplicateTemplateData>({ | ||
defaultValues: { | ||
_projectId: profile?._projectId, | ||
duplicateColumns: true, | ||
duplicateOutput: true, | ||
duplicateWebhook: true, | ||
duplicateValidator: true, | ||
}, | ||
}); | ||
|
||
return ( | ||
<form onSubmit={handleSubmit(onSubmit)} ref={focusTrapRef}> | ||
<Stack spacing="sm"> | ||
<Input | ||
autoFocus | ||
required | ||
label="Import Title" | ||
placeholder="Import title" | ||
register={register('name')} | ||
error={errors.name?.message} | ||
/> | ||
<Select | ||
required | ||
placeholder="Project" | ||
label="Project" | ||
register={register('_projectId')} | ||
data={projects?.map((project) => ({ label: project.name, value: project._id })) || []} | ||
/> | ||
<SimpleGrid cols={3}> | ||
<Checkbox label="Copy Columns?" register={register('duplicateColumns')} /> | ||
<Checkbox label="Copy Output?" register={register('duplicateOutput')} /> | ||
<Checkbox label="Copy Webhook?" register={register('duplicateWebhook')} /> | ||
<Checkbox label="Copy Validator?" register={register('duplicateValidator')} /> | ||
</SimpleGrid> | ||
<Button type="submit" fullWidth> | ||
Duplicate & Continue | ||
</Button> | ||
</Stack> | ||
</form> | ||
); | ||
} |
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
Oops, something went wrong.