Skip to content

Commit

Permalink
Extracts more code and renames methods
Browse files Browse the repository at this point in the history
  • Loading branch information
coconutcraig committed Nov 18, 2018
1 parent 0d9f09a commit 67a8edf
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/models/answer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
25 changes: 20 additions & 5 deletions src/models/attendee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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,
Expand Down
16 changes: 1 addition & 15 deletions src/resources/appointment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
6 changes: 2 additions & 4 deletions src/types/models.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,7 +27,7 @@ export interface AttendeeModel extends Model {

speaks(language: string): this;

toResponse(): object;
transform(): object;
}

export interface PreferenceModel extends Model {
Expand Down

0 comments on commit 67a8edf

Please sign in to comment.