mirror of
https://github.com/pierre42100/ComunicWeb
synced 2024-12-27 03:08:51 +00:00
92 lines
2.3 KiB
JavaScript
92 lines
2.3 KiB
JavaScript
/**
|
|
* Forez Presence Helper
|
|
*
|
|
* @author Pierre Hubert
|
|
*/
|
|
|
|
class Presence {
|
|
constructor(userID, year, month, day) {
|
|
this.userID = userID;
|
|
this.year = year;
|
|
this.month = month;
|
|
this.day = day;
|
|
}
|
|
|
|
get date() {
|
|
return new Date(this.year, this.month - 1, this.day);
|
|
}
|
|
}
|
|
|
|
|
|
class ForezPresenceHelper {
|
|
/**
|
|
* Load the list of presence
|
|
*
|
|
* @param {number} groupID Target group ID
|
|
* @returns {Promise<Presence[]>}
|
|
*/
|
|
static async GetList(groupID) {
|
|
const list = await ws("forez_presence/list", {group: groupID});
|
|
|
|
return list.map(el => {
|
|
const infos = el.split(",").map(e => Number(e));
|
|
return new Presence(...infos)
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Add a day of presence
|
|
*
|
|
* @param {number} groupID
|
|
* @param {number} year
|
|
* @param {number} month
|
|
* @param {number} day
|
|
*/
|
|
static async AddDay(groupID, year, month, day) {
|
|
await ws("forez_presence/add_day", {
|
|
group: groupID,
|
|
year: year,
|
|
month: month,
|
|
day: day
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Remove a day of presence
|
|
*
|
|
* @param {number} groupID
|
|
* @param {number} year
|
|
* @param {number} month
|
|
* @param {number} day
|
|
*/
|
|
static async DelDay(groupID, year, month, day) {
|
|
await ws("forez_presence/del_day", {
|
|
group: groupID,
|
|
year: year,
|
|
month: month,
|
|
day: day
|
|
})
|
|
}
|
|
|
|
static async UpdateEvents(groupID, oldStart, oldEnd, newStart, newEnd) {
|
|
const newDays = new Set(getDaysOfRange(newStart, newEnd).map(el => el.getTime()));
|
|
const oldDays = new Set(getDaysOfRange(oldStart, oldEnd).map(el => el.getTime()));
|
|
|
|
for (const el of newDays) {
|
|
if(oldDays.has(el)) {
|
|
newDays.delete(el)
|
|
oldDays.delete(el)
|
|
}
|
|
}
|
|
|
|
for(const newEl of newDays) {
|
|
const date = new Date(newEl);
|
|
await this.AddDay(groupID, date.getFullYear(), date.getMonth() + 1, date.getDate())
|
|
}
|
|
|
|
for(const oldEl of oldDays) {
|
|
const date = new Date(oldEl);
|
|
await this.DelDay(groupID, date.getFullYear(), date.getMonth() + 1, date.getDate())
|
|
}
|
|
}
|
|
} |