Skip to content

Commit

Permalink
Merge pull request #28 from Code-4-Community/josie
Browse files Browse the repository at this point in the history
implemented basic backend for notification
  • Loading branch information
josieek authored Nov 15, 2024
2 parents c8b11cc + 0b31dcb commit 7a5e8c9
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 52 deletions.
3 changes: 2 additions & 1 deletion backend/dist/app.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
25 changes: 18 additions & 7 deletions backend/dist/auth/auth.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion backend/dist/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
62 changes: 62 additions & 0 deletions backend/src/notifications/notifcation.service.ts
Original file line number Diff line number Diff line change
@@ -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<Notification> {

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<Notification> {


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.');
}
}
}
14 changes: 0 additions & 14 deletions backend/src/notifications/notifcations.service.ts

This file was deleted.

27 changes: 27 additions & 0 deletions backend/src/notifications/notification.controller.ts
Original file line number Diff line number Diff line change
@@ -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<Notification>): Promise<Notification> {
// 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);
}


}
6 changes: 6 additions & 0 deletions backend/src/notifications/notification.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface Notification {
notificationId: string; // Partition
userId: string;
message: string;
alertTime: Date; // Sort
}
12 changes: 12 additions & 0 deletions backend/src/notifications/notification.module.ts
Original file line number Diff line number Diff line change
@@ -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 {}
17 changes: 0 additions & 17 deletions backend/src/notifications/notifications.controller.ts

This file was deleted.

11 changes: 0 additions & 11 deletions backend/src/notifications/notifications.module.ts

This file was deleted.

0 comments on commit 7a5e8c9

Please sign in to comment.