diff --git a/backend/dist/app.module.js b/backend/dist/app.module.js index c686b92..37400a3 100644 --- a/backend/dist/app.module.js +++ b/backend/dist/app.module.js @@ -11,11 +11,12 @@ const common_1 = require("@nestjs/common"); const auth_module_1 = require("./auth/auth.module"); const user_module_1 = require("./user/user.module"); const grant_module_1 = require("./grant/grant.module"); +const notification_module_1 = require("./notifications/notification.module"); let AppModule = class AppModule { }; AppModule = __decorate([ (0, common_1.Module)({ - imports: [auth_module_1.AuthModule, user_module_1.UserModule, grant_module_1.GrantModule], + imports: [auth_module_1.AuthModule, user_module_1.UserModule, grant_module_1.GrantModule, notification_module_1.NotificationsModule], }) ], AppModule); exports.AppModule = AppModule; diff --git a/backend/dist/auth/auth.service.js b/backend/dist/auth/auth.service.js index 104255c..5e2ec0f 100644 --- a/backend/dist/auth/auth.service.js +++ b/backend/dist/auth/auth.service.js @@ -37,9 +37,6 @@ exports.AuthService = void 0; const common_1 = require("@nestjs/common"); const aws_sdk_1 = __importDefault(require("aws-sdk")); const crypto = __importStar(require("crypto")); -aws_sdk_1.default.config.update({ - region: process.env.AWS_REGION, -}); let AuthService = AuthService_1 = class AuthService { constructor() { this.logger = new common_1.Logger(AuthService_1.name); @@ -187,14 +184,28 @@ let AuthService = AuthService_1 = class AuthService { .promise(); user = newUser; } - return { access_token: idToken, user }; + return { access_token: idToken, user, message: "Login Successful!" }; } catch (error) { - if (error instanceof Error) { + /* Login Failures */ + const cognitoError = error; + if (cognitoError.code) { + switch (cognitoError.code) { + case 'NotAuthorizedException': + this.logger.warn(`Login failed: ${cognitoError.message}`); + throw new common_1.UnauthorizedException('Incorrect username or password.'); + default: + this.logger.error(`Login failed: ${cognitoError.message}`, cognitoError.stack); + throw new common_1.InternalServerErrorException('An error occurred during login.'); + } + } + else if (error instanceof Error) { + // Handle non-AWS errors this.logger.error('Login failed', error.stack); - throw new Error(error.message || 'Login failed'); + throw new common_1.InternalServerErrorException(error.message || 'Login failed.'); } - throw new Error('An unknown error occurred during login'); + // Handle unknown errors + throw new common_1.InternalServerErrorException('An unknown error occurred during login.'); } } async setNewPassword(newPassword, session, username, email) { diff --git a/backend/dist/main.js b/backend/dist/main.js index f100103..75b977a 100644 --- a/backend/dist/main.js +++ b/backend/dist/main.js @@ -33,7 +33,9 @@ const aws_sdk_1 = __importDefault(require("aws-sdk")); /* ! */ async function bootstrap() { aws_sdk_1.default.config.update({ - region: process.env.AWS_REGION + region: process.env.AWS_REGION, + accessKeyId: process.env.OPEN_HATCH, + secretAccessKey: process.env.CLOSED_HATCH }); const app = await core_1.NestFactory.create(app_module_1.AppModule); app.enableCors(); diff --git a/backend/src/app.module.ts b/backend/src/app.module.ts index 0e7f346..6f0ab68 100644 --- a/backend/src/app.module.ts +++ b/backend/src/app.module.ts @@ -2,7 +2,7 @@ import { Module } from '@nestjs/common'; import { AuthModule } from './auth/auth.module'; import { UserModule } from './user/user.module'; import { GrantModule } from './grant/grant.module'; -import { NotificationsModule } from './notifications/notifications.module'; +import { NotificationsModule } from './notifications/notification.module'; @Module({ imports: [AuthModule, UserModule, GrantModule, NotificationsModule], diff --git a/backend/src/notifications/notifcation.service.ts b/backend/src/notifications/notifcation.service.ts new file mode 100644 index 0000000..be72104 --- /dev/null +++ b/backend/src/notifications/notifcation.service.ts @@ -0,0 +1,62 @@ +// src/notifications/notifications.service.ts +import { Injectable } from '@nestjs/common'; +import * as AWS from 'aws-sdk'; +import { Notification } from './notification.model'; // Adjust the path as needed + + + + +AWS.config.update({ region: 'us-east-1' }); +const dynamodb = new AWS.DynamoDB.DocumentClient(); + + +@Injectable() +export class NotificationService { + + + // Function to create a notification + async createNotification(notification: Notification): Promise { + + const alertTime = new Date(notification.alertTime); // Ensures a Date can be created from the given alertTime + + + const params = { + TableName: process.env.DYNAMODB_NOTIFICATION_TABLE_NAME || 'TABLE_FAILURE', + Item: { + ...notification, + alertTime: alertTime.toISOString(), + }, + }; + await dynamodb.put(params).promise(); + return notification; + } + + + // function to find notifications by notification id + async getNotificationByUserId(userId: string): Promise { + + + const params = { + TableName: process.env.DYNAMODB_NOTIFICATION_TABLE_NAME || 'TABLE_FAILURE', + Key: { + userId : userId + }, + }; + + + try { + const data = await dynamodb.get(params).promise(); + + + if (!data.Item) { + throw new Error('No notification with user id ' + userId + ' found.'); + } + + + return data.Item as Notification; + } catch (error) { + console.log(error) + throw new Error('Failed to retrieve notification.'); + } +} +} diff --git a/backend/src/notifications/notifcations.service.ts b/backend/src/notifications/notifcations.service.ts deleted file mode 100644 index bf90de2..0000000 --- a/backend/src/notifications/notifcations.service.ts +++ /dev/null @@ -1,14 +0,0 @@ -// src/notifications/notifications.service.ts -import { Injectable } from '@nestjs/common'; -import * as AWS from 'aws-sdk'; - -// idk about name but maybe? -const TABLE_NAME = 'BCANNotifications'; - -@Injectable() -export class NotificationsService { - - // function to make a notification - - // function to find notifications by user id -} \ No newline at end of file diff --git a/backend/src/notifications/notification.controller.ts b/backend/src/notifications/notification.controller.ts new file mode 100644 index 0000000..9f12ad3 --- /dev/null +++ b/backend/src/notifications/notification.controller.ts @@ -0,0 +1,27 @@ +// src/notifications/notifications.controller.ts +import { Controller, Post, Body, Get, Query } from '@nestjs/common'; +import { NotificationService } from './notifcation.service'; +import { Notification } from './notification.model'; + + +@Controller('notifications') +export class NotificationController { + + + constructor(private readonly notificationService: NotificationService) { } + + + @Post() + async create(@Body() notification: Partial): Promise { + // Call the service's createNotification method and return the result + return await this.notificationService.createNotification(notification as Notification); + } + + + @Get(':userId') + async findByUser(@Query('userId') userId: string) { + return await this.notificationService.getNotificationByUserId(userId); + } + + +} diff --git a/backend/src/notifications/notification.model.ts b/backend/src/notifications/notification.model.ts new file mode 100644 index 0000000..afec6be --- /dev/null +++ b/backend/src/notifications/notification.model.ts @@ -0,0 +1,6 @@ +export interface Notification { + notificationId: string; // Partition + userId: string; + message: string; + alertTime: Date; // Sort +} diff --git a/backend/src/notifications/notification.module.ts b/backend/src/notifications/notification.module.ts new file mode 100644 index 0000000..0117a21 --- /dev/null +++ b/backend/src/notifications/notification.module.ts @@ -0,0 +1,12 @@ +// src/notifications/notifications.module.ts +import { Module } from '@nestjs/common'; +import { NotificationController } from './notification.controller'; +import { NotificationService } from './notifcation.service'; + + +@Module({ + providers: [NotificationService], + controllers: [NotificationController], + exports: [NotificationService], +}) +export class NotificationsModule {} \ No newline at end of file diff --git a/backend/src/notifications/notifications.controller.ts b/backend/src/notifications/notifications.controller.ts deleted file mode 100644 index c758aff..0000000 --- a/backend/src/notifications/notifications.controller.ts +++ /dev/null @@ -1,17 +0,0 @@ -// src/notifications/notifications.controller.ts -import { Controller, Post, Body, Get, Query } from '@nestjs/common'; - -@Controller('notifications') -export class NotificationsController { - // constructor(private readonly notificationsService: NotificationsService) {} - - @Post() - async create(@Body() notification: Partial) { - // return this.notificationsService.create(notification); - } - - @Get() - async findByUser(@Query('userId') userId: string) { - // return this.notificationsService.findByUser(userId); - } -} \ No newline at end of file diff --git a/backend/src/notifications/notifications.module.ts b/backend/src/notifications/notifications.module.ts deleted file mode 100644 index 1f3400e..0000000 --- a/backend/src/notifications/notifications.module.ts +++ /dev/null @@ -1,11 +0,0 @@ -// src/notifications/notifications.module.ts -import { Module } from '@nestjs/common'; -import { NotificationsController } from './notifications.controller'; -import { NotificationsService } from './notifcations.service'; - -@Module({ - providers: [NotificationsService], - controllers: [NotificationsController], - exports: [NotificationsService], -}) -export class NotificationsModule {} \ No newline at end of file