-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from Code-4-Community/josie
implemented basic backend for notification
- Loading branch information
Showing
11 changed files
with
131 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.'); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.