Skip to content
This repository has been archived by the owner on Dec 2, 2023. It is now read-only.

Commit

Permalink
add field options, rating and logic jump
Browse files Browse the repository at this point in the history
  • Loading branch information
wodka committed Jun 2, 2020
1 parent 5f8e9bc commit 8683a10
Show file tree
Hide file tree
Showing 12 changed files with 185 additions and 10 deletions.
13 changes: 13 additions & 0 deletions src/dto/form/form.field.input.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { Field, ID, InputType } from '@nestjs/graphql';
import { FormFieldOptionInput } from './form.field.option.input';
import { FormFieldRatingInput } from './form.field.rating.input';
import { LogicJumpInput } from './logic.jump.input';
import { LogicJumpModel } from './logic.jump.model';

@InputType()
export class FormFieldInput {
Expand All @@ -19,4 +23,13 @@ export class FormFieldInput {

@Field()
readonly value: string

@Field(() => [FormFieldOptionInput], { nullable: true })
readonly options: [FormFieldOptionInput]

@Field(() => LogicJumpInput, { nullable: true })
readonly logicJump: LogicJumpModel

@Field(() => FormFieldRatingInput, { nullable: true })
readonly rating: FormFieldRatingInput
}
15 changes: 15 additions & 0 deletions src/dto/form/form.field.model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { Field, ID, ObjectType } from '@nestjs/graphql';
import { FormFieldDocument } from '../../schema/form.field.schema';
import { FormFieldOptionModel } from './form.field.option.model';
import { FormFieldRatingModel } from './form.field.rating.model';
import { LogicJumpModel } from './logic.jump.model';

@ObjectType('FormField')
export class FormFieldModel {
Expand All @@ -21,12 +24,24 @@ export class FormFieldModel {
@Field()
readonly value: string

@Field(() => [FormFieldOptionModel])
readonly options: [FormFieldOptionModel]

@Field(() => LogicJumpModel)
readonly logicJump: LogicJumpModel

@Field(() => FormFieldRatingModel, { nullable: true })
readonly rating: FormFieldRatingModel

constructor(document: FormFieldDocument) {
this.id = document.id
this.title = document.title
this.type = document.type
this.description = document.description
this.required = document.required
this.value = document.value
this.options = document.options ? document.options.map(option => new FormFieldOptionModel(option)) : []
this.logicJump = new LogicJumpModel(document.logicJump)
this.rating = document.rating ? new FormFieldRatingModel(document.rating) : null
}
}
13 changes: 13 additions & 0 deletions src/dto/form/form.field.option.input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Field, InputType } from '@nestjs/graphql';

@InputType()
export class FormFieldOptionInput {
@Field({ nullable: true })
readonly key: string

@Field({ nullable: true })
readonly title: string

@Field()
readonly value: string
}
20 changes: 20 additions & 0 deletions src/dto/form/form.field.option.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Field, ObjectType } from '@nestjs/graphql';
import { FieldOptionDocument } from '../../schema/embedded/field.option';

@ObjectType('FormFieldOption')
export class FormFieldOptionModel {
@Field({ nullable: true })
readonly key: string

@Field({ nullable: true })
readonly title: string

@Field()
readonly value: string

constructor(option: FieldOptionDocument) {
this.key = option.key
this.title = option.title
this.value = option.value
}
}
11 changes: 11 additions & 0 deletions src/dto/form/form.field.rating.input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Field, InputType } from '@nestjs/graphql';
import { GraphQLInt } from 'graphql';

@InputType()
export class FormFieldRatingInput {
@Field(() => GraphQLInt, { nullable: true })
readonly steps: number

@Field({ nullable: true })
readonly shape: string
}
17 changes: 17 additions & 0 deletions src/dto/form/form.field.rating.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Field, ObjectType } from '@nestjs/graphql';
import { GraphQLInt } from 'graphql';
import { RatingFieldDocument } from '../../schema/embedded/rating.field';

