ip.ts 679 B

1234567891011121314151617181920212223242526
  1. import subMinutes from 'date-fns/sub_minutes';
  2. import IP from '../models/ip';
  3. export const addIP = async (newIP: string) => {
  4. const ip = await IP.findOneAndUpdate(
  5. { ip: newIP },
  6. { ip: newIP, createdAt: new Date() },
  7. { new: true, upsert: true, runValidators: true }
  8. );
  9. return ip;
  10. };
  11. export const getIP = async (ip: string) => {
  12. const matchedIp = await IP.findOne({
  13. ip,
  14. createdAt: {
  15. $gt: subMinutes(new Date(), Number(process.env.NON_USER_COOLDOWN)),
  16. },
  17. });
  18. return matchedIp;
  19. };
  20. export const clearIPs = async () =>
  21. IP.deleteMany({
  22. createdAt: {
  23. $lt: subMinutes(new Date(), Number(process.env.NON_USER_COOLDOWN)),
  24. },
  25. });