Skip to content
This repository has been archived by the owner on Sep 29, 2024. It is now read-only.

feat: notification schema and API #258

Merged
merged 7 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions client/src/lib/api-types/notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,25 @@
* SPDX-License-Identifier: GPL-3.0-only
*/

import * as z from 'zod';

import { notification } from './schemas';
import type { SuccessResponse, ErrorResponse } from './index';

/**
* Successful response for /v1/feedback endpoint
* Successful response for /v1/notification endpoint
*/
type Notification = {
id: number;
notificationMessage: string;
notificationType: string;
createdAt: Date;
updatedAt: Date;
};
export interface GetNotificationSuccAPI
extends SuccessResponse<z.infer<typeof notification.notificationSchema>> {}
extends SuccessResponse<Notification[]> {}
export type GetNotificationFailAPI = ErrorResponse<'Invalid ID!'>;

/**
* Failure response for feedback-related endpoints
* Successful response for /v1/notification/archive
*/
export type GetNotificationFailAPI =
ErrorResponse<'An unexpected error occurred. Please try again later!'>;
export interface NotificationArchiveSuccessAPI
extends SuccessResponse<{ deleted: true }> {}
export type NotificationArchiveFailAPI = ErrorResponse<'Invalid key!'>;
22 changes: 14 additions & 8 deletions server/src/lib/api-types/notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,25 @@
* SPDX-License-Identifier: GPL-3.0-only
*/

import * as z from 'zod';

import { notification } from './schemas';
import type { SuccessResponse, ErrorResponse } from './index';

/**
* Successful response for /v1/feedback endpoint
* Successful response for /v1/notification endpoint
*/
type Notification = {
id: number;
notificationMessage: string;
notificationType: string;
createdAt: Date;
updatedAt: Date;
};
export interface GetNotificationSuccAPI
extends SuccessResponse<z.infer<typeof notification.notificationSchema>> {}
extends SuccessResponse<Notification[]> {}
export type GetNotificationFailAPI = ErrorResponse<'Invalid ID!'>;

/**
* Failure response for feedback-related endpoints
* Successful response for /v1/notification/archive
*/
export type GetNotificationFailAPI =
ErrorResponse<'An unexpected error occurred. Please try again later!'>;
export interface NotificationArchiveSuccessAPI
extends SuccessResponse<{ deleted: true }> {}
export type NotificationArchiveFailAPI = ErrorResponse<'Invalid key!'>;
51 changes: 51 additions & 0 deletions server/src/v1/notification/delete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* SPDX-FileCopyrightText: 2024 Ng Jun Xiang <[email protected]>
*
* SPDX-License-Identifier: GPL-3.0-only
*/

import { and, eq } from 'drizzle-orm';
import { db } from '../../db';
import { notificationTable } from '../../db/schemas';
import type { IAuthedRouteHandler } from '../../route-map';
import type {
NotificationArchiveSuccessAPI,
NotificationArchiveFailAPI,
} from '../../lib/api-types/notification';

// Handler for /v1/notification/archive
const archiveNotification: IAuthedRouteHandler = async (req, res) => {
const { id } = req.params;
const castedId = parseInt(id);

if (isNaN(castedId)) {
return res.status(400).json({
status: 400,
errors: [{ message: 'Invalid ID!' }],
} satisfies NotificationArchiveFailAPI);
}

if (castedId < 0) {
return res.status(400).json({
status: 400,
errors: [{ message: 'Invalid ID!' }],
} satisfies NotificationArchiveFailAPI);
}

await db
.delete(notificationTable)
.where(
and(
eq(notificationTable.userId, req.user.id),
eq(notificationTable.id, castedId),
),
);

return res.status(200).json({
status: 200,
data: {
deleted: true,
},
} satisfies NotificationArchiveSuccessAPI);
};
export default archiveNotification;
49 changes: 49 additions & 0 deletions server/src/v1/notification/notification.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* SPDX-FileCopyrightText: 2024 Ng Jun Xiang <[email protected]>
*
* SPDX-License-Identifier: GPL-3.0-only
*/

import { db } from '../../db';
import { notificationTable } from '../../db/schemas';
import { IAuthedRouteHandler } from '../../route-map';
import {
GetNotificationSuccAPI,
GetNotificationFailAPI,
} from '../../lib/api-types/notification';

//
const getNotification: IAuthedRouteHandler = async (req, res) => {
const { id } = req.params;

// Find id
const castedId = parseInt(id);
if (isNaN(castedId)) {
return res.status(400).json({
status: 400,
errors: [{ message: 'Invalid ID!' }],
} satisfies GetNotificationFailAPI);
}

if (castedId < 0) {
return res.status(400).json({
status: 400,
errors: [{ message: 'Invalid ID!' }],
} satisfies GetNotificationFailAPI);
}

// Build query
const found = await db.select().from(notificationTable);

return res.status(200).json({
status: 200,
data: found.map((n) => ({
id: n.id,
notificationMessage: n.notificationMessage,
notificationType: n.notificationType,
createdAt: n.createdAt,
updatedAt: n.updatedAt,
})),
} satisfies GetNotificationSuccAPI);
};
export default getNotification;
27 changes: 27 additions & 0 deletions server/src/v1/notification/route-map.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* SPDX-FileCopyrightText: 2024 Ng Jun Xiang <[email protected]>
*
* SPDX-License-Identifier: GPL-3.0-only
*/

import type { RoutingMap } from '../../route-map';
import getNotification from './notification';
import archiveNotification from './delete';

const routeMap: RoutingMap<`/v1/notification${string}`> = {
'/v1/notification': {
GET: {
handler: getNotification,
accessLevel: 'authenticated',
authOptions: { allowNonActivated: true },
},
},
'/v1/notification/archive': {
POST: {
handler: archiveNotification,
accessLevel: 'authenticated',
authOptions: { allowNonActivated: true },
},
},
} as const;
export default routeMap;
2 changes: 2 additions & 0 deletions server/src/v1/route-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ import type { RoutingMap } from '../route-map';
import authRoutes from './auth/route-map';
import foodRoutes from './food/route-map';
import { createFeedback } from './feedback';
import notificationRoutes from './notification/route-map';
import { getUser, updateUser, deleteUser } from './user';
import { createEvent, deleteEvent, getEvent } from './event';

const routeMap: RoutingMap<`/v1/${string}`> = {
...authRoutes,
...foodRoutes,
...notificationRoutes,
'/v1/user': {
GET: {
handler: getUser,
Expand Down
22 changes: 14 additions & 8 deletions shared/api-types/src/notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,25 @@
* SPDX-License-Identifier: GPL-3.0-only
*/

import * as z from 'zod';

import { notification } from './schemas';
import type { SuccessResponse, ErrorResponse } from './index';

/**
* Successful response for /v1/feedback endpoint
* Successful response for /v1/notification endpoint
*/
type Notification = {
id: number;
notificationMessage: string;
notificationType: string;
createdAt: Date;
updatedAt: Date;
};
export interface GetNotificationSuccAPI
extends SuccessResponse<z.infer<typeof notification.notificationSchema>> {}
extends SuccessResponse<Notification[]> {}
export type GetNotificationFailAPI = ErrorResponse<'Invalid ID!'>;

/**
* Failure response for feedback-related endpoints
* Successful response for /v1/notification/archive
*/
export type GetNotificationFailAPI =
ErrorResponse<'An unexpected error occurred. Please try again later!'>;
export interface NotificationArchiveSuccessAPI
extends SuccessResponse<{ deleted: true }> {}
export type NotificationArchiveFailAPI = ErrorResponse<'Invalid key!'>;