From 1a40c72fc0f192b39e8e57f45ae05b69f66b50ff Mon Sep 17 00:00:00 2001 From: Elizabeth Carretto Date: Wed, 6 Jun 2018 12:18:10 -0500 Subject: [PATCH] fix(EntityService): Implementing edit/many --- src/core/Entity.ts | 47 ++++++++++++------------------ src/services/EntityService.ts | 55 +++++++++++++++++------------------ 2 files changed, 45 insertions(+), 57 deletions(-) diff --git a/src/core/Entity.ts b/src/core/Entity.ts index e3e6ef8..ff36abd 100644 --- a/src/core/Entity.ts +++ b/src/core/Entity.ts @@ -169,34 +169,25 @@ export class Entity extends StatefulSubject { }); return this; } - // /** - // * Make http request to get entity. Objects 'data' property will be set to response, then promise will be resolved. - // * @param property - The TO_MANY Association field - // * @param fields - Additional fields to retrieve on the TO_MANY field - // */ - // Many(property: string, fields: Array): Promise { - // Return this.http.get(`${this.endpoint}${this.value.id}/${property}`, { - // Params: { - // Fields: fields, - // ShowTotalMatched: true - // } - // }).then((response: AxiosResponse) => { - // If (!this.value.hasOwnProperty(property)) { - // Object.defineProperty(this, property, { - // Get: function getter() { - // Return this.value[property]; - // }, - // Set: function setter(value) { - // This.value[property] = value; - // }, - // Configurable: true, - // Enumerable: true - // }); - // } - // This.value[property] = response.data; - // Return response; - // }); - // } + /** + * Make http request to get entity's many relationship. Response will update the value of the entity object. + * @param property - The TO_MANY Association field + * @param fields - Additional fields to retrieve on the TO_MANY field + */ + many(property: string, fields: string[]): Entity { + // tslint:disable-next-line:no-floating-promises + this.$entity.many(property, fields, this.value).then((response: AxiosResponse) => { + if (!this._fields.includes(property)) { + this._proxy(property); + this._fields.push(property); + } + this.value[property] = response; + this.patch({ property: response }); + this._setUpObservable(this.getValue()); + return response; + }); + return this; + } /** * Create or Updates the entity based on the presence of an 'id' property diff --git a/src/services/EntityService.ts b/src/services/EntityService.ts index 6506144..e4d927c 100644 --- a/src/services/EntityService.ts +++ b/src/services/EntityService.ts @@ -36,7 +36,7 @@ export class EntityService { this.http = Staffing.http(); this.meta = new MetaService(this.type); this.parameters = { - fields: this._fields || ['id'], + fields: this._fields || ['id'] }; } @@ -78,35 +78,32 @@ export class EntityService { return result; } - // /** - // * Make http request to get entity. Objects 'data' property will be set to response, then promise will be resolved. - // * @param property - The TO_MANY Association field - // * @param fields - Additional fields to retrieve on the TO_MANY field - // */ - // Many(property: string, fields: Array): Promise { - // Return this.http.get(`${this.endpoint}${this.value.id}/${property}`, { - // Params: { - // Fields: fields, - // ShowTotalMatched: true - // } - // }).then((response: AxiosResponse) => { - // If (!this.value.hasOwnProperty(property)) { - // Object.defineProperty(this, property, { - // Get: function getter() { - // Return this.value[property]; - // }, - // Set: function setter(value) { - // This.value[property] = value; - // }, - // Configurable: true, - // Enumerable: true - // }); - // } + /** + * Make http request to get entity with recordedit layout. Objects 'data' property will be set to response, then promise will be resolved. + * @param id - Id of the Model to retrieve + */ + async edit(id: number): Promise> { + const layout = 'RecordEdit'; + const [response, meta] = await Promise.all([this.http.get(`${this.endpoint}/${id}`, { params: { layout } }), this.meta.getFull(this.parameters.fields, layout)]); + const result: BullhornEntityResponse = response.data; + result.meta = meta; + return result; + } - // This.value[property] = response.data; - // Return response; - // }); - // } + /** + * Make http request to get entity's full toMany relationship for a property. + * @param property - The TO_MANY Association field + * @param fields - Additional fields to retrieve on the TO_MANY field + */ + async many(property: string, fields: string[], value: any): Promise { + const toManyData = await this.http.get(`${this.endpoint}/${value.id}/${property}`, { + params: { + fields, + showTotalMatched: true + } + }); + return toManyData.data; + } /** * Create or Updates the entity based on the presence of an 'id' property