ip.ts 1.1 KB

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