forked from evlbon/library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
document.js
148 lines (132 loc) · 4.12 KB
/
document.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import { Class } from 'meteor/jagi:astronomy';
import { Faculty } from "../users/faculty";
import { Student } from "../users/student";
import { Books } from "./book";
import { User } from "../users/user";
export const Copy = Class.create({
name: 'Copy',
fields: {
reference: {
type: Boolean
},
checked_out_date: {
type: Date,
optional: true
},
userID: {
type: String,
optional: true
}
}
});
export const Document = Class.create({
name: 'Document',
fields: {
title: {
type: String,
validators: [{
type: 'minLength',
param: 1,
}]
},
authorsID: {
type: [String]
},
tags: {
type: [String]
},
copies: {
type: [Copy]
},
price: {
type: Number
},
release_date: { // Release Date reserved as the Built_in Data type of "Date"
type: Date,
optional: true,
},
},
helpers: {
numberOfReferences: function () {
return this.copies.filter(o => (o.reference)).length;
},
leftInLibrary: function () {
return this.copies.filter(o => !(o.checked_out_date)).length;
},
available: function () {
return this.copies.filter(o => !(o.checked_out_date || o.reference)).length;
},
numberOfCopies: function () {
return this.copies.length;
},
tillDeadline: function (userID) {
if (!this.userHas(userID)) throw new Error('user doesn\'t have the document');
let copy = this.copies.find(o => o.checked_out_date && (o.userID === userID));
let renterID = copy.userID;
let duration;
if (User.findOne({libraryID: renterID}).group === 'Faculty') {
duration = 4*7;
} else {
duration = this.bestseller ? 2*7 : 3*7;
}
return Math.floor((copy.checked_out_date - new Date()) / 864e5) + duration;
},
calculateFee: function (userID) {
let fee = this.tillDeadline(userID) * 100;
return Math.max(0, Math.min(fee, this.price));
},
renters: function () {
let renters = [];
this.copies.forEach(o => {
if (o.checked_out_date) {
let renter = User.findOne({libraryID: o.userID});
renters.push({name: renter.name, tillDeadline: this.tillDeadline(o.userID),libraryID:o.userID})
}
});
return renters;
},
rentingViaId : function(cuser){
let renters = [];
this.copies.forEach(o => {
if (o.checked_out_date) {
if ( cuser === o.userID) {
renters.push({tillDeadline: this.tillDeadline(o.userID)})
}
}
});
return renters;
},
userHas(userID) {
return this.copies.find(o => !o.reference && o.checked_out_date && (o.userID === userID));
},
canCheckOut(userID) {
return !this.userHas(userID) && this.available()
},
checkOut(userID) {
let copy = this.copies.find(o => !(o.checked_out_date || o.reference));
if (copy) {
copy.checked_out_date = new Date();
copy.userID = userID;
this.save();
return true;
} else
return false;
},
return(userID) {
let copy = this.userHas(userID);
if (copy) {
copy.checked_out_date = null;
this.save();
} else
throw new Error( 'user '+ userID +' didnt have this book' );
},
addAuthor(author) {
},
addNewCopy(documentID) {
},
switchReference(documentID, boolean) {
},
getOverdueDocuments() {
},
},
});