Skip to content

Commit

Permalink
Adds time slot resource and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
coconutcraig committed Nov 16, 2018
1 parent 6a9c91f commit 39152d7
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 0 deletions.
86 changes: 86 additions & 0 deletions src/resources/time-slot.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import mockAxios from 'axios';

import TimeSlot from './time-slot';

it('will set location filter using a number', async () => {
const resource = new TimeSlot(mockAxios);

expect(resource.at(1)).toHaveProperty('filters', {
location: 1,
});
});

it('will set start and end filters', async () => {
const resource = new TimeSlot(mockAxios);

expect(resource.between('2018-01-01', '2018-01-31')).toHaveProperty('filters', {
end: '2018-01-31',
start: '2018-01-01',
});
});

it('will set user filter using a number', async () => {
const resource = new TimeSlot(mockAxios);

expect(resource.by(1)).toHaveProperty('filters', {
user: 1,
});
});

it('will set service filter using a number', async () => {
const resource = new TimeSlot(mockAxios);

expect(resource.for(1)).toHaveProperty('filters', {
services: 1,
});
});

it('will set service filter using an array of numbers', async () => {
const resource = new TimeSlot(mockAxios);

expect(resource.for([1, 2])).toHaveProperty('filters', {
services: [1, 2],
});
});

it('can string all filterable options together', async () => {
const resource = new TimeSlot(mockAxios);

const expected = expect(
resource
.between('2018-01-01', '2018-01-31')
.at(1)
.for([1, 2])
.by(1),
);

expected.toHaveProperty('filters', {
end: '2018-01-31',
location: 1,
services: [1, 2],
start: '2018-01-01',
user: 1,
});
});

it('can get time slots', async () => {
const resource = new TimeSlot(mockAxios);

await resource
.between('2018-01-01', '2018-01-31')
.at(1)
.for([1, 2])
.by(1)
.get();

expect(mockAxios.get).toHaveBeenCalledTimes(1);
expect(mockAxios.get).toHaveBeenCalledWith('times', {
params: {
end: '2018-01-31',
location_id: 1,
service_id: [1, 2],
staff_id: 1,
start: '2018-01-01',
},
});
});
55 changes: 55 additions & 0 deletions src/resources/time-slot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { AxiosInstance } from 'axios';

import { TimeSlotFilter } from '../types/filters';
import { TimeSlotResource } from '../types/resources';
import { TimeSlotParameters } from '../types/parameters';

export default class TimeSlot implements TimeSlotResource {
protected client: AxiosInstance;
protected filters: TimeSlotFilter;

constructor(client: AxiosInstance) {
this.client = client;
this.filters = {};
}

public at(location: number): this {
this.filters.location = location;

return this;
}

public between(start: string, end: string): this {
this.filters.start = start;
this.filters.end = end;

return this;
}

public by(user: number): this {
this.filters.user = user;

return this;
}

public for(services: number | number[]): this {
this.filters.services = services;

return this;
}

public async get(): Promise<any> {
const params: TimeSlotParameters = {
end: this.filters.end,
location_id: this.filters.location,
service_id: this.filters.services,
start: this.filters.start,
};

if (this.filters.user) {
params.staff_id = this.filters.user;
}

return await this.client.get('times', { params });
}
};
8 changes: 8 additions & 0 deletions src/types/filters.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ export interface ServiceFilter {
user?: number | string;
}

export interface TimeSlotFilter {
end?: string;
location?: number;
services?: number | number[];
start?: string;
user?: number;
}

export interface UserFilter {
assigned?: boolean;
services?: number | number[] | string | string[];
Expand Down
8 changes: 8 additions & 0 deletions src/types/parameters.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ export interface ServiceParameters {
user?: number | string;
}

export interface TimeSlotParameters {
end?: string;
location_id?: number;
service_id?: number | number[];
staff_id?: number;
start?: string;
}

export interface UserParameters {
assigned?: boolean;
service?: number | number[] | string | string[];
Expand Down
10 changes: 10 additions & 0 deletions src/types/resources.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ export interface Sortable extends Resource {
sortBy(sortable: string): this;
}

export interface TimeSlotResource extends Resource {
at(location: number): this;

between(start: string, end: string): this;

by(user: number): this;

for(services: number | number[]): this;
}

export interface UserResource extends Pageable {
assigned(assigned: boolean): this;

Expand Down

0 comments on commit 39152d7

Please sign in to comment.