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. 11
      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 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, 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();

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 { 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) => {

9
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) {

10
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;

11
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: {
@ -34,7 +36,7 @@ export function TelegramWebhookRoute(
if (result.saved) {
sendTelegramMessage(
telegramID,
strings_en.telegram_inform_positive,
strings_en.telegram_inform_positive
);
informContacts(telegramID);
} else {
@ -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
);
});
});
});

3
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: {

2
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 = () => {}

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