Skip to content

Commit 7a5e8c9

Browse files
authored
Merge pull request #28 from Code-4-Community/josie
implemented basic backend for notification
2 parents c8b11cc + 0b31dcb commit 7a5e8c9

11 files changed

+131
-52
lines changed

backend/dist/app.module.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ const common_1 = require("@nestjs/common");
1111
const auth_module_1 = require("./auth/auth.module");
1212
const user_module_1 = require("./user/user.module");
1313
const grant_module_1 = require("./grant/grant.module");
14+
const notification_module_1 = require("./notifications/notification.module");
1415
let AppModule = class AppModule {
1516
};
1617
AppModule = __decorate([
1718
(0, common_1.Module)({
18-
imports: [auth_module_1.AuthModule, user_module_1.UserModule, grant_module_1.GrantModule],
19+
imports: [auth_module_1.AuthModule, user_module_1.UserModule, grant_module_1.GrantModule, notification_module_1.NotificationsModule],
1920
})
2021
], AppModule);
2122
exports.AppModule = AppModule;

backend/dist/auth/auth.service.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@ exports.AuthService = void 0;
3737
const common_1 = require("@nestjs/common");
3838
const aws_sdk_1 = __importDefault(require("aws-sdk"));
3939
const crypto = __importStar(require("crypto"));
40-
aws_sdk_1.default.config.update({
41-
region: process.env.AWS_REGION,
42-
});
4340
let AuthService = AuthService_1 = class AuthService {
4441
constructor() {
4542
this.logger = new common_1.Logger(AuthService_1.name);
@@ -187,14 +184,28 @@ let AuthService = AuthService_1 = class AuthService {
187184
.promise();
188185
user = newUser;
189186
}
190-
return { access_token: idToken, user };
187+
return { access_token: idToken, user, message: "Login Successful!" };
191188
}
192189
catch (error) {
193-
if (error instanceof Error) {
190+
/* Login Failures */
191+
const cognitoError = error;
192+
if (cognitoError.code) {
193+
switch (cognitoError.code) {
194+
case 'NotAuthorizedException':
195+
this.logger.warn(`Login failed: ${cognitoError.message}`);
196+
throw new common_1.UnauthorizedException('Incorrect username or password.');
197+
default:
198+
this.logger.error(`Login failed: ${cognitoError.message}`, cognitoError.stack);
199+
throw new common_1.InternalServerErrorException('An error occurred during login.');
200+
}
201+
}
202+
else if (error instanceof Error) {
203+
// Handle non-AWS errors
194204
this.logger.error('Login failed', error.stack);
195-
throw new Error(error.message || 'Login failed');
205+
throw new common_1.InternalServerErrorException(error.message || 'Login failed.');
196206
}
197-
throw new Error('An unknown error occurred during login');
207+
// Handle unknown errors
208+
throw new common_1.InternalServerErrorException('An unknown error occurred during login.');
198209
}
199210
}
200211
async setNewPassword(newPassword, session, username, email) {

backend/dist/main.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ const aws_sdk_1 = __importDefault(require("aws-sdk"));
3333
/* ! */
3434
async function bootstrap() {
3535
aws_sdk_1.default.config.update({
36-
region: process.env.AWS_REGION
36+
region: process.env.AWS_REGION,
37+
accessKeyId: process.env.OPEN_HATCH,
38+
secretAccessKey: process.env.CLOSED_HATCH
3739
});
3840
const app = await core_1.NestFactory.create(app_module_1.AppModule);
3941
app.enableCors();

backend/src/app.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Module } from '@nestjs/common';
22
import { AuthModule } from './auth/auth.module';
33
import { UserModule } from './user/user.module';
44
import { GrantModule } from './grant/grant.module';
5-
import { NotificationsModule } from './notifications/notifications.module';
5+
import { NotificationsModule } from './notifications/notification.module';
66

77
@Module({
88
imports: [AuthModule, UserModule, GrantModule, NotificationsModule],
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// src/notifications/notifications.service.ts
2+
import { Injectable } from '@nestjs/common';
3+
import * as AWS from 'aws-sdk';
4+
import { Notification } from './notification.model'; // Adjust the path as needed
5+
6+
7+
8+
9+
AWS.config.update({ region: 'us-east-1' });
10+
const dynamodb = new AWS.DynamoDB.DocumentClient();
11+
12+
13+
@Injectable()
14+
export class NotificationService {
15+
16+
17+
// Function to create a notification
18+
async createNotification(notification: Notification): Promise<Notification> {
19+
20+
const alertTime = new Date(notification.alertTime); // Ensures a Date can be created from the given alertTime
21+
22+
23+
const params = {
24+
TableName: process.env.DYNAMODB_NOTIFICATION_TABLE_NAME || 'TABLE_FAILURE',
25+
Item: {
26+
...notification,
27+
alertTime: alertTime.toISOString(),
28+
},
29+
};
30+
await dynamodb.put(params).promise();
31+
return notification;
32+
}
33+
34+
35+
// function to find notifications by notification id
36+
async getNotificationByUserId(userId: string): Promise<Notification> {
37+
38+
39+
const params = {
40+
TableName: process.env.DYNAMODB_NOTIFICATION_TABLE_NAME || 'TABLE_FAILURE',
41+
Key: {
42+
userId : userId
43+
},
44+
};
45+
46+
47+
try {
48+
const data = await dynamodb.get(params).promise();
49+
50+
51+
if (!data.Item) {
52+
throw new Error('No notification with user id ' + userId + ' found.');
53+
}
54+
55+
56+
return data.Item as Notification;
57+
} catch (error) {
58+
console.log(error)
59+
throw new Error('Failed to retrieve notification.');
60+
}
61+
}
62+
}

backend/src/notifications/notifcations.service.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// src/notifications/notifications.controller.ts
2+
import { Controller, Post, Body, Get, Query } from '@nestjs/common';
3+
import { NotificationService } from './notifcation.service';
4+
import { Notification } from './notification.model';
5+
6+
7+
@Controller('notifications')
8+
export class NotificationController {
9+
10+
11+
constructor(private readonly notificationService: NotificationService) { }
12+
13+
14+
@Post()
15+
async create(@Body() notification: Partial<Notification>): Promise<Notification> {
16+
// Call the service's createNotification method and return the result
17+
return await this.notificationService.createNotification(notification as Notification);
18+
}
19+
20+
21+
@Get(':userId')
22+
async findByUser(@Query('userId') userId: string) {
23+
return await this.notificationService.getNotificationByUserId(userId);
24+
}
25+
26+
27+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export interface Notification {
2+
notificationId: string; // Partition
3+
userId: string;
4+
message: string;
5+
alertTime: Date; // Sort
6+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// src/notifications/notifications.module.ts
2+
import { Module } from '@nestjs/common';
3+
import { NotificationController } from './notification.controller';
4+
import { NotificationService } from './notifcation.service';
5+
6+
7+
@Module({
8+
providers: [NotificationService],
9+
controllers: [NotificationController],
10+
exports: [NotificationService],
11+
})
12+
export class NotificationsModule {}

backend/src/notifications/notifications.controller.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)