Browse Source

split up models into separate files, other cleanup

tsMigration
Nareshkumar Rao 3 years ago
parent
commit
8d05734ee9
  1. 69
      src/db/db.ts
  2. 23
      src/db/models/Contact.ts
  3. 45
      src/db/models/User.ts
  4. 9
      src/db/utils.ts
  5. 9
      src/routes/CodeRoute.ts
  6. 10
      src/routes/LoginRoute.ts
  7. 39
      src/routes/TelegramWebhookRoute.ts
  8. 3
      src/routes/VerifyRoute.ts
  9. 2
      src/telegram.ts
  10. 11
      src/types.ts

69
src/db/db.ts

@ -1,6 +1,6 @@
import ConnectSessionSequelize from "connect-session-sequelize"; import ConnectSessionSequelize from "connect-session-sequelize";
import session from "express-session"; import session from "express-session";
import { DataTypes, Model, Optional, Sequelize } from "sequelize";
import { Sequelize } from "sequelize";
const SequelizeStore = ConnectSessionSequelize(session.Store); const SequelizeStore = ConnectSessionSequelize(session.Store);
@ -41,71 +41,4 @@ export const storeDB: Sequelize = (() => {
export const store = new SequelizeStore({ export const store = new SequelizeStore({
db: storeDB, 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, ContactAttributes>,
ContactAttributes {}
export const Contact = sequelize.define<ContactInterface>("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, UserCreationAttributes>,
UserAttributes {}
export const User = sequelize.define<UserInstance>("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(); store.sync();

23
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, ContactAttributes>,
ContactAttributes {}
export const Contact = sequelize.define<ContactInterface>("Contact", {
user: {
type: DataTypes.INTEGER,
allowNull: false,
},
with: {
type: DataTypes.INTEGER,
allowNull: false,
},
});
Contact.sync();

45
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, UserCreationAttributes>,
UserAttributes {}
export const User = sequelize.define<UserInstance>("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.");
});
}
});

9
src/db/utils.ts

@ -1,6 +1,8 @@
import { strings_en } from "../strings"; import { strings_en } from "../strings";
import { sendTelegramMessage } from "../telegram"; 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( export function addContact(
userATelegram: TelegramID, userATelegram: TelegramID,
@ -19,10 +21,7 @@ export function addContact(
console.log( console.log(
`Registering contact between ${userA!.id} and ${userBRowID}` `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"); done(true, "Successfully added contact");
}) })
.catch((e) => { .catch((e) => {

9
src/routes/CodeRoute.ts

@ -1,13 +1,8 @@
import { Request, Response } from "express"; import { Request, Response } from "express";
import { TelegramID, User, UserInstance } from "../db/db";
import bcrypt from "bcrypt"; import bcrypt from "bcrypt";
import QRCode, { QRCodeToDataURLOptions } from "qrcode"; 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) { export function CodeRoute(req: Request, res: Response) {
if (!req.session.user) { if (!req.session.user) {

10
src/routes/LoginRoute.ts

@ -1,14 +1,8 @@
import { Request, Response } from "express"; import { Request, Response } from "express";
import { TelegramID, User, UserRowID } from "../db/db";
import crypto from "crypto"; import crypto from "crypto";
import { addContact, createUser } from "../db/utils"; 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 = { type TelegramLoginResponse = {
id: TelegramID; id: TelegramID;

39
src/routes/TelegramWebhookRoute.ts

@ -1,8 +1,10 @@
import { Request, Response } from "express"; import { Request, Response } from "express";
import { Op } from "sequelize"; 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 { strings_en } from "../strings";
import { sendTelegramMessage } from "../telegram"; import { sendTelegramMessage } from "../telegram";
import { TelegramID } from "../types";
interface TelegramWebhookRequest extends Request { interface TelegramWebhookRequest extends Request {
body: { body: {
@ -27,22 +29,22 @@ export function TelegramWebhookRoute(
"Thanks for using OurSejahtera! Let's stay safer together <3" "Thanks for using OurSejahtera! Let's stay safer together <3"
); );
} else { } 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) { } catch (e) {
console.log("Could not get Telegram Message"); console.log("Could not get Telegram Message");
} }
@ -72,7 +74,10 @@ function informContacts(telegramID: TelegramID) {
}, },
}).then((otherPerson) => { }).then((otherPerson) => {
otherPerson && otherPerson &&
sendTelegramMessage(otherPerson.telegram, strings_en.telegram_inform_infect);
sendTelegramMessage(
otherPerson.telegram,
strings_en.telegram_inform_infect
);
}); });
}); });
}); });

3
src/routes/VerifyRoute.ts

@ -1,6 +1,7 @@
import { Request, Response } from "express"; import { Request, Response } from "express";
import { User, UserRowID, VerificationString } from "../db/db";
import { User } from "../db/models/User";
import { addContact } from "../db/utils"; import { addContact } from "../db/utils";
import { UserRowID, VerificationString } from "../types";
interface VerifyRequest extends Request { interface VerifyRequest extends Request {
body: { body: {

2
src/telegram.ts

@ -1,5 +1,5 @@
import axios from "axios"; import axios from "axios";
import { TelegramID } from "./db/db";
import { TelegramID } from "./types";
export function setTelegramWebHook( export function setTelegramWebHook(
callback: (success: boolean) => void = () => {} callback: (success: boolean) => void = () => {}

11
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;
}
}
Loading…
Cancel
Save