Skip to content

Commit d190b1d

Browse files
committed
refactor: enhance user data handling with optional chaining and default values, and remove unused debug file write.
1 parent b29ae7e commit d190b1d

2 files changed

Lines changed: 6 additions & 7 deletions

File tree

src/helpers/user-helper.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
} from "../repositories/user-repository.js";
55

66
export const createUserIfNotExists = async (ctx) => {
7-
if (!ctx.chat.id) {
7+
if (!ctx.chat?.id) {
88
console.log("No chat id");
99
return;
1010
}
@@ -16,10 +16,10 @@ export const createUserIfNotExists = async (ctx) => {
1616

1717
const user = {
1818
chatId: ctx.chat?.id,
19-
firstName: ctx.from?.first_name,
20-
lastName: ctx.from?.last_name,
21-
username: ctx.from?.username,
22-
language_code: ctx.from?.language_code,
19+
firstName: ctx.from?.first_name || "",
20+
lastName: ctx.from?.last_name || "",
21+
username: ctx.from?.username || "",
22+
language_code: ctx.from?.language_code || "",
2323
};
2424

2525
const newUser = await createUser(user);

src/middlewares/userAuthAndSetupMiddleware.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
import { createUserIfNotExists } from "../helpers/user-helper.js";
88
import { BOT_USERNAME } from "../config/config.js";
99
import logger from "../helpers/logger.js";
10-
import fs from "fs";
1110

1211
export const userAuthAndSetupMiddleware = async (ctx, next) => {
1312
// reject if the user is a bot
@@ -30,7 +29,7 @@ export const userAuthAndSetupMiddleware = async (ctx, next) => {
3029

3130
export const checkGroupChatMiddleware = async (ctx, next) => {
3231
// save ctx in json file
33-
fs.writeFileSync("ctx.json", JSON.stringify(ctx));
32+
3433
if (checkGroupChat(ctx) && checkIfNewUser(ctx)) {
3534
const privateChatLink = `https://t.me/${BOT_USERNAME}`;
3635
const message = `Welcome to the group! For any study materials, you can use our bot directly: <a href="${privateChatLink}">Start Private Chat with Learnerse Bot</a>`;

0 commit comments

Comments
 (0)