package com.izouma.jmrh.repo;

import com.izouma.jmrh.domain.MailCode;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import javax.transaction.Transactional;
import java.time.LocalDateTime;
import java.util.Optional;

public interface MailCodeRepo extends JpaRepository<MailCode, Long>, JpaSpecificationExecutor<MailCode> {

    @Transactional
    @Modifying
    @Query("update MailCode m set m.expired = true where m.mail = ?1 and m.expired = false")
    void expire(String mail);

    Optional<MailCode> findLastByMailAndExpireAtAfterAndExpiredFalse(String mail, LocalDateTime expire);

}
