Skip to content

Instantly share code, notes, and snippets.

@maguri
Last active July 28, 2021 08:02
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maguri/2699bed122e6112633413eb378b82bc8 to your computer and use it in GitHub Desktop.
Save maguri/2699bed122e6112633413eb378b82bc8 to your computer and use it in GitHub Desktop.
Script to Post your Factorial shifts automatically
!function(e,o){if("object"==typeof exports&&"object"==typeof module)module.exports=o();else if("function"==typeof define&&define.amd)define([],o);else{var t=o();for(var n in t)("object"==typeof exports?exports:e)[n]=t[n]}}(window,(function(){return function(e){var o={};function t(n){if(o[n])return o[n].exports;var c=o[n]={i:n,l:!1,exports:{}};return e[n].call(c.exports,c,c.exports,t),c.l=!0,c.exports}return t.m=e,t.c=o,t.d=function(e,o,n){t.o(e,o)||Object.defineProperty(e,o,{enumerable:!0,get:n})},t.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},t.t=function(e,o){if(1&o&&(e=t(e)),8&o)return e;if(4&o&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(t.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&o&&"string"!=typeof e)for(var c in e)t.d(n,c,function(o){return e[o]}.bind(null,c));return n},t.n=function(e){var o=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(o,"a",o),o},t.o=function(e,o){return Object.prototype.hasOwnProperty.call(e,o)},t.p="",t(t.s=0)}([function(e,o,t){"use strict";function n(e){return(n="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function c(e,o){for(var t=0;t<o.length;t++){var n=o[t];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}t.r(o),t.d(o,"Hacktorial",(function(){return i}));var r=[{clock_in:"9:00",clock_out:"13:00"},{clock_in:"14:00",clock_out:"18:00"}],i=function(){function e(o,t){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:null,c=arguments.length>3&&void 0!==arguments[3]?arguments[3]:[];!function(e,o){if(!(e instanceof o))throw new TypeError("Cannot call a class as a function")}(this,e),this.clock=this.setClock(n),this.year=o||(new Date).getUTCFullYear(),this.month=t||(new Date).getMonth()+1,this.holidays=c,this.getPeriod()}var o,t,i;return o=e,(t=[{key:"help",value:function(){console.log("================================HELP================================"),console.log("Usage: new Hacktorial(year, month, clock)"),console.log("Arguments:"),console.log("[OPTIONAL] Year [Integer] e.g. 2019 DEFAULT => current year"),console.log("[OPTIONAL] Month [Integer] e.g. 11 (November) DEFAULT => current month"),console.log("[OPTIONAL] Your schedule [Array of Hash] DEFAULT =>"),console.log(r),console.log("[OPTIONAL] Holidays [Array of Integers] e.g. [4, 5] DEFAULT => null"),console.log("Functions:"),console.log("run()\t - Post your shifts"),console.log("clean()\t - Clean your shifts"),console.log("=====================================================================")}},{key:"run",value:function(){this.period_id?this.setWeekdaysInMonth():(console.log("Wait, period id not set!"),console.log("Check month: ".concat(this.month," and year: ").concat(this.year,".")))}},{key:"clean",value:function(){this.removeShifts()}},{key:"setClock",value:function(e){if(!e)return r;if("object"!==n(e))throw Error("Clock format must be an object e.g. "+r);if(void 0===e.length||0==e.length)throw Error("Clock format must be a valid object e.g. "+r);e.forEach((function(e){console.log("clock_in: "+e.clock_in),console.log("clock_out: "+e.clock_out)})),this.clock=e}},{key:"getPeriod",value:function(){var e=this;fetch("https://api.factorialhr.com/attendance/periods/",{method:"GET",credentials:"include"}).then((function(o){o.text().then((function(o){JSON.parse(o).forEach((function(o){o.month==e.month&&o.year==e.year&&("pending"==o.state?e.period_id=o.id:console.log("Hey man!, you cannot update this month, period: ".concat(o.state)))}))}))}))}},{key:"setWeekdaysInMonth",value:function(){for(var e=this,o=(new Date).getDate(),t=function(o){e.isWeekday(o)&&e.clock.forEach((function(t){e.holidays.includes(o)||e.sendData(o,t.clock_in,t.clock_out)}))},n=1;n<=o;n++)t(n)}},{key:"isWeekday",value:function(e){var o=new Date(this.year,this.month-1,e).getDay();return 6!=o&&0!=o}},{key:"sendData",value:function(e,o,t){var n={clock_in:o,clock_out:t,day:e,history:[],minutes:0,observations:null,period_id:this.period_id};this.sendPost(n)}},{key:"sendPost",value:function(e){var o=this;fetch("https://api.factorialhr.com/attendance/shifts",{method:"POST",credentials:"include",headers:{"Content-Type":"application/json"},body:JSON.stringify(e)}).then((function(t){t.text().then((function(t){console.log("POST: { day: ".concat(e.day,", month: ").concat(o.month,", year: ").concat(o.year,", time: { clock_in: ").concat(e.clock_in,", clock_out: ").concat(e.clock_out," }}")),console.log(t)}))}))}},{key:"removeShifts",value:function(){var e=this;fetch("https://api.factorialhr.com/attendance/shifts",{method:"GET",credentials:"include"}).then((function(o){o.text().then((function(o){JSON.parse(o).forEach((function(o){o.period_id==e.period_id&&(console.log("DELETE:"),console.log(o),fetch("https://api.factorialhr.com/attendance/shifts/".concat(o.id),{method:"DELETE",credentials:"include"}))}))}))}))}}])&&c(o.prototype,t),i&&c(o,i),e}()}])}));
@maguri
Copy link
Author

maguri commented Jul 28, 2021

[not tested] this code is no longer used by me anymore...

Are you running this onto factorial website JS consosle?
It seams that the endpoint https://api.factorialhr.com/attendance/periods/ is not returning the expected contenct.

Check it out, and Pull Request if required ➡️
https://github.com/robitec/hacktorial/blob/master/src/hacktorial.js#L62

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment