-
Notifications
You must be signed in to change notification settings - Fork 1
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can wrap in a function checkDomainEmbeded() There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
} |
There was a problem hiding this comment.
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