diff --git a/apps/backend/src/app.module.ts b/apps/backend/src/app.module.ts index f05909e..12f0cc7 100644 --- a/apps/backend/src/app.module.ts +++ b/apps/backend/src/app.module.ts @@ -5,9 +5,10 @@ import { AuthModule } from "./aws/auth.module"; import { AuthenticationMiddleware } from "./aws/middleware/authentication.middleware"; import { UserModule } from "./users/users.module"; +import { TimesheetModule } from "./timesheet/timesheet.module"; @Module({ - imports: [AuthModule, UserModule], + imports: [AuthModule, UserModule, TimesheetModule], controllers: [AppController], providers: [AppService], }) diff --git a/apps/backend/src/aws/auth.controller.ts b/apps/backend/src/aws/auth.controller.ts deleted file mode 100644 index 52831fb..0000000 --- a/apps/backend/src/aws/auth.controller.ts +++ /dev/null @@ -1,57 +0,0 @@ -import { - Controller, - Get, - Post, - Headers, - Body, - UseGuards, -} from "@nestjs/common"; -import { AuthService } from "./auth.service"; -import { WriteEntryToTable, UserTimesheets } from "../dynamodb"; -import TokenClient from './cognito/cognito.keyparser' -import { TimeSheetSchema } from 'src/db/schemas/Timesheet'; -import * as frontendTimesheetSchemas from 'src/db/schemas/Timesheet' -import { RolesGuard } from 'src/utils/guards/roles.guard'; -import { UploadTimesheet } from 'src/db/timesheets/UploadTimesheet'; -import { TimesheetUpdateRequest } from 'src/db/schemas/UpdateTimesheet'; -import { Formatter } from 'src/db/timesheets/Formatter'; - - -@Controller("auth") -@UseGuards(RolesGuard) -export class AuthController { - - uploadApi = new UploadTimesheet(); - - constructor(private authService: AuthService) {} - - @Post('timesheet') - public async upload_timesheet( - @Headers() headers: any, - @Body() body: any - ): Promise { - const userId = await TokenClient.grabUserID(headers); - if (userId) { - console.log("Update Timesheet Request: Processing") - console.log("Request received:") - console.log(body) - const result = this.uploadApi.updateTimesheet(body, userId); - //TODO: Do something with this result? - return result; - } - } - - @Get("timesheet") - //@Roles('breaktime-management-role') - - public async grab_timesheets(@Headers() headers: any): Promise { - const userId = await TokenClient.grabUserID(headers); - - if (userId) { - console.log("Fetching timesheets for user ", userId); - return Formatter.fetchUserTimesheets(userId); - - } - return []; - } -} diff --git a/apps/backend/src/aws/auth.module.ts b/apps/backend/src/aws/auth.module.ts index 9307aa3..b9464bf 100644 --- a/apps/backend/src/aws/auth.module.ts +++ b/apps/backend/src/aws/auth.module.ts @@ -1,14 +1,13 @@ import { Module } from "@nestjs/common"; import { AuthService } from "./auth.service"; -import { AuthController } from "./auth.controller"; import { CognitoService } from "./cognito/cognito.service"; import { CognitoWrapper } from "./cognito/cognito.wrapper"; @Module({ imports: [], providers: [AuthService, CognitoService, CognitoWrapper], - controllers: [AuthController], + controllers: [], exports: [AuthService], }) export class AuthModule {} diff --git a/apps/backend/src/timesheet/timesheet.controller.ts b/apps/backend/src/timesheet/timesheet.controller.ts new file mode 100644 index 0000000..69a7a8d --- /dev/null +++ b/apps/backend/src/timesheet/timesheet.controller.ts @@ -0,0 +1,59 @@ +import { + Controller, + Get, + Post, + Headers, + Body, + UseGuards, + HttpStatus, + HttpException, +} from "@nestjs/common"; +import * as frontendTimesheetSchemas from 'src/db/schemas/Timesheet' +import { RolesGuard } from 'src/utils/guards/roles.guard'; +import { UploadTimesheet } from 'src/db/timesheets/UploadTimesheet'; +import { Formatter } from 'src/db/timesheets/Formatter'; +import { ValidatedUser } from "src/aws/auth.service"; +import { User } from "src/utils/decorators/user.decorator"; + + +@Controller("timesheet") +@UseGuards(RolesGuard) +export class TimesheetController { + uploadApi = new UploadTimesheet(); + + @Post('update') + public async updateTimesheet( + @Headers() headers: any, + @User() user: ValidatedUser, + @Body() body: any + ): Promise { + if (!user.sub) { + throw new HttpException( + "No authorized user found", + HttpStatus.UNAUTHORIZED + ); + } + + console.log("Update Timesheet Request: Processing") + console.log("Request received:") + console.log(body) + const result = this.uploadApi.updateTimesheet(body, user.sub); + + //TODO: Do something with this result? + return result; + } + + @Get("") + public async grabTimesheets(@Headers() headers: any, + @User() user: ValidatedUser, + ): Promise { + if (!user.sub) { + throw new HttpException( + "No authorized user found", + HttpStatus.UNAUTHORIZED + ); + } + + return Formatter.fetchUserTimesheets(user.sub); + } +} \ No newline at end of file diff --git a/apps/backend/src/timesheet/timesheet.module.ts b/apps/backend/src/timesheet/timesheet.module.ts new file mode 100644 index 0000000..a751e44 --- /dev/null +++ b/apps/backend/src/timesheet/timesheet.module.ts @@ -0,0 +1,12 @@ +import { Module } from "@nestjs/common"; +import { TimesheetController } from "./timesheet.controller"; +import { TimesheetService } from "./timesheet.service"; + + +@Module({ + imports: [], + providers: [TimesheetService], + controllers: [TimesheetController], + exports: [], +}) +export class TimesheetModule { } diff --git a/apps/backend/src/timesheet/timesheet.service.ts b/apps/backend/src/timesheet/timesheet.service.ts new file mode 100644 index 0000000..afad86f --- /dev/null +++ b/apps/backend/src/timesheet/timesheet.service.ts @@ -0,0 +1,9 @@ +import { + Injectable, +} from "@nestjs/common"; + + +@Injectable() +export class TimesheetService { + constructor() {} +} \ No newline at end of file