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

Teach ts sdk about product utility members #236

Merged
merged 3 commits into from
Oct 8, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
"typescript": "^3.4.5"
},
"dependencies": {
"@types/lodash": "^4.14.141",
"@types/node": "^12.0.2",
"lodash": "^4.17.15",
"reflect-metadata": "^0.1.13"
}
}
24 changes: 24 additions & 0 deletions typescript/src/entities/domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,28 @@ export class Domain extends Entity {

@Domain.property({arrayType: "Theme"})
public themes?: Array<Theme>;

public defaultCurrency = () => {
if (this.company === undefined) {
throw new Error("company is undefined, did you forget to embed it?");
}
if (this.company.defaultCurrency === undefined) {
const err = "company.defaultCurrency is undefined, did you forget to" +
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

default currency is a primary variable how can it not be embeded

" embed it?";
throw new Error(err);
}
return this.company.defaultCurrency;
}

public defaultTaxType = () => {
if (this.company === undefined) {
throw new Error("company is undefined, did you forget to embed it?");
}
if (this.company.defaultTaxType === undefined) {
const err = "company.defaultTaxType is undefined, did you forget to" +
" embed it?";
throw new Error(err);
}
return this.company.defaultTaxType;
}
}
129 changes: 129 additions & 0 deletions typescript/src/entities/product.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,4 +328,133 @@ test('primary key always serialised', () => {
});
});

test('duplicate', () => {
const merchi = new Merchi();
const product = new merchi.Product();
const testName = 'qkc6fYD8HkR';
mockFetch(true, {'product': {'name': testName}}, 200);
return product.duplicate().then(clone => {
expect(clone.name).toEqual(testName);
});
});

test('primaryImage', () => {
const merchi = new Merchi();
const product = new merchi.Product();
const i1 = new merchi.MerchiFile();
const i2 = new merchi.MerchiFile();
expect(product.primaryImage).toThrow();
product.featureImage = i1;
expect(product.primaryImage).toThrow();
product.images = [i2];
expect(product.primaryImage()).toBe(i1);
product.featureImage = null;
expect(product.primaryImage()).toBe(i2);
product.images = [];
expect(product.primaryImage()).toBe(null);
});

test('currency', () => {
const merchi = new Merchi();
const product = new merchi.Product();
expect(product.currency).toThrow();
product.domain = new merchi.Domain();
expect(product.currency).toThrow();
product.domain.company = new merchi.Company();
expect(product.currency).toThrow();
product.domain.company.defaultCurrency = "MMK";
expect(product.currency()).toEqual("MMK");
});

test('hasGroupVariationFields', () => {
const merchi = new Merchi();
const product = new merchi.Product();
expect(product.hasGroupVariationFields).toThrow();
product.groupVariationFields = [];
expect(product.hasGroupVariationFields()).toBe(false);
product.groupVariationFields = [new merchi.VariationField()];
expect(product.hasGroupVariationFields()).toBe(true);
});

test('hasIndependentVariationFields', () => {
const merchi = new Merchi();
const product = new merchi.Product();
expect(product.hasIndependentVariationFields).toThrow();
product.independentVariationFields = [];
expect(product.hasIndependentVariationFields()).toBe(false);
product.independentVariationFields = [new merchi.VariationField()];
expect(product.hasIndependentVariationFields()).toBe(true);
});

test('taxType', () => {
const merchi = new Merchi();
const product = new merchi.Product();
expect(product.taxType).toThrow();
product.domain = new merchi.Domain();
expect(product.taxType).toThrow();
product.domain.company = new merchi.Company();
expect(product.taxType).toThrow();
const tax = new merchi.CountryTax();
product.domain.company.defaultTaxType = tax;
expect(product.taxType()).toBe(tax);
product.domain.company.defaultTaxType = null;
expect(product.taxType()).toBe(null);
});

test('allVariationFields', () => {
const merchi = new Merchi();
const product = new merchi.Product();
const vf1 = new merchi.VariationField();
const vf2 = new merchi.VariationField();
expect(product.allVariationFields).toThrow();
product.groupVariationFields = [vf1];
expect(product.allVariationFields).toThrow();
product.independentVariationFields = [vf2];
expect(product.allVariationFields()).toEqual([vf1, vf2]);
});

test('removeVariationField', () => {
const merchi = new Merchi();
const product = new merchi.Product();
const vf = new merchi.VariationField();
expect(() => product.removeVariationField(vf)).toThrow();
vf.independent = false;
expect(() => product.removeVariationField(vf)).toThrow();
product.independentVariationFields = [];
expect(() => product.removeVariationField(vf)).toThrow();
product.groupVariationFields = [vf];
expect(() => product.removeVariationField(vf)).toThrow();
vf.id = 1;
expect(product.removeVariationField(vf).length).toEqual(1);
expect(product.groupVariationFields.length).toEqual(0);
vf.independent = true;
product.independentVariationFields = [vf];
expect(product.removeVariationField(vf).length).toEqual(1);
expect(product.independentVariationFields.length).toEqual(0);
});

test('buildEmptyVariations', () => {
const merchi = new Merchi();
const product = new merchi.Product();
expect(product.buildEmptyVariations).toThrow();
product.independentVariationFields = [];
expect(product.buildEmptyVariations()).toEqual([]);
product.independentVariationFields = [new merchi.VariationField()];
product.independentVariationFields[0].defaultValue = "";
product.independentVariationFields[0].fieldType = 11;
product.independentVariationFields[0].variationCost = 2;
product.independentVariationFields[0].options = [];
expect(product.buildEmptyVariations().length).toEqual(1);
});

