-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Build services/controller for creating form templates #29
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
59c73d0
created DTOs for form templates
kaiyangzheng a71abf8
Merge branch 'main' of https://github.com/sandboxnu/mfa-form-automato…
kaiyangzheng 9c2e457
added dtos for form template
kaiyangzheng ac290f3
Merge branch 'main' of https://github.com/sandboxnu/mfa-form-automato…
kaiyangzheng 5e2e6cd
Added services and routes for form templates
kaiyangzheng c35eef6
removed backend folder
kaiyangzheng 76b6a3f
generated client
kaiyangzheng 78ca18d
Merge branch 'main' of https://github.com/sandboxnu/mfa-form-automato…
kaiyangzheng bf33995
cleanup linting errors
kaiyangzheng 2b1d922
added PrismaService to tests
kaiyangzheng 4ae6635
changes from review
kaiyangzheng 02968fe
remove unuse imports
kaiyangzheng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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: 13 additions & 1 deletion
14
apps/server/src/form-instances/entities/form-instance.entity.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 +1,13 @@ | ||
export class FormInstance {} | ||
import { FormInstance } from '@prisma/client'; | ||
|
||
export class FormInstanceEntity implements FormInstance { | ||
// placeholder | ||
id: string; | ||
name: string; | ||
formDocLink: string; | ||
completed: boolean; | ||
createdAt: Date; | ||
updatedAt: Date; | ||
originatorId: string; | ||
formTemplateId: string; | ||
} |
26 changes: 25 additions & 1 deletion
26
apps/server/src/form-templates/dto/create-form-template.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 |
---|---|---|
@@ -1 +1,25 @@ | ||
export class CreateFormTemplateDto {} | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { | ||
IsNotEmpty, | ||
IsString, | ||
IsUrl, | ||
IsArray, | ||
} from 'class-validator'; | ||
import { SignatureField } from '@prisma/client'; | ||
|
||
export class CreateFormTemplateDto { | ||
@IsString() | ||
@IsNotEmpty() | ||
@ApiProperty() | ||
name: string; | ||
|
||
@IsString() | ||
@IsNotEmpty() | ||
@IsUrl() | ||
@ApiProperty() | ||
formDocLink: string; | ||
|
||
@IsArray() | ||
@ApiProperty() | ||
signatureFields: SignatureField[]; | ||
} |
33 changes: 32 additions & 1 deletion
33
apps/server/src/form-templates/entities/form-template.entity.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 +1,32 @@ | ||
export class FormTemplate {} | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { FormTemplate } from '@prisma/client'; | ||
import { Exclude } from 'class-transformer'; | ||
import { SignatureFieldEntity } from './../../signature-fields/entities/signature-field.entity'; | ||
import { FormInstanceEntity } from './../../form-instances/entities/form-instance.entity'; | ||
|
||
export class FormTemplateEntity implements FormTemplate { | ||
@ApiProperty() | ||
id: string; | ||
|
||
@ApiProperty() | ||
name: string; | ||
|
||
@ApiProperty() | ||
formDocLink: string; | ||
|
||
@Exclude() | ||
createdAt: Date; | ||
|
||
@Exclude() | ||
updatedAt: Date; | ||
|
||
@ApiProperty() | ||
signatureFields: SignatureFieldEntity[]; | ||
|
||
@ApiProperty() | ||
formInstances: FormInstanceEntity[]; | ||
|
||
constructor(partial: Partial<FormTemplateEntity>) { | ||
Object.assign(this, partial); | ||
} | ||
} |
3 changes: 2 additions & 1 deletion
3
apps/server/src/form-templates/form-templates.controller.spec.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
152 changes: 117 additions & 35 deletions
152
apps/server/src/form-templates/form-templates.controller.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,45 +1,127 @@ | ||
import { | ||
Controller, | ||
// Get, | ||
// Post, | ||
// Body, | ||
// Patch, | ||
// Param, | ||
// Delete, | ||
Get, | ||
Post, | ||
Body, | ||
Patch, | ||
Param, | ||
Delete, | ||
NotFoundException, | ||
Query, | ||
} from '@nestjs/common'; | ||
import { FormTemplatesService } from './form-templates.service'; | ||
// import { CreateFormTemplateDto } from './dto/create-form-template.dto'; | ||
// import { UpdateFormTemplateDto } from './dto/update-form-template.dto'; | ||
import { | ||
ApiBadRequestResponse, | ||
ApiCreatedResponse, | ||
ApiForbiddenResponse, | ||
ApiNotFoundResponse, | ||
ApiOkResponse, | ||
ApiTags, | ||
ApiUnprocessableEntityResponse, | ||
} from '@nestjs/swagger'; | ||
import { FormTemplateEntity } from './entities/form-template.entity'; | ||
import { AppErrorMessage } from '../app.errors'; | ||
import { CreateFormTemplateDto } from './dto/create-form-template.dto'; | ||
import { FormTemplateErrorMessage } from './form-templates.errors'; | ||
import { UpdateFormTemplateDto } from './dto/update-form-template.dto'; | ||
import { Prisma } from '@prisma/client'; | ||
|
||
@ApiTags('form-templates') | ||
@Controller('form-templates') | ||
export class FormTemplatesController { | ||
constructor(private readonly formTemplatesService: FormTemplatesService) {} | ||
|
||
// @Post() | ||
// create(@Body() createFormTemplateDto: CreateFormTemplateDto) { | ||
// return this.formTemplatesService.create(createFormTemplateDto); | ||
// } | ||
|
||
// @Get() | ||
// findAll() { | ||
// return this.formTemplatesService.findAll(); | ||
// } | ||
|
||
// @Get(':id') | ||
// findOne(@Param('id') id: string) { | ||
// return this.formTemplatesService.findOne(+id); | ||
// } | ||
|
||
// @Patch(':id') | ||
// update( | ||
// @Param('id') id: string, | ||
// @Body() updateFormTemplateDto: UpdateFormTemplateDto, | ||
// ) { | ||
// return this.formTemplatesService.update(+id, updateFormTemplateDto); | ||
// } | ||
|
||
// @Delete(':id') | ||
// remove(@Param('id') id: string) { | ||
// return this.formTemplatesService.remove(+id); | ||
// } | ||
@Post() | ||
@ApiCreatedResponse({ type: FormTemplateEntity }) | ||
@ApiForbiddenResponse({ description: AppErrorMessage.FORBIDDEN }) | ||
@ApiUnprocessableEntityResponse({ | ||
description: AppErrorMessage.UNPROCESSABLE_ENTITY, | ||
}) | ||
async create(@Body() createFormTemplateDto: CreateFormTemplateDto) { | ||
const newFormTemplate = await this.formTemplatesService.create( | ||
createFormTemplateDto, | ||
); | ||
return new FormTemplateEntity(newFormTemplate); | ||
} | ||
|
||
@Get() | ||
@ApiOkResponse({ type: [FormTemplateEntity] }) | ||
@ApiForbiddenResponse({ description: AppErrorMessage.FORBIDDEN }) | ||
@ApiBadRequestResponse({ description: AppErrorMessage.UNPROCESSABLE_ENTITY }) | ||
async findAll(@Query('limit') limit?: number) { | ||
const formTemplates = await this.formTemplatesService.findAll(limit); | ||
return formTemplates.map( | ||
(formTemplate) => new FormTemplateEntity(formTemplate), | ||
); | ||
} | ||
|
||
@Get(':id') | ||
@ApiOkResponse({ type: FormTemplateEntity }) | ||
@ApiForbiddenResponse({ description: AppErrorMessage.FORBIDDEN }) | ||
@ApiNotFoundResponse({ description: AppErrorMessage.NOT_FOUND }) | ||
@ApiBadRequestResponse({ description: AppErrorMessage.UNPROCESSABLE_ENTITY }) | ||
async findOne(@Param('id') id: string) { | ||
const formTemplate = await this.formTemplatesService.findOne(id); | ||
|
||
if (formTemplate == null) { | ||
console.log(FormTemplateErrorMessage.FORM_TEMPLATE_NOT_FOUND); | ||
throw new NotFoundException( | ||
FormTemplateErrorMessage.FORM_TEMPLATE_NOT_FOUND, | ||
); | ||
} | ||
|
||
return new FormTemplateEntity(formTemplate); | ||
} | ||
|
||
@Patch(':id') | ||
@ApiOkResponse({ type: FormTemplateEntity }) | ||
@ApiForbiddenResponse({ description: AppErrorMessage.FORBIDDEN }) | ||
@ApiNotFoundResponse({ description: AppErrorMessage.NOT_FOUND }) | ||
@ApiUnprocessableEntityResponse({ | ||
description: AppErrorMessage.UNPROCESSABLE_ENTITY, | ||
}) | ||
@ApiBadRequestResponse({ description: AppErrorMessage.UNPROCESSABLE_ENTITY }) | ||
async update( | ||
@Param('id') id: string, | ||
@Body() updateFormTemplateDto: UpdateFormTemplateDto, | ||
) { | ||
try { | ||
const updatedFormTemplate = await this.formTemplatesService.update( | ||
id, | ||
updateFormTemplateDto, | ||
); | ||
return new FormTemplateEntity(updatedFormTemplate); | ||
} catch (e) { | ||
if (e instanceof Prisma.PrismaClientKnownRequestError) { | ||
if (e.code === 'P2025') { | ||
console.log(FormTemplateErrorMessage.FORM_TEMPLATE_NOT_FOUND); | ||
throw new NotFoundException( | ||
FormTemplateErrorMessage.FORM_TEMPLATE_NOT_FOUND, | ||
); | ||
} | ||
} | ||
throw e; | ||
} | ||
} | ||
|
||
@Delete(':id') | ||
@ApiOkResponse() | ||
@ApiForbiddenResponse({ description: AppErrorMessage.FORBIDDEN }) | ||
@ApiNotFoundResponse({ description: AppErrorMessage.NOT_FOUND }) | ||
@ApiBadRequestResponse({ description: AppErrorMessage.UNPROCESSABLE_ENTITY }) | ||
async remove(@Param('id') id: string) { | ||
try { | ||
await this.formTemplatesService.remove(id); | ||
} catch (e) { | ||
if (e instanceof Prisma.PrismaClientKnownRequestError) { | ||
if (e.code === 'P2025') { | ||
console.log(FormTemplateErrorMessage.FORM_TEMPLATE_NOT_FOUND); | ||
throw new NotFoundException( | ||
FormTemplateErrorMessage.FORM_TEMPLATE_NOT_FOUND, | ||
); | ||
} | ||
} | ||
throw e; | ||
} | ||
} | ||
} |
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,4 @@ | ||
export enum FormTemplateErrorMessage { | ||
FORM_TEMPLATE_NOT_FOUND = 'Form template could not be found with this id', | ||
FORM_TEMPLATE_NOT_FOUND_CLIENT = 'Form template could not be found', | ||
} |
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,9 +1,11 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { FormTemplatesService } from './form-templates.service'; | ||
import { FormTemplatesController } from './form-templates.controller'; | ||
import { PrismaModule } from '../prisma/prisma.module'; | ||
|
||
@Module({ | ||
controllers: [FormTemplatesController], | ||
providers: [FormTemplatesService], | ||
imports: [PrismaModule], | ||
}) | ||
export class FormTemplatesModule {} |
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,22 +1,84 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
// import { CreateFormTemplateDto } from './dto/create-form-template.dto'; | ||
// import { UpdateFormTemplateDto } from './dto/update-form-template.dto'; | ||
import { CreateFormTemplateDto } from './dto/create-form-template.dto'; | ||
import { UpdateFormTemplateDto } from './dto/update-form-template.dto'; | ||
import { PrismaService } from '../prisma/prisma.service'; | ||
|
||
@Injectable() | ||
export class FormTemplatesService { | ||
// create(createFormTemplateDto: CreateFormTemplateDto) { | ||
// return 'This action adds a new formTemplate'; | ||
// } | ||
// findAll() { | ||
// return `This action returns all formTemplates`; | ||
// } | ||
// findOne(id: number) { | ||
// return `This action returns a #${id} formTemplate`; | ||
// } | ||
// update(id: number, updateFormTemplateDto: UpdateFormTemplateDto) { | ||
// return `This action updates a #${id} formTemplate`; | ||
// } | ||
// remove(id: number) { | ||
// return `This action removes a #${id} formTemplate`; | ||
// } | ||
constructor(private prisma: PrismaService) {} | ||
|
||
/** | ||
* Create a new form template. | ||
* @param createFormTemplateDto create form template dto | ||
* @returns the created employee, hydrated | ||
*/ | ||
async create(createFormTemplateDto: CreateFormTemplateDto) { | ||
const newFormTemplate = await this.prisma.formTemplate.create({ | ||
data: { | ||
name: createFormTemplateDto.name, | ||
formDocLink: createFormTemplateDto.formDocLink, | ||
// signatureFields: { create: createFormTemplateDto.signatureFields }, | ||
}, | ||
}); | ||
return newFormTemplate; | ||
} | ||
|
||
/** | ||
* Retrieve all form templates. | ||
* @param limit the number of form templates we want to retrieve (optional) | ||
* @returns all form templates, hydrated | ||
*/ | ||
async findAll(limit?: number) { | ||
const formTemplates = await this.prisma.formTemplate.findMany({ | ||
take: limit, | ||
}); | ||
return formTemplates; | ||
} | ||
|
||
/** | ||
* Retrieve a form template by id. | ||
* @param id the form template id | ||
* @returns the selected form template, hydrated | ||
*/ | ||
async findOne(id: string) { | ||
const formTemplate = await this.prisma.formTemplate.findFirstOrThrow({ | ||
where: { | ||
id: id, | ||
}, | ||
}); | ||
|
||
return formTemplate; | ||
} | ||
|
||
/** | ||
* Update a form template. | ||
* @param id the form template id | ||
* @param updateFormTemplateDto update form template dto | ||
* @returns the updated form template, hydrated | ||
*/ | ||
async update(id: string, updateFormTemplateDto: UpdateFormTemplateDto) { | ||
const updatedFormTemplate = await this.prisma.formTemplate.update({ | ||
where: { | ||
id: id, | ||
}, | ||
data: { | ||
name: updateFormTemplateDto.name, | ||
formDocLink: updateFormTemplateDto.formDocLink, | ||
signatureFields: { create: updateFormTemplateDto.signatureFields }, | ||
}, | ||
}); | ||
return updatedFormTemplate; | ||
} | ||
|
||
/** | ||
* Remove a form template. | ||
* @param id the form template id | ||
*/ | ||
async remove(id: string) { | ||
await this.prisma.formTemplate.delete({ | ||
where: { | ||
id: id, | ||
}, | ||
}); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
comment this out for now, we need to think about how updating the signature fields will work