Skip to content

Commit

Permalink
Adds create wait list endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
coconutcraig committed Nov 19, 2018
1 parent 408c4ae commit f5c8206
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 3 deletions.
29 changes: 29 additions & 0 deletions src/models/preference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ const NEXT_AVAILABLE = 1;
const CERTAIN_DAYS = 2;

export default class Preference extends Model implements PreferenceModel {
public static now() {
const today = new Date;
const date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
const time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();

return `${date} ${time}`;
}

protected attributes: PreferenceParameters;

constructor() {
Expand Down Expand Up @@ -39,6 +47,27 @@ export default class Preference extends Model implements PreferenceModel {
return this;
}

public transform(): object {
const attributes: PreferenceParameters = this.attributes;

Object.keys(attributes).forEach(key => {
const value = (attributes as any)[key];

if (value === undefined || value === null) {
delete (attributes as any)[key];
}
});

if (attributes.type === Preference.NEXT_AVAILABLE) {
attributes.start = Preference.now();
}

return {
attributes: this.attributes,
type: 'request-preferences',
};
}

static get NEXT_AVAILABLE() {
return NEXT_AVAILABLE;
}
Expand Down
56 changes: 55 additions & 1 deletion src/resources/wait-list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,61 @@ it('can set a multiple preferences for the wait list request', async () => {
});

it('can create a new wait list request for a given client using only required attributes', async () => {
//
const resource = new WaitList(mockAxios);
const attendee = new Attendee;
const preference = new Preference;

await resource
.for(
attendee.named('Jane', 'Doe').reachable({ email: '[email protected]' })
)
.at(1)
.seeking(2)
.prefers(
preference.next()
)
.add();

expect(mockAxios.post).toHaveBeenCalledTimes(1);
expect(mockAxios.post).toHaveBeenCalledWith('requests', {
data: {
attributes: {},
relationships: {
client: {
data: {
attributes: {
email: '[email protected]',
first_name: 'Jane',
last_name: 'Doe',
},
type: 'clients'
}
},
location: {
data: {
id: '1',
type: 'locations',
}
},
preferences: {
data: [{
attributes: {
start: Preference.now(),
type: Preference.NEXT_AVAILABLE,
},
type: 'request-preferences'
}]
},
service: {
data: {
id: '2',
type: 'services',
}
},
},
type: 'requests'
}
});
});

it('can create a new wait list request for a given client using all attributes', async () => {
Expand Down
42 changes: 40 additions & 2 deletions src/resources/wait-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { AxiosInstance } from 'axios';

import { WaitListAttributes } from '../types/attributes';
import { AttendeeModel, PreferenceModel } from '../types/models';
import { IncludableParameters, WaitListUrlParameters } from '../types/parameters';
import { IncludableParameters, WaitListParameters, WaitListUrlParameters } from '../types/parameters';
import { WaitListRelationship } from '../types/relationships';
import { WaitListResource } from '../types/resources';

Expand All @@ -20,7 +20,7 @@ export default class WaitList implements WaitListResource {
}

public async add(): Promise<any> {
//
return await this.client.post('requests', this.params())
}

public at(location: number | string): this {
Expand Down Expand Up @@ -91,4 +91,42 @@ export default class WaitList implements WaitListResource {

return this;
}

protected params(): WaitListParameters {
const attendee = (this.relationships.attendee as AttendeeModel).transform();
const preferences = (this.relationships.preferences as PreferenceModel[])
.map((preference: PreferenceModel): object => {
return preference.transform();
});

return {
data: {
attributes: {},
relationships: {
client: {
data: {
...attendee,
type: 'clients',
}
},
location: {
data: {
id: String(this.relationships.location),
type: 'locations',
},
},
preferences: {
data: preferences,
},
service: {
data: {
id: String(this.relationships.service),
type: 'services',
},
},
},
type: 'requests',
},
};
}
}
2 changes: 2 additions & 0 deletions src/types/models.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,6 @@ export interface PreferenceModel extends Model {
next(): this;

on(day: number): this;

transform(): object;
}
27 changes: 27 additions & 0 deletions src/types/parameters.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,33 @@ export interface UserParameters {
location?: number | string;
}

export interface WaitListParameters {
data: {
attributes: {},
relationships: {
client: {
data: object;
};
location: {
data: {
id: string;
type: string;
};
};
preferences: {
data: object[];
};
service: {
data: {
id: string;
type: string;
};
};
},
type: string;
};
}

export interface WaitListUrlParameters {
client?: number | string;
include?: string;
Expand Down

0 comments on commit f5c8206

Please sign in to comment.