-
Notifications
You must be signed in to change notification settings - Fork 4
/
storage_recurring.js
133 lines (112 loc) · 3.28 KB
/
storage_recurring.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
require("date-format-lite"); // add date format
const xssFilters = require('xss-filters');
class Storage {
constructor(connection) {
this._db = connection;
this.table = "recurring_events";
}
// get events from the table, use dynamic loading if parameters sent
async getAll(params) {
let query = "SELECT * FROM ?? ";
let queryParams = [
this.table
];
if (params.from && params.to) {
query += " WHERE `end_date` >= ? AND `start_date` < ?";
queryParams.push(params.from);
queryParams.push(params.to);
}
let result = await this._db.query(query, queryParams);
result.forEach((entry) => {
// format date and time
entry.id = xssFilters.inHTMLData(entry.id);
entry.text = xssFilters.inHTMLData(entry.text);
entry.event_pid = xssFilters.inHTMLData(entry.event_pid);
entry.event_length = xssFilters.inHTMLData(entry.event_length);
entry.rec_type = xssFilters.inHTMLData(entry.rec_type);
entry.start_date = entry.start_date.format("YYYY-MM-DD hh:mm");
entry.end_date = entry.end_date.format("YYYY-MM-DD hh:mm");
});
return result;
}
// create new event
async insert(data) {
let sql = "INSERT INTO ?? " +
"(`start_date`, `end_date`, `text`, `event_pid`, `event_length`, `rec_type`) " +
"VALUES (?, ?, ?, ?, ?, ?)";
const result = await this._db.query(
sql,
[
this.table,
data.start_date,
data.end_date,
data.text,
data.event_pid || 0,
data.event_length || 0,
data.rec_type
]);
// delete a single occurrence from recurring series
let action = "inserted";
if (data.rec_type == "none") {
action = "deleted";
}
return {
action: action,
tid: result.insertId
};
}
// update event
async update(id, data) {
if (data.rec_type && data.rec_type != "none") {
// all modified occurrences must be deleted when we update recurring series
// https://docs.dhtmlx.com/scheduler/server_integration.html#savingrecurringevents
await this._db.query(
"DELETE FROM ?? WHERE `event_pid`= ?;",
[this.table, id]);
}
await this._db.query(
"UPDATE ?? SET " +
"`start_date` = ?, `end_date` = ?, `text` = ?, `event_pid` = ?, `event_length`= ?, `rec_type` = ? "+
"WHERE id = ?",
[
this.table,
data.start_date,
data.end_date,
data.text,
data.event_pid || 0,
data.event_length || 0,
data.rec_type,
id
]);
return {
action: "updated"
};
}
// delete event
async delete(id) {
// some logic specific to recurring events support
// https://docs.dhtmlx.com/scheduler/server_integration.html#savingrecurringevents
let event = await this._db.query(
"SELECT * FROM ?? WHERE id=? LIMIT 1;",
[this.table, id]);
if (event.event_pid) {
// deleting modified occurrence from recurring series
// If an event with the event_pid value was deleted - it needs updating with rec_type==none instead of deleting.
event.rec_type = "none";
return await this.update(id, event);
}
if (event.rec_type && event.rec_type != "none") {
// if a recurring series was deleted - delete all modified occurrences of the series
await this._db.query(
"DELETE FROM ?? WHERE `event_pid`=? ;",
[this.table, id]);
}
await this._db.query(
"DELETE FROM ?? WHERE `id`= ?;",
[this.table, id]);
return {
action: "deleted"
}
}
}
module.exports = Storage;