Skip to content
Closed
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
5 changes: 5 additions & 0 deletions apps/backend/notifications/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@
"@sendgrid/mail": "^8.1.4",
"ioredis": "^5.11.1",
"jsonwebtoken": "^9.0.3",
"pg": "^8.21.0",
"pg-hstore": "^2.3.4",
"sequelize": "^6.37.8",
"web-push": "^3.6.7",
"ws": "^8.21.0"
},
"devDependencies": {
"@types/jsonwebtoken": "^9.0.10",
"@types/node": "^22.0.0",
"@types/validator": "^13.15.10",
"@types/ws": "^8.18.1",
"@types/web-push": "^3.6.4",
"@types/ws": "^8.5.0",
Comment on lines 28 to 34

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Remove the duplicate @types/ws entry.

package.json declares @types/ws twice with different versions. That makes dependency resolution ambiguous, and many tools will either reject the manifest or silently keep only the last value.

Suggested fix
   "devDependencies": {
     "`@types/jsonwebtoken`": "^9.0.10",
     "`@types/node`": "^22.0.0",
     "`@types/validator`": "^13.15.10",
     "`@types/ws`": "^8.18.1",
     "`@types/web-push`": "^3.6.4",
-    "`@types/ws`": "^8.5.0",
     "tsx": "^4.19.0",
     "typescript": "^5.7.0",
     "vitest": "^2.1.9"
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
"devDependencies": {
"@types/jsonwebtoken": "^9.0.10",
"@types/node": "^22.0.0",
"@types/validator": "^13.15.10",
"@types/ws": "^8.18.1",
"@types/web-push": "^3.6.4",
"@types/ws": "^8.5.0",
"devDependencies": {
"`@types/jsonwebtoken`": "^9.0.10",
"`@types/node`": "^22.0.0",
"`@types/validator`": "^13.15.10",
"`@types/ws`": "^8.18.1",
"`@types/web-push`": "^3.6.4",
🧰 Tools
🪛 Biome (2.5.0)

[error] 32-32: The key @types/ws was already declared.

(lint/suspicious/noDuplicateObjectKeys)

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@apps/backend/notifications/package.json` around lines 28 - 34, The
devDependencies block in package.json has a duplicate `@types/ws` entry with
conflicting versions, so remove one of the entries and keep a single consistent
`@types/ws` declaration. Make sure the remaining dependency version is the one
intended for the notifications package, and verify the manifest is valid with no
repeated keys.

Source: Linters/SAST tools

"tsx": "^4.19.0",
Expand Down
31 changes: 31 additions & 0 deletions apps/backend/notifications/src/db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Sequelize } from "sequelize";
import { createLogger } from "@delego/utils";

const log = createLogger("notifications:db", process.env.LOG_LEVEL ?? "info");

const databaseUrl = process.env.DATABASE_URL ?? "postgresql://delego:delego@localhost:5432/delego";
Comment thread
Malcolm-Wander marked this conversation as resolved.

export const sequelize = new Sequelize(databaseUrl, {
dialect: "postgres",
logging: (msg) => log.debug(msg),
pool: {
min: Number(process.env.DATABASE_POOL_MIN ?? 2),
max: Number(process.env.DATABASE_POOL_MAX ?? 10),
Comment thread
Malcolm-Wander marked this conversation as resolved.
acquire: 30000,
idle: 10000,
},
define: {
underscored: true,
timestamps: true,
},
});

export async function connectDb(): Promise<void> {
try {
await sequelize.authenticate();
log.info("Database connection established successfully in notifications service.");
} catch (err) {
log.error("Unable to connect to the database in notifications service", err instanceof Error ? { error: err.message } : { error: String(err) });
throw err;
}
}
3 changes: 3 additions & 0 deletions apps/backend/notifications/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import { createLogger, startHttpServer, route, json } from "@delego/utils";
import { initWebSocketServer } from "./websocket.js";
import { connectDb } from "./db.js";
import {
savePushSubscription,
removePushSubscription,
Expand All @@ -21,6 +22,8 @@ const port = Number(process.env.NOTIFICATIONS_PORT ?? DEFAULT_PORT);

log.info("Starting service", { port, nodeEnv });

// Connect to database first
await connectDb();
function readBody(req: IncomingMessage): Promise<unknown> {
return new Promise((resolve, reject) => {
let data = "";
Expand Down
47 changes: 47 additions & 0 deletions apps/backend/notifications/src/models/NotificationPreferences.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Model, DataTypes } from "sequelize";
import { sequelize } from "../db.js";

export interface NotificationPreferencesAttributes {
userId: string;
emailEnabled: boolean;
pushEnabled: boolean;
createdAt: Date;
updatedAt: Date;
}

export class NotificationPreferences extends Model<NotificationPreferencesAttributes> implements NotificationPreferencesAttributes {
public userId!: string;
public emailEnabled!: boolean;
public pushEnabled!: boolean;
public readonly createdAt!: Date;
public readonly updatedAt!: Date;
}

NotificationPreferences.init(
{
userId: {
type: DataTypes.UUID,
primaryKey: true,
field: "user_id",
},
emailEnabled: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: true,
field: "email_enabled",
},
pushEnabled: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: true,
field: "push_enabled",
},
},
{
sequelize,
modelName: "NotificationPreferences",
tableName: "notification_preferences",
timestamps: true,
underscored: true,
}
);
39 changes: 39 additions & 0 deletions apps/backend/notifications/src/notificationPreferences.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { NotificationPreferences } from "./models/NotificationPreferences.js";

export interface ChannelPreferences {
emailEnabled: boolean;
pushEnabled: boolean;
}

export async function getUserPreferences(userId: string): Promise<ChannelPreferences> {
const preferences = await NotificationPreferences.findByPk(userId);
if (preferences) {
return {
emailEnabled: preferences.emailEnabled,
pushEnabled: preferences.pushEnabled,
};
}
// Default to both enabled if no preferences found
return {
emailEnabled: true,
pushEnabled: true,
};
}

export async function upsertUserPreferences(
userId: string,
preferences: Partial<ChannelPreferences>
): Promise<ChannelPreferences> {
const [record] = await NotificationPreferences.upsert(
{
userId,
emailEnabled: preferences.emailEnabled ?? true,
pushEnabled: preferences.pushEnabled ?? true,
},
{ returning: true }
);
return {
emailEnabled: record.emailEnabled,
pushEnabled: record.pushEnabled,
};
}
Comment thread
Malcolm-Wander marked this conversation as resolved.
7 changes: 7 additions & 0 deletions database/migrations/004_notification_preferences.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE TABLE IF NOT EXISTS notification_preferences (
user_id UUID PRIMARY KEY REFERENCES users(id) ON DELETE CASCADE,
email_enabled BOOLEAN NOT NULL DEFAULT TRUE,
push_enabled BOOLEAN NOT NULL DEFAULT TRUE,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
Loading