diff --git a/src/db/db.ts b/src/db/db.ts index ed18cec..180856b 100644 --- a/src/db/db.ts +++ b/src/db/db.ts @@ -1,6 +1,6 @@ import ConnectSessionSequelize from "connect-session-sequelize"; import session from "express-session"; -import { DataTypes, Model, Optional, Sequelize } from "sequelize"; +import { Sequelize } from "sequelize"; const SequelizeStore = ConnectSessionSequelize(session.Store); @@ -41,71 +41,4 @@ export const storeDB: Sequelize = (() => { export const store = new SequelizeStore({ db: storeDB, }); - -export type UserRowID = number; -export type TelegramID = number; -export type VerificationString = string; - -interface ContactAttributes { - user: UserRowID; - with: UserRowID; -} -export interface ContactInterface - extends Model, - ContactAttributes {} -export const Contact = sequelize.define("Contact", { - user: { - type: DataTypes.INTEGER, - allowNull: false, - }, - with: { - type: DataTypes.INTEGER, - allowNull: false, - }, -}); - -interface UserAttributes { - id: UserRowID; - telegram: TelegramID; - verification: VerificationString; - isInfected: boolean; -} -interface UserCreationAttributes { - telegram: TelegramID; -} -export interface UserInstance - extends Model, - UserAttributes {} - -export const User = sequelize.define("User", { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, - telegram: { - type: DataTypes.INTEGER, - allowNull: false, - unique: true, - }, - verification: { - type: DataTypes.STRING, - }, - isInfected: { - type: DataTypes.BOOLEAN, - }, -}); - -Contact.sync(); - -User.sync().then(() => { - if (process.env.ADMIN_USERNAME && process.env.ADMIN_PASSWORD) { - User.create({ - telegram: 12345, - }).catch(() => { - console.log("Couldn't create admin account. Probably exists."); - }); - } -}); - store.sync(); diff --git a/src/db/models/Contact.ts b/src/db/models/Contact.ts new file mode 100644 index 0000000..c009cca --- /dev/null +++ b/src/db/models/Contact.ts @@ -0,0 +1,23 @@ +import { DataTypes, Model } from "sequelize"; +import { UserRowID } from "../../types"; +import { sequelize } from "../db"; + +interface ContactAttributes { + user: UserRowID; + with: UserRowID; +} +export interface ContactInterface + extends Model, + ContactAttributes {} +export const Contact = sequelize.define("Contact", { + user: { + type: DataTypes.INTEGER, + allowNull: false, + }, + with: { + type: DataTypes.INTEGER, + allowNull: false, + }, +}); + +Contact.sync(); diff --git a/src/db/models/User.ts b/src/db/models/User.ts new file mode 100644 index 0000000..b16b2b3 --- /dev/null +++ b/src/db/models/User.ts @@ -0,0 +1,45 @@ +import { DataTypes, Model } from "sequelize"; +import { TelegramID, UserRowID, VerificationString } from "../../types"; +import { sequelize } from "../db"; + +interface UserAttributes { + id: UserRowID; + telegram: TelegramID; + verification: VerificationString; + isInfected: boolean; +} +interface UserCreationAttributes { + telegram: TelegramID; +} +export interface UserInstance + extends Model, + UserAttributes {} + +export const User = sequelize.define("User", { + id: { + type: DataTypes.INTEGER, + autoIncrement: true, + primaryKey: true, + }, + telegram: { + type: DataTypes.INTEGER, + allowNull: false, + unique: true, + }, + verification: { + type: DataTypes.STRING, + }, + isInfected: { + type: DataTypes.BOOLEAN, + }, +}); + +User.sync().then(() => { + if (process.env.ADMIN_USERNAME && process.env.ADMIN_PASSWORD) { + User.create({ + telegram: 12345, + }).catch(() => { + console.log("Couldn't create admin account. Probably exists."); + }); + } +}); diff --git a/src/db/utils.ts b/src/db/utils.ts index 552463d..e69e054 100644 --- a/src/db/utils.ts +++ b/src/db/utils.ts @@ -1,6 +1,8 @@ import { strings_en } from "../strings"; import { sendTelegramMessage } from "../telegram"; -import { User, Contact, TelegramID, UserRowID } from "./db"; +import { TelegramID, UserRowID } from "../types"; +import { Contact } from "./models/Contact"; +import { User } from "./models/User"; export function addContact( userATelegram: TelegramID, @@ -19,10 +21,7 @@ export function addContact( console.log( `Registering contact between ${userA!.id} and ${userBRowID}` ); - sendTelegramMessage( - userB!.telegram, - strings_en.telegram_qr_scanned, - ); + sendTelegramMessage(userB!.telegram, strings_en.telegram_qr_scanned); done(true, "Successfully added contact"); }) .catch((e) => { diff --git a/src/routes/CodeRoute.ts b/src/routes/CodeRoute.ts index aee5b49..ee491b6 100644 --- a/src/routes/CodeRoute.ts +++ b/src/routes/CodeRoute.ts @@ -1,13 +1,8 @@ import { Request, Response } from "express"; -import { TelegramID, User, UserInstance } from "../db/db"; import bcrypt from "bcrypt"; import QRCode, { QRCodeToDataURLOptions } from "qrcode"; - -declare module "express-session" { - interface Session { - user: TelegramID; - } -} +import { TelegramID } from "../types"; +import { User, UserInstance } from "../db/models/User"; export function CodeRoute(req: Request, res: Response) { if (!req.session.user) { diff --git a/src/routes/LoginRoute.ts b/src/routes/LoginRoute.ts index f0329db..c40075a 100644 --- a/src/routes/LoginRoute.ts +++ b/src/routes/LoginRoute.ts @@ -1,14 +1,8 @@ import { Request, Response } from "express"; -import { TelegramID, User, UserRowID } from "../db/db"; import crypto from "crypto"; import { addContact, createUser } from "../db/utils"; - -declare module "express-session" { - interface Session { - verified: boolean; - verifiedBy: UserRowID; - } -} +import { TelegramID, UserRowID } from "../types"; +import { User } from "../db/models/User"; type TelegramLoginResponse = { id: TelegramID; diff --git a/src/routes/TelegramWebhookRoute.ts b/src/routes/TelegramWebhookRoute.ts index 983b9ee..3bd7cab 100644 --- a/src/routes/TelegramWebhookRoute.ts +++ b/src/routes/TelegramWebhookRoute.ts @@ -1,8 +1,10 @@ import { Request, Response } from "express"; import { Op } from "sequelize"; -import { Contact, TelegramID, User } from "../db/db"; +import { Contact } from "../db/models/Contact"; +import { User } from "../db/models/User"; import { strings_en } from "../strings"; import { sendTelegramMessage } from "../telegram"; +import { TelegramID } from "../types"; interface TelegramWebhookRequest extends Request { body: { @@ -27,22 +29,22 @@ export function TelegramWebhookRoute( "Thanks for using OurSejahtera! Let's stay safer together <3" ); } else { - const messageText = req.body.message.text; - const telegramID = req.body.message.from.id; - if (messageText.toLowerCase() == "/covidpositive") { - userInfected(telegramID, (result) => { - if (result.saved) { - sendTelegramMessage( - telegramID, - strings_en.telegram_inform_positive, - ); - informContacts(telegramID); - } else { - sendTelegramMessage(telegramID, "Sorry, something went wrong."); - } - }); + const messageText = req.body.message.text; + const telegramID = req.body.message.from.id; + if (messageText.toLowerCase() == "/covidpositive") { + userInfected(telegramID, (result) => { + if (result.saved) { + sendTelegramMessage( + telegramID, + strings_en.telegram_inform_positive + ); + informContacts(telegramID); + } else { + sendTelegramMessage(telegramID, "Sorry, something went wrong."); + } + }); + } } - } } catch (e) { console.log("Could not get Telegram Message"); } @@ -72,7 +74,10 @@ function informContacts(telegramID: TelegramID) { }, }).then((otherPerson) => { otherPerson && - sendTelegramMessage(otherPerson.telegram, strings_en.telegram_inform_infect); + sendTelegramMessage( + otherPerson.telegram, + strings_en.telegram_inform_infect + ); }); }); }); diff --git a/src/routes/VerifyRoute.ts b/src/routes/VerifyRoute.ts index 8a3b47b..92e93e5 100644 --- a/src/routes/VerifyRoute.ts +++ b/src/routes/VerifyRoute.ts @@ -1,6 +1,7 @@ import { Request, Response } from "express"; -import { User, UserRowID, VerificationString } from "../db/db"; +import { User } from "../db/models/User"; import { addContact } from "../db/utils"; +import { UserRowID, VerificationString } from "../types"; interface VerifyRequest extends Request { body: { @@ -45,4 +46,4 @@ function checkVerification( callback(false, "No such verification"); } }); -} \ No newline at end of file +} diff --git a/src/telegram.ts b/src/telegram.ts index 1bac0b3..7d631ab 100644 --- a/src/telegram.ts +++ b/src/telegram.ts @@ -1,5 +1,5 @@ import axios from "axios"; -import { TelegramID } from "./db/db"; +import { TelegramID } from "./types"; export function setTelegramWebHook( callback: (success: boolean) => void = () => {} diff --git a/src/types.ts b/src/types.ts new file mode 100644 index 0000000..060b9a3 --- /dev/null +++ b/src/types.ts @@ -0,0 +1,11 @@ +export type UserRowID = number; +export type TelegramID = number; +export type VerificationString = string; + +declare module "express-session" { + interface Session { + verified: boolean; + verifiedBy: UserRowID; + user: TelegramID; + } +}