Skip to content

Commit

Permalink
Add handling for tasks that are subtracted from hour balance.
Browse files Browse the repository at this point in the history
  • Loading branch information
joaalto committed Jan 18, 2017
1 parent 320a72a commit 2de81ad
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 36 deletions.
1 change: 1 addition & 0 deletions .env.example
Expand Up @@ -5,4 +5,5 @@ CLIENT_SECRET=<harvest_api_client_secret>
CALLBACK_URL=<harvest_api_redirect_uri>
ORGANIZATION=<your_harvest_organization>
IGNORE_TASK_IDS=<comma-separated_task_ids>
SUBTRACT_TASK_IDS=<comma-separated_task_ids>
START_DATE=YYYYMMDD
36 changes: 21 additions & 15 deletions server/server.js
Expand Up @@ -30,7 +30,7 @@ app.use(session({
})
}));

const forceSsl = function(req, res, next) {
const forceSsl = function (req, res, next) {
if (req.headers['x-forwarded-proto'] !== 'https') {
return res.redirect(['https://', req.get('Host'), req.url].join(''));
}
Expand All @@ -42,11 +42,11 @@ app.use(passport.initialize());
app.use(passport.session());
app.use(forceSsl);

passport.serializeUser(function(user, done) {
passport.serializeUser(function (user, done) {
done(null, user);
});

passport.deserializeUser(function(user, done) {
passport.deserializeUser(function (user, done) {
done(null, user);
});

Expand All @@ -60,7 +60,7 @@ passport.use(
callbackURL: process.env.CALLBACK_URL
},
// TODO: handle refresh tokens
function(accessToken, refreshToken, profile, done) {
function (accessToken, refreshToken, profile, done) {
request
.get(`${Api.harvestUrl}/account/who_am_i`)
.type('json')
Expand Down Expand Up @@ -102,18 +102,18 @@ app.all('*', ensureAuthenticated);

app.use('/', express.static(__dirname + '/static'));

app.get('/user', function(req, res) {
app.get('/user', function (req, res) {
if (req.isAuthenticated()) {
const sessionUser = req.session.passport.user;
User.findOne(
{ id: sessionUser.id },
(err, doc) => {
if(err) {
if (err) {
console.error(err);
}

let previousBalance = 0;
if(doc && doc.previousBalance) {
if (doc && doc.previousBalance) {
previousBalance = doc.previousBalance;
}

Expand All @@ -127,28 +127,34 @@ app.get('/user', function(req, res) {
}
});

app.get('/holidays', function(req, res) {
app.get('/holidays', function (req, res) {
const options = {
root: __dirname + '/static'
};
res.sendFile('finnish_holidays.json', options);
});

app.get('/entries', function(req, res) {
app.get('/entries', function (req, res) {
Api.fetchHourEntries(req, res)
.then(entries => res.send(entries));
});

app.get('/ignored_tasks', function(req, res) {
res.send(process.env.IGNORE_TASK_IDS
function idStringToTasks(taskIds) {
return taskIds
.split(',')
.map(taskId => {
return { taskId: parseInt(taskId) }
})
);
}

app.get('/special_tasks', function (req, res) {
res.send({
ignore: idStringToTasks(process.env.IGNORE_TASK_IDS),
subtract: idStringToTasks(process.env.SUBTRACT_TASK_IDS)
});
});

app.post('/balance', function(req, res) {
app.post('/balance', function (req, res) {
if (req.isAuthenticated()) {
const sessionUser = req.session.passport.user;
upsertUser(sessionUser.id, req.body.balance);
Expand All @@ -162,7 +168,7 @@ const upsertUser = (id, balance) => {
{ previousBalance: balance },
{ new: true, upsert: true },
(err, doc) => {
if(err) {
if (err) {
console.error(err);
}
}
Expand All @@ -173,6 +179,6 @@ const port = process.env.PORT || 8080;
app.listen(port);
console.log('Server listening in port: ' + port);

process.on('uncaughtException', function(error) {
process.on('uncaughtException', function (error) {
console.log(error.stack);
});
23 changes: 16 additions & 7 deletions src/Api.elm
Expand Up @@ -56,16 +56,25 @@ decodeHolidays =
)


getIgnoredTasks : Task Error (List HarvestTask)
getIgnoredTasks =
Http.get decodeTasks "/ignored_tasks"
getSpecialTasks : Task Error SpecialTasks
getSpecialTasks =
Http.get decodeTasks "/special_tasks"


decodeTasks : Json.Decoder (List HarvestTask)
decodeTasks : Json.Decoder SpecialTasks
decodeTasks =
list
(object1 HarvestTask
("taskId" := int)
object2 SpecialTasks
("ignore"
:= list
(object1 HarvestTask
("taskId" := int)
)
)
("subtract"
:= list
(object1 HarvestTask
("taskId" := int)
)
)


Expand Down
10 changes: 6 additions & 4 deletions src/DateUtils.elm
Expand Up @@ -55,14 +55,16 @@ dateInCurrentMonth date currentDate =

hoursForDate : DateEntries -> Model -> List Float
hoursForDate dateEntries model =
List.map (\entry -> entryHours entry model.ignoredTasks)
List.map (\entry -> entryHours entry model.specialTasks)
dateEntries.entries


entryHours : Entry -> List HarvestTask -> Float
entryHours entry ignoredTasks =
if List.any (\t -> t.id == entry.taskId) ignoredTasks then
entryHours : Entry -> SpecialTasks -> Float
entryHours entry specialTasks =
if List.any (\t -> t.id == entry.taskId) specialTasks.ignore then
0
else if List.any (\t -> t.id == entry.taskId) specialTasks.subtract then
-entry.hours
else
entry.hours

Expand Down
7 changes: 5 additions & 2 deletions src/Main.elm
Expand Up @@ -26,7 +26,7 @@ init =
, Update.getUser
, Update.getEntries
, Update.getHolidays
, Update.getIgnoredTasks
, Update.getSpecialTasks
]
)

Expand All @@ -42,7 +42,10 @@ initialModel =
, hourBalanceOfCurrentMonth = Nothing
, user = { firstName = "", lastName = "", previousBalance = 0 }
, holidays = []
, ignoredTasks = []
, specialTasks =
{ ignore = []
, subtract = []
}
, previousBalanceString = ""
, previousBalance = 0
, mdl = Material.model
Expand Down
8 changes: 7 additions & 1 deletion src/Model.elm
Expand Up @@ -15,7 +15,7 @@ type alias Model =
, hourBalanceOfCurrentMonth : Maybe Float
, user : User
, holidays : List Holiday
, ignoredTasks : List HarvestTask
, specialTasks : SpecialTasks
, previousBalanceString : String
, previousBalance : Float
, mdl : Material.Model
Expand Down Expand Up @@ -49,3 +49,9 @@ type alias Holiday =

type alias HarvestTask =
{ id : Int }


type alias SpecialTasks =
{ ignore : List HarvestTask
, subtract : List HarvestTask
}
14 changes: 7 additions & 7 deletions src/Update.elm
Expand Up @@ -24,7 +24,7 @@ type Msg
| PreviousMonth
| NextMonth
| UpdateHourBalanceOfCurrentMonth
| FetchedIgnoredTaskList (Result Http.Error (List HarvestTask))
| FetchedSpecialTaskList (Result Http.Error SpecialTasks)
| SetCurrentTime (Time.Time)
| UpdatePreviousBalance String
| SavePreviousBalance Float
Expand Down Expand Up @@ -75,7 +75,7 @@ update action model =
not
(isEmpty model.entries
|| isEmpty model.holidays
|| isEmpty model.ignoredTasks
|| isEmpty model.specialTasks.ignore
)
then
let
Expand All @@ -95,10 +95,10 @@ update action model =
UpdateHourBalanceOfCurrentMonth ->
noFx { model | hourBalanceOfCurrentMonth = Just (hourBalanceOfCurrentMonth model) }

FetchedIgnoredTaskList result ->
FetchedSpecialTaskList result ->
case result of
Ok tasks ->
update UpdateHours { model | ignoredTasks = tasks }
update UpdateHours { model | specialTasks = tasks }

Err error ->
handleError model error
Expand Down Expand Up @@ -171,6 +171,6 @@ getHolidays =
getResult Api.getNationalHolidays FetchedHolidays


getIgnoredTasks : Cmd Msg
getIgnoredTasks =
getResult Api.getIgnoredTasks FetchedIgnoredTaskList
getSpecialTasks : Cmd Msg
getSpecialTasks =
getResult Api.getSpecialTasks FetchedSpecialTaskList

0 comments on commit 2de81ad

Please sign in to comment.