Skip to content

Commit

Permalink
#3044-merged feature branch
Browse files Browse the repository at this point in the history
  • Loading branch information
caiodasilva2005 committed Dec 22, 2024
2 parents c36bc65 + 2be6d82 commit 2882d54
Show file tree
Hide file tree
Showing 59 changed files with 1,076 additions and 448 deletions.
7 changes: 7 additions & 0 deletions src/backend/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ import workPackageTemplatesRouter from './src/routes/work-package-templates.rout
import carsRouter from './src/routes/cars.routes';
import organizationRouter from './src/routes/organizations.routes';
import recruitmentRouter from './src/routes/recruitment.routes';
<<<<<<< HEAD
import { slackEvents } from './src/routes/slack.routes';
=======
import announcementsRouter from './src/routes/announcements.routes';
import onboardingRouter from './src/routes/onboarding.routes';
>>>>>>> #3074-GetUnreadAnnouncements

const app = express();

Expand Down Expand Up @@ -73,6 +78,8 @@ app.use('/templates', workPackageTemplatesRouter);
app.use('/cars', carsRouter);
app.use('/organizations', organizationRouter);
app.use('/recruitment', recruitmentRouter);
app.use('/announcements', announcementsRouter);
app.use('/onboarding', onboardingRouter);
app.use('/', (_req, res) => {
res.status(200).json('Welcome to FinishLine');
});
Expand Down
18 changes: 18 additions & 0 deletions src/backend/src/controllers/announcements.controllers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { NextFunction, Request, Response } from 'express';
import AnnouncementService from '../services/announcement.service';

export default class AnnouncementController {
static async getUserUnreadAnnouncements(req: Request, res: Response, next: NextFunction) {
try {
const { organization, currentUser } = req;

const unreadAnnouncements = await AnnouncementService.getUserUnreadAnnouncements(
currentUser.userId,
organization.organizationId
);
res.status(200).json(unreadAnnouncements);
} catch (error: unknown) {
next(error);
}
}
}
30 changes: 30 additions & 0 deletions src/backend/src/controllers/notifications.controllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,34 @@ export default class NotificationsController {
next(error);
}
}

static async getUserUnreadNotifications(req: Request, res: Response, next: NextFunction) {
try {
const { organization, currentUser } = req;

const unreadNotifications = await NotificationsService.getUserUnreadNotifications(
currentUser.userId,
organization.organizationId
);
res.status(200).json(unreadNotifications);
} catch (error: unknown) {
next(error);
}
}

static async removeUserNotification(req: Request, res: Response, next: NextFunction) {
try {
const { notificationId } = req.params;
const { organization, currentUser } = req;

const unreadNotifications = await NotificationsService.removeUserNotification(
currentUser.userId,
notificationId,
organization.organizationId
);
res.status(200).json(unreadNotifications);
} catch (error: unknown) {
next(error);
}
}
}
21 changes: 21 additions & 0 deletions src/backend/src/controllers/onboarding.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { NextFunction, Request, Response } from 'express';
import OnboardingServices from '../services/onboarding.services';

