Skip to content

Commit

Permalink
Hr 005 added upload function (#7)
Browse files Browse the repository at this point in the history
* Added More Services

* Added more Services

* added upload function
  • Loading branch information
Eriq-Kibet authored Sep 29, 2021
1 parent f07b021 commit 29870ce
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 12 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ coverage

src/config/index.ts

.env
.env

uploads
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ To start the server
- API endpoint base URL http://localhost:3000
2. Documentation
2. Create a folder 'uploads' at the root level
3. Documentation
- Documentation runs on [http://localhost:3000/documentation](http://localhost:3000/documentation)
Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"@hapi/podium": "^4.0.0",
"@hapi/vision": "^6.1.0",
"dotenv": "^10.0.0",
"fs": "0.0.1-security",
"hapi-swagger": "^14.2.4",
"hapi-swaggered": "^3.1.0",
"hapi-swaggered-ui": "^3.0.2",
Expand Down
8 changes: 7 additions & 1 deletion src/routes/timesheets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const route: Array<Route> = [
handler: timesheetController,
options: {
tags: ['api'],
description: 'get, all timesheets that have been captured in the system',
description: 'get, all timesheets for an employee that have been captured in the system',
notes: 'Returns an array of timesheets',
},
},
Expand All @@ -17,6 +17,12 @@ const route: Array<Route> = [
method: 'POST',
handler: timesheetController,
options: {
payload: {
output: 'stream',
parse: true,
allow: 'multipart/form-data',
multipart: true,
},
tags: ['api'],
description: 'post, add a timesheet for an employee to stored in the database',
notes: `Request body, an example below
Expand Down
38 changes: 31 additions & 7 deletions src/services/timesheet.service.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import fs from 'fs';
import serviceDef from '../connection/connection';
import { TimesheetsUpdate } from '../types/employee';

Expand All @@ -14,14 +15,37 @@ export function GetTimesheets(pfNumber): Promise<any> {

export function AddTimesheets(timesheet: TimesheetsUpdate) {
const { pfNumber, month, upload } = timesheet;
// eslint-disable-next-line max-len
const sql = `INSERT INTO Timesheets (pfNumber, month, upload) VALUES ('${pfNumber}', '${month}', '${upload}')`;
const name = upload.hapi.filename;
const employees = JSON.parse(pfNumber);
if (upload) {
const fileName = upload.hapi.filename;
const path = `${__dirname}/../../uploads/${fileName}`;
const file = fs.createWriteStream(path);
file.on('error', (error) => {
if (error) throw error
});
upload.pipe(file);
upload.on('end', (error) => {
if (error) throw error
const uploadDetails = {
filename: upload.hapi.filename,
headers: upload.hapi.headers,
};
return JSON.stringify(uploadDetails);
});
}
return new Promise((resolve, reject) => {
serviceDef.dbConnection().then((pool: any) => {
pool.query(sql, (error: any, results: any, fields: any) => {
if (error) reject(error);
resolve(results);
// eslint-disable-next-line no-plusplus
for (let i = 0; i < employees.length; i++) {
serviceDef.dbConnection().then((pool: any) => {
pool.query(`
INSERT INTO Timesheets(pfNumber, month, upload) VALUES(${employees[i]}, '${month}', '${name}')`,
(error: any, results: any, fields: any) => {
if (error) reject(error);
resolve(results);
},
);
});
});
}
});
}
4 changes: 2 additions & 2 deletions src/types/employee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ export interface EmployeeUpdate {
salutation: string;
}
export interface TimesheetsUpdate {
pfNumber: Number;
pfNumber: string;
month: string;
upload: string;
upload: any;
}
export interface FindReport {
department: string;
Expand Down

0 comments on commit 29870ce

Please sign in to comment.