ip.ts 966 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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 matchedIp = await knex<IP>("ips")
  23. .where({ ip: ip.toLowerCase() })
  24. .andWhere("created_at", ">", new Date().toISOString())
  25. .first();
  26. return matchedIp;
  27. };
  28. export const clearIPs = async () =>
  29. knex<IP>("ips")
  30. .where(
  31. "created_at",
  32. "<",
  33. subMinutes(
  34. new Date(),
  35. Number(process.env.NON_USER_COOLDOWN)
  36. ).toISOString()
  37. )
  38. .delete();