idbot / src /middlewares /firstStartedBotMiddleware.ts
ItsMa8di's picture
inital commit
a8ce9cb
Raw History Blame Contribute Delete
1.18 kB
import { NextFunction } from "grammy";
import { BotContext } from "../../types";
import { UserController } from "../controllers/userController";
import { IUser } from "../models/User";
export default async function firstStartedBotMiddleware(
ctx: BotContext,
next: NextFunction,
) {
try {
// TODO: FETCH USER FROM THE DB AND IF DIDN'T EXISTS ADD IT TO IT
const userid: number | undefined = ctx.from?.id;
const username: string | undefined = ctx.from?.username;
const firstname: string | undefined = ctx.from?.first_name;
const lastname: string | undefined = ctx.from?.last_name;
const isPremium: boolean = ctx.from?.is_premium as boolean;
///////////////////////////////////////////
if (!userid) return;
const user = await UserController.getByUserid(userid as number);
if (!user) {
// INSERT USER
const newUser: IUser = {
userid,
username,
firstname,
lastname,
isPremium,
savedAccountTag: "",
createdAt: new Date(),
};
await UserController.create(newUser);
}
await next();
} catch (e) {
console.error(e);
return;
} finally {
}
}