-
Notifications
You must be signed in to change notification settings - Fork 0
/
kalendar.js
68 lines (57 loc) · 1.79 KB
/
kalendar.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
import Irrigation from '@functional-lib/irrigation';
import fs from 'fs';
import {date as D} from './fp/date';
import uuid from 'uuid';
import { prop } from './utils';
const FILE = 'kalendar.json';
const setMinutes = value => d => d.setMinutes(value);
const setHours = value => d => d.setHours(value);
const setSeconds = value => d => d.setSeconds(value);
const getKalendar = (file = FILE) => {
try {
return JSON.parse(fs.readFileSync(file, 'utf8'));
} catch(error) {
return {dates: []};
}
};
const getArrayRiegosList = ({ dates = [], hour, minute, second, duration}) => {
return dates.map(d => ({
date: D.of(d)
.map(setHours(hour))
.map(setMinutes(minute))
.map(setSeconds(second)).value,
day: d,
uuid: uuid.v1(),
duration, hour, minute, second,
}));
}
const write = (riegos, file = FILE) => {
const json ={
configuration: {
priority: 'dates'
},
sheduler: "* * * * * *",
dates: riegos
};
try {
fs.writeFileSync(file, JSON.stringify(json));
return {message: 'update kalendar', status: true, json, dates: json.dates};
} catch(err) {
return {message: 'ko kalendar', status: false}
};
}
const deleteIrrigation = uuid => {
const {dates} = getKalendar();
const deleteByUuid = item => item.uuid !== uuid;
const riegos = Irrigation.from(dates).filter(deleteByUuid);
return write(riegos);
}
const _made = (data = {}) => {
const overNow = item => +new Date(item.date) > +new Date();
const previous = getKalendar();
const current = getArrayRiegosList(data);
const riegos = current.concat(prop('dates')(previous) || []).filter(overNow);
return write(riegos)
}
const madeKalendar = async (data = {}, file = FILE) => _made(data);
module.exports = {madeKalendar, getKalendar, deleteIrrigation, getArrayRiegosList};