Resources
Despite the fact that for some services such as an online video call with a psychologist we only need that a professional is available at a certain time, there are other more complex scenarios in which a series of resources are needed to organize a reservation.
Sone examples of complex services are:
- A tennis class needs a tennis teacher and usually a tennis court.
- A physiotherapy session needs a physiotherapist and a bed.
- A car repair appointment needs a parking space and a crane and a CO2 meter
Using TimeTime language this can be modeled by saying "This service needs X resources to be booked". For this we have to associate the event-type
to a specific resource group
using the resourceRules
property of the event-type.
In the following example we have an event-type
that represents a guitar class. Suppose that in addition to the other rules to give a guitar class it is necessary to have a guitar for the student and two amplifiers. This is modeled through the resourceRules
property as follows:
{
// ... otras propiedades del event-type
resourceRules: {
// Este atributo indica que recursos tienen que que estar disponibles para poder hacer una reserva
availableInGroups: [
// Al menos dos unidades del grupo "amplificadores"
{
group: { id: 'amplificadores' }
min: 2,
},
// Al menos una unidad del grupo "amplificadores"
{
group: { id: 'guitarras' }
min: 1,
}
]
}
}
Before creating the event type we will have to create the resource groups and then create resources in them.
Resource Groups
Often, when making a reservation, resources are interchangeable. For example, to make a reservation at a restaurant, a table for a certain number of people needs to be available, but the specific table is irrelevant to the customer.
Therefore, all tables in the restaurant are created as separate resources but grouped together in the resource group
tables.
Continuing with our previous example, we will need to create two resource groups: guitars
and amplifiers
.
// Create the guitar resource group
fetch("https://api.timetime.in/v1/resource-groups/guitars", {
method: "PUT",
body: JSON.stringify({
resources: [],
name: "Guitars",
}),
});
// Create the amplifier resource group
fetch("https://api.timetime.in/v1/resource-groups/amplifiers", {
method: "PUT",
body: JSON.stringify({
resources: [],
name: "Amplifiers",
}),
});
Note that the only mandatory fields are the name and the array of individual resources. As we have not yet created the resources, we can send an empty array and associate resources later.
Individual Resources
Individual resources represent each of the specific objects that we are going to have associated with a particular resource group. For example, our music school has purchased 2 guitars and 4 amplifiers.
To create a resource, we need to use the corresponding endpoint of the TimeTime REST API as follows:
// Create the guitar resource
fetch("https://api.timetime.in/v1/resources/guitar-1", {
method: "PUT",
body: JSON.stringify({
name: "Fender Stratocaster",
}),
});
Note that the only mandatory fields are the ID, included in the URL, and the name. We will perform this same process for each of our guitars and amplifiers.
A resource also has a booking rules object that allows us to create a whole series of custom availability rules for each individual resource.
For example, we could have a guitar that can only be reserved on Tuesdays from 8 to 3 with two days' notice if it is sunny in Barcelona.
Associating Resources with Resource Groups
One of the design principles of TimeTime's APIs is simplicity and idempotence. This means that both for creating a resource group and for modifying it, we have to make the same request.
In our case, we can associate the guitar created in the previous section with the guitars resource group as follows:
fetch("https://api.timetime.in/v1/resource-groups/guitars", {
method: "PUT",
body: JSON.stringify({
resources: [{ id: "guitar-1" }],
name: "Guitars",
}),
});
Other Resource Operations
The API specification for creating, reading, deleting, and editing resources and resource groups is available at:
https://docs.timetime.in/api/#tag/resources/operation/getResource