Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- I'm working on a project where I need to get all the vacant classrooms for the day and basically filter out the ones being in use.
- I get all the data from the school's own API with JSON response body that looks like this:
- {
- "status": "success",
- "reservations": [
- {
- "id": "18935",
- "subject": "subjectName",
- "modifiedDate": "2017-04-27T06:04:51",
- "startDate": "2017-04-27T11:00:00",
- "endDate": "2017-04-27T13:00:00",
- "resources": [
- {
- "id": "50",
- "type": "room",
- "code": "A440.5",
- "parent": {
- "id": "2",
- "type": "building",
- "code": "A",
- "name": "buildingA"
- },
- "name": "A440.5"
- },
- {
- "id": "2995",
- "type": "realization",
- "code": "",
- "name": ""
- },
- {
- "id": "2267",
- "type": "student_group",
- "code": "",
- "name": ""
- }
- ],
- "description": ""
- },
- {
- "id": "20362",
- "subject": "subjectName",
- "modifiedDate": "2017-04-27T06:05:05",
- "startDate": "2017-04-27T11:00:00",
- "endDate": "2017-04-27T14:00:00",
- "resources": [
- {
- "id": "51",
- "type": "room",
- "code": "A450.1",
- "parent": {
- "id": "2",
- "type": "building",
- "code": "A",
- "name": "buildingA"
- },
- "name": "A450.1"
- },
- {
- "id": "2402",
- "type": "student_group",
- "code": "",
- "name": ""
- },
- {
- "id": "3064",
- "type": "realization",
- "code": "",
- "name": ""
- }
- ],
- "description": ""
- },
- {
- "id": "20237",
- "subject": "subjectName",
- "modifiedDate": "2017-04-27T06:05:05",
- "startDate": "2017-04-27T11:15:00",
- "endDate": "2017-04-27T13:00:00",
- "resources": [
- {
- "id": "45",
- "type": "room",
- "code": "A420.4",
- "parent": {
- "id": "2",
- "type": "building",
- "code": "A",
- "name": "buildingA"
- },
- "name": "A420.4"
- },
- {
- "id": "2433",
- "type": "student_group",
- "code": "",
- "name": ""
- },
- {
- "id": "3058",
- "type": "realization",
- "code": "",
- "name": ""
- }
- ],
- "description": ""
- },
- {
- "id": "20888",
- "subject": "subjectName",
- "modifiedDate": "2017-04-27T06:04:57",
- "startDate": "2017-04-27T13:15:00",
- "endDate": "2017-04-27T16:00:00",
- "resources": [
- {
- "id": "62",
- "type": "room",
- "code": "A520.5",
- "parent": {
- "id": "2",
- "type": "building",
- "code": "A",
- "name": "buildingA"
- },
- "name": "A520.5"
- },
- {
- "id": "3092",
- "type": "realization",
- "code": "",
- "name": ""
- },
- {
- "id": "2444",
- "type": "student_group",
- "code": "",
- "name": ""
- }
- ],
- "description": ""
- },
- {
- "id": "22586",
- "subject": "subjectName",
- "modifiedDate": "2017-04-27T06:04:48",
- "startDate": "2017-04-27T13:15:00",
- "endDate": "2017-04-27T17:00:00",
- "resources": [
- {
- "id": "52",
- "type": "room",
- "code": "A450.3",
- "parent": {
- "id": "2",
- "type": "building",
- "code": "A",
- "name": "buildingA"
- },
- "name": "A450.3"
- },
- {
- "id": "3004",
- "type": "realization",
- "code": "",
- "name": ""
- },
- {
- "id": "2294",
- "type": "student_group",
- "code": "",
- "name": ""
- },
- {
- "id": "525",
- "type": "student_group",
- "code": "",
- "name": ""
- }
- ],
- "description": ""
- },
- {
- "id": "18816",
- "subject": "subjectName",
- "modifiedDate": "2017-04-27T06:04:58",
- "startDate": "2017-04-27T13:15:00",
- "endDate": "2017-04-27T16:00:00",
- "resources": [
- {
- "id": "41",
- "type": "room",
- "code": "A340.1",
- "parent": {
- "id": "2",
- "type": "building",
- "code": "A",
- "name": "buildingA"
- },
- "name": "A340.1"
- },
- {
- "id": "2989",
- "type": "realization",
- "code": "",
- "name": ""
- },
- {
- "id": "795",
- "type": "student_group",
- "code": "",
- "name": ""
- },
- {
- "id": "599",
- "type": "student_group",
- "code": "",
- "name": ""
- }
- ],
- "description": ""
- },
- {
- "id": "20431",
- "subject": "subjectName",
- "modifiedDate": "2017-04-27T06:04:56",
- "startDate": "2017-04-27T13:15:00",
- "endDate": "2017-04-27T16:00:00",
- "resources": [
- {
- "id": "40",
- "type": "room",
- "code": "A320.7",
- "parent": {
- "id": "2",
- "type": "building",
- "code": "A",
- "name": "buildingA"
- },
- "name": "A320.7/8"
- },
- {
- "id": "2416",
- "type": "realization",
- "code": "",
- "name": ""
- },
- {
- "id": "2386",
- "type": "student_group",
- "code": "",
- "name": ""
- }
- ],
- "description": ""
- },
- {
- "id": "18588",
- "subject": "subjectName",
- "modifiedDate": "2017-04-27T06:04:49",
- "startDate": "2017-04-27T13:15:00",
- "endDate": "2017-04-27T16:00:00",
- "resources": [
- {
- "id": "25",
- "type": "room",
- "code": "A130.1",
- "parent": {
- "id": "2",
- "type": "building",
- "code": "A",
- "name": "buildingA"
- },
- "name": "A130.1"
- },
- {
- "id": "26",
- "type": "room",
- "code": "A130.3",
- "parent": {
- "id": "2",
- "type": "building",
- "code": "A",
- "name": "buildingA"
- },
- "name": "A130.3"
- },
- {
- "id": "2979",
- "type": "realization",
- "code": "",
- "name": ""
- },
- {
- "id": "582",
- "type": "student_group",
- "code": "",
- "name": ""
- }
- ],
- "description": ""
- },
- {
- "id": "18940",
- "subject": "subjectName",
- "modifiedDate": "2017-04-27T06:04:53",
- "startDate": "2017-04-27T13:15:00",
- "endDate": "2017-04-27T16:00:00",
- "resources": [
- {
- "id": "2996",
- "type": "realization",
- "code": "",
- "name": ""
- },
- {
- "id": "2267",
- "type": "student_group",
- "code": "",
- "name": ""
- },
- {
- "id": "2268",
- "type": "student_group",
- "code": "",
- "name": ""
- },
- {
- "id": "31",
- "type": "room",
- "code": "A210.2",
- "parent": {
- "id": "2",
- "type": "building",
- "code": "A",
- "name": "buildingA"
- },
- "name": "A210.2"
- }
- ],
- "description": ""
- },
- {
- "id": "12041",
- "subject": "subjectName",
- "modifiedDate": "2017-04-27T06:04:53",
- "startDate": "2017-04-27T14:15:00",
- "endDate": "2017-04-27T17:00:00",
- "resources": [
- {
- "id": "2510",
- "type": "realization",
- "code": "",
- "name": ""
- },
- {
- "id": "775",
- "type": "student_group",
- "code": "",
- "name": ""
- },
- {
- "id": "23",
- "type": "room",
- "code": "A520.7",
- "parent": {
- "id": "2",
- "type": "building",
- "code": "A",
- "name": "buildingA"
- },
- "name": "A520.7"
- }
- ],
- "description": ""
- },
- {
- "id": "24630",
- "subject": "subjectName",
- "modifiedDate": "2017-04-27T06:05:05",
- "startDate": "2017-04-27T14:15:00",
- "endDate": "2017-04-27T17:00:00",
- "resources": [
- {
- "id": "3277",
- "type": "realization",
- "code": "",
- "name": ""
- },
- {
- "id": "42",
- "type": "room",
- "code": "A340.2",
- "parent": {
- "id": "2",
- "type": "building",
- "code": "A",
- "name": "buildingA"
- },
- "name": "A340.2"
- }
- ],
- "description": ""
- },
- {
- "id": "27205",
- "subject": "subjectName",
- "modifiedDate": "2017-04-27T06:05:07",
- "startDate": "2017-04-27T14:15:00",
- "endDate": "2017-04-27T17:00:00",
- "resources": [
- {
- "id": "35",
- "type": "room",
- "code": "A240.2",
- "parent": {
- "id": "2",
- "type": "building",
- "code": "A",
- "name": "buildingA"
- },
- "name": "A240.2"
- },
- {
- "id": "775",
- "type": "student_group",
- "code": "",
- "name": ""
- },
- {
- "id": "3384",
- "type": "realization",
- "code": "",
- "name": ""
- }
- ],
- "description": ""
- },
- {
- "id": "25917",
- "subject": "subjectName",
- "modifiedDate": "2017-04-27T06:05:00",
- "startDate": "2017-04-27T15:15:00",
- "endDate": "2017-04-27T16:00:00",
- "resources": [
- {
- "id": "36",
- "type": "room",
- "code": "A240.4",
- "parent": {
- "id": "2",
- "type": "building",
- "code": "A",
- "name": "buildingA"
- },
- "name": "A240.4"
- },
- {
- "id": "593",
- "type": "realization",
- "code": "",
- "name": ""
- },
- {
- "id": "595",
- "type": "student_group",
- "code": "",
- "name": ""
- },
- {
- "id": "596",
- "type": "student_group",
- "code": "",
- "name": ""
- },
- {
- "id": "313",
- "type": "student_group",
- "code": "",
- "name": ""
- }
- ],
- "description": ""
- },
- {
- "id": "21932",
- "subject": "subjectName",
- "modifiedDate": "2017-04-27T06:05:06",
- "startDate": "2017-04-27T16:00:00",
- "endDate": "2017-04-27T18:00:00",
- "resources": [
- {
- "id": "43",
- "type": "room",
- "code": "A350.1",
- "parent": {
- "id": "2",
- "type": "building",
- "code": "A",
- "name": "buildingA"
- },
- "name": "A350.1"
- },
- {
- "id": "2464",
- "type": "student_group",
- "code": "",
- "name": ""
- },
- {
- "id": "2747",
- "type": "student_group",
- "code": "",
- "name": ""
- },
- {
- "id": "199",
- "type": "student_group",
- "code": "",
- "name": ""
- },
- {
- "id": "2470",
- "type": "student_group",
- "code": "",
- "name": ""
- },
- {
- "id": "2471",
- "type": "student_group",
- "code": "",
- "name": ""
- },
- {
- "id": "2444",
- "type": "student_group",
- "code": "",
- "name": ""
- },
- {
- "id": "2498",
- "type": "student_group",
- "code": "",
- "name": ""
- },
- {
- "id": "2481",
- "type": "student_group",
- "code": "",
- "name": ""
- },
- {
- "id": "2482",
- "type": "student_group",
- "code": "",
- "name": ""
- },
- {
- "id": "2748",
- "type": "student_group",
- "code": "",
- "name": ""
- },
- {
- "id": "2577",
- "type": "student_group",
- "code": "",
- "name": ""
- },
- {
- "id": "2555",
- "type": "student_group",
- "code": "",
- "name": ""
- },
- {
- "id": "246",
- "type": "student_group",
- "code": "",
- "name": ""
- },
- {
- "id": "2558",
- "type": "student_group",
- "code": "",
- "name": ""
- },
- {
- "id": "2559",
- "type": "student_group",
- "code": "",
- "name": ""
- },
- {
- "id": "305",
- "type": "student_group",
- "code": "",
- "name": ""
- }
- ],
- "description": ""
- }
- ]
- }
- In order to get the JSON response I need to put `startDate` (the moment user searches the vacant rooms, in this situation it is: `2017-04-27T10:55`) and `endDate` (set to end of the day, `2017-04-27T22:00`). The result for this query is the JSON response above.
- The problem is that the API I'm using doesn't contain any data for the vacant rooms but only for the ones booked for certain times so I made a list of all the rooms in the building and compared it to the booked ones to filter them out:
- var rooms = ['A120.3', 'A130.1', 'A130.3', 'A140.1', 'A140.2', 'A140.4', 'A250.1', 'A240.4', 'A240.2', 'A220.5', 'A220.3',
- 'A220.1', 'A210.2', 'A320.2', 'A320.6', 'A320.7', 'A320.8', 'A340.1', 'A340.2', 'A350.1', 'A350.3', 'A440.5', 'A450.3','A450.1',
- 'A440.4', 'A440.2', 'A420.6', 'A420.5', 'A420.4', 'A420.2', 'A510.2', 'A520.5', 'A510.4', 'A520.6', 'A520.7','A540.1', 'A540.2'];
- var data = JSON.parse(responseText);
- var booking = Object.create(null);
- var free;
- data.reservations.forEach(function (reservation) {
- reservation.resources.some(function (resource) {
- if (resource.type === 'room') {
- booking[resource.code] = booking[resource.code] || [];
- booking[resource.code].push({ startDate: reservation.startDate, endDate: reservation.endDate });
- return true;
- }
- });
- });
- free = rooms.filter(function (a) {
- return !booking[a];
- });
- console.log(free);
- Results:
- A210.3
- A220.5
- A320.2
- A520.6
- A510.4
- This only returns the ones that are not being used at all during the day but I need to get the ones that are vacant for hour or two.
- For this booked class for example:
- "startDate": "2017-04-27T13:15:00",
- "endDate": "2017-04-27T16:00:00",
- "type": "room",
- "code": "A520.5"
- for this I would need to print out:
- A520.5 - 2 hours 20 minutes
- So I need to get the room / time from the `startDate` of the search (**2017-04-27T10:55**) and compare it to the `startDate` of the booked room (**2017-04-27T13:15:00**) to get the time remaining for that room to be vacant.
- Thanks in advance.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement