From 67a8edf91750e99106d7dd9ffb0edd7283dc45b0 Mon Sep 17 00:00:00 2001 From: Craig Paul Date: Sun, 18 Nov 2018 07:18:41 -0600 Subject: [PATCH] Extracts more code and renames methods --- src/models/answer.ts | 2 +- src/models/attendee.ts | 25 ++++++++++++++++++++----- src/resources/appointment.ts | 16 +--------------- src/types/models.d.ts | 6 ++---- 4 files changed, 24 insertions(+), 25 deletions(-) diff --git a/src/models/answer.ts b/src/models/answer.ts index d0073d5..496e5b9 100644 --- a/src/models/answer.ts +++ b/src/models/answer.ts @@ -26,7 +26,7 @@ export default class Answer extends Model implements AnswerModel { return this; } - public toResponse(): object { + public transform(): object { return { attributes: { question_id: this.attributes.question, diff --git a/src/models/attendee.ts b/src/models/attendee.ts index 41d238e..8113a7f 100644 --- a/src/models/attendee.ts +++ b/src/models/attendee.ts @@ -21,10 +21,6 @@ export default class Attendee extends Model implements AttendeeModel { return this; } - public getAnswers() { - return this.attributes.answers || []; - } - public located(details: LocationDetailParameters): this { this.attributes = {...this.attributes, ...details}; @@ -62,7 +58,26 @@ export default class Attendee extends Model implements AttendeeModel { return this; } - public toResponse(): object { + public transform(): object { + let parameters: object = this.parameters(); + const answers = this.attributes.answers || []; + + if (answers.length > 0) { + parameters = { + ...parameters, + relationships: { + answers: { + data: (answers as AnswerModel[]) + .map((answer: AnswerModel) => answer.transform()) + } + } + } + } + + return parameters; + } + + protected parameters(): object { const attributes: object = { address: this.attributes.address, cell_phone: this.attributes.cell_phone, diff --git a/src/resources/appointment.ts b/src/resources/appointment.ts index 4395224..b4d3ff6 100644 --- a/src/resources/appointment.ts +++ b/src/resources/appointment.ts @@ -80,21 +80,7 @@ export default class Appointment implements AppointmentResource { protected params(): AppointmentParameters { const attendees = (this.relationships.attendees as AttendeeModel[]) .map((attendee: AttendeeModel): object => { - const answers = attendee.getAnswers(); - let parameters: object = attendee.toResponse(); - - if (answers.length > 0) { - parameters = { - ...parameters, - relationships: { - answers: { - data: (answers as AnswerModel[]).map((answer: AnswerModel) => answer.toResponse()) - } - } - } - } - - return parameters; + return attendee.transform(); }); let params: AppointmentParameters = { diff --git a/src/types/models.d.ts b/src/types/models.d.ts index 8df3cc8..0a86e2e 100644 --- a/src/types/models.d.ts +++ b/src/types/models.d.ts @@ -9,14 +9,12 @@ export interface AnswerModel extends Model { is(value: string): this; - toResponse(): object; + transform(): object; } export interface AttendeeModel extends Model { answers(answers: AnswerModel | AnswerModel[]): this; - getAnswers(): AnswerModel[] | []; - located(details: LocationDetailParameters): this; messagable(): this; @@ -29,7 +27,7 @@ export interface AttendeeModel extends Model { speaks(language: string): this; - toResponse(): object; + transform(): object; } export interface PreferenceModel extends Model {