Skip to content
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

Add payment support to CustomFieldDescription #778

Draft
wants to merge 4 commits into
base: supporter_level_goal
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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();
})
});
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'});
});
});
32 changes: 32 additions & 0 deletions client/js/nonprofits/donate/parseFields/customField/index.ts
Original file line number Diff line number Diff line change
@@ -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<string>;
label: NonNullable<string>;
type: 'payment' | 'supporter'
}

export interface CustomFieldDescriptionInput {
name: NonNullable<string>;
label: NonNullable<string>;
type?: 'payment' | 'supporter'
[prop:string]: unknown
}

export function nudgeToCustomFieldDescription({name, label, type}:CustomFieldDescriptionInput): CustomFieldDescription {
return defaults({name, label, type}, defaultCustomFieldDescription);
}


const defaultCustomFieldDescription: Pick<CustomFieldDescription, 'type'> = {
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');
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}
6 changes: 3 additions & 3 deletions client/js/nonprofits/donate/parseFields/customField/new.ts
Original file line number Diff line number Diff line change
@@ -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 });;
};
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'}]);
});
});