-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added api to import timely file
- Loading branch information
1 parent
5c9d64d
commit 852b03f
Showing
4 changed files
with
86 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import * as XLSX from 'xlsx'; | ||
import moment from 'moment'; | ||
import model from '../models'; | ||
|
||
const {WorkDay} = model; | ||
const {Avatar} = model; | ||
class Workdays { | ||
/** | ||
* | ||
* @param req | ||
* @param res | ||
*/ | ||
static importTimelyFile(req, res) { | ||
// get the user id from the url | ||
const user_id = req.params.avatarId; | ||
Avatar.findById(user_id) | ||
.then(avatar => { | ||
if (avatar) { | ||
const userName = avatar.first_name + ' ' + avatar.last_name; | ||
// parse the excel file from the request | ||
if (req.file) { | ||
const workbook = XLSX.read(req.file.buffer, {type: 'buffer'}); | ||
const sheetNameList = workbook.SheetNames; | ||
const sheetInJson = XLSX.utils.sheet_to_json(workbook.Sheets[sheetNameList[1]]); | ||
// remove the first row from the array. because it contains only the total hours logged | ||
sheetInJson.shift(); | ||
const length = sheetInJson.length; | ||
for (let element of sheetInJson) { | ||
const nameInFile = element['Name']; | ||
if (nameInFile === userName) { | ||
WorkDay.create({ | ||
user_id, | ||
date: moment.utc(element['Date'], 'DD/MM/YYYY').toDate(), | ||
from: element['From'], | ||
to: element['To'], | ||
logged_hours: element['Logged'], | ||
tags: element['Tags'], | ||
notes: element['Note'] | ||
}) | ||
.then(() => { | ||
if (sheetInJson.indexOf(element) === length - 1) { | ||
return res.status(200).send({ | ||
status: true, | ||
msg: 'Entries saved to database successfully' | ||
}); | ||
} | ||
}) | ||
.catch(error => { | ||
console.error(error); | ||
return res.status(400).send(error); | ||
}); | ||
} | ||
} | ||
} else { | ||
return res.status(400).send({ | ||
status: false, | ||
errors: [ | ||
{ | ||
name: 'File not found error', | ||
detail: 'File was not found in the request.' | ||
} | ||
] | ||
}); | ||
} | ||
} else { | ||
return res.status(400).send({ | ||
status: false, | ||
errors: [ | ||
{ | ||
name: 'User not found error', | ||
details: 'User with id ' + user_id + ' was not found in the database' | ||
} | ||
] | ||
}); | ||
} | ||
}) | ||
.catch(error => res.status(400).send(error)); | ||
} | ||
} | ||
|
||
export default Workdays; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,10 @@ | ||
import express from 'express'; | ||
import multer from 'multer'; | ||
import * as XLSX from 'xlsx'; | ||
import Workday from '../controllers/workday'; | ||
|
||
const routes = express.Router(); | ||
const upload = multer({storage: multer.memoryStorage()}); | ||
routes.post('/upload', upload.single('reportFile'), (req, res, next) => { | ||
const workbook = XLSX.read(req.file.buffer, {type: 'buffer'}); | ||
const sheet_name_list = workbook.SheetNames; | ||
const sheet_in_json = XLSX.utils.sheet_to_json(workbook.Sheets[sheet_name_list[1]]); | ||
for (let element of sheet_in_json) { | ||
console.log(element); | ||
} | ||
|
||
return res.status(200).send({ | ||
status: true | ||
}); | ||
}); | ||
routes.post('/:avatarId/upload', upload.single('reportFile'), Workday.importTimelyFile); | ||
|
||
export default routes; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters