Skip to content
This repository has been archived by the owner on Nov 20, 2022. It is now read-only.

Commit

Permalink
aggregate holidays by year and compute working days for the legal hol…
Browse files Browse the repository at this point in the history
…odays
  • Loading branch information
BogdanLivadariu committed Sep 13, 2018
1 parent d927ab7 commit d693bff
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 65 deletions.
87 changes: 49 additions & 38 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"jsonwebtoken": "7.4.1",
"lodash": "4.17.4",
"method-override": "2.3.5",
"mongoose": "4.11.14",
"mongoose": "5.1.1",
"monq": "0.3.7",
"morgan": "1.9.0",
"nodemailer": "4.1.0",
Expand Down
22 changes: 20 additions & 2 deletions backend/server/controllers/holiday.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,25 @@ function update(req, res, next) {

function list(req, res, next) {
const { limit = 50, skip = 0 } = req.query;
Holiday.list({ limit, skip })
Holiday
.list({ limit, skip })
.then(holidays => res.json(holidays))
.catch(e => next(e));
}

function aggregate(req, res, next) {
Holiday
.aggregate([ { $group: {
_id: { $year: '$date' },
items: { $push: '$$ROOT' },
workingDays: {
$sum: {
$cond: {
if: { $in: [ { $dayOfWeek:{ date:'$date', timezone: 'Europe/Bucharest'} }, {$range: [ 2, 6 ] } ] },
then: 1, else: 0
}
}
}}}])
.then(holidays => res.json(holidays))
.catch(e => next(e));
}
Expand All @@ -77,4 +95,4 @@ function remove(req, res, next) {
.catch(e => next(e));
}

export default { load, get, create, update, list, remove };
export default { load, get, create, update, list, remove, aggregate };
3 changes: 3 additions & 0 deletions backend/server/routes/holiday.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ router.route('/')
.get(permit(), holidayCtrl.list)
.post(permit(ADMIN), validate(paramValidation.createHoliday), holidayCtrl.create);

router.route('/aggregate')
.get(permit(ADMIN), holidayCtrl.aggregate)

router.route('/:holidayId')
.get(permit(), holidayCtrl.get)
.put(permit(ADMIN), validate(paramValidation.updateHoliday), holidayCtrl.update)
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: '2'
services:
mongo_db:
image: 'mongo:3.4'
image: 'mongo:3.5'
ports:
- '27017:27017'
volumes:
Expand Down
59 changes: 38 additions & 21 deletions frontend/src/admin/manage/legal-holidays/view-holidays.html
Original file line number Diff line number Diff line change
@@ -1,31 +1,48 @@
<template>
<spinner if.bind="loading"></spinner>

<div if.bind="!loading" class="bootcards-list">
<div class="panel panel-default">
<div class="list-group" >
<a class="list-group-item" repeat.for="holiday of holidays"
href="#/admin/holidays/${holiday._id}/edit">
<div class="row">
<div class="col-sm-4">
<h4 class="list-group-item-heading">
${holiday.name}
</h4>
</div>
<div if.bind="!loading" class="row">
<div class="col-md-12">
<div class="panel panel-default">
<div class="list-group panel-body">
<ul class="nav nav-pills">
<li class="${$index === 0 ? 'active': ''}" repeat.for="agg of aggregatedHolidays">
<a href="#${agg._id}" data-toggle="tab">${agg._id}(${agg.workingDays})</a>
</li>
</ul>
<hr/>

<div class="col-sm-4">
<p class="list-group-item-text">
${holiday.description}
</p>
</div>
<div class="tab-content list-group">
<div
repeat.for="agg of aggregatedHolidays"
class="tab-pane ${$index === 0 ? 'active': ''}"
id="${agg._id}">

<a class="list-group-item" repeat.for="holiday of agg.items"
href="#/admin/holidays/${holiday._id}/edit">
<div class="row">
<div class="col-sm-4">
<h4 class="list-group-item-heading">
${holiday.name}
</h4>
</div>

<div class="col-sm-4">
<p class="list-group-item-text">
${holiday.description}
</p>
</div>

<div class="col-sm-4">
<p class="list-group-item-text">
${holiday.date | dateFormat}
</p>
<div class="col-sm-4">
<p class="list-group-item-text">
${holiday.date | dateFormat}
</p>
</div>
</div>
</a>
</div>
</div>
</a>
</div>
</div>
</div>
</div>
Expand Down
7 changes: 5 additions & 2 deletions frontend/src/admin/manage/legal-holidays/view-holidays.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ export class ViewHolidays {
}

async attached() {
const holidays = await this._holiday.getHolidays();
// const holidays = await this._holiday.getHolidays();
const aggregatedHolidays = await this._holiday.getAggregatedHolidays();

// this.holidays = holidays;
this.aggregatedHolidays = aggregatedHolidays;

this.holidays = holidays;
this.loading = false;
}
}
4 changes: 4 additions & 0 deletions frontend/src/services/holiday-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export class HolidayService {
return this.http.get('holidays');
}

getAggregatedHolidays() {
return this.http.get('holidays/aggregate');
}

getHoliday(id) {
return this.http.get(`holidays/${id}`);
}
Expand Down

0 comments on commit d693bff

Please sign in to comment.