import { Kysely, Transaction } from 'npm:kysely'; import { Database } from "../repo/scheme.ts"; import { UserCheckedRestrictions, UserRestrictionsInfo } from "./entities.ts"; export const checkUserRestrictions = async ( db: Kysely | Transaction, user: UserRestrictionsInfo, ): Promise => { if (user.is_banned) return { isBlocked: true, isTimeout: false } if (!user.is_timeout) { return { isBlocked: false, isTimeout: false } } const now = new Date() if (user.timeout_until && user.timeout_until <= now) { await db.updateTable('users').set({ is_timeout: false, timeout_until: null, }).where('tg_id', '=', user.tg_id).execute() return { isBlocked: false, isTimeout: false } } return { isBlocked: false, isTimeout: true } } export const getActiveInviteLink = async ( db: Kysely | Transaction, tg_id: number ): Promise => { const invite_links = await db.selectFrom('invite_links') .selectAll().where('expect_user_tg_id', '=', tg_id).execute() if (invite_links.length < 1) return const now = new Date() const link = invite_links[0] if (link.valid_until && link.valid_until < now) { await db.deleteFrom('invite_links') .where('link', '=', link.link).execute() return } return link.link }