Skip to content

Commit

Permalink
Merge pull request #1480 from AletheiaFact/script-novu-subscribers-fr…
Browse files Browse the repository at this point in the history
…om-db

Add script to create Novu subscribers from database users
  • Loading branch information
thesocialdev authored Dec 3, 2024
2 parents 530caa5 + f37d343 commit 6f85518
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions server/scripts/createNovuSubscribersFromDB.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { NestFactory } from "@nestjs/core";
import { AppModule } from "../app.module";
import { NestExpressApplication } from "@nestjs/platform-express";
import { UsersService } from "../users/users.service";
import { NotificationService } from "../notifications/notifications.service";
import { WinstonLogger } from "../winstonLogger";
import loadConfig from "../configLoader";

async function createNovuSubscriber(userFromDB, novuService) {
if (!userFromDB || !userFromDB.id) {
throw new Error(`Invalid user data: ${JSON.stringify(userFromDB)}`);
}

try {
await novuService.createSubscriber({
_id: userFromDB.id,
email: userFromDB.email,
name: userFromDB.name,
});
console.log(`Subscriber created for user ${userFromDB.email}`);
} catch (error) {
throw new Error(
`Failed to create Novu subscriber for user ${userFromDB.email}: ${error.message}`
);
}
}

async function initApp() {
const options = loadConfig();
const logger = new WinstonLogger();

const app = await NestFactory.create<NestExpressApplication>(
AppModule.register(options)
);

logger.log(`AppModule loaded`);

const userService = await app.resolve(UsersService);
const novuService = await app.resolve(NotificationService);

try {
const users = await userService.getAllUsers();

for (const user of users) {
await createNovuSubscriber(user, novuService);
logger.log(`Novu subscriber created for user: ${user.email}`);
}

logger.log("All users have been processed for Novu subscription.");
} catch (error) {
logger.error(
"An error occurred while creating Novu subscribers:",
error
);
} finally {
await app.close();
}
}

initApp();

0 comments on commit 6f85518

Please sign in to comment.