place.ts 2.31 KB
Newer Older
1
import { Day } from './day';
mdsecurity's avatar
mdsecurity committed
2
3
import { Time } from './time';

mdsecurity's avatar
mdsecurity committed
4
export class Place {
mdsecurity's avatar
mdsecurity committed
5
6
7
8
9
10
11
	main_schedule_times: Day[] = [];
	special_schedule_times = [];
	id: number;
	last_modified: string;
	name: string;
	category: number;
	location: string;
mdsecurity's avatar
mdsecurity committed
12
13
	constructor(main_schedule_times?: Day[], special_schedule_times?, id?: number, last_modified?: string,
		name?: string, category?: number, location?: string) {
mdsecurity's avatar
mdsecurity committed
14

mdsecurity's avatar
mdsecurity committed
15
16
17
18
19
20
21
		this.main_schedule_times = main_schedule_times || [];
		this.special_schedule_times = special_schedule_times || [];
		this.id = id || 0;
		this.last_modified = last_modified || '';
		this.name = name || '';
		this.category = category || 0;
		this.location = location || '';
mdsecurity's avatar
mdsecurity committed
22
23
24
25
26
27
	}
	// This method does NOT work with Special schedules
	isOpen(): boolean {
		const currTime = new Date();
		const inSeconds = currTime.getHours() * 60 * 60 + currTime.getMinutes() * 60 + currTime.getSeconds();
		const dayOfWeek = currTime.getDay() - 1;
mdsecurity's avatar
mdsecurity committed
28

mdsecurity's avatar
mdsecurity committed
29
30
		for (let i = 0; i < this.main_schedule_times.length; i++) {
			const day = this.main_schedule_times[i];
31
32
33
34
35
36
37
			// change the order of if statements at some point
			if (day.start_day !== day.end_day) {
				if (day.end_day === dayOfWeek) {
					if (day.end_time.inSeconds() > inSeconds) {
						return true;
					}
				} else if (day.start_day === dayOfWeek) {
mdsecurity's avatar
mdsecurity committed
38
39
					return true;
				}
40
41
42
43
44
			} else {
				if (day.start_day === dayOfWeek) {
					if (day.end_time.inSeconds() > inSeconds) {
						return true;
					}
mdsecurity's avatar
mdsecurity committed
45
46
47
48
49
				}
			}
		}
		return false;
	}
50

mdsecurity's avatar
mdsecurity committed
51
	openFor(): Time {
mdsecurity's avatar
mdsecurity committed
52
		const currTime = new Date();
53
54
55
56
57
58
59
60
61
62
63
64
65
		const today = currTime.getDay() - 1;
		const inSeconds = currTime.getHours() * 60 * 60 + currTime.getMinutes() * 60 + currTime.getSeconds();
		const dayOfWeek = currTime.getDay() - 1;
		let timeTilClose = new Time();
		let sumSeconds = 0;
		// clean up if statements later
		if (this.isOpen()) {
			for (let i = 0; i < this.main_schedule_times.length; i++) {
				const day = this.main_schedule_times[i];
				if (day.end_day !== day.start_day) {
					if (dayOfWeek === day.end_day) {
						sumSeconds = day.end_time.inSeconds() - inSeconds - 86400;
					} else if (dayOfWeek === day.start_day) {
mdsecurity's avatar
mdsecurity committed
66
						sumSeconds = day.end_time.inSeconds() + 86400 - inSeconds;
67
68
69
70
					}
				} else {
					sumSeconds = day.end_time.inSeconds() - inSeconds;
				}
mdsecurity's avatar
mdsecurity committed
71

mdsecurity's avatar
mdsecurity committed
72
				return timeTilClose.fromSeconds(sumSeconds);
73
			}
mdsecurity's avatar
mdsecurity committed
74
75
		} else {
			return timeTilClose;
76
		}
mdsecurity's avatar
mdsecurity committed
77
	}
mdsecurity's avatar
mdsecurity committed
78
79
}

80