export default class OnboardingController {
static async downloadImage(req: Request, res: Response, next: NextFunction) {
try {
const { fileId } = req.params;

const imageData = await OnboardingServices.downloadImage(fileId);

// Set the appropriate headers for the HTTP response
res.setHeader('content-type', String(imageData.type));
res.setHeader('content-length', imageData.buffer.length);

// Send the Buffer as the response body
res.send(imageData.buffer);
} catch (error: unknown) {
return next(error);
}
}
}
20 changes: 19 additions & 1 deletion src/backend/src/controllers/organizations.controllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export default class OrganizationsController {
if (!req.file) {
throw new HttpException(400, 'Invalid or undefined image data');
}

const updatedOrg = await OrganizationsService.setLogoImage(req.file, req.currentUser, req.organization);

res.status(200).json(updatedOrg);
Expand All @@ -88,7 +89,9 @@ export default class OrganizationsController {

static async getOrganizationLogoImage(req: Request, res: Response, next: NextFunction) {
try {
const logoImageId = await OrganizationsService.getLogoImage(req.organization.organizationId);
const { organization } = req;

const logoImageId = await OrganizationsService.getLogoImage(organization.organizationId);
res.status(200).json(logoImageId);
} catch (error: unknown) {
next(error);
Expand Down Expand Up @@ -117,4 +120,19 @@ export default class OrganizationsController {
next(error);
}
}

static async setSlackWorkspaceId(req: Request, res: Response, next: NextFunction) {
try {
const { workspaceId } = req.body;

const updatedOrg = await OrganizationsService.setSlackWorkspaceId(
workspaceId,
req.currentUser,
req.organization.organizationId
);
res.status(200).json(updatedOrg);
} catch (error: unknown) {
next(error);
}
}
}
38 changes: 0 additions & 38 deletions src/backend/src/controllers/users.controllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,42 +191,4 @@ export default class UsersController {
next(error);
}
}

static async getUserUnreadNotifications(req: Request, res: Response, next: NextFunction) {
try {
const { organization, currentUser } = req;

const unreadNotifications = await UsersService.getUserUnreadNotifications(currentUser.userId, organization);
res.status(200).json(unreadNotifications);
} catch (error: unknown) {
next(error);
}
}

static async removeUserNotification(req: Request, res: Response, next: NextFunction) {
try {
const { notificationId } = req.params;
const { organization, currentUser } = req;

const unreadNotifications = await UsersService.removeUserNotification(
currentUser.userId,
notificationId,
organization
);
res.status(200).json(unreadNotifications);
} catch (error: unknown) {
next(error);
}
}

static async getUserUnreadAnnouncements(req: Request, res: Response, next: NextFunction) {
try {
const { organization, currentUser } = req;

const unreadAnnouncements = await UsersService.getUserUnreadAnnouncements(currentUser.userId, organization);
res.status(200).json(unreadAnnouncements);
} catch (error: unknown) {
next(error);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ ALTER TABLE "Project" ADD COLUMN "organizationId" TEXT;
CREATE TABLE "Announcement" (
"announcementId" TEXT NOT NULL,
"text" TEXT NOT NULL,
"dateCreated" TIMESTAMP(3) NOT NULL,
"dateMessageSent" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"dateDeleted" TIMESTAMP(3),
"senderName" TEXT NOT NULL,
"slackEventId" TEXT NOT NULL,
Expand Down
2 changes: 1 addition & 1 deletion src/backend/src/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -935,7 +935,7 @@ model Announcement {
announcementId String @id @default(uuid())
text String
usersReceived User[] @relation("receivedAnnouncements")
dateCreated DateTime
dateMessageSent DateTime @default(now())
dateDeleted DateTime?
senderName String
slackEventId String @unique
Expand Down
8 changes: 8 additions & 0 deletions src/backend/src/routes/announcements.routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import express from 'express';
import AnnouncementController from '../controllers/announcements.controllers';

const announcementsRouter = express.Router();

announcementsRouter.get('/current-user', AnnouncementController.getUserUnreadAnnouncements);

export default announcementsRouter;
2 changes: 2 additions & 0 deletions src/backend/src/routes/notifications.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ import NotificationsController from '../controllers/notifications.controllers';
const notificationsRouter = express.Router();

notificationsRouter.post('/task-deadlines', NotificationsController.sendDailySlackNotifications);
notificationsRouter.get('/current-user', NotificationsController.getUserUnreadNotifications);
notificationsRouter.post('/:notificationId/remove', NotificationsController.removeUserNotification);

export default notificationsRouter;
8 changes: 8 additions & 0 deletions src/backend/src/routes/onboarding.routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import express from 'express';
import OnboardingController from '../controllers/onboarding.controller';

const onboardingRouter = express.Router();

onboardingRouter.get('/image/:fileId', OnboardingController.downloadImage);

export default onboardingRouter;
5 changes: 5 additions & 0 deletions src/backend/src/routes/organizations.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,9 @@ organizationRouter.post(
OrganizationsController.setOrganizationDescription
);
organizationRouter.get('/featured-projects', OrganizationsController.getOrganizationFeaturedProjects);
organizationRouter.post(
'/workspaceId/set',
nonEmptyString(body('workspaceId')),
OrganizationsController.setSlackWorkspaceId
);
export default organizationRouter;
3 changes: 0 additions & 3 deletions src/backend/src/routes/users.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,5 @@ userRouter.post(
validateInputs,
UsersController.getManyUserTasks
);
userRouter.get('/notifications/current-user', UsersController.getUserUnreadNotifications);
userRouter.get('/announcements/current-user', UsersController.getUserUnreadAnnouncements);
userRouter.post('/notifications/:notificationId/remove', UsersController.removeUserNotification);

export default userRouter;
26 changes: 24 additions & 2 deletions src/backend/src/services/announcement.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import prisma from '../prisma/prisma';
import { getAnnouncementQueryArgs } from '../prisma-query-args/announcements.query.args';
import announcementTransformer from '../transformers/announcements.transformer';
import { NotFoundException } from '../utils/errors.utils';
import { HttpException } from '../utils/errors.utils';

export default class AnnouncementService {
/**
Expand All @@ -20,7 +21,7 @@ export default class AnnouncementService {
static async createAnnouncement(
text: string,
usersReceivedIds: string[],
dateCreated: Date,
dateMessageSent: Date,
senderName: string,
slackEventId: string,
slackChannelName: string,
Expand All @@ -34,7 +35,7 @@ export default class AnnouncementService {
userId: id
}))
},
dateCreated,
dateMessageSent,
senderName,
slackEventId,
slackChannelName
Expand Down Expand Up @@ -104,4 +105,25 @@ export default class AnnouncementService {

return announcementTransformer(announcement);
}

/**
* Gets all of a user's unread announcements
* @param userId id of the current user
* @param organization the user's orgainzation
* @returns the unread announcements of the user
*/
static async getUserUnreadAnnouncements(userId: string, organizationId: string) {
const unreadAnnouncements = await prisma.announcement.findMany({
where: {
usersReceived: {
some: { userId }
}
},
...getAnnouncementQueryArgs(organizationId)
});

if (!unreadAnnouncements) throw new HttpException(404, 'User Unread Announcements Not Found');

return unreadAnnouncements.map(announcementTransformer);
}
}
Loading

0 comments on commit 2882d54

Please sign in to comment.