ip.ts 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import { subMinutes } from "date-fns";
  2. import knex from "../../knex";
  3. import env from "../../env";
  4. export const addIP = async (ipToGet: string) => {
  5. const ip = ipToGet.toLowerCase();
  6. const currentIP = await knex<IP>("ips")
  7. .where({ ip })
  8. .first();
  9. if (currentIP) {
  10. const currentDate = new Date().toISOString();
  11. await knex<IP>("ips")
  12. .where({ ip })
  13. .update({
  14. created_at: currentDate,
  15. updated_at: currentDate
  16. });
  17. } else {
  18. await knex<IP>("ips").insert({ ip });
  19. }
  20. return ip;
  21. };
  22. export const getIP = async (ip: string) => {
  23. const cooldownConfig = env.NON_USER_COOLDOWN;
  24. const matchedIp = await knex<IP>("ips")
  25. .where({ ip: ip.toLowerCase() })
  26. .andWhere(
  27. "created_at",
  28. ">",
  29. subMinutes(new Date(), cooldownConfig).toISOString()
  30. )
  31. .first();
  32. return matchedIp;
  33. };
  34. export const clearIPs = async () =>
  35. knex<IP>("ips")
  36. .where(
  37. "created_at",
  38. "<",
  39. subMinutes(new Date(), env.NON_USER_COOLDOWN).toISOString()
  40. )
  41. .delete();