test('buildEmptyVariationGroup', () => {
const merchi = new Merchi();
const product = new merchi.Product();
expect(product.buildEmptyVariationGroup).toThrow();
product.groupVariationFields = [new merchi.VariationField()];
product.groupVariationFields[0].defaultValue = "";
product.groupVariationFields[0].fieldType = 11;
product.groupVariationFields[0].variationCost = 2;
product.groupVariationFields[0].options = [];
expect(product.buildEmptyVariationGroup().groupCost).toEqual(0);
});
134 changes: 134 additions & 0 deletions typescript/src/entities/product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,138 @@ export class Product extends Entity {

@Product.property({arrayType: "User"})
public suppliers?: Array<User>;

public duplicate = () => {
/* create a clone of this product on the backend, returning it. */
const resourceName = (this.constructor as typeof Product).resourceName;
const resource = `/${resourceName}/${String(this.id)}/copy/`;
const fetchOptions = {method: 'POST'};
return this.merchi.authenticatedFetch(resource, fetchOptions).
then((data: any) => {
const product = new this.merchi.Product();
product.fromJson(data);
return product;
});
}

public primaryImage = () => {
if (this.featureImage === undefined) {
throw new Error("featureImage is undefined, did you forget to embed it?");
}
if (this.images === undefined) {
throw new Error("images is undefined, did you forget to embed it?");
}
if (this.featureImage !== null) {
return this.featureImage;
}
if (this.images.length > 0) {
return this.images[0];
}
return null;
}

public currency = () => {
if (this.domain === undefined) {
throw new Error("domain is undefined, did you forget to embed it?");
}
return this.domain.defaultCurrency();
}

public hasGroupVariationFields = () => {
if (this.groupVariationFields === undefined) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ideally can be warp in a function checkEmbeded(this.groupVariationFields) or do some magic on property annotations.

Not required for this pr, but just raise this issue #241

const err = "groupVariationFields is undefined, did you forget to embed" +
" it?";
throw new Error(err);
}
return this.groupVariationFields.length > 0;
}

public hasIndependentVariationFields = () => {
if (this.independentVariationFields === undefined) {
const err = "independentVariationFields is undefined, did you forget to" +
" embed it?";
throw new Error(err);
}
return this.independentVariationFields.length > 0;
}

public taxType = () => {
if (this.domain === undefined) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can wrap in a function checkDomainEmbeded()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not until typescript supports "assertion signatures" (i.e., 3.7)

throw new Error("domain is undefined, did you forget to embed it?");
}
return this.domain.defaultTaxType();
}

public allVariationFields = () => {
if (this.groupVariationFields === undefined) {
const err = "groupVariationFields is undefined, did you forget to embed" +
" it?";
throw new Error(err);
}
if (this.independentVariationFields === undefined) {
const err = "independentVariationFields is undefined, did you forget to" +
" embed it?";
throw new Error(err);
}
const result: Array<VariationField> = [];
return result.concat(this.groupVariationFields,
this.independentVariationFields);
}

public buildEmptyVariations = () => {
if (this.independentVariationFields === undefined) {
const err = "independentVariationFields is undefined, did you forget to" +
" embed it?";
throw new Error(err);
}
return this.independentVariationFields.map(field =>
field.buildEmptyVariation());
}

public buildEmptyVariationGroup = () => {
if (this.groupVariationFields === undefined) {
const err = "groupVariationFields is undefined, did you forget to embed" +
" it?";
throw new Error(err);
}
const result = new this.merchi.VariationsGroup();
const variations = [];
let cost = 0;
result.quantity = 0;
for (const variationField of this.groupVariationFields) {
const empty = variationField.buildEmptyVariation();
variations.push(empty);
cost += empty.cost as number;
}
result.groupCost = cost;
result.variations = variations;
return result;
}

public removeVariationField = (variationField: VariationField) => {
if (variationField.independent === undefined) {
throw new Error("variation.independent is undefined, did you " +
"forget to embed it?");
}
if (this.independentVariationFields === undefined) {
const err = "independentVariationFields is undefined, did you forget to" +
" embed it?";
throw new Error(err);
}
if (this.groupVariationFields === undefined) {
const err = "groupVariationFields is undefined, did you forget to embed" +
" it?";
throw new Error(err);
}
const variationFields = variationField.independent ?
this.independentVariationFields : this.groupVariationFields;
const index = variationFields.findIndex(v => {
if (v.id === undefined) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how could a miss embed cause variation.id to be null.

throw new Error("variation id is undefined, did you forget to " +
"embed it?");
}
return v.id === variationField.id;
});
return variationFields.splice(index, 1);
}
}
30 changes: 30 additions & 0 deletions typescript/src/entities/variation_field.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,33 @@ test('can make VariationField', () => {
const variationField = new merchi.VariationField();
expect(variationField).toBeTruthy();
});

test('isSelectable', () => {
const merchi = new Merchi();
const vf = new merchi.VariationField();
expect(vf.isSelectable).toThrow();
vf.fieldType = 1;
expect(vf.isSelectable()).toBe(false);
vf.fieldType = 11;
expect(vf.isSelectable()).toBe(true);
});

test('buildEmptyVariation', () => {
const merchi = new Merchi();
const vf = new merchi.VariationField();
expect(vf.buildEmptyVariation).toThrow();
vf.defaultValue = "a";
vf.fieldType = 11;
expect(vf.buildEmptyVariation).toThrow();
vf.variationCost = 2;
expect(vf.buildEmptyVariation).toThrow();
const o1 = new merchi.VariationFieldsOption();
const o2 = new merchi.VariationFieldsOption();
o1.default = true;
vf.options = [o1, o2];
expect(vf.buildEmptyVariation).toThrow();
o1.variationCost = 3;
expect(vf.buildEmptyVariation().onceOffCost).toEqual(3);
vf.fieldType = 1;
expect(vf.buildEmptyVariation().onceOffCost).toEqual(2);
});
Loading