diff --git a/client/js/nonprofits/donate/components/info-step/customFields/customField.spec.ts b/client/js/nonprofits/donate/components/info-step/customFields/customField.spec.ts index 7fa52c987..03c975f5e 100644 --- a/client/js/nonprofits/donate/components/info-step/customFields/customField.spec.ts +++ b/client/js/nonprofits/donate/components/info-step/customFields/customField.spec.ts @@ -3,6 +3,6 @@ import customField from "./customField" describe('customField', () => { it('sets the the field correctly', () => { - expect(customField({name: "Name of Field", label: "Field Label"})).toMatchSnapshot(); + expect(customField({name: "Name of Field", label: "Field Label", type:"supporter"})).toMatchSnapshot(); }) }); \ No newline at end of file diff --git a/client/js/nonprofits/donate/components/info-step/customFields/index.spec.ts b/client/js/nonprofits/donate/components/info-step/customFields/index.spec.ts index f0c30979a..643d5689c 100644 --- a/client/js/nonprofits/donate/components/info-step/customFields/index.spec.ts +++ b/client/js/nonprofits/donate/components/info-step/customFields/index.spec.ts @@ -4,8 +4,8 @@ import customFields from "." describe('customFields', () => { it('sets the the field correctly', () => { expect(customFields([ - {name: "Name of Field", label: "Field Label"}, - {name: "Another Field Name", label: "Label2"} + {name: "Name of Field", label: "Field Label", type: 'supporter'}, + {name: "Another Field Name", label: "Label2", type: 'supporter'} ])).toMatchSnapshot(); }); diff --git a/client/js/nonprofits/donate/parseFields/customField/index.spec.ts b/client/js/nonprofits/donate/parseFields/customField/index.spec.ts index 3f1f09be5..4182426ec 100644 --- a/client/js/nonprofits/donate/parseFields/customField/index.spec.ts +++ b/client/js/nonprofits/donate/parseFields/customField/index.spec.ts @@ -3,10 +3,10 @@ import parseCustomField from '.'; describe('.parseCustomField', () => { it('when only name provided, label is name', () => { - expect(parseCustomField(" Supporter Tier ")).toStrictEqual({name: "Supporter Tier", label: "Supporter Tier"}); + expect(parseCustomField(" Supporter Tier ")).toStrictEqual({name: "Supporter Tier", label: "Supporter Tier", type: 'supporter'}); }); it('when label provided, label is set too', () => { - expect(parseCustomField(" Custom Supp Level : Supporter Tier ")).toStrictEqual({name: "Custom Supp Level", label: "Supporter Tier"}); + expect(parseCustomField(" Custom Supp Level : Supporter Tier ")).toStrictEqual({name: "Custom Supp Level", label: "Supporter Tier", type: 'supporter'}); }); }); \ No newline at end of file diff --git a/client/js/nonprofits/donate/parseFields/customField/index.ts b/client/js/nonprofits/donate/parseFields/customField/index.ts index 33e196933..83cda6d0a 100644 --- a/client/js/nonprofits/donate/parseFields/customField/index.ts +++ b/client/js/nonprofits/donate/parseFields/customField/index.ts @@ -1,9 +1,41 @@ // License: LGPL-3.0-or-later +import has from "lodash/has"; +import get from "lodash/get"; +import defaults from "lodash/defaults"; import { parseCustomField } from "./legacy"; export interface CustomFieldDescription { name: NonNullable; label: NonNullable; + type: 'payment' | 'supporter' +} + +export interface CustomFieldDescriptionInput { + name: NonNullable; + label: NonNullable; + type?: 'payment' | 'supporter' + [prop:string]: unknown +} + +export function nudgeToCustomFieldDescription({name, label, type}:CustomFieldDescriptionInput): CustomFieldDescription { + return defaults({name, label, type}, defaultCustomFieldDescription); +} + + +const defaultCustomFieldDescription: Pick = { + type: 'payment' +} + +export function isCustomFieldDesc(item:unknown) : item is CustomFieldDescription { + return isCustomFieldDescInput(item) && + get(item, 'type') === 'payment' && + get(item, 'type') === 'supporter'; +} + +export function isCustomFieldDescInput(item:unknown) : item is CustomFieldDescriptionInput { + return typeof item == 'object' && + has(item, 'name') && + has(item, 'label'); } diff --git a/client/js/nonprofits/donate/parseFields/customField/legacy.ts b/client/js/nonprofits/donate/parseFields/customField/legacy.ts index 4d252e6ae..c2abd1d4b 100644 --- a/client/js/nonprofits/donate/parseFields/customField/legacy.ts +++ b/client/js/nonprofits/donate/parseFields/customField/legacy.ts @@ -4,5 +4,5 @@ import { CustomFieldDescription } from '.'; export function parseCustomField(f:string) :CustomFieldDescription { const [name, label] = Rmap(Rtrim, Rsplit(':', f)) - return {name, label: label || name} + return {type: 'supporter', name, label: label || name} } \ No newline at end of file diff --git a/client/js/nonprofits/donate/parseFields/customField/new.ts b/client/js/nonprofits/donate/parseFields/customField/new.ts index a8e31ebc0..a1fda7323 100644 --- a/client/js/nonprofits/donate/parseFields/customField/new.ts +++ b/client/js/nonprofits/donate/parseFields/customField/new.ts @@ -1,12 +1,12 @@ // License: LGPL-3.0-or-later -import { CustomFieldDescription } from "."; +import { CustomFieldDescription, nudgeToCustomFieldDescription } from "."; export function newParseCustomField(input:string) : CustomFieldDescription { const [name, ...rest] = input.split(":").map((s) => s.trim()) - let label = null; + let label:string|null = null; if (rest.length > 0) { label = rest[0] } - return {name, label: label || name } as CustomFieldDescription; + return nudgeToCustomFieldDescription({ name, label: label || name });; }; diff --git a/client/js/nonprofits/donate/parseFields/customFields/JsonStringParser.spec.ts b/client/js/nonprofits/donate/parseFields/customFields/JsonStringParser.spec.ts index d7c690b46..aa43eaad0 100644 --- a/client/js/nonprofits/donate/parseFields/customFields/JsonStringParser.spec.ts +++ b/client/js/nonprofits/donate/parseFields/customFields/JsonStringParser.spec.ts @@ -23,9 +23,11 @@ describe('JsonStringParser', () => { }); describe.each([ - ['with non-custom-field-description', "[{name:'name', label: 'another'}]", [{name: 'name', label: 'another'}]], - ['with different json quote', '[{name:"name", label: "another"}]', [{name: 'name', label: 'another'}]], + ['with non-custom-field-description', "[{name:'name', label: 'another'}]", [{name: 'name', label: 'another', type: 'payment'}]], + ['with different json quote', '[{name:"name", label: "another"}]', [{name: 'name', label: 'another', type: 'payment'}]], ['when an empty array', '[]', []], + ['type = payment','[{name:"name", label: "another", type: "payment"}]', [{name: 'name', label: 'another', type: 'payment'}]], + ['type = supporter', '[{name:"name", label: "another", type: "supporter"}]', [{name: 'name', label: 'another', type: 'supporter'}]], ])("when valid %s", (_name, input, result) => { const parser = new JsonStringParser(input); diff --git a/client/js/nonprofits/donate/parseFields/customFields/JsonStringParser.ts b/client/js/nonprofits/donate/parseFields/customFields/JsonStringParser.ts index a729eac54..412024686 100644 --- a/client/js/nonprofits/donate/parseFields/customFields/JsonStringParser.ts +++ b/client/js/nonprofits/donate/parseFields/customFields/JsonStringParser.ts @@ -1,14 +1,14 @@ // License: LGPL-3.0-or-later -import has from 'lodash/has'; import { parse } from 'json5'; -import { CustomFieldDescription } from '../customField'; +import { CustomFieldDescription, isCustomFieldDescInput, nudgeToCustomFieldDescription} from '../customField'; + -function isCustomFieldDesc(item:unknown) : item is CustomFieldDescription { - return typeof item == 'object' && has(item, 'name') && has(item, 'label'); -} export default class JsonStringParser { - public errors:SyntaxError[] = []; + + public readonly errors: SyntaxError[] = []; + public readonly results: CustomFieldDescription[] = []; + constructor(public readonly fieldsString:string) { this._parse(); } @@ -17,14 +17,13 @@ export default class JsonStringParser { return this.errors.length == 0; } - private _parse = (): void => { try { const result = parse(this.fieldsString) if (result instanceof Array) { result.forEach((i) => { - if (isCustomFieldDesc(i)) { - this.results.push({ ...i}); + if (isCustomFieldDescInput(i)) { + this.results.push(nudgeToCustomFieldDescription(i)); } else { diff --git a/client/js/nonprofits/donate/parseFields/customFields/index.spec.ts b/client/js/nonprofits/donate/parseFields/customFields/index.spec.ts index ea9f161fd..89bf8874f 100644 --- a/client/js/nonprofits/donate/parseFields/customFields/index.spec.ts +++ b/client/js/nonprofits/donate/parseFields/customFields/index.spec.ts @@ -3,18 +3,18 @@ import parseCustomFields from '.'; describe('.parseCustomFields', () => { it('when only name provided, label is name', () => { - expect(parseCustomFields(" Supporter Tier ")).toStrictEqual([{name: "Supporter Tier", label: "Supporter Tier"}]); + expect(parseCustomFields(" Supporter Tier ")).toStrictEqual([{name: "Supporter Tier", label: "Supporter Tier", type: 'supporter'}]); }); it('when label provided, label is set too', () => { - expect(parseCustomFields(" Custom Supp Level : Supporter Tier ")).toStrictEqual([{name: "Custom Supp Level", label: "Supporter Tier"}]); + expect(parseCustomFields(" Custom Supp Level : Supporter Tier ")).toStrictEqual([{name: "Custom Supp Level", label: "Supporter Tier", type: 'supporter'}]); }); it('when passed an array looking thing, it treats a standard label', () => { - expect(parseCustomFields(" [Custom Supp Level] : Supporter Tier ")).toStrictEqual([{name: "[Custom Supp Level]", label: "Supporter Tier"}]); + expect(parseCustomFields(" [Custom Supp Level] : Supporter Tier ")).toStrictEqual([{name: "[Custom Supp Level]", label: "Supporter Tier", type: 'supporter'}]); }); it('when passed an array of json, it parses it', () => { - expect(parseCustomFields("[{name: 'name', label: 'labeled'}] ")).toStrictEqual([{name: "name", label: "labeled"}]); + expect(parseCustomFields("[{name: 'name', label: 'labeled'}] ")).toStrictEqual([{name: "name", label: "labeled", type: 'payment'}]); }); }); \ No newline at end of file