Nareshkumar Rao
3 years ago
8 changed files with 205 additions and 294 deletions
@ -1,80 +1,60 @@ |
|||
import { TelegramID, UserRowID, VerificationString } from "../../types"; |
|||
import { User, UserInstance } from "./User"; |
|||
|
|||
export function getUserByTelegramID( |
|||
telegramID: TelegramID, |
|||
callback: (user?: UserInstance, message?: string) => void |
|||
): void { |
|||
User.findOne({ |
|||
export async function getUserByTelegramID( |
|||
telegramID: TelegramID |
|||
): Promise<UserInstance | null> { |
|||
const user = await User.findOne({ |
|||
where: { |
|||
telegram: telegramID, |
|||
}, |
|||
}) |
|||
.then((result) => { |
|||
callback(!!result ? result : undefined); |
|||
}) |
|||
.catch(() => { |
|||
callback(undefined); |
|||
}); |
|||
return user; |
|||
} |
|||
|
|||
export function getUserByRowID( |
|||
rowID: UserRowID, |
|||
callback: (user?: UserInstance, message?: string) => void |
|||
): void { |
|||
User.findOne({ |
|||
export async function getUserByRowID( |
|||
rowID: UserRowID |
|||
): Promise<UserInstance | null> { |
|||
const user = await User.findOne({ |
|||
where: { |
|||
id: rowID, |
|||
}, |
|||
}) |
|||
.then((result) => { |
|||
callback(!!result ? result : undefined); |
|||
}) |
|||
.catch(() => { |
|||
callback(undefined); |
|||
}); |
|||
return user; |
|||
} |
|||
|
|||
export function getUserByVerification( |
|||
verification: VerificationString, |
|||
callback: (user?: UserInstance, message?: string) => void |
|||
): void { |
|||
User.findOne({ |
|||
export async function getUserByVerification( |
|||
verification: VerificationString |
|||
): Promise<UserInstance | null> { |
|||
const user = await User.findOne({ |
|||
where: { |
|||
verification: verification, |
|||
}, |
|||
}) |
|||
.then((result) => { |
|||
callback(!!result ? result : undefined); |
|||
}) |
|||
.catch(() => { |
|||
callback(undefined); |
|||
}); |
|||
return user; |
|||
} |
|||
|
|||
export function getUserCovidPositivity(telegramID: TelegramID, callback: (isInfected?: boolean) => void): void { |
|||
getUserByTelegramID(telegramID, user => { |
|||
if (!!user) { |
|||
export async function getUserCovidPositivity( |
|||
telegramID: TelegramID |
|||
): Promise<boolean> { |
|||
const user = await getUserByTelegramID(telegramID); |
|||
if (!user) throw new Error("User not found"); |
|||
const infectionDuration = +user.infectionDate - Date.now(); |
|||
if (infectionDuration > 60 * 60 * 24 * 14) { |
|||
setUserCovidPositivity(telegramID, false, success => { |
|||
callback(success ? false : undefined); |
|||
}); |
|||
} else { |
|||
callback(user.isInfected); |
|||
} |
|||
await setUserCovidPositivity(telegramID, false); |
|||
return false; |
|||
} else { |
|||
callback(); |
|||
return user.isInfected; |
|||
} |
|||
}); |
|||
} |
|||
|
|||
export function setUserCovidPositivity(telegramID: TelegramID, infectionState: boolean, callback: (success: boolean) => void): void { |
|||
getUserByTelegramID(telegramID, user => { |
|||
if (!!user) { |
|||
export async function setUserCovidPositivity( |
|||
telegramID: TelegramID, |
|||
infectionState: boolean |
|||
): Promise<void> { |
|||
const user = await getUserByTelegramID(telegramID); |
|||
if (!user) throw new Error("User not found"); |
|||
user.isInfected = infectionState; |
|||
user.infectionDate = new Date(); |
|||
user.save().then(() => callback(true)).catch(() => callback(false)); |
|||
} else { callback(false) } |
|||
}); |
|||
if (!(await user.save())) throw new Error("Could not save user state"); |
|||
} |
|||
|
@ -1,56 +1,30 @@ |
|||
import { strings_en } from "../strings"; |
|||
import { sendTelegramMessage } from "../telegram"; |
|||
import { TelegramID, UserRowID } from "../types"; |
|||
import { TelegramID } from "../types"; |
|||
import { Contact } from "./models/Contact"; |
|||
import { User } from "./models/User"; |
|||
import { getUserByRowID, getUserByTelegramID } from "./models/User.helper"; |
|||
import { User, UserInstance } from "./models/User"; |
|||
import { getUserByTelegramID } from "./models/User.helper"; |
|||
|
|||
export function addContact( |
|||
export async function addContact( |
|||
userATelegram: TelegramID, |
|||
userBTelegram: TelegramID, |
|||
callback: (success: boolean, message?: string) => void |
|||
): void { |
|||
getUserByTelegramID(userATelegram, (userA) => { |
|||
getUserByTelegramID(userBTelegram, (userB) => { |
|||
userBTelegram: TelegramID |
|||
): Promise<void> { |
|||
const userA = await getUserByTelegramID(userATelegram); |
|||
const userB = await getUserByTelegramID(userBTelegram); |
|||
|
|||
if (!userA || !userB) { |
|||
callback(false, "Could not find user."); |
|||
return; |
|||
throw new Error("Could not found users"); |
|||
} |
|||
|
|||
Contact.create({ user: userA.id, with: userB.id }) |
|||
.then(() => { |
|||
console.log( |
|||
`Registering contact between ${userA.id} and ${userB.id}` |
|||
); |
|||
sendTelegramMessage(userB.telegram, strings_en.telegram_qr_scanned); |
|||
callback(true, "Successfully added contact"); |
|||
}) |
|||
.catch((e) => { |
|||
callback(false, e); |
|||
}); |
|||
}); |
|||
}); |
|||
await Contact.create({ user: userA.id, with: userB.id }); |
|||
await sendTelegramMessage(userB.telegram, strings_en.telegram_qr_scanned); |
|||
} |
|||
|
|||
export function createUser( |
|||
telegram: TelegramID, |
|||
callback: (success: boolean, message: string) => void |
|||
): void { |
|||
User.create({ |
|||
export async function createUser( |
|||
telegram: TelegramID |
|||
): Promise<UserInstance | null> { |
|||
const user = await User.create({ |
|||
telegram: telegram, |
|||
}) |
|||
.then((user) => { |
|||
if (!user) { |
|||
callback(false, "Could not create user"); |
|||
} else { |
|||
callback(true, "Success"); |
|||
} |
|||
}) |
|||
.catch((reason) => { |
|||
if (reason.name == "SequelizeUniqueConstraintError") { |
|||
callback(false, "User already exists"); |
|||
} else { |
|||
callback(false, "Unknown error"); |
|||
} |
|||
}); |
|||
return user; |
|||
} |
|||
|
Loading…
Reference in new issue