ip.ts 721 B

1234567891011121314151617181920212223242526272829303132333435
  1. import { subMinutes } from "date-fns";
  2. import knex from "../knex";
  3. import env from "../env";
  4. export const add = async (ipToAdd: string) => {
  5. const ip = ipToAdd.toLowerCase();
  6. const currentIP = await knex<IP>("ips")
  7. .where("ip", 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 clear = async () =>
  23. knex<IP>("ips")
  24. .where(
  25. "created_at",
  26. "<",
  27. subMinutes(new Date(), env.NON_USER_COOLDOWN).toISOString()
  28. )
  29. .delete();