@ObjectType('FormFieldRating')
export class FormFieldRatingModel {
@Field(() => GraphQLInt, { nullable: true })
readonly steps: number

@Field({ nullable: true })
readonly shape: string

constructor(option: RatingFieldDocument) {
this.steps = option.steps
this.shape = option.shape
}
}
19 changes: 19 additions & 0 deletions src/dto/form/logic.jump.input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Field, ID, InputType } from '@nestjs/graphql';

@InputType()
export class LogicJumpInput {
@Field(() => ID, { nullable: true })
readonly fieldA: string

@Field({ nullable: true })
readonly valueB: string

@Field({ nullable: true })
readonly expressionString: string

@Field(() => ID, { nullable: true })
readonly jumpTo: string

@Field({ nullable: true })
readonly enabled: boolean
}
33 changes: 33 additions & 0 deletions src/dto/form/logic.jump.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Field, ID, ObjectType } from '@nestjs/graphql';
import { LogicJumpDocument } from '../../schema/embedded/logic.jump';

@ObjectType('LogicJump')
export class LogicJumpModel {
@Field(() => ID, { nullable: true })
readonly fieldA: string

@Field({ nullable: true })
readonly valueB: string

@Field({ nullable: true })
readonly expressionString: string

@Field(() => ID, { nullable: true })
readonly jumpTo: string

@Field()
readonly enabled: boolean

constructor(document: LogicJumpDocument) {
if (!document) {
this.enabled = false
return
}

this.fieldA = document.fieldA
this.valueB = document.valueB
this.expressionString = document.expressionString
this.jumpTo = document.jumpTo
this.enabled = !!document.enabled
}
}
25 changes: 17 additions & 8 deletions src/schema/embedded/field.option.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
import { SchemaDefinition } from 'mongoose';
import { Document, SchemaDefinition } from 'mongoose';

export interface FieldOptionDocument extends Document {
readonly key?: string
readonly title?: string
readonly value: string
}

export const FieldOption: SchemaDefinition = {
id: {
alias: 'option_id',
type: Number,
// eslint-disable-next-line @typescript-eslint/camelcase
option_id: {
alias: 'key',
type: String,
},
title: {
alias: 'option_title',
// eslint-disable-next-line @typescript-eslint/camelcase
option_title: {
alias: 'title',
type: String,
},
value: {
alias: 'option_value',
// eslint-disable-next-line @typescript-eslint/camelcase
option_value: {
alias: 'value',
type: String,
trim: true,
},
Expand Down
10 changes: 9 additions & 1 deletion src/schema/embedded/logic.jump.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { Schema, SchemaDefinition } from 'mongoose';
import { Document, Schema, SchemaDefinition } from 'mongoose';
import { FormFieldSchemaName } from '../form.field.schema';

export interface LogicJumpDocument extends Document {
readonly expressionString?: string
readonly fieldA?: string
readonly valueB?: string
readonly jumpTo?: string
readonly enabled?: boolean
}

export const LogicJump: SchemaDefinition = {
expressionString: {
type: String,
Expand Down
7 changes: 6 additions & 1 deletion src/schema/embedded/rating.field.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { SchemaDefinition } from 'mongoose';
import { Document, SchemaDefinition } from 'mongoose';

export interface RatingFieldDocument extends Document {
readonly steps?: number
readonly shape?: string
}

export const RatingField: SchemaDefinition = {
steps: {
Expand Down
12 changes: 12 additions & 0 deletions src/service/form/form.update.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ export class FormUpdateService {
field.set('required', nextField.required)
field.set('value', nextField.value)

if (nextField.logicJump !== undefined) {
field.set('logicJump', nextField.logicJump)
}

if (nextField.options !== undefined) {
field.set('options', nextField.options)
}

if (nextField.rating !== undefined) {
field.set('rating', nextField.rating)
}

return field
}))

Expand Down

0 comments on commit 8683a10

Please sign in to comment.