Skip to content

Commit b155b48

Browse files
feat: add entropy to shifts
1 parent cd0eb63 commit b155b48

File tree

4 files changed

+32
-9
lines changed

4 files changed

+32
-9
lines changed

src/config.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import parseArgs from "https://deno.land/x/[email protected]/mod.ts";
2-
import { printHelp } from "./printHelp.ts";
32

43
const env = await Promise.resolve().then(() => Deno.env.toObject()).catch(
5-
() => ({})
4+
() => ({}),
65
);
76

87
const args: any = parseArgs(Deno.args);
98

109
if (args.help) {
11-
printHelp();
1210
Deno.exit();
1311
}
1412

@@ -22,4 +20,6 @@ export const config = {
2220
USER_EMAIL: args.e || args.email || env.FACTORIAL_USER_EMAIL || "???",
2321
USER_PASSWORD: args.p || args.password || env.FACTORIAL_USER_PASSWORD ||
2422
"???",
23+
SHIFT_MINUTES_RANDOMNESS: args.minutesRandomness ||
24+
env.SHIFT_MINUTES_RANDOMNESS || 3,
2525
};

src/factorial.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ cli
1919
{
2020
default: config.MONTH,
2121
},
22+
)
23+
.option(
24+
"--randomness <randomness>",
25+
"Sets amount of minutes for the randomness.",
26+
{
27+
default: config.SHIFT_MINUTES_RANDOMNESS,
28+
},
2229
)
2330
.option(
2431
"--email <email>",
@@ -31,15 +38,15 @@ cli
3138
"--password <password>",
3239
"The password of your factorial account. Also configurable via FACTORIAL_USER_PASSWORD env variable.",
3340
)
34-
.example("factorial fill-shifts --year 2021 --month 1")
35-
.action(({ year, month, email, password = config.USER_PASSWORD }) => {
41+
.example("factorial fill-shifts --year 2021 --month 1 --randomness 5")
42+
.action(({ year, month, randomness, email, password = config.USER_PASSWORD }) => {
3643
if (email === "???" || password === "???") {
3744
console.log("error: Missing email/password\n");
3845
console.log("For more information try --help");
3946
Deno.exit(1);
4047
}
4148

42-
return fillShifts(email, password, year, month);
49+
return fillShifts(email, password, year, month, randomness);
4350
});
4451

4552
cli.help();

src/fillShift.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ import { HttpClientFetch } from "./api/HttpClientFetch.ts";
22
import { QueryStringServiceImpl } from "./api/services/QueryStringServiceImpl.ts";
33
import { CookieParserServiceImpl } from "./api/services/CookieParserServiceImpl.ts";
44
import { createFactorialClient } from "./createFactorialClient.ts";
5+
import { addRandomnessToTime } from "./utils/addRandomnessToTime.ts";
56

67
export async function fillShifts(
78
email: string,
89
password: string,
910
year: number,
1011
month: number,
12+
randomness: number,
1113
) {
1214
const client = HttpClientFetch.create(
1315
{ baseURL: "https://api.factorialhr.com" },
@@ -60,14 +62,25 @@ export async function fillShifts(
6062
month,
6163
});
6264

65+
6366
for (const day of calendar) {
6467
if (factorial.isLaborable(day) && factorial.isInThePast(day)) {
65-
console.log(`Establishing shift for day ${day.day}`);
68+
const clockIn = {
69+
hours: "08",
70+
minutes: addRandomnessToTime(25, randomness).toString().padStart(1, "0"),
71+
};
72+
const clockOut = {
73+
hours: "16",
74+
minutes: addRandomnessToTime(35, randomness).toString().padStart(1, "0"),
75+
};
76+
77+
console.log(`Establishing shift for day ${day.day.toString().padStart(2, "0")} --> [${clockIn.hours}:${clockIn.minutes} - ${clockOut.hours}:${clockOut.minutes}]`);
78+
6679

6780
await factorial.createShift({
6881
periodId: period.id,
69-
clockIn: "08:00",
70-
clockOut: "16:00",
82+
clockIn: `${clockIn.hours}:${clockIn.minutes}`,
83+
clockOut: `${clockOut.hours}:${clockOut.minutes}`,
7184
minutes: 0,
7285
day: day.day,
7386
observations: null,

src/utils/addRandomnessToTime.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const addRandomnessToTime = (timeInMinutes: number, randomness: number) =>
2+
(Math.ceil(Math.random() * randomness) *
3+
(Math.round(Math.random()) ? 1 : -1)) + timeInMinutes;

0 commit comments

Comments
 (0)