From 7d14e393b483bc86745e37c654ff7c05171a0490 Mon Sep 17 00:00:00 2001 From: Michael Schramm Date: Fri, 19 Jun 2020 11:34:25 +0200 Subject: [PATCH] add slug and fix missing admin error https://github.com/ohmyform/ohmyform/issues/100 --- CHANGELOG.md | 2 ++ src/config/fields.ts | 1 + src/dto/form/form.field.input.ts | 3 +++ src/dto/form/form.field.model.ts | 4 ++++ src/resolver/form/form.resolver.ts | 6 +++++- src/schema/form.field.schema.ts | 9 ++++++++- src/service/form/form.update.service.ts | 4 ++++ 7 files changed, 27 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0210410..f6cb74a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Added - default index.html for api without bundled ui +- slug for form fields can now be saved ### Changed @@ -18,6 +19,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Fixed - bug in settings resolver with nullable fields +- bug if user was deleted and form still exists ### Security diff --git a/src/config/fields.ts b/src/config/fields.ts index de664b1..0169fb7 100644 --- a/src/config/fields.ts +++ b/src/config/fields.ts @@ -19,6 +19,7 @@ export const matchType = { color: /^#([A-F0-9]{6}|[A-F0-9]{3})$/i, url: /((([A-Z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)/i, email: /.+@.+\..+/, + slug: /^[a-z0-9_]+$/, } export const validatePassword = (password: string): true | string => { diff --git a/src/dto/form/form.field.input.ts b/src/dto/form/form.field.input.ts index 10e8624..6c076ec 100644 --- a/src/dto/form/form.field.input.ts +++ b/src/dto/form/form.field.input.ts @@ -15,6 +15,9 @@ export class FormFieldInput { @Field() readonly type: string + @Field({ nullable: true }) + readonly slug?: string + @Field() readonly description: string diff --git a/src/dto/form/form.field.model.ts b/src/dto/form/form.field.model.ts index 37a5f8f..78346e2 100644 --- a/src/dto/form/form.field.model.ts +++ b/src/dto/form/form.field.model.ts @@ -12,6 +12,9 @@ export class FormFieldModel { @Field() readonly title: string + @Field({ nullable: true }) + readonly slug?: string + @Field() readonly type: string @@ -36,6 +39,7 @@ export class FormFieldModel { constructor(document: FormFieldDocument) { this.id = document.id this.title = document.title + this.slug = document.slug this.type = document.type this.description = document.description this.required = document.required diff --git a/src/resolver/form/form.resolver.ts b/src/resolver/form/form.resolver.ts index 27a1892..67c3131 100644 --- a/src/resolver/form/form.resolver.ts +++ b/src/resolver/form/form.resolver.ts @@ -126,7 +126,7 @@ export class FormResolver { return new PageModel(form.endPage) } - @ResolveField('admin', () => UserModel) + @ResolveField('admin', () => UserModel, { nullable: true }) @Roles('admin') async getAdmin( @Parent() parent: FormModel, @@ -139,6 +139,10 @@ export class FormResolver { await form.execPopulate() } + if (!form.admin) { + return null + } + return new UserModel(form.admin) } } diff --git a/src/schema/form.field.schema.ts b/src/schema/form.field.schema.ts index e443e33..1e023bd 100644 --- a/src/schema/form.field.schema.ts +++ b/src/schema/form.field.schema.ts @@ -1,5 +1,5 @@ import { Document, Schema } from 'mongoose'; -import { fieldTypes } from '../config/fields'; +import { fieldTypes, matchType } from '../config/fields' import { FieldOption } from './embedded/field.option'; import { LogicJump } from './embedded/logic.jump'; import { RatingField } from './embedded/rating.field'; @@ -9,6 +9,7 @@ export const FormFieldSchemaName = 'FormField' export interface FormFieldDocument extends Document { title: string description: string + slug?: string logicJump: any rating: any options: any @@ -27,6 +28,12 @@ export const FormFieldSchema = new Schema({ type: String, default: '', }, + slug: { + type: String, + required: false, + match: matchType.slug, + trim: true, + }, logicJump: { type: LogicJump, }, diff --git a/src/service/form/form.update.service.ts b/src/service/form/form.update.service.ts index 7a474f2..c4cc5ea 100644 --- a/src/service/form/form.update.service.ts +++ b/src/service/form/form.update.service.ts @@ -49,6 +49,10 @@ export class FormUpdateService { field.set('required', nextField.required) field.set('value', nextField.value) + if (nextField.slug !== undefined) { + field.set('slug', nextField.slug) + } + if (nextField.logicJump !== undefined) { field.set('logicJump', nextField.logicJump) }